blob: b0526f45456fc1a97bae5a502e84144560690543 [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
evan@chromium.org875bb6e2009-12-29 09:32:52 +090028#if defined(OS_POSIX) && !defined(OS_MACOSX)
ajwong@chromium.org94d2a582011-04-21 01:02:23 +090029#include <gdk/gdk.h>
30#include <gdk/gdkx.h>
dsh@google.com119a2522008-10-04 01:52:59 +090031#include "base/message_pump_glib.h"
32#endif
rjkroege@google.com3080f442010-10-23 01:17:47 +090033#if defined(TOUCH_UI)
34#include "base/message_pump_glib_x.h"
35#endif
dkegel@google.com9e044ae2008-09-19 03:46:26 +090036
dsh@google.com0f8dd262008-10-28 05:43:33 +090037using base::TimeDelta;
jar@chromium.org9b0fb062010-11-07 07:23:29 +090038using base::TimeTicks;
dsh@google.com0f8dd262008-10-28 05:43:33 +090039
erg@chromium.orga7528522010-07-16 02:23:23 +090040namespace {
41
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090042// A lazily created thread local storage for quick access to a thread's message
43// loop, if one exists. This should be safe and free of static constructors.
erg@chromium.orga7528522010-07-16 02:23:23 +090044base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr(
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +090045 base::LINKER_INITIALIZED);
initial.commit3f4a7322008-07-27 06:49:38 +090046
initial.commit3f4a7322008-07-27 06:49:38 +090047// Logical events for Histogram profiling. Run with -message-loop-histogrammer
48// to get an accounting of messages and actions taken on each thread.
erg@chromium.orga7528522010-07-16 02:23:23 +090049const int kTaskRunEvent = 0x1;
50const int kTimerEvent = 0x2;
initial.commit3f4a7322008-07-27 06:49:38 +090051
52// Provide range of message IDs for use in histogramming and debug display.
erg@chromium.orga7528522010-07-16 02:23:23 +090053const int kLeastNonZeroMessageId = 1;
54const int kMaxMessageId = 1099;
55const int kNumberOfDistinctMessagesDisplayed = 1100;
56
57// Provide a macro that takes an expression (such as a constant, or macro
58// constant) and creates a pair to initalize an array of pairs. In this case,
59// our pair consists of the expressions value, and the "stringized" version
60// of the expression (i.e., the exrpression put in quotes). For example, if
61// we have:
62// #define FOO 2
63// #define BAR 5
64// then the following:
65// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
66// will expand to:
67// {7, "FOO + BAR"}
68// We use the resulting array as an argument to our histogram, which reads the
69// number as a bucket identifier, and proceeds to use the corresponding name
70// in the pair (i.e., the quoted string) when printing out a histogram.
71#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
72
brettw@chromium.org275c2ec2010-10-14 13:38:38 +090073const base::LinearHistogram::DescriptionPair event_descriptions_[] = {
erg@chromium.orga7528522010-07-16 02:23:23 +090074 // Provide some pretty print capability in our histogram for our internal
75 // messages.
76
77 // A few events we handle (kindred to messages), and used to profile actions.
78 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
79 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
80
81 {-1, NULL} // The list must be null terminated, per API to histogram.
82};
83
84bool enable_histogrammer_ = false;
85
ajwong@chromium.org94d2a582011-04-21 01:02:23 +090086// TODO(ajwong): This is one use case for having a Owned() tag that behaves
87// like a "Unique" pointer. If we had that, and Tasks were always safe to
88// delete on MessageLoop shutdown, this class could just be a function.
89class TaskClosureAdapter : public base::RefCounted<TaskClosureAdapter> {
90 public:
91 // |should_leak_task| points to a flag variable that can be used to determine
92 // if this class should leak the Task on destruction. This is important
93 // at MessageLoop shutdown since not all tasks can be safely deleted without
94 // running. See MessageLoop::DeletePendingTasks() for the exact behavior
95 // of when a Task should be deleted. It is subtle.
96 TaskClosureAdapter(Task* task, bool* should_leak_task)
97 : task_(task),
98 should_leak_task_(should_leak_task) {
99 }
100
101 void Run() {
102 task_->Run();
103 delete task_;
104 task_ = NULL;
105 }
106
107 private:
108 friend class base::RefCounted<TaskClosureAdapter>;
109
110 ~TaskClosureAdapter() {
111 if (!*should_leak_task_) {
112 delete task_;
113 }
114 }
115
116 Task* task_;
117 bool* should_leak_task_;
118};
119
erg@chromium.orga7528522010-07-16 02:23:23 +0900120} // namespace
initial.commit3f4a7322008-07-27 06:49:38 +0900121
122//------------------------------------------------------------------------------
123
darin@google.com981f3552008-08-16 12:09:05 +0900124#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900125
initial.commit3f4a7322008-07-27 06:49:38 +0900126// Upon a SEH exception in this thread, it restores the original unhandled
127// exception filter.
128static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
129 ::SetUnhandledExceptionFilter(old_filter);
130 return EXCEPTION_CONTINUE_SEARCH;
131}
132
133// Retrieves a pointer to the current unhandled exception filter. There
134// is no standalone getter method.
135static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
136 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
137 top_filter = ::SetUnhandledExceptionFilter(0);
138 ::SetUnhandledExceptionFilter(top_filter);
139 return top_filter;
140}
141
darin@google.com981f3552008-08-16 12:09:05 +0900142#endif // defined(OS_WIN)
143
initial.commit3f4a7322008-07-27 06:49:38 +0900144//------------------------------------------------------------------------------
145
erg@chromium.org493f5f62010-07-16 06:03:54 +0900146MessageLoop::TaskObserver::TaskObserver() {
147}
148
149MessageLoop::TaskObserver::~TaskObserver() {
150}
151
152MessageLoop::DestructionObserver::~DestructionObserver() {
153}
154
155//------------------------------------------------------------------------------
156
darin@google.comd936b5b2008-08-26 14:53:57 +0900157MessageLoop::MessageLoop(Type type)
158 : type_(type),
darin@google.comee6fa722008-08-13 08:25:43 +0900159 nestable_tasks_allowed_(true),
darin@google.com12d40bb2008-08-20 03:36:23 +0900160 exception_restoration_(false),
jar@chromium.org34571142011-04-05 13:48:53 +0900161 message_histogram_(NULL),
darin@google.combe165ae2008-09-07 17:08:29 +0900162 state_(NULL),
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900163 should_leak_tasks_(true),
ananta@chromium.orgc542fec2011-03-24 12:40:28 +0900164#ifdef OS_WIN
165 os_modal_loop_(false),
166#endif // OS_WIN
darin@google.combe165ae2008-09-07 17:08:29 +0900167 next_sequence_num_(0) {
deanm@chromium.orgcd1ce302008-09-10 19:54:06 +0900168 DCHECK(!current()) << "should only have one message loop per thread";
169 lazy_tls_ptr.Pointer()->Set(this);
darin@google.comd936b5b2008-08-26 14:53:57 +0900170
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900171// TODO(rvargas): Get rid of the OS guards.
darin@google.com981f3552008-08-16 12:09:05 +0900172#if defined(OS_WIN)
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900173#define MESSAGE_PUMP_UI new base::MessagePumpForUI()
174#define MESSAGE_PUMP_IO new base::MessagePumpForIO()
175#elif defined(OS_MACOSX)
176#define MESSAGE_PUMP_UI base::MessagePumpMac::Create()
177#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
rjkroege@google.com3080f442010-10-23 01:17:47 +0900178#elif defined(TOUCH_UI)
sadrul@chromium.orgcff2c642010-12-17 04:43:30 +0900179#define MESSAGE_PUMP_UI new base::MessagePumpGlibX()
rjkroege@google.com3080f442010-10-23 01:17:47 +0900180#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
abarth@chromium.org1f1c2172010-12-01 17:45:51 +0900181#elif defined(OS_NACL)
182// Currently NaCl doesn't have a UI or an IO MessageLoop.
183// TODO(abarth): Figure out if we need these.
184#define MESSAGE_PUMP_UI NULL
185#define MESSAGE_PUMP_IO NULL
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900186#elif defined(OS_POSIX) // POSIX but not MACOSX.
187#define MESSAGE_PUMP_UI new base::MessagePumpForUI()
188#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
evan@chromium.org875bb6e2009-12-29 09:32:52 +0900189#else
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900190#error Not implemented
evan@chromium.org875bb6e2009-12-29 09:32:52 +0900191#endif
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900192
193 if (type_ == TYPE_UI) {
194 pump_ = MESSAGE_PUMP_UI;
dsh@google.com119a2522008-10-04 01:52:59 +0900195 } else if (type_ == TYPE_IO) {
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900196 pump_ = MESSAGE_PUMP_IO;
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900197 } else {
thestig@chromium.org7016bac2010-04-15 10:04:29 +0900198 DCHECK_EQ(TYPE_DEFAULT, type_);
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900199 pump_ = new base::MessagePumpDefault();
200 }
initial.commit3f4a7322008-07-27 06:49:38 +0900201}
202
203MessageLoop::~MessageLoop() {
thestig@chromium.org226880a2010-11-11 05:28:06 +0900204 DCHECK_EQ(this, current());
darin@google.com965e5342008-08-06 08:16:41 +0900205
darin@google.com0e500502008-09-09 14:55:35 +0900206 DCHECK(!state_);
207
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900208 // Clean up any unprocessed tasks, but take care: deleting a task could
209 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
210 // limit on the number of times we will allow a deleted task to generate more
211 // tasks. Normally, we should only pass through this loop once or twice. If
212 // we end up hitting the loop limit, then it is probably due to one task that
213 // is being stubborn. Inspect the queues to see who is left.
214 bool did_work;
215 for (int i = 0; i < 100; ++i) {
216 DeletePendingTasks();
217 ReloadWorkQueue();
218 // If we end up with empty queues, then break out of the loop.
219 did_work = DeletePendingTasks();
220 if (!did_work)
221 break;
darin@google.com0e500502008-09-09 14:55:35 +0900222 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900223 DCHECK(!did_work);
224
sanjeevr@chromium.org03b44d52010-11-30 09:25:29 +0900225 // Let interested parties have one last shot at accessing this.
226 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
227 WillDestroyCurrentMessageLoop());
228
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900229 // OK, now make it so that no one can find us.
deanm@chromium.orge4cc5922008-09-10 20:14:56 +0900230 lazy_tls_ptr.Pointer()->Set(NULL);
mbelshe@chromium.orgbee85b32011-05-16 04:20:49 +0900231
232#if defined(OS_WIN)
233 // If we left the high-resolution timer activated, deactivate it now.
234 // Doing this is not-critical, it is mainly to make sure we track
235 // the high resolution timer activations properly in our unit tests.
236 if (!high_resolution_timer_expiration_.is_null()) {
237 base::Time::ActivateHighResolutionTimer(false);
238 high_resolution_timer_expiration_ = base::TimeTicks();
239 }
240#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900241}
242
erg@google.com67a25432011-01-08 05:23:43 +0900243// static
244MessageLoop* MessageLoop::current() {
245 // TODO(darin): sadly, we cannot enable this yet since people call us even
246 // when they have no intention of using us.
247 // DCHECK(loop) << "Ouch, did you forget to initialize me?";
248 return lazy_tls_ptr.Pointer()->Get();
249}
250
251// static
252void MessageLoop::EnableHistogrammer(bool enable) {
253 enable_histogrammer_ = enable;
254}
255
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900256void MessageLoop::AddDestructionObserver(
257 DestructionObserver* destruction_observer) {
thestig@chromium.org226880a2010-11-11 05:28:06 +0900258 DCHECK_EQ(this, current());
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900259 destruction_observers_.AddObserver(destruction_observer);
darin@google.com965e5342008-08-06 08:16:41 +0900260}
261
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900262void MessageLoop::RemoveDestructionObserver(
263 DestructionObserver* destruction_observer) {
thestig@chromium.org226880a2010-11-11 05:28:06 +0900264 DCHECK_EQ(this, current());
sky@chromium.org18c66dc2010-09-16 07:14:36 +0900265 destruction_observers_.RemoveObserver(destruction_observer);
darin@google.com965e5342008-08-06 08:16:41 +0900266}
267
darin@google.combe165ae2008-09-07 17:08:29 +0900268void MessageLoop::PostTask(
269 const tracked_objects::Location& from_here, Task* task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900270 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900271 PendingTask pending_task(
272 base::Bind(&TaskClosureAdapter::Run,
273 new TaskClosureAdapter(task, &should_leak_tasks_)),
274 from_here,
275 CalculateDelayedRuntime(0), true);
276 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900277}
278
279void MessageLoop::PostDelayedTask(
phajdan.jr@chromium.orgc3c92252009-06-18 02:23:51 +0900280 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900281 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900282 PendingTask pending_task(
283 base::Bind(&TaskClosureAdapter::Run,
284 new TaskClosureAdapter(task, &should_leak_tasks_)),
285 from_here,
286 CalculateDelayedRuntime(delay_ms), true);
287 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900288}
289
290void MessageLoop::PostNonNestableTask(
291 const tracked_objects::Location& from_here, Task* task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900292 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900293 PendingTask pending_task(
294 base::Bind(&TaskClosureAdapter::Run,
295 new TaskClosureAdapter(task, &should_leak_tasks_)),
296 from_here,
297 CalculateDelayedRuntime(0), false);
298 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900299}
300
301void MessageLoop::PostNonNestableDelayedTask(
phajdan.jr@chromium.orgc3c92252009-06-18 02:23:51 +0900302 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900303 CHECK(task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900304 PendingTask pending_task(
305 base::Bind(&TaskClosureAdapter::Run,
306 new TaskClosureAdapter(task, &should_leak_tasks_)),
307 from_here,
308 CalculateDelayedRuntime(delay_ms), false);
309 AddToIncomingQueue(&pending_task);
310}
311
312void MessageLoop::PostTask(
313 const tracked_objects::Location& from_here, const base::Closure& task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900314 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900315 PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), true);
316 AddToIncomingQueue(&pending_task);
317}
318
319void MessageLoop::PostDelayedTask(
320 const tracked_objects::Location& from_here, const base::Closure& task,
321 int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900322 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900323 PendingTask pending_task(task, from_here,
324 CalculateDelayedRuntime(delay_ms), true);
325 AddToIncomingQueue(&pending_task);
326}
327
328void MessageLoop::PostNonNestableTask(
329 const tracked_objects::Location& from_here, const base::Closure& task) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900330 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900331 PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), false);
332 AddToIncomingQueue(&pending_task);
333}
334
335void MessageLoop::PostNonNestableDelayedTask(
336 const tracked_objects::Location& from_here, const base::Closure& task,
337 int64 delay_ms) {
apatrick@chromium.org47db3702011-05-11 06:52:21 +0900338 CHECK(!task.is_null());
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900339 PendingTask pending_task(task, from_here,
340 CalculateDelayedRuntime(delay_ms), false);
341 AddToIncomingQueue(&pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900342}
343
erg@google.com67a25432011-01-08 05:23:43 +0900344void MessageLoop::Run() {
345 AutoRunState save_state(this);
346 RunHandler();
347}
darin@google.com0795f572008-08-30 09:22:48 +0900348
erg@google.com67a25432011-01-08 05:23:43 +0900349void MessageLoop::RunAllPending() {
350 AutoRunState save_state(this);
351 state_->quit_received = true; // Means run until we would otherwise block.
352 RunHandler();
353}
darin@google.com0795f572008-08-30 09:22:48 +0900354
erg@google.com67a25432011-01-08 05:23:43 +0900355void MessageLoop::Quit() {
356 DCHECK_EQ(this, current());
357 if (state_) {
358 state_->quit_received = true;
darin@google.com0795f572008-08-30 09:22:48 +0900359 } else {
erg@google.com67a25432011-01-08 05:23:43 +0900360 NOTREACHED() << "Must be inside Run to call Quit";
darin@google.com0795f572008-08-30 09:22:48 +0900361 }
erg@google.com67a25432011-01-08 05:23:43 +0900362}
darin@google.com0795f572008-08-30 09:22:48 +0900363
erg@google.com67a25432011-01-08 05:23:43 +0900364void MessageLoop::QuitNow() {
365 DCHECK_EQ(this, current());
366 if (state_) {
367 pump_->Quit();
368 } else {
369 NOTREACHED() << "Must be inside Run to call Quit";
mbelshe@chromium.orgde50b7d2010-06-29 13:58:15 +0900370 }
initial.commit3f4a7322008-07-27 06:49:38 +0900371}
372
373void MessageLoop::SetNestableTasksAllowed(bool allowed) {
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900374 if (nestable_tasks_allowed_ != allowed) {
375 nestable_tasks_allowed_ = allowed;
376 if (!nestable_tasks_allowed_)
377 return;
378 // Start the native pump if we are not already pumping.
darin@google.com981f3552008-08-16 12:09:05 +0900379 pump_->ScheduleWork();
mpcomplete@google.com989d5f82008-08-09 09:14:09 +0900380 }
initial.commit3f4a7322008-07-27 06:49:38 +0900381}
382
383bool MessageLoop::NestableTasksAllowed() const {
384 return nestable_tasks_allowed_;
385}
386
jcampan@chromium.orgeac57172009-07-02 04:53:59 +0900387bool MessageLoop::IsNested() {
388 return state_->run_depth > 1;
389}
390
erg@google.com67a25432011-01-08 05:23:43 +0900391void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
392 DCHECK_EQ(this, current());
393 task_observers_.AddObserver(task_observer);
394}
395
396void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
397 DCHECK_EQ(this, current());
398 task_observers_.RemoveObserver(task_observer);
399}
400
willchan@chromium.org3a397672011-01-26 09:53:48 +0900401void MessageLoop::AssertIdle() const {
402 // We only check |incoming_queue_|, since we don't want to lock |work_queue_|.
403 base::AutoLock lock(incoming_queue_lock_);
404 DCHECK(incoming_queue_.empty());
405}
406
initial.commit3f4a7322008-07-27 06:49:38 +0900407//------------------------------------------------------------------------------
initial.commit3f4a7322008-07-27 06:49:38 +0900408
erg@google.com67a25432011-01-08 05:23:43 +0900409// Runs the loop in two different SEH modes:
410// enable_SEH_restoration_ = false : any unhandled exception goes to the last
411// one that calls SetUnhandledExceptionFilter().
412// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
413// that was existed before the loop was run.
414void MessageLoop::RunHandler() {
415#if defined(OS_WIN)
416 if (exception_restoration_) {
417 RunInternalInSEHFrame();
418 return;
419 }
420#endif
421
422 RunInternal();
423}
424
425#if defined(OS_WIN)
426__declspec(noinline) void MessageLoop::RunInternalInSEHFrame() {
427 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
428 __try {
429 RunInternal();
430 } __except(SEHFilter(current_filter)) {
431 }
432 return;
433}
434#endif
435
436void MessageLoop::RunInternal() {
437 DCHECK_EQ(this, current());
438
439 StartHistogrammer();
440
441#if !defined(OS_MACOSX)
442 if (state_->dispatcher && type() == TYPE_UI) {
443 static_cast<base::MessagePumpForUI*>(pump_.get())->
444 RunWithDispatcher(this, state_->dispatcher);
445 return;
446 }
447#endif
448
449 pump_->Run(this);
450}
451
452bool MessageLoop::ProcessNextDelayedNonNestableTask() {
453 if (state_->run_depth != 1)
454 return false;
455
456 if (deferred_non_nestable_work_queue_.empty())
457 return false;
458
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900459 PendingTask pending_task = deferred_non_nestable_work_queue_.front();
erg@google.com67a25432011-01-08 05:23:43 +0900460 deferred_non_nestable_work_queue_.pop();
461
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900462 RunTask(pending_task);
erg@google.com67a25432011-01-08 05:23:43 +0900463 return true;
464}
465
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900466void MessageLoop::RunTask(const PendingTask& pending_task) {
initial.commit3f4a7322008-07-27 06:49:38 +0900467 DCHECK(nestable_tasks_allowed_);
468 // Execute the task and assume the worst: It is probably not reentrant.
469 nestable_tasks_allowed_ = false;
darin@google.combe165ae2008-09-07 17:08:29 +0900470
apatrick@chromium.org87164042011-05-20 07:28:25 +0900471 // Before running the task, store the program counter where it was posted
472 // and deliberately alias it to ensure it is on the stack if the task
473 // crashes. Be careful not to assume that the variable itself will have the
474 // expected value when displayed by the optimizer in an optimized build.
475 // Look at a memory dump of the stack.
476 const void* program_counter = pending_task.birth_program_counter;
477 base::debug::Alias(&program_counter);
478
darin@google.combe165ae2008-09-07 17:08:29 +0900479 HistogramEvent(kTaskRunEvent);
willchan@chromium.orga9047632010-06-10 06:20:41 +0900480 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900481 WillProcessTask(pending_task.time_posted));
482 pending_task.task.Run();
483 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
484 DidProcessTask(pending_task.time_posted));
485
486#if defined(TRACK_ALL_TASK_OBJECTS)
487 if (tracked_objects::ThreadData::IsActive() && pending_task.post_births) {
488 tracked_objects::ThreadData::current()->TallyADeath(
489 *pending_task.post_births,
490 TimeTicks::Now() - pending_task.time_posted);
491 }
492#endif // defined(TRACK_ALL_TASK_OBJECTS)
darin@google.combe165ae2008-09-07 17:08:29 +0900493
494 nestable_tasks_allowed_ = true;
initial.commit3f4a7322008-07-27 06:49:38 +0900495}
496
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900497bool MessageLoop::DeferOrRunPendingTask(
498 const PendingTask& pending_task) {
darin@google.combe165ae2008-09-07 17:08:29 +0900499 if (pending_task.nestable || state_->run_depth == 1) {
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900500 RunTask(pending_task);
darin@google.combe165ae2008-09-07 17:08:29 +0900501 // Show that we ran a task (Note: a new one might arrive as a
502 // consequence!).
503 return true;
504 }
505
506 // We couldn't run the task now because we're in a nested message loop
507 // and the task isn't nestable.
508 deferred_non_nestable_work_queue_.push(pending_task);
509 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900510}
511
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900512void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
513 // Move to the delayed work queue. Initialize the sequence number
514 // before inserting into the delayed_work_queue_. The sequence number
515 // is used to faciliate FIFO sorting when two tasks have the same
516 // delayed_run_time value.
517 PendingTask new_pending_task(pending_task);
518 new_pending_task.sequence_num = next_sequence_num_++;
519 delayed_work_queue_.push(new_pending_task);
520}
521
initial.commit3f4a7322008-07-27 06:49:38 +0900522void MessageLoop::ReloadWorkQueue() {
523 // We can improve performance of our loading tasks from incoming_queue_ to
darin@google.com981f3552008-08-16 12:09:05 +0900524 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
525 // load. That reduces the number of locks-per-task significantly when our
darin@google.combe165ae2008-09-07 17:08:29 +0900526 // queues get large.
527 if (!work_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900528 return; // Wait till we *really* need to lock and load.
529
530 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commit3f4a7322008-07-27 06:49:38 +0900531 {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +0900532 base::AutoLock lock(incoming_queue_lock_);
darin@google.combe165ae2008-09-07 17:08:29 +0900533 if (incoming_queue_.empty())
initial.commit3f4a7322008-07-27 06:49:38 +0900534 return;
darin@chromium.orgb80ef1a2009-09-03 05:05:21 +0900535 incoming_queue_.Swap(&work_queue_); // Constant time
darin@google.combe165ae2008-09-07 17:08:29 +0900536 DCHECK(incoming_queue_.empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900537 }
538}
539
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900540bool MessageLoop::DeletePendingTasks() {
541 bool did_work = !work_queue_.empty();
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900542 // TODO(darin): Delete all tasks once it is safe to do so.
543 // Until it is totally safe, just do it when running Purify or
544 // Valgrind.
545 //
546 // See http://crbug.com/61131
547 //
548#if defined(PURIFY) || defined(USE_HEAPCHECKER)
549 should_leak_tasks_ = false;
550#else
551 if (RunningOnValgrind())
552 should_leak_tasks_ = false;
553#endif // defined(OS_POSIX)
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900554 while (!work_queue_.empty()) {
555 PendingTask pending_task = work_queue_.front();
556 work_queue_.pop();
557 if (!pending_task.delayed_run_time.is_null()) {
558 // We want to delete delayed tasks in the same order in which they would
559 // normally be deleted in case of any funny dependencies between delayed
560 // tasks.
561 AddToDelayedWorkQueue(pending_task);
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900562 }
initial.commit3f4a7322008-07-27 06:49:38 +0900563 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900564 did_work |= !deferred_non_nestable_work_queue_.empty();
565 while (!deferred_non_nestable_work_queue_.empty()) {
jar@chromium.org2fa6b4b2009-03-12 04:53:50 +0900566 deferred_non_nestable_work_queue_.pop();
initial.commit3f4a7322008-07-27 06:49:38 +0900567 }
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900568 did_work |= !delayed_work_queue_.empty();
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900569
570 // Historically, we always delete the task regardless of valgrind status. It's
571 // not completely clear why we want to leak them in the loops above. This
572 // code is replicating legacy behavior, and should not be considered
573 // absolutely "correct" behavior. See TODO above about deleting all tasks
574 // when it's safe.
575 should_leak_tasks_ = false;
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900576 while (!delayed_work_queue_.empty()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900577 delayed_work_queue_.pop();
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900578 }
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900579 should_leak_tasks_ = true;
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900580 return did_work;
initial.commit3f4a7322008-07-27 06:49:38 +0900581}
582
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900583TimeTicks MessageLoop::CalculateDelayedRuntime(int64 delay_ms) {
584 TimeTicks delayed_run_time;
erg@google.com67a25432011-01-08 05:23:43 +0900585 if (delay_ms > 0) {
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900586 delayed_run_time =
erg@google.com67a25432011-01-08 05:23:43 +0900587 TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms);
588
589#if defined(OS_WIN)
590 if (high_resolution_timer_expiration_.is_null()) {
591 // Windows timers are granular to 15.6ms. If we only set high-res
592 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
593 // which as a percentage is pretty inaccurate. So enable high
594 // res timers for any timer which is within 2x of the granularity.
595 // This is a tradeoff between accuracy and power management.
596 bool needs_high_res_timers =
597 delay_ms < (2 * base::Time::kMinLowResolutionThresholdMs);
598 if (needs_high_res_timers) {
mbelshe@chromium.orgbee85b32011-05-16 04:20:49 +0900599 if (base::Time::ActivateHighResolutionTimer(true)) {
600 high_resolution_timer_expiration_ = TimeTicks::Now() +
601 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
602 }
erg@google.com67a25432011-01-08 05:23:43 +0900603 }
604 }
605#endif
606 } else {
607 DCHECK_EQ(delay_ms, 0) << "delay should not be negative";
608 }
609
610#if defined(OS_WIN)
611 if (!high_resolution_timer_expiration_.is_null()) {
612 if (TimeTicks::Now() > high_resolution_timer_expiration_) {
613 base::Time::ActivateHighResolutionTimer(false);
614 high_resolution_timer_expiration_ = TimeTicks();
615 }
616 }
617#endif
618
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900619 return delayed_run_time;
620}
621
622// Possibly called on a background thread!
623void MessageLoop::AddToIncomingQueue(PendingTask* pending_task) {
erg@google.com67a25432011-01-08 05:23:43 +0900624 // Warning: Don't try to short-circuit, and handle this thread's tasks more
625 // directly, as it could starve handling of foreign threads. Put every task
626 // into this queue.
627
628 scoped_refptr<base::MessagePump> pump;
629 {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +0900630 base::AutoLock locked(incoming_queue_lock_);
erg@google.com67a25432011-01-08 05:23:43 +0900631
632 bool was_empty = incoming_queue_.empty();
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900633 incoming_queue_.push(*pending_task);
634 pending_task->task.Reset();
erg@google.com67a25432011-01-08 05:23:43 +0900635 if (!was_empty)
636 return; // Someone else should have started the sub-pump.
637
638 pump = pump_;
639 }
640 // Since the incoming_queue_ may contain a task that destroys this message
641 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
642 // We use a stack-based reference to the message pump so that we can call
643 // ScheduleWork outside of incoming_queue_lock_.
644
645 pump->ScheduleWork();
646}
647
648//------------------------------------------------------------------------------
649// Method and data for histogramming events and actions taken by each instance
650// on each thread.
651
652void MessageLoop::StartHistogrammer() {
jar@chromium.org34571142011-04-05 13:48:53 +0900653 if (enable_histogrammer_ && !message_histogram_
erg@google.com67a25432011-01-08 05:23:43 +0900654 && base::StatisticsRecorder::IsActive()) {
655 DCHECK(!thread_name_.empty());
656 message_histogram_ = base::LinearHistogram::FactoryGet(
657 "MsgLoop:" + thread_name_,
658 kLeastNonZeroMessageId, kMaxMessageId,
659 kNumberOfDistinctMessagesDisplayed,
660 message_histogram_->kHexRangePrintingFlag);
661 message_histogram_->SetRangeDescriptions(event_descriptions_);
662 }
663}
664
665void MessageLoop::HistogramEvent(int event) {
jar@chromium.org34571142011-04-05 13:48:53 +0900666 if (message_histogram_)
erg@google.com67a25432011-01-08 05:23:43 +0900667 message_histogram_->Add(event);
668}
669
darin@google.com981f3552008-08-16 12:09:05 +0900670bool MessageLoop::DoWork() {
darin@google.combe165ae2008-09-07 17:08:29 +0900671 if (!nestable_tasks_allowed_) {
672 // Task can't be executed right now.
673 return false;
674 }
675
676 for (;;) {
677 ReloadWorkQueue();
678 if (work_queue_.empty())
679 break;
680
681 // Execute oldest task.
682 do {
683 PendingTask pending_task = work_queue_.front();
684 work_queue_.pop();
685 if (!pending_task.delayed_run_time.is_null()) {
darin@chromium.orge3af17f2008-09-10 09:37:07 +0900686 AddToDelayedWorkQueue(pending_task);
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900687 // If we changed the topmost task, then it is time to reschedule.
688 if (delayed_work_queue_.top().task.Equals(pending_task.task))
darin@google.combe165ae2008-09-07 17:08:29 +0900689 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
690 } else {
691 if (DeferOrRunPendingTask(pending_task))
692 return true;
693 }
694 } while (!work_queue_.empty());
695 }
696
697 // Nothing happened.
698 return false;
darin@google.com981f3552008-08-16 12:09:05 +0900699}
700
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900701bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) {
jar@chromium.org40355072010-10-21 15:32:33 +0900702 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900703 recent_time_ = *next_delayed_work_time = TimeTicks();
darin@google.combe165ae2008-09-07 17:08:29 +0900704 return false;
705 }
jeremy@chromium.org2ed223c2008-12-09 02:36:06 +0900706
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900707 // When we "fall behind," there will be a lot of tasks in the delayed work
jar@chromium.org94f73832010-11-05 08:23:42 +0900708 // queue that are ready to run. To increase efficiency when we fall behind,
709 // we will only call Time::Now() intermittently, and then process all tasks
710 // that are ready to run before calling it again. As a result, the more we
711 // fall behind (and have a lot of ready-to-run delayed tasks), the more
712 // efficient we'll be at handling the tasks.
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900713
714 TimeTicks next_run_time = delayed_work_queue_.top().delayed_run_time;
jar@chromium.org94f73832010-11-05 08:23:42 +0900715 if (next_run_time > recent_time_) {
jar@chromium.org9b0fb062010-11-07 07:23:29 +0900716 recent_time_ = TimeTicks::Now(); // Get a better view of Now();
jar@chromium.org94f73832010-11-05 08:23:42 +0900717 if (next_run_time > recent_time_) {
718 *next_delayed_work_time = next_run_time;
719 return false;
720 }
darin@google.combe165ae2008-09-07 17:08:29 +0900721 }
darin@google.com981f3552008-08-16 12:09:05 +0900722
jar@chromium.org40355072010-10-21 15:32:33 +0900723 PendingTask pending_task = delayed_work_queue_.top();
724 delayed_work_queue_.pop();
jeremy@chromium.org2ed223c2008-12-09 02:36:06 +0900725
jar@chromium.org40355072010-10-21 15:32:33 +0900726 if (!delayed_work_queue_.empty())
darin@google.combe165ae2008-09-07 17:08:29 +0900727 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
darin@google.com981f3552008-08-16 12:09:05 +0900728
darin@google.combe165ae2008-09-07 17:08:29 +0900729 return DeferOrRunPendingTask(pending_task);
darin@google.com981f3552008-08-16 12:09:05 +0900730}
731
732bool MessageLoop::DoIdleWork() {
733 if (ProcessNextDelayedNonNestableTask())
734 return true;
735
736 if (state_->quit_received)
737 pump_->Quit();
738
739 return false;
740}
741
742//------------------------------------------------------------------------------
743// MessageLoop::AutoRunState
744
745MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
746 // Make the loop reference us.
747 previous_state_ = loop_->state_;
748 if (previous_state_) {
749 run_depth = previous_state_->run_depth + 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900750 } else {
darin@google.com981f3552008-08-16 12:09:05 +0900751 run_depth = 1;
darin@google.com6ddeb842008-08-15 16:31:20 +0900752 }
darin@google.com981f3552008-08-16 12:09:05 +0900753 loop_->state_ = this;
754
755 // Initialize the other fields:
756 quit_received = false;
evan@chromium.org875bb6e2009-12-29 09:32:52 +0900757#if !defined(OS_MACOSX)
darin@google.com981f3552008-08-16 12:09:05 +0900758 dispatcher = NULL;
759#endif
760}
761
762MessageLoop::AutoRunState::~AutoRunState() {
763 loop_->state_ = previous_state_;
darin@google.comee6fa722008-08-13 08:25:43 +0900764}
765
initial.commit3f4a7322008-07-27 06:49:38 +0900766//------------------------------------------------------------------------------
darin@google.combe165ae2008-09-07 17:08:29 +0900767// MessageLoop::PendingTask
initial.commit3f4a7322008-07-27 06:49:38 +0900768
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900769MessageLoop::PendingTask::PendingTask(
770 const base::Closure& task,
771 const tracked_objects::Location& posted_from,
772 TimeTicks delayed_run_time,
773 bool nestable)
774 : task(task),
775 time_posted(TimeTicks::Now()),
776 delayed_run_time(delayed_run_time),
777 sequence_num(0),
apatrick@chromium.org87164042011-05-20 07:28:25 +0900778 nestable(nestable),
779 birth_program_counter(posted_from.program_counter()) {
ajwong@chromium.org94d2a582011-04-21 01:02:23 +0900780#if defined(TRACK_ALL_TASK_OBJECTS)
781 if (tracked_objects::ThreadData::IsActive()) {
782 tracked_objects::ThreadData* current_thread_data =
783 tracked_objects::ThreadData::current();
784 if (current_thread_data) {
785 post_births = current_thread_data->TallyABirth(posted_from);
786 } else {
787 // Shutdown started, and this thread wasn't registered.
788 post_births = NULL;
789 }
790 }
791#endif // defined(TRACK_ALL_TASK_OBJECTS)
792}
793
794MessageLoop::PendingTask::~PendingTask() {
795}
796
darin@google.combe165ae2008-09-07 17:08:29 +0900797bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
798 // Since the top of a priority queue is defined as the "greatest" element, we
799 // need to invert the comparison here. We want the smaller time to be at the
800 // top of the heap.
initial.commit3f4a7322008-07-27 06:49:38 +0900801
darin@google.combe165ae2008-09-07 17:08:29 +0900802 if (delayed_run_time < other.delayed_run_time)
803 return false;
initial.commit3f4a7322008-07-27 06:49:38 +0900804
darin@google.combe165ae2008-09-07 17:08:29 +0900805 if (delayed_run_time > other.delayed_run_time)
806 return true;
initial.commit3f4a7322008-07-27 06:49:38 +0900807
darin@google.combe165ae2008-09-07 17:08:29 +0900808 // If the times happen to match, then we use the sequence number to decide.
809 // Compare the difference to support integer roll-over.
810 return (sequence_num - other.sequence_num) > 0;
initial.commit3f4a7322008-07-27 06:49:38 +0900811}
812
813//------------------------------------------------------------------------------
darin@google.comd936b5b2008-08-26 14:53:57 +0900814// MessageLoopForUI
815
816#if defined(OS_WIN)
darin@google.comd936b5b2008-08-26 14:53:57 +0900817void MessageLoopForUI::DidProcessMessage(const MSG& message) {
818 pump_win()->DidProcessMessage(message);
819}
darin@google.comd936b5b2008-08-26 14:53:57 +0900820#endif // defined(OS_WIN)
821
ajwong@chromium.orgc2064f12011-02-26 03:43:23 +0900822#if defined(USE_X11)
823Display* MessageLoopForUI::GetDisplay() {
ajwong@chromium.org440e0762011-02-19 09:32:44 +0900824 return gdk_x11_get_default_xdisplay();
825}
ajwong@chromium.orgc2064f12011-02-26 03:43:23 +0900826#endif // defined(USE_X11)
ajwong@chromium.org440e0762011-02-19 09:32:44 +0900827
abarth@chromium.org1f1c2172010-12-01 17:45:51 +0900828#if !defined(OS_MACOSX) && !defined(OS_NACL)
jcampan@chromium.org05423582009-08-01 07:53:37 +0900829void MessageLoopForUI::AddObserver(Observer* observer) {
830 pump_ui()->AddObserver(observer);
831}
832
833void MessageLoopForUI::RemoveObserver(Observer* observer) {
834 pump_ui()->RemoveObserver(observer);
835}
836
837void MessageLoopForUI::Run(Dispatcher* dispatcher) {
838 AutoRunState save_state(this);
839 state_->dispatcher = dispatcher;
840 RunHandler();
841}
abarth@chromium.org1f1c2172010-12-01 17:45:51 +0900842#endif // !defined(OS_MACOSX) && !defined(OS_NACL)
jcampan@chromium.org05423582009-08-01 07:53:37 +0900843
darin@google.comd936b5b2008-08-26 14:53:57 +0900844//------------------------------------------------------------------------------
845// MessageLoopForIO
846
847#if defined(OS_WIN)
848
rvargas@google.com9e49aa22008-10-10 08:58:43 +0900849void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) {
850 pump_io()->RegisterIOHandler(file, handler);
851}
852
rvargas@google.com73887542008-11-08 06:52:15 +0900853bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) {
854 return pump_io()->WaitForIOCompletion(timeout, filter);
rvargas@google.com9e49aa22008-10-10 08:58:43 +0900855}
856
abarth@chromium.org1f1c2172010-12-01 17:45:51 +0900857#elif defined(OS_POSIX) && !defined(OS_NACL)
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900858
jeremy@chromium.orgefc0db02008-12-16 07:02:17 +0900859bool MessageLoopForIO::WatchFileDescriptor(int fd,
860 bool persistent,
861 Mode mode,
862 FileDescriptorWatcher *controller,
863 Watcher *delegate) {
864 return pump_libevent()->WatchFileDescriptor(
865 fd,
866 persistent,
867 static_cast<base::MessagePumpLibevent::Mode>(mode),
868 controller,
869 delegate);
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900870}
871
dkegel@google.com9e044ae2008-09-19 03:46:26 +0900872#endif