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/cxx17_backports.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<DWORD>::value, "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 DCHECK(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 =
419 clamp(delay_msec, UINT(USER_TIMER_MINIMUM), 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 auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
526
527 TRACE_EVENT("base,toplevel", "MessagePumpForUI DispatchMessage",
528 [&](perfetto::EventContext ctx) {
529 ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>()
530 ->set_chrome_message_pump_for_ui()
531 ->set_message_id(msg.message);
532 });
533
534 for (Observer& observer : observers_)
535 observer.WillDispatchMSG(msg);
536 ::TranslateMessage(&msg);
537 ::DispatchMessage(&msg);
538 for (Observer& observer : observers_)
539 observer.DidDispatchMSG(msg);
540
541 return true;
542 }
543
ProcessPumpReplacementMessage()544 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
545 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
546
547 // When we encounter a kMsgHaveWork message, this method is called to peek and
548 // process a replacement message. The goal is to make the kMsgHaveWork as non-
549 // intrusive as possible, even though a continuous stream of such messages are
550 // posted. This method carefully peeks a message while there is no chance for
551 // a kMsgHaveWork to be pending, then resets the |have_work_| flag (allowing a
552 // replacement kMsgHaveWork to possibly be posted), and finally dispatches
553 // that peeked replacement. Note that the re-post of kMsgHaveWork may be
554 // asynchronous to this thread!!
555
556 MSG msg;
557 bool have_message = false;
558 {
559 // Note: Ideally this call wouldn't process sent-messages (as we already did
560 // that in the PeekMessage call that lead to receiving this kMsgHaveWork),
561 // but there's no way to specify this (omitting PM_QS_SENDMESSAGE as in
562 // crrev.com/791043 doesn't do anything). Hence this call must be considered
563 // as a potential work item.
564 auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
565 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("base"),
566 "MessagePumpForUI::ProcessPumpReplacementMessage PeekMessage");
567 have_message = ::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) != FALSE;
568 }
569
570 // Expect no message or a message different than kMsgHaveWork.
571 DCHECK(!have_message || kMsgHaveWork != msg.message ||
572 msg.hwnd != message_window_.hwnd());
573
574 // Since we discarded a kMsgHaveWork message, we must update the flag.
575 DCHECK(work_scheduled_);
576 work_scheduled_ = false;
577
578 // We don't need a special time slice if we didn't |have_message| to process.
579 if (!have_message)
580 return false;
581
582 if (msg.message == WM_QUIT) {
583 // If we're in a nested ::GetMessage() loop then we must let that loop see
584 // the WM_QUIT in order for it to exit. If we're in DoRunLoop then the re-
585 // posted WM_QUIT will be either ignored, or handled, by
586 // ProcessMessageHelper() called directly from ProcessNextWindowsMessage().
587 ::PostQuitMessage(static_cast<int>(msg.wParam));
588 // Note: we *must not* ScheduleWork() here as WM_QUIT is a low-priority
589 // message on Windows (it is only returned by ::PeekMessage() when idle) :
590 // https://blogs.msdn.microsoft.com/oldnewthing/20051104-33/?p=33453. As
591 // such posting a kMsgHaveWork message via ScheduleWork() would cause an
592 // infinite loop (kMsgHaveWork message handled first means we end up here
593 // again and repost WM_QUIT+ScheduleWork() again, etc.). Not leaving a
594 // kMsgHaveWork message behind however is also problematic as unwinding
595 // multiple layers of nested ::GetMessage() loops can result in starving
596 // application tasks. TODO(https://crbug.com/890016) : Fix this.
597
598 // The return value is mostly irrelevant but return true like we would after
599 // processing a QuitClosure() task.
600 return true;
601 } else if (msg.message == WM_TIMER &&
602 msg.wParam == reinterpret_cast<UINT_PTR>(this)) {
603 // This happens when a native nested loop invokes HandleWorkMessage() =>
604 // ProcessPumpReplacementMessage() which finds the WM_TIMER message
605 // installed by ScheduleNativeTimer(). That message needs to be handled
606 // directly as handing it off to ProcessMessageHelper() below would cause an
607 // unnecessary ScopedDoWorkItem which may incorrectly lead the Delegate's
608 // heuristics to conclude that the DoWork() in HandleTimerMessage() is
609 // nested inside a native work item. It's also safe to skip the below
610 // ScheduleWork() as it is not mandatory before invoking DoWork() and
611 // HandleTimerMessage() handles re-installing the necessary followup
612 // messages.
613 HandleTimerMessage();
614 return true;
615 }
616
617 // Guarantee we'll get another time slice in the case where we go into native
618 // windows code. This ScheduleWork() may hurt performance a tiny bit when
619 // tasks appear very infrequently, but when the event queue is busy, the
620 // kMsgHaveWork events get (percentage wise) rarer and rarer.
621 ScheduleWork();
622 return ProcessMessageHelper(msg);
623 }
624
625 //-----------------------------------------------------------------------------
626 // MessagePumpForIO public:
627
IOContext()628 MessagePumpForIO::IOContext::IOContext() {
629 memset(&overlapped, 0, sizeof(overlapped));
630 }
631
IOHandler(const Location & from_here)632 MessagePumpForIO::IOHandler::IOHandler(const Location& from_here)
633 : io_handler_location_(from_here) {}
634
635 MessagePumpForIO::IOHandler::~IOHandler() = default;
636
MessagePumpForIO()637 MessagePumpForIO::MessagePumpForIO() {
638 port_.Set(::CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr,
639 reinterpret_cast<ULONG_PTR>(nullptr), 1));
640 DCHECK(port_.is_valid());
641 }
642
643 MessagePumpForIO::~MessagePumpForIO() = default;
644
ScheduleWork()645 void MessagePumpForIO::ScheduleWork() {
646 // This is the only MessagePumpForIO method which can be called outside of
647 // |bound_thread_|.
648
649 bool not_scheduled = false;
650 if (!work_scheduled_.compare_exchange_strong(not_scheduled, true))
651 return; // Someone else continued the pumping.
652
653 // Make sure the MessagePump does some work for us.
654 const BOOL ret = ::PostQueuedCompletionStatus(
655 port_.get(), 0, reinterpret_cast<ULONG_PTR>(this),
656 reinterpret_cast<OVERLAPPED*>(this));
657 if (ret)
658 return; // Post worked perfectly.
659
660 // See comment in MessagePumpForUI::ScheduleWork() for this error recovery.
661
662 work_scheduled_ = false; // Clarify that we didn't succeed.
663 TRACE_EVENT_INSTANT0("base",
664 "Chrome.MessageLoopProblem.COMPLETION_POST_ERROR",
665 TRACE_EVENT_SCOPE_THREAD);
666 }
667
ScheduleDelayedWork(const Delegate::NextWorkInfo & next_work_info)668 void MessagePumpForIO::ScheduleDelayedWork(
669 const Delegate::NextWorkInfo& next_work_info) {
670 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
671
672 // Since this is always called from |bound_thread_|, there is nothing to do as
673 // the loop is already running. It will WaitForWork() in
674 // DoRunLoop() with the correct timeout when it's out of immediate tasks.
675 }
676
RegisterIOHandler(HANDLE file_handle,IOHandler * handler)677 HRESULT MessagePumpForIO::RegisterIOHandler(HANDLE file_handle,
678 IOHandler* handler) {
679 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
680
681 HANDLE port = ::CreateIoCompletionPort(
682 file_handle, port_.get(), reinterpret_cast<ULONG_PTR>(handler), 1);
683 return (port != nullptr) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
684 }
685
RegisterJobObject(HANDLE job_handle,IOHandler * handler)686 bool MessagePumpForIO::RegisterJobObject(HANDLE job_handle,
687 IOHandler* handler) {
688 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
689
690 JOBOBJECT_ASSOCIATE_COMPLETION_PORT info;
691 info.CompletionKey = handler;
692 info.CompletionPort = port_.get();
693 return ::SetInformationJobObject(job_handle,
694 JobObjectAssociateCompletionPortInformation,
695 &info, sizeof(info)) != FALSE;
696 }
697
698 //-----------------------------------------------------------------------------
699 // MessagePumpForIO private:
700
DoRunLoop()701 void MessagePumpForIO::DoRunLoop() {
702 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
703
704 for (;;) {
705 // If we do any work, we may create more messages etc., and more work may
706 // possibly be waiting in another task group. When we (for example)
707 // WaitForIOCompletion(), there is a good chance there are still more
708 // messages waiting. On the other hand, when any of these methods return
709 // having done no work, then it is pretty unlikely that calling them
710 // again quickly will find any work to do. Finally, if they all say they
711 // had no work, then it is a good time to consider sleeping (waiting) for
712 // more work.
713
714 Delegate::NextWorkInfo next_work_info = run_state_->delegate->DoWork();
715 bool more_work_is_plausible = next_work_info.is_immediate();
716 if (run_state_->should_quit)
717 break;
718
719 run_state_->delegate->BeforeWait();
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 auto scoped_do_work_item = run_state_->delegate->BeginWorkItem();
767
768 TRACE_EVENT(
769 "base,toplevel", "IOHandler::OnIOCompleted",
770 [&](perfetto::EventContext ctx) {
771 ctx.event()->set_chrome_message_pump()->set_io_handler_location_iid(
772 base::trace_event::InternedSourceLocation::Get(
773 &ctx, base::trace_event::TraceSourceLocation(
774 item.handler->io_handler_location())));
775 });
776
777 item.handler.ExtractAsDangling()->OnIOCompleted(
778 item.context.ExtractAsDangling(), item.bytes_transfered, item.error);
779
780 return true;
781 }
782
783 // Asks the OS for another IO completion result.
GetIOItem(DWORD timeout,IOItem * item)784 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) {
785 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
786
787 memset(item, 0, sizeof(*item));
788 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(nullptr);
789 OVERLAPPED* overlapped = nullptr;
790 if (!::GetQueuedCompletionStatus(port_.get(), &item->bytes_transfered, &key,
791 &overlapped, timeout)) {
792 if (!overlapped)
793 return false; // Nothing in the queue.
794 item->error = GetLastError();
795 item->bytes_transfered = 0;
796 }
797
798 item->handler = reinterpret_cast<IOHandler*>(key);
799 item->context = reinterpret_cast<IOContext*>(overlapped);
800 return true;
801 }
802
ProcessInternalIOItem(const IOItem & item)803 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) {
804 DCHECK_CALLED_ON_VALID_THREAD(bound_thread_);
805
806 if (reinterpret_cast<void*>(this) ==
807 reinterpret_cast<void*>(item.context.get()) &&
808 reinterpret_cast<void*>(this) ==
809 reinterpret_cast<void*>(item.handler.get())) {
810 // This is our internal completion.
811 DCHECK(!item.bytes_transfered);
812 work_scheduled_ = false;
813 return true;
814 }
815 return false;
816 }
817
818 } // namespace base
819