1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop.h"
#include <algorithm>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_pump_default.h"
#include "base/string_util.h"
#include "base/thread_local.h"
#include "mozilla/Atomics.h"
#include "mozilla/Mutex.h"
#include "mozilla/ProfilerRunnable.h"
#include "nsIEventTarget.h"
#include "nsITargetShutdownTask.h"
#include "nsThreadUtils.h"
#if defined(XP_DARWIN)
# include "base/message_pump_mac.h"
#endif
#if defined(XP_UNIX)
# include "base/message_pump_libevent.h"
#endif
#if defined(XP_LINUX) || defined(__DragonFly__) || defined(XP_FREEBSD) || \
defined(XP_NETBSD) || defined(XP_OPENBSD)
# if defined(MOZ_WIDGET_GTK)
# include "base/message_pump_glib.h"
# endif
#endif
#ifdef ANDROID
# include "base/message_pump_android.h"
#endif
#include "nsISerialEventTarget.h"
#include "mozilla/ipc/MessagePump.h"
#include "nsThreadUtils.h"
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
using mozilla::Runnable;
static base::ThreadLocalPointer<MessageLoop>& get_tls_ptr() {
static base::ThreadLocalPointer<MessageLoop> tls_ptr;
return tls_ptr;
}
//------------------------------------------------------------------------------
#if defined(XP_WIN)
// Upon a SEH exception in this thread, it restores the original unhandled
// exception filter.
static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
::SetUnhandledExceptionFilter(old_filter);
return EXCEPTION_CONTINUE_SEARCH;
}
// Retrieves a pointer to the current unhandled exception filter. There
// is no standalone getter method.
static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
top_filter = ::SetUnhandledExceptionFilter(0);
::SetUnhandledExceptionFilter(top_filter);
return top_filter;
}
#endif // defined(XP_WIN)
//------------------------------------------------------------------------------
class MessageLoop::EventTarget : public nsISerialEventTarget,
public nsITargetShutdownTask,
public MessageLoop::DestructionObserver {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIEVENTTARGET_FULL
void TargetShutdown() override {
nsTArray<nsCOMPtr<nsITargetShutdownTask>> shutdownTasks;
{
mozilla::MutexAutoLock lock(mMutex);
if (mShutdownTasksRun) {
return;
}
mShutdownTasksRun = true;
shutdownTasks = std::move(mShutdownTasks);
mShutdownTasks.Clear();
}
for (auto& task : shutdownTasks) {
task->TargetShutdown();
}
}
explicit EventTarget(MessageLoop* aLoop)
: mMutex("MessageLoop::EventTarget"), mLoop(aLoop) {
aLoop->AddDestructionObserver(this);
}
private:
virtual ~EventTarget() {
if (mLoop) {
mLoop->RemoveDestructionObserver(this);
}
MOZ_ASSERT(mShutdownTasks.IsEmpty());
}
void WillDestroyCurrentMessageLoop() override {
{
mozilla::MutexAutoLock lock(mMutex);
// The MessageLoop is being destroyed and we are called from its
// destructor There's no real need to remove ourselves from the
// destruction observer list. But it makes things look tidier.
mLoop->RemoveDestructionObserver(this);
mLoop = nullptr;
}
TargetShutdown();
}
mozilla::Mutex mMutex;
bool mShutdownTasksRun MOZ_GUARDED_BY(mMutex) = false;
nsTArray<nsCOMPtr<nsITargetShutdownTask>> mShutdownTasks
MOZ_GUARDED_BY(mMutex);
MessageLoop* mLoop MOZ_GUARDED_BY(mMutex);
};
NS_IMPL_ISUPPORTS(MessageLoop::EventTarget, nsIEventTarget,
nsISerialEventTarget)
NS_IMETHODIMP_(bool)
MessageLoop::EventTarget::IsOnCurrentThreadInfallible() {
mozilla::MutexAutoLock lock(mMutex);
return mLoop == MessageLoop::current();
}
NS_IMETHODIMP
MessageLoop::EventTarget::IsOnCurrentThread(bool* aResult) {
*aResult = IsOnCurrentThreadInfallible();
return NS_OK;
}
NS_IMETHODIMP
MessageLoop::EventTarget::DispatchFromScript(nsIRunnable* aEvent,
uint32_t aFlags) {
nsCOMPtr<nsIRunnable> event(aEvent);
return Dispatch(event.forget(), aFlags);
}
NS_IMETHODIMP
MessageLoop::EventTarget::Dispatch(already_AddRefed<nsIRunnable> aEvent,
uint32_t aFlags) {
mozilla::MutexAutoLock lock(mMutex);
if (!mLoop) {
return NS_ERROR_NOT_INITIALIZED;
}
if (aFlags != NS_DISPATCH_NORMAL) {
return NS_ERROR_NOT_IMPLEMENTED;
}
mLoop->PostTask(std::move(aEvent));
return NS_OK;
}
NS_IMETHODIMP
MessageLoop::EventTarget::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
uint32_t aDelayMs) {
mozilla::MutexAutoLock lock(mMutex);
if (!mLoop) {
return NS_ERROR_NOT_INITIALIZED;
}
mLoop->PostDelayedTask(std::move(aEvent), aDelayMs);
return NS_OK;
}
NS_IMETHODIMP
MessageLoop::EventTarget::RegisterShutdownTask(nsITargetShutdownTask* aTask) {
mozilla::MutexAutoLock lock(mMutex);
if (!mLoop || mShutdownTasksRun) {
return NS_ERROR_UNEXPECTED;
}
MOZ_ASSERT(!mShutdownTasks.Contains(aTask));
mShutdownTasks.AppendElement(aTask);
return NS_OK;
}
NS_IMETHODIMP
MessageLoop::EventTarget::UnregisterShutdownTask(nsITargetShutdownTask* aTask) {
mozilla::MutexAutoLock lock(mMutex);
if (!mLoop || mShutdownTasksRun) {
return NS_ERROR_UNEXPECTED;
}
return mShutdownTasks.RemoveElement(aTask) ? NS_OK : NS_ERROR_UNEXPECTED;
}
//------------------------------------------------------------------------------
// static
MessageLoop* MessageLoop::current() { return get_tls_ptr().Get(); }
// static
void MessageLoop::set_current(MessageLoop* loop) { get_tls_ptr().Set(loop); }
static mozilla::Atomic<int32_t> message_loop_id_seq(0);
MessageLoop::MessageLoop(Type type, nsISerialEventTarget* aEventTarget)
: type_(type),
id_(++message_loop_id_seq),
nestable_tasks_allowed_(true),
exception_restoration_(false),
incoming_queue_lock_("MessageLoop Incoming Queue Lock"),
state_(NULL),
run_depth_base_(1),
shutting_down_(false),
#ifdef XP_WIN
os_modal_loop_(false),
#endif // XP_WIN
transient_hang_timeout_(0),
permanent_hang_timeout_(0),
next_sequence_num_(0) {
DCHECK(!current()) << "should only have one message loop per thread";
get_tls_ptr().Set(this);
// Must initialize after current() is initialized.
mEventTarget = new EventTarget(this);
switch (type_) {
case TYPE_MOZILLA_PARENT:
MOZ_RELEASE_ASSERT(!aEventTarget);
pump_ = new mozilla::ipc::MessagePump(aEventTarget);
return;
case TYPE_MOZILLA_CHILD:
MOZ_RELEASE_ASSERT(!aEventTarget);
pump_ = new mozilla::ipc::MessagePumpForChildProcess();
// There is a MessageLoop Run call from XRE_InitChildProcess
// and another one from MessagePumpForChildProcess. The one
// from MessagePumpForChildProcess becomes the base, so we need
// to set run_depth_base_ to 2 or we'll never be able to process
// Idle tasks.
run_depth_base_ = 2;
return;
case TYPE_MOZILLA_NONMAINTHREAD:
pump_ = new mozilla::ipc::MessagePumpForNonMainThreads(aEventTarget);
return;
#if defined(XP_WIN) || defined(XP_DARWIN)
case TYPE_MOZILLA_NONMAINUITHREAD:
pump_ = new mozilla::ipc::MessagePumpForNonMainUIThreads(aEventTarget);
return;
#elif defined(MOZ_WIDGET_ANDROID)
case TYPE_MOZILLA_ANDROID_UI:
MOZ_RELEASE_ASSERT(aEventTarget);
pump_ = new mozilla::ipc::MessagePumpForAndroidUI(aEventTarget);
return;
#endif // defined(MOZ_WIDGET_ANDROID)
default:
// Create one of Chromium's standard MessageLoop types below.
break;
}
#if defined(XP_WIN)
// TODO(rvargas): Get rid of the OS guards.
if (type_ == TYPE_DEFAULT) {
pump_ = new base::MessagePumpDefault();
} else if (type_ == TYPE_IO) {
pump_ = new base::MessagePumpForIO();
} else {
DCHECK(type_ == TYPE_UI);
pump_ = new base::MessagePumpForUI();
}
#else
if (type_ == TYPE_UI) {
# if defined(XP_DARWIN)
pump_ = base::MessagePumpMac::Create();
# elif defined(XP_LINUX) || defined(__DragonFly__) || defined(XP_FREEBSD) || \
defined(XP_NETBSD) || defined(XP_OPENBSD)
pump_ = new base::MessagePumpForUI();
# endif // XP_LINUX
} else if (type_ == TYPE_IO) {
pump_ = new base::MessagePumpLibevent();
} else {
pump_ = new base::MessagePumpDefault();
}
#endif
// We want GetCurrentSerialEventTarget() to return the real nsThread if it
// will be used to dispatch tasks. However, under all other cases; we'll want
// it to return this MessageLoop's EventTarget.
if (nsISerialEventTarget* thread = pump_->GetXPCOMThread()) {
MOZ_ALWAYS_SUCCEEDS(thread->RegisterShutdownTask(mEventTarget));
} else {
mozilla::SerialEventTargetGuard::Set(mEventTarget);
}
}
MessageLoop::~MessageLoop() {
DCHECK(this == current());
// Let interested parties have one last shot at accessing this.
FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
WillDestroyCurrentMessageLoop());
DCHECK(!state_);
// Clean up any unprocessed tasks, but take care: deleting a task could
// result in the addition of more tasks (e.g., via DeleteSoon). We set a
// limit on the number of times we will allow a deleted task to generate more
// tasks. Normally, we should only pass through this loop once or twice. If
// we end up hitting the loop limit, then it is probably due to one task that
// is being stubborn. Inspect the queues to see who is left.
bool did_work;
for (int i = 0; i < 100; ++i) {
DeletePendingTasks();
ReloadWorkQueue();
// If we end up with empty queues, then break out of the loop.
did_work = DeletePendingTasks();
if (!did_work) break;
}
DCHECK(!did_work);
// OK, now make it so that no one can find us.
get_tls_ptr().Set(NULL);
}
void MessageLoop::AddDestructionObserver(DestructionObserver* obs) {
DCHECK(this == current());
destruction_observers_.AddObserver(obs);
}
void MessageLoop::RemoveDestructionObserver(DestructionObserver* obs) {
DCHECK(this == current());
destruction_observers_.RemoveObserver(obs);
}
void MessageLoop::Run() {
AutoRunState save_state(this);
RunHandler();
}
// Runs the loop in two different SEH modes:
// enable_SEH_restoration_ = false : any unhandled exception goes to the last
// one that calls SetUnhandledExceptionFilter().
// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
// that was existed before the loop was run.
void MessageLoop::RunHandler() {
#if defined(XP_WIN)
if (exception_restoration_) {
LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
MOZ_SEH_TRY { RunInternal(); }
MOZ_SEH_EXCEPT(SEHFilter(current_filter)) {}
return;
}
#endif
RunInternal();
}
//------------------------------------------------------------------------------
void MessageLoop::RunInternal() {
DCHECK(this == current());
pump_->Run(this);
}
//------------------------------------------------------------------------------
// Wrapper functions for use in above message loop framework.
bool MessageLoop::ProcessNextDelayedNonNestableTask() {
if (state_->run_depth > run_depth_base_) return false;
if (deferred_non_nestable_work_queue_.empty()) return false;
nsCOMPtr<nsIRunnable> task =
std::move(deferred_non_nestable_work_queue_.front().task);
deferred_non_nestable_work_queue_.pop();
RunTask(task.forget());
return true;
}
//------------------------------------------------------------------------------
void MessageLoop::Quit() {
DCHECK(current() == this);
if (state_) {
state_->quit_received = true;
} else {
NOTREACHED() << "Must be inside Run to call Quit";
}
}
void MessageLoop::PostTask(already_AddRefed<nsIRunnable> task) {
PostTask_Helper(std::move(task), 0);
}
void MessageLoop::PostDelayedTask(already_AddRefed<nsIRunnable> task,
int delay_ms) {
PostTask_Helper(std::move(task), delay_ms);
}
void MessageLoop::PostIdleTask(already_AddRefed<nsIRunnable> task) {
DCHECK(current() == this);
MOZ_ASSERT(NS_IsMainThread());
PendingTask pending_task(std::move(task), false);
mozilla::LogRunnable::LogDispatch(pending_task.task.get());
deferred_non_nestable_work_queue_.push(std::move(pending_task));
}
// Possibly called on a background thread!
void MessageLoop::PostTask_Helper(already_AddRefed<nsIRunnable> task,
int delay_ms) {
if (nsISerialEventTarget* target = pump_->GetXPCOMThread()) {
nsresult rv;
if (delay_ms) {
rv = target->DelayedDispatch(std::move(task), delay_ms);
} else {
rv = target->Dispatch(std::move(task), 0);
}
MOZ_ALWAYS_SUCCEEDS(rv);
return;
}
// Tasks should only be queued before or during the Run loop, not after.
MOZ_ASSERT(!shutting_down_);
PendingTask pending_task(std::move(task), true);
if (delay_ms > 0) {
pending_task.delayed_run_time =
TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms);
} else {
DCHECK(delay_ms == 0) << "delay should not be negative";
}
// Warning: Don't try to short-circuit, and handle this thread's tasks more
// directly, as it could starve handling of foreign threads. Put every task
// into this queue.
RefPtr<base::MessagePump> pump;
{
mozilla::MutexAutoLock locked(incoming_queue_lock_);
mozilla::LogRunnable::LogDispatch(pending_task.task.get());
incoming_queue_.push(std::move(pending_task));
pump = pump_;
}
// Since the incoming_queue_ may contain a task that destroys this message
// loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
// We use a stack-based reference to the message pump so that we can call
// ScheduleWork outside of incoming_queue_lock_.
pump->ScheduleWork();
}
void MessageLoop::SetNestableTasksAllowed(bool allowed) {
if (nestable_tasks_allowed_ != allowed) {
nestable_tasks_allowed_ = allowed;
if (!nestable_tasks_allowed_) return;
// Start the native pump if we are not already pumping.
pump_->ScheduleWorkForNestedLoop();
}
}
void MessageLoop::ScheduleWork() {
// Start the native pump if we are not already pumping.
pump_->ScheduleWork();
}
bool MessageLoop::NestableTasksAllowed() const {
return nestable_tasks_allowed_;
}
//------------------------------------------------------------------------------
void MessageLoop::RunTask(already_AddRefed<nsIRunnable> aTask) {
DCHECK(nestable_tasks_allowed_);
// Execute the task and assume the worst: It is probably not reentrant.
nestable_tasks_allowed_ = false;
nsCOMPtr<nsIRunnable> task = aTask;
{
mozilla::LogRunnable::Run log(task.get());
AUTO_PROFILE_FOLLOWING_RUNNABLE(task);
task->Run();
task = nullptr;
}
nestable_tasks_allowed_ = true;
}
bool MessageLoop::DeferOrRunPendingTask(PendingTask&& pending_task) {
if (pending_task.nestable || state_->run_depth <= run_depth_base_) {
RunTask(pending_task.task.forget());
// Show that we ran a task (Note: a new one might arrive as a
// consequence!).
return true;
}
// We couldn't run the task now because we're in a nested message loop
// and the task isn't nestable.
mozilla::LogRunnable::LogDispatch(pending_task.task.get());
deferred_non_nestable_work_queue_.push(std::move(pending_task));
return false;
}
void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
// Move to the delayed work queue. Initialize the sequence number
// before inserting into the delayed_work_queue_. The sequence number
// is used to faciliate FIFO sorting when two tasks have the same
// delayed_run_time value.
PendingTask new_pending_task(pending_task);
new_pending_task.sequence_num = next_sequence_num_++;
mozilla::LogRunnable::LogDispatch(new_pending_task.task.get());
delayed_work_queue_.push(std::move(new_pending_task));
}
void MessageLoop::ReloadWorkQueue() {
// We can improve performance of our loading tasks from incoming_queue_ to
// work_queue_ by waiting until the last minute (work_queue_ is empty) to
// load. That reduces the number of locks-per-task significantly when our
// queues get large.
if (!work_queue_.empty())
return; // Wait till we *really* need to lock and load.
// Acquire all we can from the inter-thread queue with one lock acquisition.
{
mozilla::MutexAutoLock lock(incoming_queue_lock_);
if (incoming_queue_.empty()) return;
std::swap(incoming_queue_, work_queue_);
DCHECK(incoming_queue_.empty());
}
}
bool MessageLoop::DeletePendingTasks() {
MOZ_ASSERT(work_queue_.empty());
bool did_work = !deferred_non_nestable_work_queue_.empty();
while (!deferred_non_nestable_work_queue_.empty()) {
deferred_non_nestable_work_queue_.pop();
}
did_work |= !delayed_work_queue_.empty();
while (!delayed_work_queue_.empty()) {
delayed_work_queue_.pop();
}
return did_work;
}
bool MessageLoop::DoWork() {
if (!nestable_tasks_allowed_) {
// Task can't be executed right now.
return false;
}
for (;;) {
ReloadWorkQueue();
if (work_queue_.empty()) break;
// Execute oldest task.
do {
PendingTask pending_task = std::move(work_queue_.front());
work_queue_.pop();
if (!pending_task.delayed_run_time.is_null()) {
// NB: Don't move, because we use this later!
AddToDelayedWorkQueue(pending_task);
// If we changed the topmost task, then it is time to re-schedule.
if (delayed_work_queue_.top().task == pending_task.task)
pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
} else {
if (DeferOrRunPendingTask(std::move(pending_task))) return true;
}
} while (!work_queue_.empty());
}
// Nothing happened.
return false;
}
bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) {
if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
*next_delayed_work_time = TimeTicks();
return false;
}
if (delayed_work_queue_.top().delayed_run_time > TimeTicks::Now()) {
*next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
return false;
}
PendingTask pending_task = delayed_work_queue_.top();
delayed_work_queue_.pop();
if (!delayed_work_queue_.empty())
*next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
return DeferOrRunPendingTask(std::move(pending_task));
}
bool MessageLoop::DoIdleWork() {
if (ProcessNextDelayedNonNestableTask()) return true;
if (state_->quit_received) pump_->Quit();
return false;
}
//------------------------------------------------------------------------------
// MessageLoop::AutoRunState
MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
// Top-level Run should only get called once.
MOZ_ASSERT(!loop_->shutting_down_);
// Make the loop reference us.
previous_state_ = loop_->state_;
if (previous_state_) {
run_depth = previous_state_->run_depth + 1;
} else {
run_depth = 1;
}
loop_->state_ = this;
// Initialize the other fields:
quit_received = false;
#if defined(XP_WIN)
dispatcher = NULL;
#endif
}
MessageLoop::AutoRunState::~AutoRunState() {
loop_->state_ = previous_state_;
// If exiting a top-level Run, then we're shutting down.
loop_->shutting_down_ = !previous_state_;
}
//------------------------------------------------------------------------------
// MessageLoop::PendingTask
bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
// Since the top of a priority queue is defined as the "greatest" element, we
// need to invert the comparison here. We want the smaller time to be at the
// top of the heap.
if (delayed_run_time < other.delayed_run_time) return false;
if (delayed_run_time > other.delayed_run_time) return true;
// If the times happen to match, then we use the sequence number to decide.
// Compare the difference to support integer roll-over.
return (sequence_num - other.sequence_num) > 0;
}
//------------------------------------------------------------------------------
// MessageLoop::SerialEventTarget
nsISerialEventTarget* MessageLoop::SerialEventTarget() { return mEventTarget; }
//------------------------------------------------------------------------------
// MessageLoopForUI
#if defined(XP_WIN)
void MessageLoopForUI::Run(Dispatcher* dispatcher) {
AutoRunState save_state(this);
state_->dispatcher = dispatcher;
RunHandler();
}
void MessageLoopForUI::AddObserver(Observer* observer) {
pump_win()->AddObserver(observer);
}
void MessageLoopForUI::RemoveObserver(Observer* observer) {
pump_win()->RemoveObserver(observer);
}
void MessageLoopForUI::WillProcessMessage(const MSG& message) {
pump_win()->WillProcessMessage(message);
}
void MessageLoopForUI::DidProcessMessage(const MSG& message) {
pump_win()->DidProcessMessage(message);
}
void MessageLoopForUI::PumpOutPendingPaintMessages() {
pump_ui()->PumpOutPendingPaintMessages();
}
#endif // defined(XP_WIN)
//------------------------------------------------------------------------------
// MessageLoopForIO
#if defined(XP_WIN)
void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) {
pump_io()->RegisterIOHandler(file, handler);
}
bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) {
return pump_io()->WaitForIOCompletion(timeout, filter);
}
#else
bool MessageLoopForIO::WatchFileDescriptor(int fd, bool persistent, Mode mode,
FileDescriptorWatcher* controller,
Watcher* delegate) {
return pump_libevent()->WatchFileDescriptor(
fd, persistent, static_cast<base::MessagePumpLibevent::Mode>(mode),
controller, delegate);
}
bool MessageLoopForIO::CatchSignal(int sig, SignalEvent* sigevent,
SignalWatcher* delegate) {
return pump_libevent()->CatchSignal(sig, sigevent, delegate);
}
#endif
|