blob: 10f4a262f55f422393f7ad88f6e56986ec6e1221 [file] [log] [blame]
jar@chromium.org34571142011-04-05 13:48:53 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
darin@google.com6ddeb842008-08-15 16:31:20 +09005#include "base/message_loop.h"
6
darin@google.com981f3552008-08-16 12:09:05 +09007#include <algorithm>
8
ajwong@chromium.org94d2a582011-04-21 01:02:23 +09009#include "base/bind.h"
mmentovai@google.comfa5f9932008-08-22 07:26:06 +090010#include "base/compiler_specific.h"
apatrick@chromium.org87164042011-05-20 07:28:25 +090011#include "base/debug/alias.h"
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090012#include "base/lazy_instance.h"
initial.commit3f4a7322008-07-27 06:49:38 +090013#include "base/logging.h"
thestig@chromium.orgefd4aaf2011-06-15 13:14:23 +090014#include "base/memory/scoped_ptr.h"
darin@google.com12d40bb2008-08-20 03:36:23 +090015#include "base/message_pump_default.h"
brettw@chromium.org275c2ec2010-10-14 13:38:38 +090016#include "base/metrics/histogram.h"
timurrrr@chromium.org490200b2011-01-05 04:06:51 +090017#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
brettw@chromium.org63965582010-12-31 07:18:56 +090018#include "base/threading/thread_local.h"
mbelshe@chromium.orgbee85b32011-05-16 04:20:49 +090019#include "base/time.h"
ajwong@chromium.org94d2a582011-04-21 01:02:23 +090020#include "base/tracked_objects.h"
initial.commit3f4a7322008-07-27 06:49:38 +090021
mark@chromium.org059d0492008-09-24 06:08:28 +090022#if defined(OS_MACOSX)
23#include "base/message_pump_mac.h"
24#endif
dkegel@google.com9e044ae2008-09-19 03:46:26 +090025#if defined(OS_POSIX)
26#include "base/message_pump_libevent.h"
27#endif
michaelbai@google.com686190b2011-08-03 01:11:16 +090028#if defined(OS_ANDROID)
29#include "base/message_pump_android.h"
30#endif
31#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
ajwong@chromium.org94d2a582011-04-21 01:02:23 +090032#include <gdk/gdk.h>
33#include <gdk/gdkx.h>
sadrul@chromium.orgf7f3b262011-06-25 05:10:25 +090034#endif // defined(OS_POSIX) && !defined(OS_MACOSX)
dkegel@google.com9e044ae2008-09-19 03:46:26 +090035
dsh@google.com0f8dd262008-10-28 05:43:33 +090036using base::TimeDelta;
jar@chromium.org9b0fb062010-11-07 07:23:29 +090037using base::TimeTicks;
dsh@google.com0f8dd262008-10-28 05:43:33 +090038
erg@chromium.orga7528522010-07-16 02:23:23 +090039namespace {
40
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090041// A lazily created thread local storage for quick access to a thread's message
42// loop, if one exists. This should be safe and free of static constructors.
erg@chromium.orga7528522010-07-16 02:23:23 +090043base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090044 base::LINKER_INITIALIZED);
initial.commit3f4a7322008-07-27 06:49:38 +090045
initial.commit3f4a7322008-07-27 06:49:38 +090046// Logical events for Histogram profiling. Run with -message-loop-histogrammer
47// to get an accounting of messages and actions taken on each thread.
erg@chromium.orga7528522010-07-16 02:23:23 +090048const int kTaskRunEvent = 0x1;
49const int kTimerEvent = 0x2;
initial.commit3f4a7322008-07-27 06:49:38 +090050
51// Provide range of message IDs for use in histogramming and debug display.
erg@chromium.orga7528522010-07-16 02:23:23 +090052const int kLeastNonZeroMessageId = 1;
53const int kMaxMessageId = 1099;
54const int kNumberOfDistinctMessagesDisplayed = 1100;
55
56// Provide a macro that takes an expression (such as a constant, or macro
57// constant) and creates a pair to initalize an array of pairs. In this case,
58// our pair consists of the expressions value, and the "stringized" version
59// of the expression (i.e., the exrpression put in quotes). For example, if
60// we have:
61// #define FOO 2
62// #define BAR 5
63// then the following:
64// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
65// will expand to:
66// {7, "FOO + BAR"}
67// We use the resulting array as an argument to our histogram, which reads the
68// number as a bucket identifier, and proceeds to use the corresponding name
69// in the pair (i.e., the quoted string) when printing out a histogram.
70#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
71
brettw@chromium.org275c2ec2010-10-14 13:38:38 +090072const base::LinearHistogram::DescriptionPair event_descriptions_[] = {
erg@chromium.orga7528522010-07-16 02:23:23 +090073 // Provide some pretty print capability in our histogram for our internal
74 // messages.
75
76 // A few events we handle (kindred to messages), and used to profile actions.
77 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
78 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
79
80 {-1, NULL} // The list must be null terminated, per API to histogram.
81};
82
83bool enable_histogrammer_ = false;
84
michaelbai@google.com686190b2011-08-03 01:11:16 +090085MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL;
86
erg@chromium.orga7528522010-07-16 02:23:23 +090087} // namespace
initial.commit3f4a7322008-07-27 06:49:38 +090088
89//------------------------------------------------------------------------------
90
darin@google.com981f3552008-08-16 12:09:05 +090091#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090092
initial.commit3f4a7322008-07-27 06:49:38 +090093// Upon a SEH exception in this thread, it restores the original unhandled
94// exception filter.
95static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
96 ::SetUnhandledExceptionFilter(old_filter);
97 return EXCEPTION_CONTINUE_SEARCH;
98}
99
100// Retrieves a pointer to the current unhandled exception filter. There
101// is no standalone getter method.
102static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
103 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
104 top_filter = ::SetUnhandledExceptionFilter(0);
105 ::SetUnhandledExceptionFilter(top_filter);
106 return top_filter;
107}
108
darin@google.com981f3552008-08-16 12:09:05 +0900109#endif // defined(OS_WIN)
110
initial.commit3f4a7322008-07-27 06:49:38 +0900111//------------------------------------------------------------------------------
112
erg@chromium.org493f5f62010-07-16 06:03:54 +0900113MessageLoop::TaskObserver::TaskObserver() {
114}
115
116MessageLoop::TaskObserver::~TaskObserver() {
117}
118
119MessageLoop::DestructionObserver::~DestructionObserver() {
120}
121
122//------------------------------------------------------------------------------
123
darin@google.comd936b5b2008-08-26 14:53:57 +0900124MessageLoop::MessageLoop(Type type)
125 : type_(type),
darin@google.comee6fa722008-08-13 08:25:43 +0900126 nestable_tasks_allowed_(true),
darin@google.com12d40bb2008-08-20 03:36:23 +0900127 exception_restoration_(false),
jar@chromium.org34571142011-04-05 13:48:53 +0900128 message_histogram_(NULL),
darin@google.combe165ae2008-09-07 17:08:29 +0900129 state_(NULL),
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900130 should_leak_tasks_(true),
ananta@chromium.orgc542fec2011-03-24 12:40:28 +0900131#ifdef OS_WIN
132 os_modal_loop_(false),
133#endif // OS_WIN
darin@google.combe165ae2008-09-07 17:08:29 +0900134 next_sequence_num_(0) {
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +0900135 DCHECK(!current()) << "should only have one message loop per thread";
136 lazy_tls_ptr.Pointer()->Set(this);
darin@google.comd936b5b2008-08-26 14:53:57 +0900137
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900138// TODO(rvargas): Get rid of the OS guards.
darin@google.com981f3552008-08-16 12:09:05 +0900139#if defined(OS_WIN)
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900140#define MESSAGE_PUMP_UI new base::MessagePumpForUI()
141#define MESSAGE_PUMP_IO new base::MessagePumpForIO()
142#elif defined(OS_MACOSX)
143#define MESSAGE_PUMP_UI base::MessagePumpMac::Create()
144#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
michaelbai@google.com686190b2011-08-03 01:11:16 +0900145#elif defined(OS_ANDROID)
146#define MESSAGE_PUMP_UI new base::MessagePumpForUI()
147#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
dnicoara@chromium.org63a7b5d2011-08-12 03:20:22 +0900148#elif defined(USE_WAYLAND)
149#define MESSAGE_PUMP_UI new base::MessagePumpWayland()
150#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
rjkroege@google.com3080f442010-10-23 01:17:47 +0900151#elif defined(TOUCH_UI)
sadrul@chromium.orgf7f3b262011-06-25 05:10:25 +0900152#define MESSAGE_PUMP_UI new base::MessagePumpX()
rjkroege@google.com3080f442010-10-23 01:17:47 +0900153#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
abarth@chromium.org1f1c2172010-12-01 17:45:51 +0900154#elif defined(OS_NACL)
155// Currently NaCl doesn't have a UI or an IO MessageLoop.
156// TODO(abarth): Figure out if we need these.
157#define MESSAGE_PUMP_UI NULL
158#define MESSAGE_PUMP_IO NULL
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900159#elif defined(OS_POSIX) // POSIX but not MACOSX.
sadrul@chromium.orgf7f3b262011-06-25 05:10:25 +0900160#define MESSAGE_PUMP_UI new base::MessagePumpGtk()
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900161#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
evan@chromium.org875bb6e2009-12-29 09:32:52 +0900162#else
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900163#error Not implemented
evan@chromium.org875bb6e2009-12-29 09:32:52 +0900164#endif
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900165
166 if (type_ == TYPE_UI) {
michaelbai@google.com686190b2011-08-03 01:11:16 +0900167 if (message_pump_for_ui_factory_)
168 pump_ = message_pump_for_ui_factory_();
169 else
170 pump_ = MESSAGE_PUMP_UI;
dsh@google.com119a2522008-10-04 01:52:59 +0900171 } else if (type_ == TYPE_IO) {
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900172 pump_ = MESSAGE_PUMP_IO;
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900173 } else {
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900174 DCHECK_EQ(TYPE_DEFAULT, type_);
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900175 pump_ = new base::MessagePumpDefault();
176 }
initial.commit3f4a7322008-07-27 06:49:38 +0900177}
178
179MessageLoop::~MessageLoop() {
thestig@chromium.org226880a2010-11-11 05:28:06 +0900180 DCHECK_EQ(this, current());
darin@google.com965e5342008-08-06 08:16:41 +0900181
darin@google.com0e500502008-09-09 14:55:35 +0900182 DCHECK(!state_);
183
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900184 // Clean up any unprocessed tasks, but take care: deleting a task could
185 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
186 // limit on the number of times we will allow a deleted task to generate more
187 // tasks. Normally, we should only pass through this loop once or twice. If
188 // we end up hitting the loop limit, then it is probably due to one task that
189 // is being stubborn. Inspect the queues to see who is left.
190 bool did_work;
191 for (int i = 0; i < 100; ++i) {
192 DeletePendingTasks();
193 ReloadWorkQueue();
194 // If we end up with empty queues, then break out of the loop.
195 did_work = DeletePendingTasks();
196 if (!did_work)
197 break;
darin@google.com0e500502008-09-09 14:55:35 +0900198 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900199 DCHECK(!did_work);
200
sanjeevr@chromium.org03b44d52010-11-30 09:25:29 +0900201 // Let interested parties have one last shot at accessing this.
202 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
203 WillDestroyCurrentMessageLoop());
204
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900205 // OK, now make it so that no one can find us.
deanm@chromium.orge4cc5922008-09-10 20:14:56 +0900206 lazy_tls_ptr.Pointer()->Set(NULL);
mbelshe@chromium.orgbee85b32011-05-16 04:20:49 +0900207
208#if defined(OS_WIN)
209 // If we left the high-resolution timer activated, deactivate it now.
210 // Doing this is not-critical, it is mainly to make sure we track
211 // the high resolution timer activations properly in our unit tests.
212 if (!high_resolution_timer_expiration_.is_null()) {
213 base::Time::ActivateHighResolutionTimer(false);
214 high_resolution_timer_expiration_ = base::TimeTicks();
215 }
216#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900217}
218
erg@google.com67a25432011-01-08 05:23:43 +0900219// static
220MessageLoop* MessageLoop::current() {
221 // TODO(darin): sadly, we cannot enable this yet since people call us even
222 // when they have no intention of using us.
223 // DCHECK(loop) << "Ouch, did you forget to initialize me?";
224 return lazy_tls_ptr.Pointer()->Get();
225}
226
227// static
228void MessageLoop::EnableHistogrammer(bool enable) {
229 enable_histogrammer_ = enable;
230}
231
michaelbai@google.com686190b2011-08-03 01:11:16 +0900232// static
233void MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) {
234 DCHECK(!message_pump_for_ui_factory_);
235 message_pump_for_ui_factory_ = factory;
236}
237
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900238void MessageLoop::AddDestructionObserver(
239 DestructionObserver* destruction_observer) {
thestig@chromium.org226880a2010-11-11 05:28:06 +0900240 DCHECK_EQ(this, current());
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900241 destruction_observers_.AddObserver(destruction_observer);
darin@google.com965e5342008-08-06 08:16:41 +0900242}
243
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900244void MessageLoop::RemoveDestructionObserver(
245 DestructionObserver* destruction_observer) {
thestig@chromium.org226880a2010-11-11 05:28:06 +0900246 DCHECK_EQ(this, current());
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900247 destruction_observers_.RemoveObserver(destruction_observer);
darin@google.com965e5342008-08-06 08:16:41 +0900248}
249
darin@google.combe165ae2008-09-07 17:08:29 +0900250void MessageLoop::PostTask(
251 const tracked_objects::Location& from_here, Task* task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900252 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900253 PendingTask pending_task(
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900254 base::Bind(
255 &base::subtle::TaskClosureAdapter::Run,
256 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900257 from_here,
258 CalculateDelayedRuntime(0), true);
259 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900260}
261
262void MessageLoop::PostDelayedTask(
phajdan.jr@chromium.orgc3c92252009-06-18 02:23:51 +0900263 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900264 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900265 PendingTask pending_task(
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900266 base::Bind(
267 &base::subtle::TaskClosureAdapter::Run,
268 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900269 from_here,
270 CalculateDelayedRuntime(delay_ms), true);
271 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900272}
273
274void MessageLoop::PostNonNestableTask(
275 const tracked_objects::Location& from_here, Task* task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900276 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900277 PendingTask pending_task(
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900278 base::Bind(
279 &base::subtle::TaskClosureAdapter::Run,
280 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900281 from_here,
282 CalculateDelayedRuntime(0), false);
283 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900284}
285
286void MessageLoop::PostNonNestableDelayedTask(
phajdan.jr@chromium.orgc3c92252009-06-18 02:23:51 +0900287 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900288 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900289 PendingTask pending_task(
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900290 base::Bind(
291 &base::subtle::TaskClosureAdapter::Run,
292 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)),
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900293 from_here,
294 CalculateDelayedRuntime(delay_ms), false);
295 AddToIncomingQueue(&pending_task);
296}
297
298void MessageLoop::PostTask(
299 const tracked_objects::Location& from_here, const base::Closure& task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900300 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900301 PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), true);
302 AddToIncomingQueue(&pending_task);
303}
304
305void MessageLoop::PostDelayedTask(
306 const tracked_objects::Location& from_here, const base::Closure& task,
307 int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900308 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900309 PendingTask pending_task(task, from_here,
310 CalculateDelayedRuntime(delay_ms), true);
311 AddToIncomingQueue(&pending_task);
312}
313
314void MessageLoop::PostNonNestableTask(
315 const tracked_objects::Location& from_here, const base::Closure& task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900316 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900317 PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), false);
318 AddToIncomingQueue(&pending_task);
319}
320
321void MessageLoop::PostNonNestableDelayedTask(
322 const tracked_objects::Location& from_here, const base::Closure& task,
323 int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900324 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900325 PendingTask pending_task(task, from_here,
326 CalculateDelayedRuntime(delay_ms), false);
327 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900328}
329
erg@google.com67a25432011-01-08 05:23:43 +0900330void MessageLoop::Run() {
331 AutoRunState save_state(this);
332 RunHandler();
333}
darin@google.com0795f572008-08-30 09:22:48 +0900334
erg@google.com67a25432011-01-08 05:23:43 +0900335void MessageLoop::RunAllPending() {
336 AutoRunState save_state(this);
337 state_->quit_received = true; // Means run until we would otherwise block.
338 RunHandler();
339}
darin@google.com0795f572008-08-30 09:22:48 +0900340
erg@google.com67a25432011-01-08 05:23:43 +0900341void MessageLoop::Quit() {
342 DCHECK_EQ(this, current());
343 if (state_) {
344 state_->quit_received = true;
darin@google.com0795f572008-08-30 09:22:48 +0900345 } else {
erg@google.com67a25432011-01-08 05:23:43 +0900346 NOTREACHED() << "Must be inside Run to call Quit";
darin@google.com0795f572008-08-30 09:22:48 +0900347 }
erg@google.com67a25432011-01-08 05:23:43 +0900348}
darin@google.com0795f572008-08-30 09:22:48 +0900349
erg@google.com67a25432011-01-08 05:23:43 +0900350void MessageLoop::QuitNow() {
351 DCHECK_EQ(this, current());
352 if (state_) {
353 pump_->Quit();
354 } else {
355 NOTREACHED() << "Must be inside Run to call Quit";
mbelshe@chromium.orgde50b7d2010-06-29 13:58:15 +0900356 }
initial.commit3f4a7322008-07-27 06:49:38 +0900357}
358
359void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900360 if (nestable_tasks_allowed_ != allowed) {
361 nestable_tasks_allowed_ = allowed;
362 if (!nestable_tasks_allowed_)
363 return;
364 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900365 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900366 }
initial.commit3f4a7322008-07-27 06:49:38 +0900367}
368
369bool MessageLoop::NestableTasksAllowed() const {
370 return nestable_tasks_allowed_;
371}
372
jcampan@chromium.orgeac57172009-07-02 04:53:59 +0900373bool MessageLoop::IsNested() {
374 return state_->run_depth > 1;
375}
376
erg@google.com67a25432011-01-08 05:23:43 +0900377void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
378 DCHECK_EQ(this, current());
379 task_observers_.AddObserver(task_observer);
380}
381
382void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
383 DCHECK_EQ(this, current());
384 task_observers_.RemoveObserver(task_observer);
385}
386
willchan@chromium.org3a397672011-01-26 09:53:48 +0900387void MessageLoop::AssertIdle() const {
388 // We only check |incoming_queue_|, since we don't want to lock |work_queue_|.
389 base::AutoLock lock(incoming_queue_lock_);
390 DCHECK(incoming_queue_.empty());
391}
392
initial.commit3f4a7322008-07-27 06:49:38 +0900393//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900394
erg@google.com67a25432011-01-08 05:23:43 +0900395// Runs the loop in two different SEH modes:
396// enable_SEH_restoration_ = false : any unhandled exception goes to the last
397// one that calls SetUnhandledExceptionFilter().
398// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
399// that was existed before the loop was run.
400void MessageLoop::RunHandler() {
401#if defined(OS_WIN)
402 if (exception_restoration_) {
403 RunInternalInSEHFrame();
404 return;
405 }
406#endif
407
408 RunInternal();
409}
410
411#if defined(OS_WIN)
412__declspec(noinline) void MessageLoop::RunInternalInSEHFrame() {
413 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
414 __try {
415 RunInternal();
416 } __except(SEHFilter(current_filter)) {
417 }
418 return;
419}
420#endif
421
422void MessageLoop::RunInternal() {
423 DCHECK_EQ(this, current());
424
425 StartHistogrammer();
426
michaelbai@google.com686190b2011-08-03 01:11:16 +0900427#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
erg@google.com67a25432011-01-08 05:23:43 +0900428 if (state_->dispatcher && type() == TYPE_UI) {
429 static_cast<base::MessagePumpForUI*>(pump_.get())->
430 RunWithDispatcher(this, state_->dispatcher);
431 return;
432 }
433#endif
434
435 pump_->Run(this);
436}
437
438bool MessageLoop::ProcessNextDelayedNonNestableTask() {
439 if (state_->run_depth != 1)
440 return false;
441
442 if (deferred_non_nestable_work_queue_.empty())
443 return false;
444
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900445 PendingTask pending_task = deferred_non_nestable_work_queue_.front();
erg@google.com67a25432011-01-08 05:23:43 +0900446 deferred_non_nestable_work_queue_.pop();
447
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900448 RunTask(pending_task);
erg@google.com67a25432011-01-08 05:23:43 +0900449 return true;
450}
451
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900452void MessageLoop::RunTask(const PendingTask& pending_task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900453 DCHECK(nestable_tasks_allowed_);
454 // Execute the task and assume the worst: It is probably not reentrant.
455 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900456
apatrick@chromium.org87164042011-05-20 07:28:25 +0900457 // Before running the task, store the program counter where it was posted
458 // and deliberately alias it to ensure it is on the stack if the task
459 // crashes. Be careful not to assume that the variable itself will have the
460 // expected value when displayed by the optimizer in an optimized build.
461 // Look at a memory dump of the stack.
462 const void* program_counter = pending_task.birth_program_counter;
463 base::debug::Alias(&program_counter);
464
darin@google.combe165ae2008-09-07 17:08:29 +0900465 HistogramEvent(kTaskRunEvent);
willchan@chromium.orga9047632010-06-10 06:20:41 +0900466 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900467 WillProcessTask(pending_task.time_posted));
468 pending_task.task.Run();
469 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
470 DidProcessTask(pending_task.time_posted));
471
472#if defined(TRACK_ALL_TASK_OBJECTS)
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900473 tracked_objects::ThreadData::TallyADeathIfActive(
474 pending_task.post_births,
475 TimeTicks::Now() - pending_task.time_posted);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900476#endif // defined(TRACK_ALL_TASK_OBJECTS)
darin@google.combe165ae2008-09-07 17:08:29 +0900477
478 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900479}
480
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900481bool MessageLoop::DeferOrRunPendingTask(
482 const PendingTask& pending_task) {
darin@google.combe165ae2008-09-07 17:08:29 +0900483 if (pending_task.nestable || state_->run_depth == 1) {
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900484 RunTask(pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900485 // Show that we ran a task (Note: a new one might arrive as a
486 // consequence!).
487 return true;
488 }
489
490 // We couldn't run the task now because we're in a nested message loop
491 // and the task isn't nestable.
492 deferred_non_nestable_work_queue_.push(pending_task);
493 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900494}
495
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900496void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
497 // Move to the delayed work queue. Initialize the sequence number
498 // before inserting into the delayed_work_queue_. The sequence number
499 // is used to faciliate FIFO sorting when two tasks have the same
500 // delayed_run_time value.
501 PendingTask new_pending_task(pending_task);
502 new_pending_task.sequence_num = next_sequence_num_++;
503 delayed_work_queue_.push(new_pending_task);
504}
505
initial.commit3f4a7322008-07-27 06:49:38 +0900506void MessageLoop::ReloadWorkQueue() {
507 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900508 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
509 // load. That reduces the number of locks-per-task significantly when our
darin@google.combe165ae2008-09-07 17:08:29 +0900510 // queues get large.
511 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900512 return; // Wait till we *really* need to lock and load.
513
514 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commit3f4a7322008-07-27 06:49:38 +0900515 {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +0900516 base::AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900517 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900518 return;
darin@chromium.orgb80ef1a2009-09-03 05:05:21 +0900519 incoming_queue_.Swap(&work_queue_); // Constant time
darin@google.combe165ae2008-09-07 17:08:29 +0900520 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900521 }
522}
523
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900524bool MessageLoop::DeletePendingTasks() {
525 bool did_work = !work_queue_.empty();
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900526 // TODO(darin): Delete all tasks once it is safe to do so.
527 // Until it is totally safe, just do it when running Purify or
528 // Valgrind.
529 //
530 // See http://crbug.com/61131
531 //
532#if defined(PURIFY) || defined(USE_HEAPCHECKER)
533 should_leak_tasks_ = false;
534#else
535 if (RunningOnValgrind())
536 should_leak_tasks_ = false;
537#endif // defined(OS_POSIX)
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900538 while (!work_queue_.empty()) {
539 PendingTask pending_task = work_queue_.front();
540 work_queue_.pop();
541 if (!pending_task.delayed_run_time.is_null()) {
542 // We want to delete delayed tasks in the same order in which they would
543 // normally be deleted in case of any funny dependencies between delayed
544 // tasks.
545 AddToDelayedWorkQueue(pending_task);
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900546 }
initial.commit3f4a7322008-07-27 06:49:38 +0900547 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900548 did_work |= !deferred_non_nestable_work_queue_.empty();
549 while (!deferred_non_nestable_work_queue_.empty()) {
jar@chromium.org2fa6b4b2009-03-12 04:53:50 +0900550 deferred_non_nestable_work_queue_.pop();
initial.commit3f4a7322008-07-27 06:49:38 +0900551 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900552 did_work |= !delayed_work_queue_.empty();
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900553
554 // Historically, we always delete the task regardless of valgrind status. It's
555 // not completely clear why we want to leak them in the loops above. This
556 // code is replicating legacy behavior, and should not be considered
557 // absolutely "correct" behavior. See TODO above about deleting all tasks
558 // when it's safe.
559 should_leak_tasks_ = false;
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900560 while (!delayed_work_queue_.empty()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900561 delayed_work_queue_.pop();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900562 }
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900563 should_leak_tasks_ = true;
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900564 return did_work;
initial.commit3f4a7322008-07-27 06:49:38 +0900565}
566
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900567TimeTicks MessageLoop::CalculateDelayedRuntime(int64 delay_ms) {
568 TimeTicks delayed_run_time;
erg@google.com67a25432011-01-08 05:23:43 +0900569 if (delay_ms > 0) {
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900570 delayed_run_time =
erg@google.com67a25432011-01-08 05:23:43 +0900571 TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms);
572
573#if defined(OS_WIN)
574 if (high_resolution_timer_expiration_.is_null()) {
575 // Windows timers are granular to 15.6ms. If we only set high-res
576 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
577 // which as a percentage is pretty inaccurate. So enable high
578 // res timers for any timer which is within 2x of the granularity.
579 // This is a tradeoff between accuracy and power management.
580 bool needs_high_res_timers =
581 delay_ms < (2 * base::Time::kMinLowResolutionThresholdMs);
582 if (needs_high_res_timers) {
mbelshe@chromium.orgbee85b32011-05-16 04:20:49 +0900583 if (base::Time::ActivateHighResolutionTimer(true)) {
584 high_resolution_timer_expiration_ = TimeTicks::Now() +
585 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
586 }
erg@google.com67a25432011-01-08 05:23:43 +0900587 }
588 }
589#endif
590 } else {
591 DCHECK_EQ(delay_ms, 0) << "delay should not be negative";
592 }
593
594#if defined(OS_WIN)
595 if (!high_resolution_timer_expiration_.is_null()) {
596 if (TimeTicks::Now() > high_resolution_timer_expiration_) {
597 base::Time::ActivateHighResolutionTimer(false);
598 high_resolution_timer_expiration_ = TimeTicks();
599 }
600 }
601#endif
602
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900603 return delayed_run_time;
604}
605
606// Possibly called on a background thread!
607void MessageLoop::AddToIncomingQueue(PendingTask* pending_task) {
erg@google.com67a25432011-01-08 05:23:43 +0900608 // Warning: Don't try to short-circuit, and handle this thread's tasks more
609 // directly, as it could starve handling of foreign threads. Put every task
610 // into this queue.
611
612 scoped_refptr<base::MessagePump> pump;
613 {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +0900614 base::AutoLock locked(incoming_queue_lock_);
erg@google.com67a25432011-01-08 05:23:43 +0900615
616 bool was_empty = incoming_queue_.empty();
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900617 incoming_queue_.push(*pending_task);
618 pending_task->task.Reset();
erg@google.com67a25432011-01-08 05:23:43 +0900619 if (!was_empty)
620 return; // Someone else should have started the sub-pump.
621
622 pump = pump_;
623 }
624 // Since the incoming_queue_ may contain a task that destroys this message
625 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
626 // We use a stack-based reference to the message pump so that we can call
627 // ScheduleWork outside of incoming_queue_lock_.
628
629 pump->ScheduleWork();
630}
631
632//------------------------------------------------------------------------------
633// Method and data for histogramming events and actions taken by each instance
634// on each thread.
635
636void MessageLoop::StartHistogrammer() {
jar@chromium.org34571142011-04-05 13:48:53 +0900637 if (enable_histogrammer_ && !message_histogram_
erg@google.com67a25432011-01-08 05:23:43 +0900638 && base::StatisticsRecorder::IsActive()) {
639 DCHECK(!thread_name_.empty());
640 message_histogram_ = base::LinearHistogram::FactoryGet(
641 "MsgLoop:" + thread_name_,
642 kLeastNonZeroMessageId, kMaxMessageId,
643 kNumberOfDistinctMessagesDisplayed,
644 message_histogram_->kHexRangePrintingFlag);
645 message_histogram_->SetRangeDescriptions(event_descriptions_);
646 }
647}
648
649void MessageLoop::HistogramEvent(int event) {
jar@chromium.org34571142011-04-05 13:48:53 +0900650 if (message_histogram_)
erg@google.com67a25432011-01-08 05:23:43 +0900651 message_histogram_->Add(event);
652}
653
darin@google.com981f3552008-08-16 12:09:05 +0900654bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900655 if (!nestable_tasks_allowed_) {
656 // Task can't be executed right now.
657 return false;
658 }
659
660 for (;;) {
661 ReloadWorkQueue();
662 if (work_queue_.empty())
663 break;
664
665 // Execute oldest task.
666 do {
667 PendingTask pending_task = work_queue_.front();
668 work_queue_.pop();
669 if (!pending_task.delayed_run_time.is_null()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900670 AddToDelayedWorkQueue(pending_task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900671 // If we changed the topmost task, then it is time to reschedule.
672 if (delayed_work_queue_.top().task.Equals(pending_task.task))
darin@google.combe165ae2008-09-07 17:08:29 +0900673 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
674 } else {
675 if (DeferOrRunPendingTask(pending_task))
676 return true;
677 }
678 } while (!work_queue_.empty());
679 }
680
681 // Nothing happened.
682 return false;
darin@google.com981f3552008-08-16 12:09:05 +0900683}
684
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900685bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) {
jar@chromium.org40355072010-10-21 15:32:33 +0900686 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900687 recent_time_ = *next_delayed_work_time = TimeTicks();
darin@google.combe165ae2008-09-07 17:08:29 +0900688 return false;
689 }
jeremy@chromium.org2ed223c2008-12-09 02:36:06 +0900690
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900691 // When we "fall behind," there will be a lot of tasks in the delayed work
jar@chromium.org94f73832010-11-05 08:23:42 +0900692 // queue that are ready to run. To increase efficiency when we fall behind,
693 // we will only call Time::Now() intermittently, and then process all tasks
694 // that are ready to run before calling it again. As a result, the more we
695 // fall behind (and have a lot of ready-to-run delayed tasks), the more
696 // efficient we'll be at handling the tasks.
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900697
698 TimeTicks next_run_time = delayed_work_queue_.top().delayed_run_time;
jar@chromium.org94f73832010-11-05 08:23:42 +0900699 if (next_run_time > recent_time_) {
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900700 recent_time_ = TimeTicks::Now(); // Get a better view of Now();
jar@chromium.org94f73832010-11-05 08:23:42 +0900701 if (next_run_time > recent_time_) {
702 *next_delayed_work_time = next_run_time;
703 return false;
704 }
darin@google.combe165ae2008-09-07 17:08:29 +0900705 }
darin@google.com981f3552008-08-16 12:09:05 +0900706
jar@chromium.org40355072010-10-21 15:32:33 +0900707 PendingTask pending_task = delayed_work_queue_.top();
708 delayed_work_queue_.pop();
jeremy@chromium.org2ed223c2008-12-09 02:36:06 +0900709
jar@chromium.org40355072010-10-21 15:32:33 +0900710 if (!delayed_work_queue_.empty())
darin@google.combe165ae2008-09-07 17:08:29 +0900711 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
darin@google.com981f3552008-08-16 12:09:05 +0900712
darin@google.combe165ae2008-09-07 17:08:29 +0900713 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900714}
715
716bool MessageLoop::DoIdleWork() {
717 if (ProcessNextDelayedNonNestableTask())
718 return true;
719
720 if (state_->quit_received)
721 pump_->Quit();
722
723 return false;
724}
725
726//------------------------------------------------------------------------------
727// MessageLoop::AutoRunState
728
729MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
730 // Make the loop reference us.
731 previous_state_ = loop_->state_;
732 if (previous_state_) {
733 run_depth = previous_state_->run_depth + 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900734 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900735 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900736 }
darin@google.com981f3552008-08-16 12:09:05 +0900737 loop_->state_ = this;
738
739 // Initialize the other fields:
740 quit_received = false;
michaelbai@google.com686190b2011-08-03 01:11:16 +0900741#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
darin@google.com981f3552008-08-16 12:09:05 +0900742 dispatcher = NULL;
743#endif
744}
745
746MessageLoop::AutoRunState::~AutoRunState() {
747 loop_->state_ = previous_state_;
darin@google.comee6fa722008-08-13 08:25:43 +0900748}
749
initial.commit3f4a7322008-07-27 06:49:38 +0900750//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900751// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900752
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900753MessageLoop::PendingTask::PendingTask(
754 const base::Closure& task,
755 const tracked_objects::Location& posted_from,
756 TimeTicks delayed_run_time,
757 bool nestable)
758 : task(task),
759 time_posted(TimeTicks::Now()),
760 delayed_run_time(delayed_run_time),
761 sequence_num(0),
apatrick@chromium.org87164042011-05-20 07:28:25 +0900762 nestable(nestable),
763 birth_program_counter(posted_from.program_counter()) {
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900764#if defined(TRACK_ALL_TASK_OBJECTS)
ajwong@chromium.org12fa0922011-07-27 03:25:16 +0900765 post_births = tracked_objects::ThreadData::TallyABirthIfActive(posted_from);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900766#endif // defined(TRACK_ALL_TASK_OBJECTS)
767}
768
769MessageLoop::PendingTask::~PendingTask() {
770}
771
darin@google.combe165ae2008-09-07 17:08:29 +0900772bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
773 // Since the top of a priority queue is defined as the "greatest" element, we
774 // need to invert the comparison here. We want the smaller time to be at the
775 // top of the heap.
initial.commit3f4a7322008-07-27 06:49:38 +0900776
darin@google.combe165ae2008-09-07 17:08:29 +0900777 if (delayed_run_time < other.delayed_run_time)
778 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900779
darin@google.combe165ae2008-09-07 17:08:29 +0900780 if (delayed_run_time > other.delayed_run_time)
781 return true;
initial.commit3f4a7322008-07-27 06:49:38 +0900782
darin@google.combe165ae2008-09-07 17:08:29 +0900783 // If the times happen to match, then we use the sequence number to decide.
784 // Compare the difference to support integer roll-over.
785 return (sequence_num - other.sequence_num) > 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900786}
787
788//------------------------------------------------------------------------------
darin@google.comd936b5b2008-08-26 14:53:57 +0900789// MessageLoopForUI
790
791#if defined(OS_WIN)
darin@google.comd936b5b2008-08-26 14:53:57 +0900792void MessageLoopForUI::DidProcessMessage(const MSG& message) {
793 pump_win()->DidProcessMessage(message);
794}
darin@google.comd936b5b2008-08-26 14:53:57 +0900795#endif // defined(OS_WIN)
796
michaelbai@google.com686190b2011-08-03 01:11:16 +0900797#if defined(OS_ANDROID)
798void MessageLoopForUI::Start() {
799 // No Histogram support for UI message loop as it is managed by Java side
800 static_cast<base::MessagePumpForUI*>(pump_.get())->Start(this);
801}
802#endif
803
804#if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID)
jcampan@chromium.org05423582009-08-01 07:53:37 +0900805void MessageLoopForUI::AddObserver(Observer* observer) {
806 pump_ui()->AddObserver(observer);
807}
808
809void MessageLoopForUI::RemoveObserver(Observer* observer) {
810 pump_ui()->RemoveObserver(observer);
811}
812
813void MessageLoopForUI::Run(Dispatcher* dispatcher) {
814 AutoRunState save_state(this);
815 state_->dispatcher = dispatcher;
816 RunHandler();
817}
michaelbai@google.com686190b2011-08-03 01:11:16 +0900818#endif // !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID)
jcampan@chromium.org05423582009-08-01 07:53:37 +0900819
darin@google.comd936b5b2008-08-26 14:53:57 +0900820//------------------------------------------------------------------------------
821// MessageLoopForIO
822
823#if defined(OS_WIN)
824
rvargas@google.com9e49aa22008-10-10 08:58:43 +0900825void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) {
826 pump_io()->RegisterIOHandler(file, handler);
827}
828
rvargas@google.com73887542008-11-08 06:52:15 +0900829bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) {
830 return pump_io()->WaitForIOCompletion(timeout, filter);
rvargas@google.com9e49aa22008-10-10 08:58:43 +0900831}
832
abarth@chromium.org1f1c2172010-12-01 17:45:51 +0900833#elif defined(OS_POSIX) && !defined(OS_NACL)
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900834
jeremy@chromium.orgefc0db02008-12-16 07:02:17 +0900835bool MessageLoopForIO::WatchFileDescriptor(int fd,
836 bool persistent,
837 Mode mode,
838 FileDescriptorWatcher *controller,
839 Watcher *delegate) {
840 return pump_libevent()->WatchFileDescriptor(
841 fd,
842 persistent,
843 static_cast<base::MessagePumpLibevent::Mode>(mode),
844 controller,
845 delegate);
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900846}
847
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900848#endif