OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef BASE_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MESSAGE_LOOP_H_ |
6 #define BASE_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // | 90 // |
91 // The NonNestable variants work similarly except that they promise never to | 91 // The NonNestable variants work similarly except that they promise never to |
92 // dispatch the task from a nested invocation of MessageLoop::Run. Instead, | 92 // dispatch the task from a nested invocation of MessageLoop::Run. Instead, |
93 // such tasks get deferred until the top-most MessageLoop::Run is executing. | 93 // such tasks get deferred until the top-most MessageLoop::Run is executing. |
94 // | 94 // |
95 // The MessageLoop takes ownership of the Task, and deletes it after it has | 95 // The MessageLoop takes ownership of the Task, and deletes it after it has |
96 // been Run(). | 96 // been Run(). |
97 // | 97 // |
98 // NOTE: These methods may be called on any thread. The Task will be invoked | 98 // NOTE: These methods may be called on any thread. The Task will be invoked |
99 // on the thread that executes MessageLoop::Run(). | 99 // on the thread that executes MessageLoop::Run(). |
100 | 100 |
101 void PostTask( | 101 void PostTask( |
102 const tracked_objects::Location& from_here, Task* task); | 102 const tracked_objects::Location& from_here, Task* task); |
103 | 103 |
104 void PostDelayedTask( | 104 void PostDelayedTask( |
105 const tracked_objects::Location& from_here, Task* task, int delay_ms); | 105 const tracked_objects::Location& from_here, Task* task, int delay_ms); |
106 | 106 |
107 void PostNonNestableTask( | 107 void PostNonNestableTask( |
108 const tracked_objects::Location& from_here, Task* task); | 108 const tracked_objects::Location& from_here, Task* task); |
109 | 109 |
110 void PostNonNestableDelayedTask( | 110 void PostNonNestableDelayedTask( |
111 const tracked_objects::Location& from_here, Task* task, int delay_ms); | 111 const tracked_objects::Location& from_here, Task* task, int delay_ms); |
112 | 112 |
113 // A variant on PostTask that deletes the given object. This is useful | 113 // A variant on PostTask that deletes the given object. This is useful |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // once it becomes idle. | 240 // once it becomes idle. |
241 bool quit_received; | 241 bool quit_received; |
242 | 242 |
243 #if defined(OS_WIN) | 243 #if defined(OS_WIN) |
244 base::MessagePumpWin::Dispatcher* dispatcher; | 244 base::MessagePumpWin::Dispatcher* dispatcher; |
245 #endif | 245 #endif |
246 }; | 246 }; |
247 | 247 |
248 class AutoRunState : RunState { | 248 class AutoRunState : RunState { |
249 public: | 249 public: |
250 AutoRunState(MessageLoop* loop); | 250 explicit AutoRunState(MessageLoop* loop); |
251 ~AutoRunState(); | 251 ~AutoRunState(); |
252 private: | 252 private: |
253 MessageLoop* loop_; | 253 MessageLoop* loop_; |
254 RunState* previous_state_; | 254 RunState* previous_state_; |
255 }; | 255 }; |
256 | 256 |
257 // This structure is copied around by value. | 257 // This structure is copied around by value. |
258 struct PendingTask { | 258 struct PendingTask { |
259 Task* task; // The task to run. | 259 Task* task; // The task to run. |
260 Time delayed_run_time; // The time when the task should be run. | 260 Time delayed_run_time; // The time when the task should be run. |
261 int sequence_num; // Used to facilitate sorting by run time. | 261 int sequence_num; // Used to facilitate sorting by run time. |
262 bool nestable; // True if OK to dispatch from a nested loop. | 262 bool nestable; // True if OK to dispatch from a nested loop. |
263 | 263 |
264 PendingTask(Task* task, bool nestable) | 264 PendingTask(Task* task, bool nestable) |
265 : task(task), sequence_num(0), nestable(nestable) { | 265 : task(task), sequence_num(0), nestable(nestable) { |
266 } | 266 } |
267 | 267 |
268 // Used to support sorting. | 268 // Used to support sorting. |
269 bool operator<(const PendingTask& other) const; | 269 bool operator<(const PendingTask& other) const; |
270 }; | 270 }; |
271 | 271 |
272 typedef std::queue<PendingTask> TaskQueue; | 272 typedef std::queue<PendingTask> TaskQueue; |
273 typedef std::priority_queue<PendingTask> DelayedTaskQueue; | 273 typedef std::priority_queue<PendingTask> DelayedTaskQueue; |
274 | 274 |
275 #if defined(OS_WIN) | 275 #if defined(OS_WIN) |
276 base::MessagePumpWin* pump_win() { | 276 base::MessagePumpWin* pump_win() { |
277 return static_cast<base::MessagePumpWin*>(pump_.get()); | 277 return static_cast<base::MessagePumpWin*>(pump_.get()); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 void HistogramEvent(int event); | 347 void HistogramEvent(int event); |
348 | 348 |
349 static const LinearHistogram::DescriptionPair event_descriptions_[]; | 349 static const LinearHistogram::DescriptionPair event_descriptions_[]; |
350 static bool enable_histogrammer_; | 350 static bool enable_histogrammer_; |
351 | 351 |
352 Type type_; | 352 Type type_; |
353 | 353 |
354 // A list of tasks that need to be processed by this instance. Note that | 354 // A list of tasks that need to be processed by this instance. Note that |
355 // this queue is only accessed (push/pop) by our current thread. | 355 // this queue is only accessed (push/pop) by our current thread. |
356 TaskQueue work_queue_; | 356 TaskQueue work_queue_; |
357 | 357 |
358 // Contains delayed tasks, sorted by their 'delayed_run_time' property. | 358 // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
359 DelayedTaskQueue delayed_work_queue_; | 359 DelayedTaskQueue delayed_work_queue_; |
360 | 360 |
361 // A queue of non-nestable tasks that we had to defer because when it came | 361 // A queue of non-nestable tasks that we had to defer because when it came |
362 // time to execute them we were in a nested message loop. They will execute | 362 // time to execute them we were in a nested message loop. They will execute |
363 // once we're out of nested message loops. | 363 // once we're out of nested message loops. |
364 TaskQueue deferred_non_nestable_work_queue_; | 364 TaskQueue deferred_non_nestable_work_queue_; |
365 | 365 |
366 scoped_refptr<base::MessagePump> pump_; | 366 scoped_refptr<base::MessagePump> pump_; |
367 | 367 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 protected: | 469 protected: |
470 // TODO(rvargas): Make this platform independent. | 470 // TODO(rvargas): Make this platform independent. |
471 base::MessagePumpForIO* pump_io() { | 471 base::MessagePumpForIO* pump_io() { |
472 return static_cast<base::MessagePumpForIO*>(pump_.get()); | 472 return static_cast<base::MessagePumpForIO*>(pump_.get()); |
473 } | 473 } |
474 | 474 |
475 #elif defined(OS_POSIX) | 475 #elif defined(OS_POSIX) |
476 typedef base::MessagePumpLibevent::Watcher Watcher; | 476 typedef base::MessagePumpLibevent::Watcher Watcher; |
477 | 477 |
478 // Please see MessagePumpLibevent for definitions of these methods. | 478 // Please see MessagePumpLibevent for definitions of these methods. |
479 void WatchSocket(int socket, short interest_mask, | 479 void WatchSocket(int socket, short interest_mask, |
480 struct event* e, Watcher* watcher); | 480 struct event* e, Watcher* watcher); |
481 void UnwatchSocket(struct event* e); | 481 void UnwatchSocket(struct event* e); |
482 #endif // defined(OS_POSIX) | 482 #endif // defined(OS_POSIX) |
483 }; | 483 }; |
484 | 484 |
485 // Do not add any member variables to MessageLoopForIO! This is important b/c | 485 // Do not add any member variables to MessageLoopForIO! This is important b/c |
486 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 486 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
487 // data that you need should be stored on the MessageLoop's pump_ instance. | 487 // data that you need should be stored on the MessageLoop's pump_ instance. |
488 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 488 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
489 MessageLoopForIO_should_not_have_extra_member_variables); | 489 MessageLoopForIO_should_not_have_extra_member_variables); |
490 | 490 |
491 #endif // BASE_MESSAGE_LOOP_H_ | 491 #endif // BASE_MESSAGE_LOOP_H_ |
OLD | NEW |