OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "webrtc/base/atomicops.h" | 12 #include "webrtc/base/atomicops.h" |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/common.h" | 14 #include "webrtc/base/common.h" |
15 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
16 #include "webrtc/base/messagequeue.h" | 16 #include "webrtc/base/messagequeue.h" |
17 #include "webrtc/base/stringencode.h" | 17 #include "webrtc/base/stringencode.h" |
18 #include "webrtc/base/thread.h" | 18 #include "webrtc/base/thread.h" |
19 #include "webrtc/base/trace_event.h" | 19 #include "webrtc/base/trace_event.h" |
20 | 20 |
21 namespace rtc { | 21 namespace rtc { |
| 22 namespace { |
22 | 23 |
23 const int kMaxMsgLatency = 150; // 150 ms | 24 const int kMaxMsgLatency = 150; // 150 ms |
24 const int kSlowDispatchLoggingThreshold = 50; // 50 ms | 25 const int kSlowDispatchLoggingThreshold = 50; // 50 ms |
25 | 26 |
| 27 class SCOPED_LOCKABLE DebugNonReentrantCritScope { |
| 28 public: |
| 29 DebugNonReentrantCritScope(const CriticalSection* cs, bool* locked) |
| 30 EXCLUSIVE_LOCK_FUNCTION(cs) |
| 31 : cs_(cs), locked_(locked) { |
| 32 cs_->Enter(); |
| 33 ASSERT(!*locked_); |
| 34 *locked_ = true; |
| 35 } |
| 36 |
| 37 ~DebugNonReentrantCritScope() UNLOCK_FUNCTION() { |
| 38 *locked_ = false; |
| 39 cs_->Leave(); |
| 40 } |
| 41 |
| 42 private: |
| 43 const CriticalSection* const cs_; |
| 44 bool* locked_; |
| 45 |
| 46 RTC_DISALLOW_COPY_AND_ASSIGN(DebugNonReentrantCritScope); |
| 47 }; |
| 48 } // namespace |
| 49 |
26 //------------------------------------------------------------------ | 50 //------------------------------------------------------------------ |
27 // MessageQueueManager | 51 // MessageQueueManager |
28 | 52 |
29 MessageQueueManager* MessageQueueManager::instance_ = NULL; | 53 MessageQueueManager* MessageQueueManager::instance_ = NULL; |
30 | 54 |
31 MessageQueueManager* MessageQueueManager::Instance() { | 55 MessageQueueManager* MessageQueueManager::Instance() { |
32 // Note: This is not thread safe, but it is first called before threads are | 56 // Note: This is not thread safe, but it is first called before threads are |
33 // spawned. | 57 // spawned. |
34 if (!instance_) | 58 if (!instance_) |
35 instance_ = new MessageQueueManager; | 59 instance_ = new MessageQueueManager; |
36 return instance_; | 60 return instance_; |
37 } | 61 } |
38 | 62 |
39 bool MessageQueueManager::IsInitialized() { | 63 bool MessageQueueManager::IsInitialized() { |
40 return instance_ != NULL; | 64 return instance_ != NULL; |
41 } | 65 } |
42 | 66 |
43 MessageQueueManager::MessageQueueManager() {} | 67 MessageQueueManager::MessageQueueManager() : locked_(false) {} |
44 | 68 |
45 MessageQueueManager::~MessageQueueManager() { | 69 MessageQueueManager::~MessageQueueManager() { |
46 } | 70 } |
47 | 71 |
48 void MessageQueueManager::Add(MessageQueue *message_queue) { | 72 void MessageQueueManager::Add(MessageQueue *message_queue) { |
49 return Instance()->AddInternal(message_queue); | 73 return Instance()->AddInternal(message_queue); |
50 } | 74 } |
51 void MessageQueueManager::AddInternal(MessageQueue *message_queue) { | 75 void MessageQueueManager::AddInternal(MessageQueue *message_queue) { |
52 // MessageQueueManager methods should be non-reentrant, so we | 76 DebugNonReentrantCritScope cs(&crit_, &locked_); |
53 // ASSERT that is the case. If any of these ASSERT, please | |
54 // contact bpm or jbeda. | |
55 #if CS_DEBUG_CHECKS // CurrentThreadIsOwner returns true by default. | |
56 ASSERT(!crit_.CurrentThreadIsOwner()); | |
57 #endif | |
58 CritScope cs(&crit_); | |
59 message_queues_.push_back(message_queue); | 77 message_queues_.push_back(message_queue); |
60 } | 78 } |
61 | 79 |
62 void MessageQueueManager::Remove(MessageQueue *message_queue) { | 80 void MessageQueueManager::Remove(MessageQueue *message_queue) { |
63 // If there isn't a message queue manager instance, then there isn't a queue | 81 // If there isn't a message queue manager instance, then there isn't a queue |
64 // to remove. | 82 // to remove. |
65 if (!instance_) return; | 83 if (!instance_) return; |
66 return Instance()->RemoveInternal(message_queue); | 84 return Instance()->RemoveInternal(message_queue); |
67 } | 85 } |
68 void MessageQueueManager::RemoveInternal(MessageQueue *message_queue) { | 86 void MessageQueueManager::RemoveInternal(MessageQueue *message_queue) { |
69 #if CS_DEBUG_CHECKS // CurrentThreadIsOwner returns true by default. | |
70 ASSERT(!crit_.CurrentThreadIsOwner()); // See note above. | |
71 #endif | |
72 // If this is the last MessageQueue, destroy the manager as well so that | 87 // If this is the last MessageQueue, destroy the manager as well so that |
73 // we don't leak this object at program shutdown. As mentioned above, this is | 88 // we don't leak this object at program shutdown. As mentioned above, this is |
74 // not thread-safe, but this should only happen at program termination (when | 89 // not thread-safe, but this should only happen at program termination (when |
75 // the ThreadManager is destroyed, and threads are no longer active). | 90 // the ThreadManager is destroyed, and threads are no longer active). |
76 bool destroy = false; | 91 bool destroy = false; |
77 { | 92 { |
78 CritScope cs(&crit_); | 93 DebugNonReentrantCritScope cs(&crit_, &locked_); |
79 std::vector<MessageQueue *>::iterator iter; | 94 std::vector<MessageQueue *>::iterator iter; |
80 iter = std::find(message_queues_.begin(), message_queues_.end(), | 95 iter = std::find(message_queues_.begin(), message_queues_.end(), |
81 message_queue); | 96 message_queue); |
82 if (iter != message_queues_.end()) { | 97 if (iter != message_queues_.end()) { |
83 message_queues_.erase(iter); | 98 message_queues_.erase(iter); |
84 } | 99 } |
85 destroy = message_queues_.empty(); | 100 destroy = message_queues_.empty(); |
86 } | 101 } |
87 if (destroy) { | 102 if (destroy) { |
88 instance_ = NULL; | 103 instance_ = NULL; |
89 delete this; | 104 delete this; |
90 } | 105 } |
91 } | 106 } |
92 | 107 |
93 void MessageQueueManager::Clear(MessageHandler *handler) { | 108 void MessageQueueManager::Clear(MessageHandler *handler) { |
94 // If there isn't a message queue manager instance, then there aren't any | 109 // If there isn't a message queue manager instance, then there aren't any |
95 // queues to remove this handler from. | 110 // queues to remove this handler from. |
96 if (!instance_) return; | 111 if (!instance_) return; |
97 return Instance()->ClearInternal(handler); | 112 return Instance()->ClearInternal(handler); |
98 } | 113 } |
99 void MessageQueueManager::ClearInternal(MessageHandler *handler) { | 114 void MessageQueueManager::ClearInternal(MessageHandler *handler) { |
100 #if CS_DEBUG_CHECKS // CurrentThreadIsOwner returns true by default. | 115 DebugNonReentrantCritScope cs(&crit_, &locked_); |
101 ASSERT(!crit_.CurrentThreadIsOwner()); // See note above. | |
102 #endif | |
103 CritScope cs(&crit_); | |
104 std::vector<MessageQueue *>::iterator iter; | 116 std::vector<MessageQueue *>::iterator iter; |
105 for (iter = message_queues_.begin(); iter != message_queues_.end(); iter++) | 117 for (iter = message_queues_.begin(); iter != message_queues_.end(); iter++) |
106 (*iter)->Clear(handler); | 118 (*iter)->Clear(handler); |
107 } | 119 } |
108 | 120 |
109 void MessageQueueManager::ProcessAllMessageQueues() { | 121 void MessageQueueManager::ProcessAllMessageQueues() { |
110 if (!instance_) { | 122 if (!instance_) { |
111 return; | 123 return; |
112 } | 124 } |
113 return Instance()->ProcessAllMessageQueuesInternal(); | 125 return Instance()->ProcessAllMessageQueuesInternal(); |
114 } | 126 } |
115 | 127 |
116 void MessageQueueManager::ProcessAllMessageQueuesInternal() { | 128 void MessageQueueManager::ProcessAllMessageQueuesInternal() { |
117 #if CS_DEBUG_CHECKS // CurrentThreadIsOwner returns true by default. | |
118 ASSERT(!crit_.CurrentThreadIsOwner()); // See note above. | |
119 #endif | |
120 // Post a delayed message at the current time and wait for it to be dispatched | 129 // Post a delayed message at the current time and wait for it to be dispatched |
121 // on all queues, which will ensure that all messages that came before it were | 130 // on all queues, which will ensure that all messages that came before it were |
122 // also dispatched. | 131 // also dispatched. |
123 volatile int queues_not_done; | 132 volatile int queues_not_done; |
124 auto functor = [&queues_not_done] { AtomicOps::Decrement(&queues_not_done); }; | 133 auto functor = [&queues_not_done] { AtomicOps::Decrement(&queues_not_done); }; |
125 FunctorMessageHandler<void, decltype(functor)> handler(functor); | 134 FunctorMessageHandler<void, decltype(functor)> handler(functor); |
126 { | 135 { |
127 CritScope cs(&crit_); | 136 DebugNonReentrantCritScope cs(&crit_, &locked_); |
128 queues_not_done = static_cast<int>(message_queues_.size()); | 137 queues_not_done = static_cast<int>(message_queues_.size()); |
129 for (MessageQueue* queue : message_queues_) { | 138 for (MessageQueue* queue : message_queues_) { |
130 queue->PostDelayed(RTC_FROM_HERE, 0, &handler); | 139 queue->PostDelayed(RTC_FROM_HERE, 0, &handler); |
131 } | 140 } |
132 } | 141 } |
133 // Note: One of the message queues may have been on this thread, which is why | 142 // Note: One of the message queues may have been on this thread, which is why |
134 // we can't synchronously wait for queues_not_done to go to 0; we need to | 143 // we can't synchronously wait for queues_not_done to go to 0; we need to |
135 // process messages as well. | 144 // process messages as well. |
136 while (AtomicOps::AcquireLoad(&queues_not_done) > 0) { | 145 while (AtomicOps::AcquireLoad(&queues_not_done) > 0) { |
137 rtc::Thread::Current()->ProcessMessages(0); | 146 rtc::Thread::Current()->ProcessMessages(0); |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 pmsg->phandler->OnMessage(pmsg); | 512 pmsg->phandler->OnMessage(pmsg); |
504 int64_t end_time = TimeMillis(); | 513 int64_t end_time = TimeMillis(); |
505 int64_t diff = TimeDiff(end_time, start_time); | 514 int64_t diff = TimeDiff(end_time, start_time); |
506 if (diff >= kSlowDispatchLoggingThreshold) { | 515 if (diff >= kSlowDispatchLoggingThreshold) { |
507 LOG(LS_INFO) << "Message took " << diff << "ms to dispatch. Posted from: " | 516 LOG(LS_INFO) << "Message took " << diff << "ms to dispatch. Posted from: " |
508 << pmsg->posted_from.ToString(); | 517 << pmsg->posted_from.ToString(); |
509 } | 518 } |
510 } | 519 } |
511 | 520 |
512 } // namespace rtc | 521 } // namespace rtc |
OLD | NEW |