OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "base/message_pump_libevent.h" | 28 #include "base/message_pump_libevent.h" |
29 #endif | 29 #endif |
30 #if defined(OS_ANDROID) | 30 #if defined(OS_ANDROID) |
31 #include "base/message_pump_android.h" | 31 #include "base/message_pump_android.h" |
32 #endif | 32 #endif |
33 #if defined(TOOLKIT_USES_GTK) | 33 #if defined(TOOLKIT_USES_GTK) |
34 #include <gdk/gdk.h> | 34 #include <gdk/gdk.h> |
35 #include <gdk/gdkx.h> | 35 #include <gdk/gdkx.h> |
36 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) | 36 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
37 | 37 |
| 38 using base::PendingTask; |
38 using base::TimeDelta; | 39 using base::TimeDelta; |
39 using base::TimeTicks; | 40 using base::TimeTicks; |
40 | 41 |
41 namespace { | 42 namespace { |
42 | 43 |
43 // A lazily created thread local storage for quick access to a thread's message | 44 // A lazily created thread local storage for quick access to a thread's message |
44 // loop, if one exists. This should be safe and free of static constructors. | 45 // loop, if one exists. This should be safe and free of static constructors. |
45 base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( | 46 base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( |
46 base::LINKER_INITIALIZED); | 47 base::LINKER_INITIALIZED); |
47 | 48 |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 void MessageLoop::RemoveDestructionObserver( | 254 void MessageLoop::RemoveDestructionObserver( |
254 DestructionObserver* destruction_observer) { | 255 DestructionObserver* destruction_observer) { |
255 DCHECK_EQ(this, current()); | 256 DCHECK_EQ(this, current()); |
256 destruction_observers_.RemoveObserver(destruction_observer); | 257 destruction_observers_.RemoveObserver(destruction_observer); |
257 } | 258 } |
258 | 259 |
259 void MessageLoop::PostTask( | 260 void MessageLoop::PostTask( |
260 const tracked_objects::Location& from_here, Task* task) { | 261 const tracked_objects::Location& from_here, Task* task) { |
261 DCHECK(task); | 262 DCHECK(task); |
262 PendingTask pending_task( | 263 PendingTask pending_task( |
| 264 from_here, |
263 base::Bind( | 265 base::Bind( |
264 &base::subtle::TaskClosureAdapter::Run, | 266 &base::subtle::TaskClosureAdapter::Run, |
265 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), | 267 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), |
266 from_here, | |
267 CalculateDelayedRuntime(0), true); | 268 CalculateDelayedRuntime(0), true); |
268 AddToIncomingQueue(&pending_task); | 269 AddToIncomingQueue(&pending_task); |
269 } | 270 } |
270 | 271 |
271 void MessageLoop::PostDelayedTask( | 272 void MessageLoop::PostDelayedTask( |
272 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { | 273 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
273 DCHECK(task); | 274 DCHECK(task); |
274 PendingTask pending_task( | 275 PendingTask pending_task( |
| 276 from_here, |
275 base::Bind( | 277 base::Bind( |
276 &base::subtle::TaskClosureAdapter::Run, | 278 &base::subtle::TaskClosureAdapter::Run, |
277 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), | 279 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), |
278 from_here, | |
279 CalculateDelayedRuntime(delay_ms), true); | 280 CalculateDelayedRuntime(delay_ms), true); |
280 AddToIncomingQueue(&pending_task); | 281 AddToIncomingQueue(&pending_task); |
281 } | 282 } |
282 | 283 |
283 void MessageLoop::PostNonNestableTask( | 284 void MessageLoop::PostNonNestableTask( |
284 const tracked_objects::Location& from_here, Task* task) { | 285 const tracked_objects::Location& from_here, Task* task) { |
285 DCHECK(task); | 286 DCHECK(task); |
286 PendingTask pending_task( | 287 PendingTask pending_task( |
| 288 from_here, |
287 base::Bind( | 289 base::Bind( |
288 &base::subtle::TaskClosureAdapter::Run, | 290 &base::subtle::TaskClosureAdapter::Run, |
289 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), | 291 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), |
290 from_here, | |
291 CalculateDelayedRuntime(0), false); | 292 CalculateDelayedRuntime(0), false); |
292 AddToIncomingQueue(&pending_task); | 293 AddToIncomingQueue(&pending_task); |
293 } | 294 } |
294 | 295 |
295 void MessageLoop::PostNonNestableDelayedTask( | 296 void MessageLoop::PostNonNestableDelayedTask( |
296 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { | 297 const tracked_objects::Location& from_here, Task* task, int64 delay_ms) { |
297 DCHECK(task); | 298 DCHECK(task); |
298 PendingTask pending_task( | 299 PendingTask pending_task( |
| 300 from_here, |
299 base::Bind( | 301 base::Bind( |
300 &base::subtle::TaskClosureAdapter::Run, | 302 &base::subtle::TaskClosureAdapter::Run, |
301 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), | 303 new base::subtle::TaskClosureAdapter(task, &should_leak_tasks_)), |
302 from_here, | |
303 CalculateDelayedRuntime(delay_ms), false); | 304 CalculateDelayedRuntime(delay_ms), false); |
304 AddToIncomingQueue(&pending_task); | 305 AddToIncomingQueue(&pending_task); |
305 } | 306 } |
306 | 307 |
307 void MessageLoop::PostTask( | 308 void MessageLoop::PostTask( |
308 const tracked_objects::Location& from_here, const base::Closure& task) { | 309 const tracked_objects::Location& from_here, const base::Closure& task) { |
309 DCHECK(!task.is_null()) << from_here.ToString(); | 310 DCHECK(!task.is_null()) << from_here.ToString(); |
310 PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), true); | 311 PendingTask pending_task(from_here, task, CalculateDelayedRuntime(0), true); |
311 AddToIncomingQueue(&pending_task); | 312 AddToIncomingQueue(&pending_task); |
312 } | 313 } |
313 | 314 |
314 void MessageLoop::PostDelayedTask( | 315 void MessageLoop::PostDelayedTask( |
315 const tracked_objects::Location& from_here, const base::Closure& task, | 316 const tracked_objects::Location& from_here, const base::Closure& task, |
316 int64 delay_ms) { | 317 int64 delay_ms) { |
317 DCHECK(!task.is_null()) << from_here.ToString(); | 318 DCHECK(!task.is_null()) << from_here.ToString(); |
318 PendingTask pending_task(task, from_here, | 319 PendingTask pending_task(from_here, task, |
319 CalculateDelayedRuntime(delay_ms), true); | 320 CalculateDelayedRuntime(delay_ms), true); |
320 AddToIncomingQueue(&pending_task); | 321 AddToIncomingQueue(&pending_task); |
321 } | 322 } |
322 | 323 |
323 void MessageLoop::PostNonNestableTask( | 324 void MessageLoop::PostNonNestableTask( |
324 const tracked_objects::Location& from_here, const base::Closure& task) { | 325 const tracked_objects::Location& from_here, const base::Closure& task) { |
325 DCHECK(!task.is_null()) << from_here.ToString(); | 326 DCHECK(!task.is_null()) << from_here.ToString(); |
326 PendingTask pending_task(task, from_here, CalculateDelayedRuntime(0), false); | 327 PendingTask pending_task(from_here, task, CalculateDelayedRuntime(0), false); |
327 AddToIncomingQueue(&pending_task); | 328 AddToIncomingQueue(&pending_task); |
328 } | 329 } |
329 | 330 |
330 void MessageLoop::PostNonNestableDelayedTask( | 331 void MessageLoop::PostNonNestableDelayedTask( |
331 const tracked_objects::Location& from_here, const base::Closure& task, | 332 const tracked_objects::Location& from_here, const base::Closure& task, |
332 int64 delay_ms) { | 333 int64 delay_ms) { |
333 DCHECK(!task.is_null()) << from_here.ToString(); | 334 DCHECK(!task.is_null()) << from_here.ToString(); |
334 PendingTask pending_task(task, from_here, | 335 PendingTask pending_task(from_here, task, |
335 CalculateDelayedRuntime(delay_ms), false); | 336 CalculateDelayedRuntime(delay_ms), false); |
336 AddToIncomingQueue(&pending_task); | 337 AddToIncomingQueue(&pending_task); |
337 } | 338 } |
338 | 339 |
339 void MessageLoop::Run() { | 340 void MessageLoop::Run() { |
340 AutoRunState save_state(this); | 341 AutoRunState save_state(this); |
341 RunHandler(); | 342 RunHandler(); |
342 } | 343 } |
343 | 344 |
344 void MessageLoop::RunAllPending() { | 345 void MessageLoop::RunAllPending() { |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 768 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
768 dispatcher = NULL; | 769 dispatcher = NULL; |
769 #endif | 770 #endif |
770 } | 771 } |
771 | 772 |
772 MessageLoop::AutoRunState::~AutoRunState() { | 773 MessageLoop::AutoRunState::~AutoRunState() { |
773 loop_->state_ = previous_state_; | 774 loop_->state_ = previous_state_; |
774 } | 775 } |
775 | 776 |
776 //------------------------------------------------------------------------------ | 777 //------------------------------------------------------------------------------ |
777 // MessageLoop::PendingTask | |
778 | |
779 MessageLoop::PendingTask::PendingTask( | |
780 const base::Closure& task, | |
781 const tracked_objects::Location& posted_from, | |
782 TimeTicks delayed_run_time, | |
783 bool nestable) | |
784 : base::TrackingInfo(posted_from, delayed_run_time), | |
785 task(task), | |
786 posted_from(posted_from), | |
787 sequence_num(0), | |
788 nestable(nestable) { | |
789 } | |
790 | |
791 MessageLoop::PendingTask::~PendingTask() { | |
792 } | |
793 | |
794 bool MessageLoop::PendingTask::operator<(const PendingTask& other) const { | |
795 // Since the top of a priority queue is defined as the "greatest" element, we | |
796 // need to invert the comparison here. We want the smaller time to be at the | |
797 // top of the heap. | |
798 | |
799 if (delayed_run_time < other.delayed_run_time) | |
800 return false; | |
801 | |
802 if (delayed_run_time > other.delayed_run_time) | |
803 return true; | |
804 | |
805 // If the times happen to match, then we use the sequence number to decide. | |
806 // Compare the difference to support integer roll-over. | |
807 return (sequence_num - other.sequence_num) > 0; | |
808 } | |
809 | |
810 //------------------------------------------------------------------------------ | |
811 // MessageLoopForUI | 778 // MessageLoopForUI |
812 | 779 |
813 #if defined(OS_WIN) | 780 #if defined(OS_WIN) |
814 void MessageLoopForUI::DidProcessMessage(const MSG& message) { | 781 void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
815 pump_win()->DidProcessMessage(message); | 782 pump_win()->DidProcessMessage(message); |
816 } | 783 } |
817 #endif // defined(OS_WIN) | 784 #endif // defined(OS_WIN) |
818 | 785 |
819 #if defined(OS_ANDROID) | 786 #if defined(OS_ANDROID) |
820 void MessageLoopForUI::Start() { | 787 void MessageLoopForUI::Start() { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 Watcher *delegate) { | 836 Watcher *delegate) { |
870 return pump_libevent()->WatchFileDescriptor( | 837 return pump_libevent()->WatchFileDescriptor( |
871 fd, | 838 fd, |
872 persistent, | 839 persistent, |
873 static_cast<base::MessagePumpLibevent::Mode>(mode), | 840 static_cast<base::MessagePumpLibevent::Mode>(mode), |
874 controller, | 841 controller, |
875 delegate); | 842 delegate); |
876 } | 843 } |
877 | 844 |
878 #endif | 845 #endif |
OLD | NEW |