blob: 591a6b834f92e4ff221fc85c96829a54fc23c9e6 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000010#include <algorithm>
11
deadbeeff5f03e82016-06-06 11:16:06 -070012#include "webrtc/base/atomicops.h"
danilchapbebf54c2016-04-28 01:32:48 -070013#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#include "webrtc/base/common.h"
15#include "webrtc/base/logging.h"
16#include "webrtc/base/messagequeue.h"
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070017#include "webrtc/base/stringencode.h"
deadbeeff5f03e82016-06-06 11:16:06 -070018#include "webrtc/base/thread.h"
pbos79e28422016-04-29 08:48:05 -070019#include "webrtc/base/trace_event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
21namespace rtc {
andrespcdf61722016-07-08 02:45:40 -070022namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Honghai Zhang82d78622016-05-06 11:29:15 -070024const int kMaxMsgLatency = 150; // 150 ms
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070025const int kSlowDispatchLoggingThreshold = 50; // 50 ms
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
andrespcdf61722016-07-08 02:45:40 -070027class 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050//------------------------------------------------------------------
51// MessageQueueManager
52
53MessageQueueManager* MessageQueueManager::instance_ = NULL;
54
55MessageQueueManager* MessageQueueManager::Instance() {
56 // Note: This is not thread safe, but it is first called before threads are
57 // spawned.
58 if (!instance_)
59 instance_ = new MessageQueueManager;
60 return instance_;
61}
62
63bool MessageQueueManager::IsInitialized() {
64 return instance_ != NULL;
65}
66
andrespcdf61722016-07-08 02:45:40 -070067MessageQueueManager::MessageQueueManager() : locked_(false) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
69MessageQueueManager::~MessageQueueManager() {
70}
71
72void MessageQueueManager::Add(MessageQueue *message_queue) {
73 return Instance()->AddInternal(message_queue);
74}
75void MessageQueueManager::AddInternal(MessageQueue *message_queue) {
andrespcdf61722016-07-08 02:45:40 -070076 DebugNonReentrantCritScope cs(&crit_, &locked_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 message_queues_.push_back(message_queue);
78}
79
80void MessageQueueManager::Remove(MessageQueue *message_queue) {
81 // If there isn't a message queue manager instance, then there isn't a queue
82 // to remove.
83 if (!instance_) return;
84 return Instance()->RemoveInternal(message_queue);
85}
86void MessageQueueManager::RemoveInternal(MessageQueue *message_queue) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 // If this is the last MessageQueue, destroy the manager as well so that
88 // we don't leak this object at program shutdown. As mentioned above, this is
89 // not thread-safe, but this should only happen at program termination (when
90 // the ThreadManager is destroyed, and threads are no longer active).
91 bool destroy = false;
92 {
andrespcdf61722016-07-08 02:45:40 -070093 DebugNonReentrantCritScope cs(&crit_, &locked_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 std::vector<MessageQueue *>::iterator iter;
95 iter = std::find(message_queues_.begin(), message_queues_.end(),
96 message_queue);
97 if (iter != message_queues_.end()) {
98 message_queues_.erase(iter);
99 }
100 destroy = message_queues_.empty();
101 }
102 if (destroy) {
103 instance_ = NULL;
104 delete this;
105 }
106}
107
108void MessageQueueManager::Clear(MessageHandler *handler) {
109 // If there isn't a message queue manager instance, then there aren't any
110 // queues to remove this handler from.
111 if (!instance_) return;
112 return Instance()->ClearInternal(handler);
113}
114void MessageQueueManager::ClearInternal(MessageHandler *handler) {
andrespcdf61722016-07-08 02:45:40 -0700115 DebugNonReentrantCritScope cs(&crit_, &locked_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 std::vector<MessageQueue *>::iterator iter;
117 for (iter = message_queues_.begin(); iter != message_queues_.end(); iter++)
118 (*iter)->Clear(handler);
119}
120
deadbeeff5f03e82016-06-06 11:16:06 -0700121void MessageQueueManager::ProcessAllMessageQueues() {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700122 if (!instance_) {
123 return;
124 }
deadbeeff5f03e82016-06-06 11:16:06 -0700125 return Instance()->ProcessAllMessageQueuesInternal();
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700126}
127
deadbeeff5f03e82016-06-06 11:16:06 -0700128void MessageQueueManager::ProcessAllMessageQueuesInternal() {
deadbeeff5f03e82016-06-06 11:16:06 -0700129 // Post a delayed message at the current time and wait for it to be dispatched
130 // on all queues, which will ensure that all messages that came before it were
131 // also dispatched.
132 volatile int queues_not_done;
133 auto functor = [&queues_not_done] { AtomicOps::Decrement(&queues_not_done); };
134 FunctorMessageHandler<void, decltype(functor)> handler(functor);
135 {
andrespcdf61722016-07-08 02:45:40 -0700136 DebugNonReentrantCritScope cs(&crit_, &locked_);
deadbeeff5f03e82016-06-06 11:16:06 -0700137 queues_not_done = static_cast<int>(message_queues_.size());
138 for (MessageQueue* queue : message_queues_) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700139 queue->PostDelayed(RTC_FROM_HERE, 0, &handler);
deadbeeff5f03e82016-06-06 11:16:06 -0700140 }
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700141 }
deadbeeff5f03e82016-06-06 11:16:06 -0700142 // Note: One of the message queues may have been on this thread, which is why
143 // we can't synchronously wait for queues_not_done to go to 0; we need to
144 // process messages as well.
145 while (AtomicOps::AcquireLoad(&queues_not_done) > 0) {
146 rtc::Thread::Current()->ProcessMessages(0);
147 }
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700148}
149
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150//------------------------------------------------------------------
151// MessageQueue
jbauch25d1f282016-02-05 00:25:02 -0800152MessageQueue::MessageQueue(SocketServer* ss, bool init_queue)
jbauch9ccedc32016-02-25 01:14:56 -0800153 : fStop_(false), fPeekKeep_(false),
154 dmsgq_next_num_(0), fInitialized_(false), fDestroyed_(false), ss_(ss) {
danilchapbebf54c2016-04-28 01:32:48 -0700155 RTC_DCHECK(ss);
156 // Currently, MessageQueue holds a socket server, and is the base class for
157 // Thread. It seems like it makes more sense for Thread to hold the socket
158 // server, and provide it to the MessageQueue, since the Thread controls
159 // the I/O model, and MQ is agnostic to those details. Anyway, this causes
160 // messagequeue_unittest to depend on network libraries... yuck.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 ss_->SetMessageQueue(this);
jbauch25d1f282016-02-05 00:25:02 -0800162 if (init_queue) {
163 DoInit();
164 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165}
166
danilchapbebf54c2016-04-28 01:32:48 -0700167MessageQueue::MessageQueue(std::unique_ptr<SocketServer> ss, bool init_queue)
168 : MessageQueue(ss.get(), init_queue) {
169 own_ss_ = std::move(ss);
170}
171
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172MessageQueue::~MessageQueue() {
jbauch25d1f282016-02-05 00:25:02 -0800173 DoDestroy();
174}
175
176void MessageQueue::DoInit() {
177 if (fInitialized_) {
178 return;
179 }
180
181 fInitialized_ = true;
182 MessageQueueManager::Add(this);
183}
184
185void MessageQueue::DoDestroy() {
186 if (fDestroyed_) {
187 return;
188 }
189
190 fDestroyed_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 // The signal is done from here to ensure
192 // that it always gets called when the queue
193 // is going away.
194 SignalQueueDestroyed();
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000195 MessageQueueManager::Remove(this);
196 Clear(NULL);
jbauch9ccedc32016-02-25 01:14:56 -0800197
198 SharedScope ss(&ss_lock_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 if (ss_) {
200 ss_->SetMessageQueue(NULL);
201 }
202}
203
jbauch9ccedc32016-02-25 01:14:56 -0800204SocketServer* MessageQueue::socketserver() {
205 SharedScope ss(&ss_lock_);
206 return ss_;
207}
208
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209void MessageQueue::set_socketserver(SocketServer* ss) {
jbauch9ccedc32016-02-25 01:14:56 -0800210 // Need to lock exclusively here to prevent simultaneous modifications from
211 // other threads. Can't be a shared lock to prevent races with other reading
212 // threads.
213 // Other places that only read "ss_" can use a shared lock as simultaneous
214 // read access is allowed.
215 ExclusiveScope es(&ss_lock_);
danilchapbebf54c2016-04-28 01:32:48 -0700216 ss_ = ss ? ss : own_ss_.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000217 ss_->SetMessageQueue(this);
218}
219
jbauch9ccedc32016-02-25 01:14:56 -0800220void MessageQueue::WakeUpSocketServer() {
221 SharedScope ss(&ss_lock_);
222 ss_->WakeUp();
223}
224
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225void MessageQueue::Quit() {
226 fStop_ = true;
jbauch9ccedc32016-02-25 01:14:56 -0800227 WakeUpSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228}
229
230bool MessageQueue::IsQuitting() {
231 return fStop_;
232}
233
234void MessageQueue::Restart() {
235 fStop_ = false;
236}
237
238bool MessageQueue::Peek(Message *pmsg, int cmsWait) {
239 if (fPeekKeep_) {
240 *pmsg = msgPeek_;
241 return true;
242 }
243 if (!Get(pmsg, cmsWait))
244 return false;
245 msgPeek_ = *pmsg;
246 fPeekKeep_ = true;
247 return true;
248}
249
250bool MessageQueue::Get(Message *pmsg, int cmsWait, bool process_io) {
251 // Return and clear peek if present
252 // Always return the peek if it exists so there is Peek/Get symmetry
253
254 if (fPeekKeep_) {
255 *pmsg = msgPeek_;
256 fPeekKeep_ = false;
257 return true;
258 }
259
260 // Get w/wait + timer scan / dispatch + socket / event multiplexer dispatch
261
Honghai Zhang82d78622016-05-06 11:29:15 -0700262 int64_t cmsTotal = cmsWait;
263 int64_t cmsElapsed = 0;
264 int64_t msStart = TimeMillis();
265 int64_t msCurrent = msStart;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266 while (true) {
267 // Check for sent messages
268 ReceiveSends();
269
270 // Check for posted events
Honghai Zhang82d78622016-05-06 11:29:15 -0700271 int64_t cmsDelayNext = kForever;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 bool first_pass = true;
273 while (true) {
274 // All queue operations need to be locked, but nothing else in this loop
275 // (specifically handling disposed message) can happen inside the crit.
276 // Otherwise, disposed MessageHandlers will cause deadlocks.
277 {
278 CritScope cs(&crit_);
279 // On the first pass, check for delayed messages that have been
280 // triggered and calculate the next trigger time.
281 if (first_pass) {
282 first_pass = false;
283 while (!dmsgq_.empty()) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700284 if (msCurrent < dmsgq_.top().msTrigger_) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 cmsDelayNext = TimeDiff(dmsgq_.top().msTrigger_, msCurrent);
286 break;
287 }
288 msgq_.push_back(dmsgq_.top().msg_);
289 dmsgq_.pop();
290 }
291 }
292 // Pull a message off the message queue, if available.
293 if (msgq_.empty()) {
294 break;
295 } else {
296 *pmsg = msgq_.front();
297 msgq_.pop_front();
298 }
299 } // crit_ is released here.
300
301 // Log a warning for time-sensitive messages that we're late to deliver.
302 if (pmsg->ts_sensitive) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700303 int64_t delay = TimeDiff(msCurrent, pmsg->ts_sensitive);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 if (delay > 0) {
305 LOG_F(LS_WARNING) << "id: " << pmsg->message_id << " delay: "
306 << (delay + kMaxMsgLatency) << "ms";
307 }
308 }
309 // If this was a dispose message, delete it and skip it.
310 if (MQID_DISPOSE == pmsg->message_id) {
311 ASSERT(NULL == pmsg->phandler);
312 delete pmsg->pdata;
313 *pmsg = Message();
314 continue;
315 }
316 return true;
317 }
318
319 if (fStop_)
320 break;
321
322 // Which is shorter, the delay wait or the asked wait?
323
Honghai Zhang82d78622016-05-06 11:29:15 -0700324 int64_t cmsNext;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325 if (cmsWait == kForever) {
326 cmsNext = cmsDelayNext;
327 } else {
Honghai Zhang82d78622016-05-06 11:29:15 -0700328 cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000329 if ((cmsDelayNext != kForever) && (cmsDelayNext < cmsNext))
330 cmsNext = cmsDelayNext;
331 }
332
jbauch9ccedc32016-02-25 01:14:56 -0800333 {
334 // Wait and multiplex in the meantime
335 SharedScope ss(&ss_lock_);
Honghai Zhang82d78622016-05-06 11:29:15 -0700336 if (!ss_->Wait(static_cast<int>(cmsNext), process_io))
jbauch9ccedc32016-02-25 01:14:56 -0800337 return false;
338 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339
340 // If the specified timeout expired, return
341
Honghai Zhang82d78622016-05-06 11:29:15 -0700342 msCurrent = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 cmsElapsed = TimeDiff(msCurrent, msStart);
344 if (cmsWait != kForever) {
345 if (cmsElapsed >= cmsWait)
346 return false;
347 }
348 }
349 return false;
350}
351
352void MessageQueue::ReceiveSends() {
353}
354
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700355void MessageQueue::Post(const Location& posted_from,
356 MessageHandler* phandler,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200357 uint32_t id,
358 MessageData* pdata,
359 bool time_sensitive) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000360 if (fStop_)
361 return;
362
363 // Keep thread safe
364 // Add the message to the end of the queue
365 // Signal for the multiplexer to return
366
jbauch9ccedc32016-02-25 01:14:56 -0800367 {
368 CritScope cs(&crit_);
369 Message msg;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700370 msg.posted_from = posted_from;
jbauch9ccedc32016-02-25 01:14:56 -0800371 msg.phandler = phandler;
372 msg.message_id = id;
373 msg.pdata = pdata;
374 if (time_sensitive) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700375 msg.ts_sensitive = TimeMillis() + kMaxMsgLatency;
jbauch9ccedc32016-02-25 01:14:56 -0800376 }
377 msgq_.push_back(msg);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000378 }
jbauch9ccedc32016-02-25 01:14:56 -0800379 WakeUpSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000380}
381
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700382void MessageQueue::PostDelayed(const Location& posted_from,
383 int cmsDelay,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000384 MessageHandler* phandler,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385 uint32_t id,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000386 MessageData* pdata) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700387 return DoDelayPost(posted_from, cmsDelay, TimeAfter(cmsDelay), phandler, id,
388 pdata);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000389}
390
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700391void MessageQueue::PostAt(const Location& posted_from,
392 uint32_t tstamp,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000393 MessageHandler* phandler,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200394 uint32_t id,
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000395 MessageData* pdata) {
Honghai Zhang82d78622016-05-06 11:29:15 -0700396 // This should work even if it is used (unexpectedly).
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700397 int64_t delay = static_cast<uint32_t>(TimeMillis()) - tstamp;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700398 return DoDelayPost(posted_from, delay, tstamp, phandler, id, pdata);
Honghai Zhang82d78622016-05-06 11:29:15 -0700399}
400
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700401void MessageQueue::PostAt(const Location& posted_from,
402 int64_t tstamp,
Honghai Zhang82d78622016-05-06 11:29:15 -0700403 MessageHandler* phandler,
404 uint32_t id,
405 MessageData* pdata) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700406 return DoDelayPost(posted_from, TimeUntil(tstamp), tstamp, phandler, id,
407 pdata);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000408}
409
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700410void MessageQueue::DoDelayPost(const Location& posted_from,
411 int64_t cmsDelay,
Honghai Zhang82d78622016-05-06 11:29:15 -0700412 int64_t tstamp,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413 MessageHandler* phandler,
414 uint32_t id,
415 MessageData* pdata) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700416 if (fStop_) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000417 return;
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700418 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419
420 // Keep thread safe
421 // Add to the priority queue. Gets sorted soonest first.
422 // Signal for the multiplexer to return.
423
jbauch9ccedc32016-02-25 01:14:56 -0800424 {
425 CritScope cs(&crit_);
426 Message msg;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700427 msg.posted_from = posted_from;
jbauch9ccedc32016-02-25 01:14:56 -0800428 msg.phandler = phandler;
429 msg.message_id = id;
430 msg.pdata = pdata;
431 DelayedMessage dmsg(cmsDelay, tstamp, dmsgq_next_num_, msg);
432 dmsgq_.push(dmsg);
433 // If this message queue processes 1 message every millisecond for 50 days,
434 // we will wrap this number. Even then, only messages with identical times
435 // will be misordered, and then only briefly. This is probably ok.
436 VERIFY(0 != ++dmsgq_next_num_);
437 }
438 WakeUpSocketServer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000439}
440
441int MessageQueue::GetDelay() {
442 CritScope cs(&crit_);
443
444 if (!msgq_.empty())
445 return 0;
446
447 if (!dmsgq_.empty()) {
448 int delay = TimeUntil(dmsgq_.top().msTrigger_);
449 if (delay < 0)
450 delay = 0;
451 return delay;
452 }
453
454 return kForever;
455}
456
Peter Boström0c4e06b2015-10-07 12:23:21 +0200457void MessageQueue::Clear(MessageHandler* phandler,
458 uint32_t id,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000459 MessageList* removed) {
460 CritScope cs(&crit_);
461
462 // Remove messages with phandler
463
464 if (fPeekKeep_ && msgPeek_.Match(phandler, id)) {
465 if (removed) {
466 removed->push_back(msgPeek_);
467 } else {
468 delete msgPeek_.pdata;
469 }
470 fPeekKeep_ = false;
471 }
472
473 // Remove from ordered message queue
474
475 for (MessageList::iterator it = msgq_.begin(); it != msgq_.end();) {
476 if (it->Match(phandler, id)) {
477 if (removed) {
478 removed->push_back(*it);
479 } else {
480 delete it->pdata;
481 }
482 it = msgq_.erase(it);
483 } else {
484 ++it;
485 }
486 }
487
488 // Remove from priority queue. Not directly iterable, so use this approach
decurtis@webrtc.org2af30572015-02-21 01:59:50 +0000489
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490 PriorityQueue::container_type::iterator new_end = dmsgq_.container().begin();
491 for (PriorityQueue::container_type::iterator it = new_end;
492 it != dmsgq_.container().end(); ++it) {
493 if (it->msg_.Match(phandler, id)) {
494 if (removed) {
495 removed->push_back(it->msg_);
496 } else {
497 delete it->msg_.pdata;
498 }
499 } else {
decurtis@webrtc.org2af30572015-02-21 01:59:50 +0000500 *new_end++ = *it;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000501 }
502 }
503 dmsgq_.container().erase(new_end, dmsgq_.container().end());
504 dmsgq_.reheap();
505}
506
507void MessageQueue::Dispatch(Message *pmsg) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700508 TRACE_EVENT2("webrtc", "MessageQueue::Dispatch", "src_file_and_line",
509 pmsg->posted_from.file_and_line(), "src_func",
510 pmsg->posted_from.function_name());
511 int64_t start_time = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000512 pmsg->phandler->OnMessage(pmsg);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700513 int64_t end_time = TimeMillis();
514 int64_t diff = TimeDiff(end_time, start_time);
515 if (diff >= kSlowDispatchLoggingThreshold) {
516 LOG(LS_INFO) << "Message took " << diff << "ms to dispatch. Posted from: "
517 << pmsg->posted_from.ToString();
518 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000519}
520
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000521} // namespace rtc