1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
10 #include "base/threading/non_thread_safe.h"
17 class BASE_EXPORT MessagePump
: public NonThreadSafe
{
19 // Please see the comments above the Run method for an illustration of how
20 // these delegate methods are used.
21 class BASE_EXPORT Delegate
{
23 virtual ~Delegate() {}
25 // Called from within Run in response to ScheduleWork or when the message
26 // pump would otherwise call DoDelayedWork. Returns true to indicate that
27 // work was done. DoDelayedWork will still be called if DoWork returns
28 // true, but DoIdleWork will not.
29 virtual bool DoWork() = 0;
31 // Called from within Run in response to ScheduleDelayedWork or when the
32 // message pump would otherwise sleep waiting for more work. Returns true
33 // to indicate that delayed work was done. DoIdleWork will not be called
34 // if DoDelayedWork returns true. Upon return |next_delayed_work_time|
35 // indicates the time when DoDelayedWork should be called again. If
36 // |next_delayed_work_time| is null (per Time::is_null), then the queue of
37 // future delayed work (timer events) is currently empty, and no additional
38 // calls to this function need to be scheduled.
39 virtual bool DoDelayedWork(TimeTicks
* next_delayed_work_time
) = 0;
41 // Called from within Run just before the message pump goes to sleep.
42 // Returns true to indicate that idle work was done.
43 virtual bool DoIdleWork() = 0;
45 // Via the two required out pointers, returns the length of the Delegate's
46 // work queue and the length of time that the first item in the queue has
47 // been waiting to run. If the work queue is empty, the count and delay
49 // Note that this only counts the tasks in the ready-to-run queue and not
50 // the incoming queue that is used by other threads to post tasks. The
51 // latter queue requires holding a lock, which is deemed too expensive for
52 // instrumentation code. Under normal conditions, the incoming queue should
53 // be small or zero, but under heavy loads it may be much larger and
54 // |queue_count| may be up to 1/4 the size of the incoming queue.
55 virtual void GetQueueingInformation(size_t* queue_count
,
56 TimeDelta
* queueing_delay
) {}
60 virtual ~MessagePump();
62 // The Run method is called to enter the message pump's run loop.
64 // Within the method, the message pump is responsible for processing native
65 // messages as well as for giving cycles to the delegate periodically. The
66 // message pump should take care to mix delegate callbacks with native
67 // message processing so neither type of event starves the other of cycles.
69 // The anatomy of a typical run loop:
72 // bool did_work = DoInternalWork();
76 // did_work |= delegate_->DoWork();
80 // TimeTicks next_time;
81 // did_work |= delegate_->DoDelayedWork(&next_time);
88 // did_work = delegate_->DoIdleWork();
98 // Here, DoInternalWork is some private method of the message pump that is
99 // responsible for dispatching the next UI message or notifying the next IO
100 // completion (for example). WaitForWork is a private method that simply
101 // blocks until there is more work of any type to do.
103 // Notice that the run loop cycles between calling DoInternalWork, DoWork,
104 // and DoDelayedWork methods. This helps ensure that none of these work
105 // queues starve the others. This is important for message pumps that are
106 // used to drive animations, for example.
108 // Notice also that after each callout to foreign code, the run loop checks
109 // to see if it should quit. The Quit method is responsible for setting this
110 // flag. No further work is done once the quit flag is set.
112 // NOTE: Care must be taken to handle Run being called again from within any
113 // of the callouts to foreign code. Native message pumps may also need to
114 // deal with other native message pumps being run outside their control
115 // (e.g., the MessageBox API on Windows pumps UI messages!). To be specific,
116 // the callouts (DoWork and DoDelayedWork) MUST still be provided even in
117 // nested sub-loops that are "seemingly" outside the control of this message
118 // pump. DoWork in particular must never be starved for time slices unless
119 // it returns false (meaning it has run out of things to do).
121 virtual void Run(Delegate
* delegate
) = 0;
123 // Quit immediately from the most recently entered run loop. This method may
124 // only be used on the thread that called Run.
125 virtual void Quit() = 0;
127 // Schedule a DoWork callback to happen reasonably soon. Does nothing if a
128 // DoWork callback is already scheduled. This method may be called from any
129 // thread. Once this call is made, DoWork should not be "starved" at least
130 // until it returns a value of false.
131 virtual void ScheduleWork() = 0;
133 // Schedule a DoDelayedWork callback to happen at the specified time,
134 // cancelling any pending DoDelayedWork callback. This method may only be
135 // used on the thread that called Run.
136 virtual void ScheduleDelayedWork(const TimeTicks
& delayed_work_time
) = 0;
141 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_