• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_
7 
8 #include <memory>
9 #include <utility>
10 
11 #include "base/base_export.h"
12 #include "base/check.h"
13 #include "base/check_op.h"
14 #include "base/memory/raw_ptr_exclusion.h"
15 #include "base/message_loop/message_pump_type.h"
16 #include "base/message_loop/timer_slack.h"
17 #include "base/sequence_checker.h"
18 #include "base/time/time.h"
19 #include "build/build_config.h"
20 
21 namespace base {
22 
23 class TimeTicks;
24 
25 class BASE_EXPORT MessagePump {
26  public:
27   using MessagePumpFactory = std::unique_ptr<MessagePump>();
28   // Uses the given base::MessagePumpFactory to override the default MessagePump
29   // implementation for 'MessagePumpType::UI'. May only be called once.
30   static void OverrideMessagePumpForUIFactory(MessagePumpFactory* factory);
31 
32   // Returns true if the MessagePumpForUI has been overidden.
33   static bool IsMessagePumpForUIFactoryOveridden();
34 
35   // Creates the default MessagePump based on |type|. Caller owns return value.
36   static std::unique_ptr<MessagePump> Create(MessagePumpType type);
37 
38   // Please see the comments above the Run method for an illustration of how
39   // these delegate methods are used.
40   class BASE_EXPORT Delegate {
41    public:
42     virtual ~Delegate() = default;
43 
44     struct NextWorkInfo {
45       // Helper to extract a TimeDelta for pumps that need a
46       // timeout-till-next-task.
remaining_delayNextWorkInfo47       TimeDelta remaining_delay() const {
48         DCHECK(!delayed_run_time.is_null() && !delayed_run_time.is_max());
49         DCHECK_GE(TimeTicks::Now(), recent_now);
50         return delayed_run_time - recent_now;
51       }
52 
53       // Helper to verify if the next task is ready right away.
is_immediateNextWorkInfo54       bool is_immediate() const { return delayed_run_time.is_null(); }
55 
56       // The next PendingTask's |delayed_run_time|. is_null() if there's extra
57       // work to run immediately. is_max() if there are no more immediate nor
58       // delayed tasks.
59       TimeTicks delayed_run_time;
60 
61       // A recent view of TimeTicks::Now(). Only valid if |delayed_run_time|
62       // isn't null nor max. MessagePump impls should use remaining_delay()
63       // instead of resampling Now() if they wish to sleep for a TimeDelta.
64       TimeTicks recent_now;
65 
66       // If true, native messages should be processed before executing more work
67       // from the Delegate. This is an optional hint; not all message pumps
68       // implement this.
69       bool yield_to_native = false;
70     };
71 
72     // Executes an immediate task or a ripe delayed task. Returns information
73     // about when DoWork() should be called again. If the returned NextWorkInfo
74     // is_immediate(), DoWork() must be invoked again shortly. Else, DoWork()
75     // must be invoked at |NextWorkInfo::delayed_run_time| or when
76     // ScheduleWork() is invoked, whichever comes first. Redundant/spurious
77     // invocations of DoWork() outside of those requirements are tolerated.
78     // DoIdleWork() will not be called so long as this returns a NextWorkInfo
79     // which is_immediate().
80     virtual NextWorkInfo DoWork() = 0;
81 
82     // Called from within Run just before the message pump goes to sleep.
83     // Returns true to indicate that idle work was done; in which case Run()
84     // should resume with calling DoWork(). Returning false means the pump
85     // should now wait.
86     virtual bool DoIdleWork() = 0;
87 
88     class ScopedDoWorkItem {
89      public:
ScopedDoWorkItem()90       ScopedDoWorkItem() : outer_(nullptr), work_item_depth_(0) {}
91 
~ScopedDoWorkItem()92       ~ScopedDoWorkItem() {
93         if (outer_) {
94           outer_->OnEndWorkItem(work_item_depth_);
95         }
96       }
97 
ScopedDoWorkItem(ScopedDoWorkItem && rhs)98       ScopedDoWorkItem(ScopedDoWorkItem&& rhs)
99           : outer_(std::exchange(rhs.outer_, nullptr)),
100             work_item_depth_(rhs.work_item_depth_) {}
101       ScopedDoWorkItem& operator=(ScopedDoWorkItem&& rhs) {
102         // We should only ever go from an empty ScopedDoWorkItem to an
103         // initialized one, or from an initialized one to an empty one.
104         CHECK_NE(IsNull(), rhs.IsNull());
105         // Since we're overwriting this ScopedDoWorkItem, we need to record its
106         // destruction.
107         if (outer_) {
108           outer_->OnEndWorkItem(work_item_depth_);
109         }
110 
111         work_item_depth_ = rhs.work_item_depth_;
112         outer_ = std::exchange(rhs.outer_, nullptr);
113         return *this;
114       }
115 
IsNull()116       bool IsNull() { return !outer_; }
117 
118      private:
119       friend Delegate;
120 
ScopedDoWorkItem(Delegate * outer)121       explicit ScopedDoWorkItem(Delegate* outer) : outer_(outer) {
122         outer_->OnBeginWorkItem();
123         work_item_depth_ = outer_->RunDepth();
124       }
125 
126       // `outer_` is not a raw_ptr<...> for performance reasons (based on
127       // analysis of sampling profiler data and tab_search:top100:2020).
128       RAW_PTR_EXCLUSION Delegate* outer_;
129 
130       // Records the run level at which this DoWorkItem was created to allow
131       // detection of exits of nested loops.
132       int work_item_depth_;
133     };
134 
135     // Called before a unit of work is executed. This allows reports
136     // about individual units of work to be produced. The unit of work ends when
137     // the returned ScopedDoWorkItem goes out of scope.
138     // TODO(crbug.com/851163): Place calls for all platforms. Without this, some
139     // state like the top-level "ThreadController active" trace event will not
140     // be correct when work is performed.
BeginWorkItem()141     [[nodiscard]] ScopedDoWorkItem BeginWorkItem() {
142       return ScopedDoWorkItem(this);
143     }
144 
145     // Called before the message pump starts waiting for work. This indicates
146     // that the message pump is idle (out of application work and ideally out of
147     // native work -- if it can tell).
148     virtual void BeforeWait() = 0;
149 
150     // Returns the nesting level at which the Delegate is currently running.
151     virtual int RunDepth() = 0;
152 
153    private:
154     // Called upon entering/exiting a ScopedDoWorkItem.
155     virtual void OnBeginWorkItem() = 0;
156     virtual void OnEndWorkItem(int work_item_depth) = 0;
157   };
158 
159   MessagePump();
160   virtual ~MessagePump();
161 
162   // The Run method is called to enter the message pump's run loop.
163   //
164   // Within the method, the message pump is responsible for processing native
165   // messages as well as for giving cycles to the delegate periodically. The
166   // message pump should take care to mix delegate callbacks with native message
167   // processing so neither type of event starves the other of cycles. Each call
168   // to a delegate function is considered the beginning of a new "unit of work".
169   //
170   // The anatomy of a typical run loop:
171   //
172   //   for (;;) {
173   //     bool did_native_work = false;
174   //     {
175   //       auto scoped_do_work_item = state_->delegate->BeginWorkItem();
176   //       did_native_work = DoNativeWork();
177   //     }
178   //     if (should_quit_)
179   //       break;
180   //
181   //     Delegate::NextWorkInfo next_work_info = delegate->DoWork();
182   //     if (should_quit_)
183   //       break;
184   //
185   //     if (did_native_work || next_work_info.is_immediate())
186   //       continue;
187   //
188   //     bool did_idle_work = delegate_->DoIdleWork();
189   //     if (should_quit_)
190   //       break;
191   //
192   //     if (did_idle_work)
193   //       continue;
194   //
195   //     WaitForWork();
196   //   }
197   //
198 
199   // Here, DoNativeWork is some private method of the message pump that is
200   // responsible for dispatching the next UI message or notifying the next IO
201   // completion (for example).  WaitForWork is a private method that simply
202   // blocks until there is more work of any type to do.
203   //
204   // Notice that the run loop cycles between calling DoNativeWork and DoWork
205   // methods. This helps ensure that none of these work queues starve the
206   // others. This is important for message pumps that are used to drive
207   // animations, for example.
208   //
209   // Notice also that after each callout to foreign code, the run loop checks to
210   // see if it should quit.  The Quit method is responsible for setting this
211   // flag.  No further work is done once the quit flag is set.
212   //
213   // NOTE 1: Run may be called reentrantly from any of the callouts to foreign
214   // code (internal work, DoWork, DoIdleWork). As a result, DoWork and
215   // DoIdleWork must be reentrant.
216   //
217   // NOTE 2: Run implementations must arrange for DoWork to be invoked as
218   // expected if a callout to foreign code enters a message pump outside their
219   // control. For example, the MessageBox API on Windows pumps UI messages. If
220   // the MessageBox API is called (indirectly) from within Run, it is expected
221   // that DoWork will be invoked from within that call in response to
222   // ScheduleWork or as requested by the last NextWorkInfo returned by DoWork.
223   // The MessagePump::Delegate may then elect to do nested work or not depending
224   // on its policy in that context. Regardless of that decision (and return
225   // value of the nested DoWork() call), DoWork() will be invoked again when the
226   // nested loop unwinds.
227   virtual void Run(Delegate* delegate) = 0;
228 
229   // Quit immediately from the most recently entered run loop.  This method may
230   // only be used on the thread that called Run.
231   virtual void Quit() = 0;
232 
233   // Schedule a DoWork callback to happen reasonably soon.  Does nothing if a
234   // DoWork callback is already scheduled. Once this call is made, DoWork is
235   // guaranteed to be called repeatedly at least until it returns a
236   // non-immediate NextWorkInfo. This call can be expensive and callers should
237   // attempt not to invoke it again before a non-immediate NextWorkInfo was
238   // returned from DoWork(). Thread-safe (and callers should avoid holding a
239   // Lock at all cost while making this call as some platforms' priority
240   // boosting features have been observed to cause the caller to get descheduled
241   // : https://crbug.com/890978).
242   virtual void ScheduleWork() = 0;
243 
244   // Schedule a DoWork callback to happen at the specified time, cancelling any
245   // pending callback scheduled by this method. This method may only be used on
246   // the thread that called Run.
247   //
248   // It isn't necessary to call this during normal execution, as the pump wakes
249   // up as requested by the return value of DoWork().
250   // TODO(crbug.com/885371): Determine if this must be called to ensure that
251   // delayed tasks run when a message pump outside the control of Run is
252   // entered.
253   virtual void ScheduleDelayedWork(
254       const Delegate::NextWorkInfo& next_work_info) = 0;
255 
256   // Sets the timer slack to the specified value.
257   virtual void SetTimerSlack(TimerSlack timer_slack);
258 };
259 
260 }  // namespace base
261 
262 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_
263