• 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 #include "base/message_loop/message_pump_win.h"
6 
7 #include <algorithm>
8 #include <cstdint>
9 #include <type_traits>
10 
11 #include "base/auto_reset.h"
12 #include "base/check.h"
13 #include "base/debug/alias.h"
14 #include "base/feature_list.h"
15 #include "base/functional/bind.h"
16 #include "base/metrics/histogram_macros.h"
17 #include "base/numerics/safe_conversions.h"
18 #include "base/trace_event/base_tracing.h"
19 #include "base/tracing_buildflags.h"
20 
21 #if BUILDFLAG(ENABLE_BASE_TRACING)
22 #include "third_party/perfetto/protos/perfetto/trace/track_event/chrome_message_pump.pbzero.h"
23 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
24 
25 namespace base {
26 
27 namespace {
28 
29 enum MessageLoopProblems {
30   MESSAGE_POST_ERROR,
31   COMPLETION_POST_ERROR,
32   SET_TIMER_ERROR,
33   RECEIVED_WM_QUIT_ERROR,
34   MESSAGE_LOOP_PROBLEM_MAX,
35 };
36 
37 // Returns the number of milliseconds before |next_task_time|, clamped between
38 // zero and the biggest DWORD value (or INFINITE if |next_task_time.is_max()|).
39 // Optionally, a recent value of Now() may be passed in to avoid resampling it.
GetSleepTimeoutMs(TimeTicks next_task_time,TimeTicks recent_now=TimeTicks ())40 DWORD GetSleepTimeoutMs(TimeTicks next_task_time,
41                         TimeTicks recent_now = TimeTicks()) {
42   // Shouldn't need to sleep or install a timer when there's pending immediate
43   // work.
44   DCHECK(!next_task_time.is_null());
45 
46   if (next_task_time.is_max())
47     return INFINITE;
48 
49   auto now = recent_now.is_null() ? TimeTicks::Now() : recent_now;
50   auto timeout_ms = (next_task_time - now).InMillisecondsRoundedUp();
51 
52   // A saturated_cast with an unsigned destination automatically clamps negative
53   // values at zero.
54   static_assert(!std::is_signed_v<DWORD>, "DWORD is unexpectedly signed");
55   return saturated_cast<DWORD>(timeout_ms);
56 }
57 
58 }  // namespace
59 
60 // Message sent to get an additional time slice for pumping (processing) another
61 // task (a series of such messages creates a continuous task pump).
62 static const int kMsgHaveWork = WM_USER + 1;
63 
64 //-----------------------------------------------------------------------------
65 // MessagePumpWin public:
66 
67 MessagePumpWin::MessagePumpWin() = default;
68 MessagePumpWin::~MessagePumpWin() = default;
69 
Run(Delegate * delegate)70 void MessagePumpWin::Run(Delegate* delegate) {
71   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
72 
73   RunState run_state(delegate);
74   if (run_state_)
75     run_state.is_nested = true;
76 
77   AutoReset<RunState*> auto_reset_run_state(&run_state_, &run_state);
78   DoRunLoop();
79 }
80 
Quit()81 void MessagePumpWin::Quit() {
82   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
83 
84   DCHECK(run_state_);
85   run_state_->should_quit = true;
86 }
87 
88 //-----------------------------------------------------------------------------
89 // MessagePumpForUI public:
90 
MessagePumpForUI()91 MessagePumpForUI::MessagePumpForUI() {
92   bool succeeded = message_window_.Create(
93       BindRepeating(&MessagePumpForUI::MessageCallback, Unretained(this)));
94   CHECK(succeeded);
95 }
96 
97 MessagePumpForUI::~MessagePumpForUI() = default;
98 
ScheduleWork()99 void MessagePumpForUI::ScheduleWork() {
100   // This is the only MessagePumpForUI method which can be called outside of
101   // |bound_thread_|.
102 
103   bool not_scheduled = false;
104   if (!work_scheduled_.compare_exchange_strong(not_scheduled, true))
105     return;  // Someone else continued the pumping.
106 
107   // Make sure the MessagePump does some work for us.
108   const BOOL ret = ::PostMessage(message_window_.hwnd(), kMsgHaveWork, 0, 0);
109   if (ret)
110     return;  // There was room in the Window Message queue.
111 
112   // We have failed to insert a have-work message, so there is a chance that we
113   // will starve tasks/timers while sitting in a nested run loop.  Nested
114   // loops only look at Windows Message queues, and don't look at *our* task
115   // queues, etc., so we might not get a time slice in such. :-(
116   // We could abort here, but the fear is that this failure mode is plausibly
117   // common (queue is full, of about 2000 messages), so we'll do a near-graceful
118   // recovery.  Nested loops are pretty transient (we think), so this will
119   // probably be recoverable.
120 
121   // Clarify that we didn't really insert.
122   work_scheduled_ = false;
123   TRACE_EVENT_INSTANT0("base", "Chrome.MessageLoopProblem.MESSAGE_POST_ERROR",
124                        TRACE_EVENT_SCOPE_THREAD);
125 }
126 
ScheduleDelayedWork(const Delegate::NextWorkInfo & next_work_info)127 void MessagePumpForUI::ScheduleDelayedWork(
128     const Delegate::NextWorkInfo& next_work_info) {
129   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
130 
131   // Since this is always called from |bound_thread_|, there is almost always
132   // nothing to do as the loop is already running. When the loop becomes idle,
133   // it will typically WaitForWork() in DoRunLoop() with the timeout provided by
134   // DoWork(). The only alternative to this is entering a native nested loop
135   // (e.g. modal dialog) under a ScopedNestableTaskAllower, in which case
136   // HandleWorkMessage() will be invoked when the system picks up kMsgHaveWork
137   // and it will ScheduleNativeTimer() if it's out of immediate work. However,
138   // in that alternate scenario : it's possible for a Windows native work item
139   // (e.g. https://docs.microsoft.com/en-us/windows/desktop/winmsg/using-hooks)
140   // to wake the native nested loop and PostDelayedTask() to the current thread
141   // from it. This is the only case where we must install/adjust the native
142   // timer from ScheduleDelayedWork() because if we don't, the native loop will
143   // go back to sleep, unaware of the new |delayed_work_time|.
144   // See MessageLoopTest.PostDelayedTaskFromSystemPump for an example.
145   // TODO(gab): This could potentially be replaced by a ForegroundIdleProc hook
146   // if Windows ends up being the only platform requiring ScheduleDelayedWork().
147   if (in_native_loop_ && !work_scheduled_) {
148     ScheduleNativeTimer(next_work_info);
149   }
150 }
151 
AddObserver(Observer * observer)152 void MessagePumpForUI::AddObserver(Observer* observer) {
153   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
154   observers_.AddObserver(observer);
155 }
156 
RemoveObserver(Observer * observer)157 void MessagePumpForUI::RemoveObserver(Observer* observer) {
158   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
159   observers_.RemoveObserver(observer);
160 }
161 
162 //-----------------------------------------------------------------------------
163 // MessagePumpForUI private:
164 
MessageCallback(UINT message,WPARAM wparam,LPARAM lparam,LRESULT * result)165 bool MessagePumpForUI::MessageCallback(
166     UINT message, WPARAM wparam, LPARAM lparam, LRESULT* result) {
167   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
168   switch (message) {
169     case kMsgHaveWork:
170       HandleWorkMessage();
171       break;
172     case WM_TIMER:
173       if (wparam == reinterpret_cast<UINT_PTR>(this))
174         HandleTimerMessage();
175       break;
176   }
177   return false;
178 }
179 
DoRunLoop()180 void MessagePumpForUI::DoRunLoop() {
181   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
182 
183   // IF this was just a simple PeekMessage() loop (servicing all possible work
184   // queues), then Windows would try to achieve the following order according
185   // to MSDN documentation about PeekMessage with no filter):
186   //    * Sent messages
187   //    * Posted messages
188   //    * Sent messages (again)
189   //    * WM_PAINT messages
190   //    * WM_TIMER messages
191   //
192   // Summary: none of the above classes is starved, and sent messages has twice
193   // the chance of being processed (i.e., reduced service time).
194 
195   for (;;) {
196     // If we do any work, we may create more messages etc., and more work may
197     // possibly be waiting in another task group.  When we (for example)
198     // ProcessNextWindowsMessage(), there is a good chance there are still more
199     // messages waiting.  On the other hand, when any of these methods return
200     // having done no work, then it is pretty unlikely that calling them again
201     // quickly will find any work to do.  Finally, if they all say they had no
202     // work, then it is a good time to consider sleeping (waiting) for more
203     // work.
204 
205     in_native_loop_ = false;
206 
207     bool more_work_is_plausible = ProcessNextWindowsMessage();
208     in_native_loop_ = false;
209     if (run_state_->should_quit)
210       break;
211 
212     Delegate::NextWorkInfo next_work_info = run_state_->delegate->DoWork();
213     in_native_loop_ = false;
214     more_work_is_plausible |= next_work_info.is_immediate();
215     if (run_state_->should_quit)
216       break;
217 
218     if (installed_native_timer_) {
219       // As described in ScheduleNativeTimer(), the native timer is only
220       // installed and needed while in a nested native loop. If it is installed,
221       // it means the above work entered such a loop. Having now resumed, the
222       // native timer is no longer needed.
223       KillNativeTimer();
224     }
225 
226     if (more_work_is_plausible)
227       continue;
228 
229     more_work_is_plausible = run_state_->delegate->DoIdleWork();
230     // DoIdleWork() shouldn't end up in native nested loops and thus shouldn't
231     // have any chance of reinstalling a native timer.
232     DCHECK(!in_native_loop_);
233     DCHECK(!installed_native_timer_);
234     if (run_state_->should_quit)
235       break;
236 
237     if (more_work_is_plausible)
238       continue;
239 
240     WaitForWork(next_work_info);
241   }
242 }
243 
WaitForWork(Delegate::NextWorkInfo next_work_info)244 void MessagePumpForUI::WaitForWork(Delegate::NextWorkInfo next_work_info) {
245   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
246 
247   // Wait until a message is available, up to the time needed by the timer
248   // manager to fire the next set of timers.
249   DWORD wait_flags = MWMO_INPUTAVAILABLE;
250   bool last_wakeup_was_spurious = false;
251   for (DWORD delay = GetSleepTimeoutMs(next_work_info.delayed_run_time,
252                                        next_work_info.recent_now);
253        delay != 0; delay = GetSleepTimeoutMs(next_work_info.delayed_run_time)) {
254     if (!last_wakeup_was_spurious) {
255       run_state_->delegate->BeforeWait();
256     }
257     last_wakeup_was_spurious = false;
258 
259     // Tell the optimizer to retain these values to simplify analyzing hangs.
260     base::debug::Alias(&delay);
261     base::debug::Alias(&wait_flags);
262     DWORD result = MsgWaitForMultipleObjectsEx(0, nullptr, delay, QS_ALLINPUT,
263                                                wait_flags);
264 
265     if (WAIT_OBJECT_0 == result) {
266       // A WM_* message is available.
267       // If a parent child relationship exists between windows across threads
268       // then their thread inputs are implicitly attached.
269       // This causes the MsgWaitForMultipleObjectsEx API to return indicating
270       // that messages are ready for processing (Specifically, mouse messages
271       // intended for the child window may appear if the child window has
272       // capture).
273       // The subsequent PeekMessages call may fail to return any messages thus
274       // causing us to enter a tight loop at times.
275       // The code below is a workaround to give the child window
276       // some time to process its input messages by looping back to
277       // MsgWaitForMultipleObjectsEx above when there are no messages for the
278       // current thread.
279 
280       // As in ProcessNextWindowsMessage().
281       auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
282       {
283         TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("base"),
284                      "MessagePumpForUI::WaitForWork GetQueueStatus");
285         if (HIWORD(::GetQueueStatus(QS_SENDMESSAGE)) & QS_SENDMESSAGE)
286           return;
287       }
288       {
289         MSG msg;
290         TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("base"),
291                      "MessagePumpForUI::WaitForWork PeekMessage");
292         if (::PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE))
293           return;
294       }
295 
296       // We know there are no more messages for this thread because PeekMessage
297       // has returned false. Reset |wait_flags| so that we wait for a *new*
298       // message.
299       wait_flags = 0;
300     } else {
301       last_wakeup_was_spurious = true;
302       TRACE_EVENT_INSTANT("base",
303                           "MessagePumpForUI::WaitForWork Spurious Wakeup",
304                           "reason: ", result);
305     }
306 
307     DCHECK_NE(WAIT_FAILED, result) << GetLastError();
308   }
309 }
310 
HandleWorkMessage()311 void MessagePumpForUI::HandleWorkMessage() {
312   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
313 
314   // The kMsgHaveWork message was consumed by a native loop, we must assume
315   // we're in one until DoRunLoop() gets control back.
316   in_native_loop_ = true;
317 
318   // If we are being called outside of the context of Run, then don't try to do
319   // any work.  This could correspond to a MessageBox call or something of that
320   // sort.
321   if (!run_state_) {
322     // Since we handled a kMsgHaveWork message, we must still update this flag.
323     work_scheduled_ = false;
324     return;
325   }
326 
327   // Let whatever would have run had we not been putting messages in the queue
328   // run now.  This is an attempt to make our dummy message not starve other
329   // messages that may be in the Windows message queue.
330   ProcessPumpReplacementMessage();
331 
332   Delegate::NextWorkInfo next_work_info = run_state_->delegate->DoWork();
333   if (next_work_info.is_immediate()) {
334     ScheduleWork();
335   } else {
336     run_state_->delegate->BeforeWait();
337     ScheduleNativeTimer(next_work_info);
338   }
339 }
340 
HandleTimerMessage()341 void MessagePumpForUI::HandleTimerMessage() {
342   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
343 
344   // ::KillTimer doesn't remove pending WM_TIMER messages from the queue,
345   // explicitly ignore the last WM_TIMER message in that case to avoid handling
346   // work from here when DoRunLoop() is active (which could result in scheduling
347   // work from two places at once). Note: we're still fine in the event that a
348   // second native nested loop is entered before such a dead WM_TIMER message is
349   // discarded because ::SetTimer merely resets the timer if invoked twice with
350   // the same id.
351   if (!installed_native_timer_)
352     return;
353 
354   // We only need to fire once per specific delay, another timer may be
355   // scheduled below but we're done with this one.
356   KillNativeTimer();
357 
358   // If we are being called outside of the context of Run, then don't do
359   // anything.  This could correspond to a MessageBox call or something of
360   // that sort.
361   if (!run_state_)
362     return;
363 
364   Delegate::NextWorkInfo next_work_info = run_state_->delegate->DoWork();
365   if (next_work_info.is_immediate()) {
366     ScheduleWork();
367   } else {
368     run_state_->delegate->BeforeWait();
369     ScheduleNativeTimer(next_work_info);
370   }
371 }
372 
ScheduleNativeTimer(Delegate::NextWorkInfo next_work_info)373 void MessagePumpForUI::ScheduleNativeTimer(
374     Delegate::NextWorkInfo next_work_info) {
375   DCHECK(!next_work_info.is_immediate());
376   DCHECK(in_native_loop_);
377 
378   // Do not redundantly set the same native timer again if it was already set.
379   // This can happen when a nested native loop goes idle with pending delayed
380   // tasks, then gets woken up by an immediate task, and goes back to idle with
381   // the same pending delay. No need to kill the native timer if there is
382   // already one but the |delayed_run_time| has changed as ::SetTimer reuses the
383   // same id and will replace and reset the existing timer.
384   if (installed_native_timer_ &&
385       *installed_native_timer_ == next_work_info.delayed_run_time) {
386     return;
387   }
388 
389   if (next_work_info.delayed_run_time.is_max())
390     return;
391 
392   // We do not use native Windows timers in general as they have a poor, 10ms,
393   // granularity. Instead we rely on MsgWaitForMultipleObjectsEx's
394   // high-resolution timeout to sleep without timers in WaitForWork(). However,
395   // when entering a nested native ::GetMessage() loop (e.g. native modal
396   // windows) under a ScopedNestableTaskAllower, we have to rely on a native
397   // timer when HandleWorkMessage() runs out of immediate work. Since
398   // ScopedNestableTaskAllower invokes ScheduleWork() : we are guaranteed that
399   // HandleWorkMessage() will be called after entering a nested native loop that
400   // should process application tasks. But once HandleWorkMessage() is out of
401   // immediate work, ::SetTimer() is used to guarantee we are invoked again
402   // should the next delayed task expire before the nested native loop ends. The
403   // native timer being unnecessary once we return to our DoRunLoop(), we
404   // ::KillTimer when it resumes (nested native loops should be rare so we're
405   // not worried about ::SetTimer<=>::KillTimer churn).
406   // TODO(gab): The long-standing legacy dependency on the behavior of
407   // ScopedNestableTaskAllower is unfortunate, would be nice to make this a
408   // MessagePump concept (instead of requiring impls to invoke ScheduleWork()
409   // one-way and no-op DoWork() the other way).
410 
411   UINT delay_msec = strict_cast<UINT>(GetSleepTimeoutMs(
412       next_work_info.delayed_run_time, next_work_info.recent_now));
413   if (delay_msec == 0) {
414     ScheduleWork();
415   } else {
416     // TODO(gab): ::SetTimer()'s documentation claims it does this for us.
417     // Consider removing this safety net.
418     delay_msec = std::clamp(delay_msec, static_cast<UINT>(USER_TIMER_MINIMUM),
419                             static_cast<UINT>(USER_TIMER_MAXIMUM));
420 
421     // Tell the optimizer to retain the delay to simplify analyzing hangs.
422     base::debug::Alias(&delay_msec);
423     const UINT_PTR ret =
424         ::SetTimer(message_window_.hwnd(), reinterpret_cast<UINT_PTR>(this),
425                    delay_msec, nullptr);
426 
427     if (ret) {
428       installed_native_timer_ = next_work_info.delayed_run_time;
429       return;
430     }
431     // This error is likely similar to MESSAGE_POST_ERROR (i.e. native queue is
432     // full). Since we only use ScheduleNativeTimer() in native nested loops
433     // this likely means this pump will not be given a chance to run application
434     // tasks until the nested loop completes.
435     TRACE_EVENT_INSTANT0("base", "Chrome.MessageLoopProblem.SET_TIMER_ERROR",
436                          TRACE_EVENT_SCOPE_THREAD);
437   }
438 }
439 
KillNativeTimer()440 void MessagePumpForUI::KillNativeTimer() {
441   DCHECK(installed_native_timer_);
442   const bool success =
443       ::KillTimer(message_window_.hwnd(), reinterpret_cast<UINT_PTR>(this));
444   DPCHECK(success);
445   installed_native_timer_.reset();
446 }
447 
ProcessNextWindowsMessage()448 bool MessagePumpForUI::ProcessNextWindowsMessage() {
449   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
450 
451   MSG msg;
452   bool has_msg = false;
453   bool more_work_is_plausible = false;
454   {
455     // ::PeekMessage() may process sent and/or internal messages (regardless of
456     // |had_messages| as ::GetQueueStatus() is an optimistic check that may
457     // racily have missed an incoming event -- it doesn't hurt to have empty
458     // internal units of work when ::PeekMessage turns out to be a no-op).
459     // Instantiate |scoped_do_work| ahead of GetQueueStatus() so that
460     // trace events it emits fully outscope GetQueueStatus' events
461     // (GetQueueStatus() itself not being expected to do work; it's fine to use
462     // only one ScopedDoWorkItem for both calls -- we trace them independently
463     // just in case internal work stalls).
464     auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
465 
466     {
467       // Individually trace ::GetQueueStatus and ::PeekMessage because sampling
468       // profiler is hinting that we're spending a surprising amount of time
469       // with these on top of the stack. Tracing will be able to tell us whether
470       // this is a bias of sampling profiler (e.g. kernel takes ::GetQueueStatus
471       // as an opportunity to swap threads and is more likely to schedule the
472       // sampling profiler's thread while the sampled thread is swapped out on
473       // this frame).
474       TRACE_EVENT0(
475           TRACE_DISABLED_BY_DEFAULT("base"),
476           "MessagePumpForUI::ProcessNextWindowsMessage GetQueueStatus");
477       DWORD queue_status = ::GetQueueStatus(QS_SENDMESSAGE);
478 
479       // If there are sent messages in the queue then PeekMessage internally
480       // dispatches the message and returns false. We return true in this case
481       // to ensure that the message loop peeks again instead of calling
482       // MsgWaitForMultipleObjectsEx.
483       if (HIWORD(queue_status) & QS_SENDMESSAGE)
484         more_work_is_plausible = true;
485     }
486 
487     {
488       // PeekMessage can run a message if there are sent messages, trace that
489       // and emit the boolean param to see if it ever janks independently (ref.
490       // comment on GetQueueStatus).
491       TRACE_EVENT(
492           TRACE_DISABLED_BY_DEFAULT("base"),
493           "MessagePumpForUI::ProcessNextWindowsMessage PeekMessage",
494           [&](perfetto::EventContext ctx) {
495             perfetto::protos::pbzero::ChromeMessagePump* msg_pump_data =
496                 ctx.event()->set_chrome_message_pump();
497             msg_pump_data->set_sent_messages_in_queue(more_work_is_plausible);
498           });
499       has_msg = ::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) != FALSE;
500     }
501   }
502   if (has_msg)
503     more_work_is_plausible |= ProcessMessageHelper(msg);
504 
505   return more_work_is_plausible;
506 }
507 
ProcessMessageHelper(const MSG & msg)508 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) {
509   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
510 
511   if (msg.message == WM_QUIT) {
512     // WM_QUIT is the standard way to exit a ::GetMessage() loop. Our
513     // MessageLoop has its own quit mechanism, so WM_QUIT is generally
514     // unexpected.
515     TRACE_EVENT_INSTANT0("base",
516                          "Chrome.MessageLoopProblem.RECEIVED_WM_QUIT_ERROR",
517                          TRACE_EVENT_SCOPE_THREAD);
518     return true;
519   }
520 
521   // While running our main message pump, we discard kMsgHaveWork messages.
522   if (msg.message == kMsgHaveWork && msg.hwnd == message_window_.hwnd())
523     return ProcessPumpReplacementMessage();
524 
525   run_state_->delegate->BeginNativeWorkBeforeDoWork();
526   auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
527 
528   TRACE_EVENT("base,toplevel", "MessagePumpForUI DispatchMessage",
529               [&](perfetto::EventContext ctx) {
530                 ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>()
531                     ->set_chrome_message_pump_for_ui()
532                     ->set_message_id(msg.message);
533               });
534 
535   for (Observer& observer : observers_)
536     observer.WillDispatchMSG(msg);
537   ::TranslateMessage(&msg);
538   ::DispatchMessage(&msg);
539   for (Observer& observer : observers_)
540     observer.DidDispatchMSG(msg);
541 
542   return true;
543 }
544 
ProcessPumpReplacementMessage()545 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
546   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
547 
548   // When we encounter a kMsgHaveWork message, this method is called to peek and
549   // process a replacement message. The goal is to make the kMsgHaveWork as non-
550   // intrusive as possible, even though a continuous stream of such messages are
551   // posted. This method carefully peeks a message while there is no chance for
552   // a kMsgHaveWork to be pending, then resets the |have_work_| flag (allowing a
553   // replacement kMsgHaveWork to possibly be posted), and finally dispatches
554   // that peeked replacement. Note that the re-post of kMsgHaveWork may be
555   // asynchronous to this thread!!
556 
557   MSG msg;
558   bool have_message = false;
559   {
560     // Note: Ideally this call wouldn't process sent-messages (as we already did
561     // that in the PeekMessage call that lead to receiving this kMsgHaveWork),
562     // but there's no way to specify this (omitting PM_QS_SENDMESSAGE as in
563     // crrev.com/791043 doesn't do anything). Hence this call must be considered
564     // as a potential work item.
565     auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
566     TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("base"),
567                  "MessagePumpForUI::ProcessPumpReplacementMessage PeekMessage");
568     have_message = ::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) != FALSE;
569   }
570 
571   // Expect no message or a message different than kMsgHaveWork.
572   DCHECK(!have_message || kMsgHaveWork != msg.message ||
573          msg.hwnd != message_window_.hwnd());
574 
575   // Since we discarded a kMsgHaveWork message, we must update the flag.
576   DCHECK(work_scheduled_);
577   work_scheduled_ = false;
578 
579   // We don't need a special time slice if we didn't |have_message| to process.
580   if (!have_message)
581     return false;
582 
583   if (msg.message == WM_QUIT) {
584     // If we're in a nested ::GetMessage() loop then we must let that loop see
585     // the WM_QUIT in order for it to exit. If we're in DoRunLoop then the re-
586     // posted WM_QUIT will be either ignored, or handled, by
587     // ProcessMessageHelper() called directly from ProcessNextWindowsMessage().
588     ::PostQuitMessage(static_cast<int>(msg.wParam));
589     // Note: we *must not* ScheduleWork() here as WM_QUIT is a low-priority
590     // message on Windows (it is only returned by ::PeekMessage() when idle) :
591     // https://blogs.msdn.microsoft.com/oldnewthing/20051104-33/?p=33453. As
592     // such posting a kMsgHaveWork message via ScheduleWork() would cause an
593     // infinite loop (kMsgHaveWork message handled first means we end up here
594     // again and repost WM_QUIT+ScheduleWork() again, etc.). Not leaving a
595     // kMsgHaveWork message behind however is also problematic as unwinding
596     // multiple layers of nested ::GetMessage() loops can result in starving
597     // application tasks. TODO(https://crbug.com/890016) : Fix this.
598 
599     // The return value is mostly irrelevant but return true like we would after
600     // processing a QuitClosure() task.
601     return true;
602   } else if (msg.message == WM_TIMER &&
603              msg.wParam == reinterpret_cast<UINT_PTR>(this)) {
604     // This happens when a native nested loop invokes HandleWorkMessage() =>
605     // ProcessPumpReplacementMessage() which finds the WM_TIMER message
606     // installed by ScheduleNativeTimer(). That message needs to be handled
607     // directly as handing it off to ProcessMessageHelper() below would cause an
608     // unnecessary ScopedDoWorkItem which may incorrectly lead the Delegate's
609     // heuristics to conclude that the DoWork() in HandleTimerMessage() is
610     // nested inside a native work item. It's also safe to skip the below
611     // ScheduleWork() as it is not mandatory before invoking DoWork() and
612     // HandleTimerMessage() handles re-installing the necessary followup
613     // messages.
614     HandleTimerMessage();
615     return true;
616   }
617 
618   // Guarantee we'll get another time slice in the case where we go into native
619   // windows code. This ScheduleWork() may hurt performance a tiny bit when
620   // tasks appear very infrequently, but when the event queue is busy, the
621   // kMsgHaveWork events get (percentage wise) rarer and rarer.
622   ScheduleWork();
623   return ProcessMessageHelper(msg);
624 }
625 
626 //-----------------------------------------------------------------------------
627 // MessagePumpForIO public:
628 
IOContext()629 MessagePumpForIO::IOContext::IOContext() {
630   memset(&overlapped, 0, sizeof(overlapped));
631 }
632 
IOHandler(const Location & from_here)633 MessagePumpForIO::IOHandler::IOHandler(const Location& from_here)
634     : io_handler_location_(from_here) {}
635 
636 MessagePumpForIO::IOHandler::~IOHandler() = default;
637 
MessagePumpForIO()638 MessagePumpForIO::MessagePumpForIO() {
639   port_.Set(::CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr,
640                                      reinterpret_cast<ULONG_PTR>(nullptr), 1));
641   DCHECK(port_.is_valid());
642 }
643 
644 MessagePumpForIO::~MessagePumpForIO() = default;
645 
ScheduleWork()646 void MessagePumpForIO::ScheduleWork() {
647   // This is the only MessagePumpForIO method which can be called outside of
648   // |bound_thread_|.
649 
650   bool not_scheduled = false;
651   if (!work_scheduled_.compare_exchange_strong(not_scheduled, true))
652     return;  // Someone else continued the pumping.
653 
654   // Make sure the MessagePump does some work for us.
655   const BOOL ret = ::PostQueuedCompletionStatus(
656       port_.get(), 0, reinterpret_cast<ULONG_PTR>(this),
657       reinterpret_cast<OVERLAPPED*>(this));
658   if (ret)
659     return;  // Post worked perfectly.
660 
661   // See comment in MessagePumpForUI::ScheduleWork() for this error recovery.
662 
663   work_scheduled_ = false;  // Clarify that we didn't succeed.
664   TRACE_EVENT_INSTANT0("base",
665                        "Chrome.MessageLoopProblem.COMPLETION_POST_ERROR",
666                        TRACE_EVENT_SCOPE_THREAD);
667 }
668 
ScheduleDelayedWork(const Delegate::NextWorkInfo & next_work_info)669 void MessagePumpForIO::ScheduleDelayedWork(
670     const Delegate::NextWorkInfo& next_work_info) {
671   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
672 
673   // Since this is always called from |bound_thread_|, there is nothing to do as
674   // the loop is already running. It will WaitForWork() in
675   // DoRunLoop() with the correct timeout when it's out of immediate tasks.
676 }
677 
RegisterIOHandler(HANDLE file_handle,IOHandler * handler)678 HRESULT MessagePumpForIO::RegisterIOHandler(HANDLE file_handle,
679                                             IOHandler* handler) {
680   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
681 
682   HANDLE port = ::CreateIoCompletionPort(
683       file_handle, port_.get(), reinterpret_cast<ULONG_PTR>(handler), 1);
684   return (port != nullptr) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
685 }
686 
RegisterJobObject(HANDLE job_handle,IOHandler * handler)687 bool MessagePumpForIO::RegisterJobObject(HANDLE job_handle,
688                                          IOHandler* handler) {
689   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
690 
691   JOBOBJECT_ASSOCIATE_COMPLETION_PORT info;
692   info.CompletionKey = handler;
693   info.CompletionPort = port_.get();
694   return ::SetInformationJobObject(job_handle,
695                                    JobObjectAssociateCompletionPortInformation,
696                                    &info, sizeof(info)) != FALSE;
697 }
698 
699 //-----------------------------------------------------------------------------
700 // MessagePumpForIO private:
701 
DoRunLoop()702 void MessagePumpForIO::DoRunLoop() {
703   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
704 
705   for (;;) {
706     // If we do any work, we may create more messages etc., and more work may
707     // possibly be waiting in another task group.  When we (for example)
708     // WaitForIOCompletion(), there is a good chance there are still more
709     // messages waiting.  On the other hand, when any of these methods return
710     // having done no work, then it is pretty unlikely that calling them
711     // again quickly will find any work to do.  Finally, if they all say they
712     // had no work, then it is a good time to consider sleeping (waiting) for
713     // more work.
714 
715     Delegate::NextWorkInfo next_work_info = run_state_->delegate->DoWork();
716     bool more_work_is_plausible = next_work_info.is_immediate();
717     if (run_state_->should_quit)
718       break;
719 
720     more_work_is_plausible |= WaitForIOCompletion(0);
721     if (run_state_->should_quit)
722       break;
723 
724     if (more_work_is_plausible)
725       continue;
726 
727     more_work_is_plausible = run_state_->delegate->DoIdleWork();
728     if (run_state_->should_quit)
729       break;
730 
731     if (more_work_is_plausible)
732       continue;
733 
734     run_state_->delegate->BeforeWait();
735     WaitForWork(next_work_info);
736   }
737 }
738 
739 // Wait until IO completes, up to the time needed by the timer manager to fire
740 // the next set of timers.
WaitForWork(Delegate::NextWorkInfo next_work_info)741 void MessagePumpForIO::WaitForWork(Delegate::NextWorkInfo next_work_info) {
742   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
743 
744   // We do not support nested IO message loops. This is to avoid messy
745   // recursion problems.
746   DCHECK(!run_state_->is_nested) << "Cannot nest an IO message loop!";
747 
748   DWORD timeout = GetSleepTimeoutMs(next_work_info.delayed_run_time,
749                                     next_work_info.recent_now);
750 
751   // Tell the optimizer to retain these values to simplify analyzing hangs.
752   base::debug::Alias(&timeout);
753   WaitForIOCompletion(timeout);
754 }
755 
WaitForIOCompletion(DWORD timeout)756 bool MessagePumpForIO::WaitForIOCompletion(DWORD timeout) {
757   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
758 
759   IOItem item;
760   if (!GetIOItem(timeout, &item))
761     return false;
762 
763   if (ProcessInternalIOItem(item))
764     return true;
765 
766   run_state_->delegate->BeginNativeWorkBeforeDoWork();
767   auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
768 
769   TRACE_EVENT(
770       "base,toplevel", "IOHandler::OnIOCompleted",
771       [&](perfetto::EventContext ctx) {
772         ctx.event()->set_chrome_message_pump()->set_io_handler_location_iid(
773             base::trace_event::InternedSourceLocation::Get(
774                 &ctx, base::trace_event::TraceSourceLocation(
775                           item.handler->io_handler_location())));
776       });
777 
778   item.handler.ExtractAsDangling()->OnIOCompleted(
779       item.context.ExtractAsDangling(), item.bytes_transfered, item.error);
780 
781   return true;
782 }
783 
784 // Asks the OS for another IO completion result.
GetIOItem(DWORD timeout,IOItem * item)785 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) {
786   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
787 
788   memset(item, 0, sizeof(*item));
789   ULONG_PTR key = reinterpret_cast<ULONG_PTR>(nullptr);
790   OVERLAPPED* overlapped = nullptr;
791   if (!::GetQueuedCompletionStatus(port_.get(), &item->bytes_transfered, &key,
792                                    &overlapped, timeout)) {
793     if (!overlapped)
794       return false;  // Nothing in the queue.
795     item->error = GetLastError();
796     item->bytes_transfered = 0;
797   }
798 
799   item->handler = reinterpret_cast<IOHandler*>(key);
800   item->context = reinterpret_cast<IOContext*>(overlapped);
801   return true;
802 }
803 
ProcessInternalIOItem(const IOItem & item)804 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) {
805   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
806 
807   if (reinterpret_cast<void*>(this) ==
808           reinterpret_cast<void*>(item.context.get()) &&
809       reinterpret_cast<void*>(this) ==
810           reinterpret_cast<void*>(item.handler.get())) {
811     // This is our internal completion.
812     DCHECK(!item.bytes_transfered);
813     work_scheduled_ = false;
814     return true;
815   }
816   return false;
817 }
818 
819 }  // namespace base
820