1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #include "base/message_pump_win.h"
11 #include "base/message_loop.h"
12 #include "base/histogram.h"
13 #include "base/win_util.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/ProfilerLabels.h"
16 #include "mozilla/ProfilerThreadSleep.h"
23 static const wchar_t kWndClass
[] = L
"Chrome_MessagePumpWindow";
25 // Message sent to get an additional time slice for pumping (processing) another
26 // task (a series of such messages creates a continuous task pump).
27 static const int kMsgHaveWork
= WM_USER
+ 1;
29 //-----------------------------------------------------------------------------
30 // MessagePumpWin public:
32 void MessagePumpWin::AddObserver(Observer
* observer
) {
33 observers_
.AddObserver(observer
);
36 void MessagePumpWin::RemoveObserver(Observer
* observer
) {
37 observers_
.RemoveObserver(observer
);
40 void MessagePumpWin::WillProcessMessage(const MSG
& msg
) {
41 FOR_EACH_OBSERVER(Observer
, observers_
, WillProcessMessage(msg
));
44 void MessagePumpWin::DidProcessMessage(const MSG
& msg
) {
45 FOR_EACH_OBSERVER(Observer
, observers_
, DidProcessMessage(msg
));
48 void MessagePumpWin::RunWithDispatcher(Delegate
* delegate
,
49 Dispatcher
* dispatcher
) {
51 s
.delegate
= delegate
;
52 s
.dispatcher
= dispatcher
;
53 s
.should_quit
= false;
54 s
.run_depth
= state_
? state_
->run_depth
+ 1 : 1;
56 RunState
* previous_state
= state_
;
61 state_
= previous_state
;
64 void MessagePumpWin::Quit() {
66 state_
->should_quit
= true;
69 //-----------------------------------------------------------------------------
70 // MessagePumpWin protected:
72 int MessagePumpWin::GetCurrentDelay() const {
73 if (delayed_work_time_
.is_null()) return -1;
75 // Be careful here. TimeDelta has a precision of microseconds, but we want a
76 // value in milliseconds. If there are 5.5ms left, should the delay be 5 or
77 // 6? It should be 6 to avoid executing delayed work too early.
79 ceil((delayed_work_time_
- TimeTicks::Now()).InMillisecondsF());
81 // If this value is negative, then we need to run delayed work soon.
82 int delay
= static_cast<int>(timeout
);
83 if (delay
< 0) delay
= 0;
88 //-----------------------------------------------------------------------------
89 // MessagePumpForUI public:
91 MessagePumpForUI::MessagePumpForUI() { InitMessageWnd(); }
93 MessagePumpForUI::~MessagePumpForUI() {
94 DestroyWindow(message_hwnd_
);
95 UnregisterClass(kWndClass
, GetModuleHandle(NULL
));
98 void MessagePumpForUI::ScheduleWork() {
99 if (InterlockedExchange(&have_work_
, 1))
100 return; // Someone else continued the pumping.
102 // Make sure the MessagePump does some work for us.
103 PostMessage(message_hwnd_
, kMsgHaveWork
, reinterpret_cast<WPARAM
>(this), 0);
105 // In order to wake up any cross-process COM calls which may currently be
106 // pending on the main thread, we also have to post a UI message.
107 PostMessage(message_hwnd_
, WM_NULL
, 0, 0);
110 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks
& delayed_work_time
) {
112 // We would *like* to provide high resolution timers. Windows timers using
113 // SetTimer() have a 10ms granularity. We have to use WM_TIMER as a wakeup
114 // mechanism because the application can enter modal windows loops where it
115 // is not running our MessageLoop; the only way to have our timers fire in
116 // these cases is to post messages there.
118 // To provide sub-10ms timers, we process timers directly from our run loop.
119 // For the common case, timers will be processed there as the run loop does
120 // its normal work. However, we *also* set the system timer so that WM_TIMER
121 // events fire. This mops up the case of timers not being able to work in
122 // modal message loops. It is possible for the SetTimer to pop and have no
123 // pending timers, because they could have already been processed by the
126 // We use a single SetTimer corresponding to the timer that will expire
127 // soonest. As new timers are created and destroyed, we update SetTimer.
128 // Getting a spurrious SetTimer event firing is benign, as we'll just be
129 // processing an empty timer queue.
131 delayed_work_time_
= delayed_work_time
;
133 int delay_msec
= GetCurrentDelay();
134 DCHECK(delay_msec
>= 0);
135 if (delay_msec
< USER_TIMER_MINIMUM
) delay_msec
= USER_TIMER_MINIMUM
;
137 // Create a WM_TIMER event that will wake us up to check for any pending
138 // timers (in case we are running within a nested, external sub-pump).
139 SetTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this), delay_msec
, NULL
);
142 void MessagePumpForUI::PumpOutPendingPaintMessages() {
143 // If we are being called outside of the context of Run, then don't try to do
147 // Create a mini-message-pump to force immediate processing of only Windows
148 // WM_PAINT messages. Don't provide an infinite loop, but do enough peeking
149 // to get the job done. Actual common max is 4 peeks, but we'll be a little
151 const int kMaxPeekCount
= 20;
153 for (peek_count
= 0; peek_count
< kMaxPeekCount
; ++peek_count
) {
155 if (!PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
| PM_QS_PAINT
)) break;
156 ProcessMessageHelper(msg
);
157 if (state_
->should_quit
) break;
161 //-----------------------------------------------------------------------------
162 // MessagePumpForUI private:
165 LRESULT CALLBACK
MessagePumpForUI::WndProcThunk(HWND hwnd
, UINT message
,
166 WPARAM wparam
, LPARAM lparam
) {
169 reinterpret_cast<MessagePumpForUI
*>(wparam
)->HandleWorkMessage();
172 reinterpret_cast<MessagePumpForUI
*>(wparam
)->HandleTimerMessage();
175 return DefWindowProc(hwnd
, message
, wparam
, lparam
);
178 void MessagePumpForUI::DoRunLoop() {
179 // IF this was just a simple PeekMessage() loop (servicing all possible work
180 // queues), then Windows would try to achieve the following order according
181 // to MSDN documentation about PeekMessage with no filter):
184 // * Sent messages (again)
185 // * WM_PAINT messages
186 // * WM_TIMER messages
188 // Summary: none of the above classes is starved, and sent messages has twice
189 // the chance of being processed (i.e., reduced service time).
192 // If we do any work, we may create more messages etc., and more work may
193 // possibly be waiting in another task group. When we (for example)
194 // ProcessNextWindowsMessage(), there is a good chance there are still more
195 // messages waiting. On the other hand, when any of these methods return
196 // having done no work, then it is pretty unlikely that calling them again
197 // quickly will find any work to do. Finally, if they all say they had no
198 // work, then it is a good time to consider sleeping (waiting) for more
201 bool more_work_is_plausible
= ProcessNextWindowsMessage();
202 if (state_
->should_quit
) break;
204 more_work_is_plausible
|= state_
->delegate
->DoWork();
205 if (state_
->should_quit
) break;
207 more_work_is_plausible
|=
208 state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
209 // If we did not process any delayed work, then we can assume that our
210 // existing WM_TIMER if any will fire when delayed work should run. We
211 // don't want to disturb that timer if it is already in flight. However,
212 // if we did do all remaining delayed work, then lets kill the WM_TIMER.
213 if (more_work_is_plausible
&& delayed_work_time_
.is_null())
214 KillTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this));
215 if (state_
->should_quit
) break;
217 if (more_work_is_plausible
) continue;
219 more_work_is_plausible
= state_
->delegate
->DoIdleWork();
220 if (state_
->should_quit
) break;
222 if (more_work_is_plausible
) continue;
224 WaitForWork(); // Wait (sleep) until we have work to do again.
228 void MessagePumpForUI::InitMessageWnd() {
229 HINSTANCE hinst
= GetModuleHandle(NULL
);
232 wc
.cbSize
= sizeof(wc
);
233 wc
.lpfnWndProc
= WndProcThunk
;
234 wc
.hInstance
= hinst
;
235 wc
.lpszClassName
= kWndClass
;
236 RegisterClassEx(&wc
);
239 CreateWindow(kWndClass
, 0, 0, 0, 0, 0, 0, HWND_MESSAGE
, 0, hinst
, 0);
240 DCHECK(message_hwnd_
);
243 void MessagePumpForUI::WaitForWork() {
244 AUTO_PROFILER_LABEL("MessagePumpForUI::WaitForWork", IDLE
);
246 // Wait until a message is available, up to the time needed by the timer
247 // manager to fire the next set of timers.
248 int delay
= GetCurrentDelay();
249 if (delay
< 0) // Negative value means no timers waiting.
252 mozilla::widget::WinUtils::WaitForMessage(delay
);
255 void MessagePumpForUI::HandleWorkMessage() {
256 // If we are being called outside of the context of Run, then don't try to do
257 // any work. This could correspond to a MessageBox call or something of that
260 // Since we handled a kMsgHaveWork message, we must still update this flag.
261 InterlockedExchange(&have_work_
, 0);
265 // Let whatever would have run had we not been putting messages in the queue
266 // run now. This is an attempt to make our dummy message not starve other
267 // messages that may be in the Windows message queue.
268 ProcessPumpReplacementMessage();
270 // Now give the delegate a chance to do some work. He'll let us know if he
271 // needs to do more work.
272 if (state_
->delegate
->DoWork()) ScheduleWork();
275 void MessagePumpForUI::HandleTimerMessage() {
276 KillTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this));
278 // If we are being called outside of the context of Run, then don't do
279 // anything. This could correspond to a MessageBox call or something of
283 state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
284 if (!delayed_work_time_
.is_null()) {
285 // A bit gratuitous to set delayed_work_time_ again, but oh well.
286 ScheduleDelayedWork(delayed_work_time_
);
290 bool MessagePumpForUI::ProcessNextWindowsMessage() {
291 // If there are sent messages in the queue then PeekMessage internally
292 // dispatches the message and returns false. We return true in this
293 // case to ensure that the message loop peeks again instead of calling
294 // MsgWaitForMultipleObjectsEx again.
295 bool sent_messages_in_queue
= false;
296 DWORD queue_status
= ::GetQueueStatus(QS_SENDMESSAGE
);
297 if (HIWORD(queue_status
) & QS_SENDMESSAGE
) sent_messages_in_queue
= true;
300 if (::PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
301 return ProcessMessageHelper(msg
);
303 return sent_messages_in_queue
;
306 bool MessagePumpForUI::ProcessMessageHelper(const MSG
& msg
) {
307 if (WM_QUIT
== msg
.message
) {
308 // WM_QUIT is the standard way to exit a ::GetMessage() loop. Our
309 // MessageLoop has its own quit mechanism, so WM_QUIT is unexpected and
310 // should be ignored.
314 // While running our main message pump, we discard kMsgHaveWork messages.
315 if (msg
.message
== kMsgHaveWork
&& msg
.hwnd
== message_hwnd_
)
316 return ProcessPumpReplacementMessage();
318 WillProcessMessage(msg
);
320 if (state_
->dispatcher
) {
321 if (!state_
->dispatcher
->Dispatch(msg
)) state_
->should_quit
= true;
323 ::TranslateMessage(&msg
);
324 ::DispatchMessage(&msg
);
327 DidProcessMessage(msg
);
331 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
332 // When we encounter a kMsgHaveWork message, this method is called to peek
333 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The
334 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though
335 // a continuous stream of such messages are posted. This method carefully
336 // peeks a message while there is no chance for a kMsgHaveWork to be pending,
337 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to
338 // possibly be posted), and finally dispatches that peeked replacement. Note
339 // that the re-post of kMsgHaveWork may be asynchronous to this thread!!
342 bool have_message
= false;
343 if (MessageLoop::current()->os_modal_loop()) {
344 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above.
345 have_message
= ::PeekMessage(&msg
, NULL
, WM_PAINT
, WM_PAINT
, PM_REMOVE
) ||
346 ::PeekMessage(&msg
, NULL
, WM_TIMER
, WM_TIMER
, PM_REMOVE
);
348 have_message
= (0 != ::PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
));
350 if (have_message
&& msg
.message
== WM_NULL
)
351 have_message
= (0 != ::PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
));
354 DCHECK(!have_message
|| kMsgHaveWork
!= msg
.message
||
355 msg
.hwnd
!= message_hwnd_
);
357 // Since we discarded a kMsgHaveWork message, we must update the flag.
358 int old_have_work
= InterlockedExchange(&have_work_
, 0);
359 DCHECK(old_have_work
);
361 // We don't need a special time slice if we didn't have_message to process.
362 if (!have_message
) return false;
364 if (WM_QUIT
== msg
.message
) {
365 // If we're in a nested ::GetMessage() loop then we must let that loop see
366 // the WM_QUIT in order for it to exit. If we're in DoRunLoop then the re-
367 // posted WM_QUIT will be either ignored, or handled, by
368 // ProcessMessageHelper() called directly from ProcessNextWindowsMessage().
369 ::PostQuitMessage(static_cast<int>(msg
.wParam
));
370 // Note: we *must not* ScheduleWork() here as WM_QUIT is a low-priority
371 // message on Windows (it is only returned by ::PeekMessage() when idle) :
372 // https://blogs.msdn.microsoft.com/oldnewthing/20051104-33/?p=33453. As
373 // such posting a kMsgHaveWork message via ScheduleWork() would cause an
374 // infinite loop (kMsgHaveWork message handled first means we end up here
375 // again and repost WM_QUIT+ScheduleWork() again, etc.). Not leaving a
376 // kMsgHaveWork message behind however is also problematic as unwinding
377 // multiple layers of nested ::GetMessage() loops can result in starving
378 // application tasks. TODO(https://crbug.com/890016) : Fix this.
380 // The return value is mostly irrelevant but return true like we would after
381 // processing a QuitClosure() task.
385 // Guarantee we'll get another time slice in the case where we go into native
386 // windows code. This ScheduleWork() may hurt performance a tiny bit when
387 // tasks appear very infrequently, but when the event queue is busy, the
388 // kMsgHaveWork events get (percentage wise) rarer and rarer.
390 return ProcessMessageHelper(msg
);
393 //-----------------------------------------------------------------------------
394 // MessagePumpForIO public:
396 MessagePumpForIO::MessagePumpForIO() {
397 port_
.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE
, NULL
, 0, 1));
398 DCHECK(port_
.IsValid());
401 void MessagePumpForIO::ScheduleWork() {
402 if (InterlockedExchange(&have_work_
, 1))
403 return; // Someone else continued the pumping.
405 // Make sure the MessagePump does some work for us.
407 PostQueuedCompletionStatus(port_
, 0, reinterpret_cast<ULONG_PTR
>(this),
408 reinterpret_cast<OVERLAPPED
*>(this));
412 void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks
& delayed_work_time
) {
413 // We know that we can't be blocked right now since this method can only be
414 // called on the same thread as Run, so we only need to update our record of
415 // how long to sleep when we do sleep.
416 delayed_work_time_
= delayed_work_time
;
419 void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle
,
420 IOHandler
* handler
) {
421 ULONG_PTR key
= reinterpret_cast<ULONG_PTR
>(handler
);
422 HANDLE port
= CreateIoCompletionPort(file_handle
, port_
, key
, 1);
423 DCHECK(port
== port_
.Get());
426 //-----------------------------------------------------------------------------
427 // MessagePumpForIO private:
429 void MessagePumpForIO::DoRunLoop() {
431 // If we do any work, we may create more messages etc., and more work may
432 // possibly be waiting in another task group. When we (for example)
433 // WaitForIOCompletion(), there is a good chance there are still more
434 // messages waiting. On the other hand, when any of these methods return
435 // having done no work, then it is pretty unlikely that calling them
436 // again quickly will find any work to do. Finally, if they all say they
437 // had no work, then it is a good time to consider sleeping (waiting) for
440 bool more_work_is_plausible
= state_
->delegate
->DoWork();
441 if (state_
->should_quit
) break;
443 more_work_is_plausible
|= WaitForIOCompletion(0, NULL
);
444 if (state_
->should_quit
) break;
446 more_work_is_plausible
|=
447 state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
448 if (state_
->should_quit
) break;
450 if (more_work_is_plausible
) continue;
452 more_work_is_plausible
= state_
->delegate
->DoIdleWork();
453 if (state_
->should_quit
) break;
455 if (more_work_is_plausible
) continue;
457 WaitForWork(); // Wait (sleep) until we have work to do again.
461 // Wait until IO completes, up to the time needed by the timer manager to fire
462 // the next set of timers.
463 void MessagePumpForIO::WaitForWork() {
464 // We do not support nested IO message loops. This is to avoid messy
465 // recursion problems.
466 DCHECK(state_
->run_depth
== 1) << "Cannot nest an IO message loop!";
468 int timeout
= GetCurrentDelay();
469 if (timeout
< 0) // Negative value means no timers waiting.
472 WaitForIOCompletion(timeout
, NULL
);
475 bool MessagePumpForIO::WaitForIOCompletion(DWORD timeout
, IOHandler
* filter
) {
477 if (completed_io_
.empty() || !MatchCompletedIOItem(filter
, &item
)) {
478 // We have to ask the system for another IO completion.
479 if (!GetIOItem(timeout
, &item
)) return false;
481 if (ProcessInternalIOItem(item
)) return true;
484 if (item
.context
->handler
) {
485 if (filter
&& item
.handler
!= filter
) {
486 // Save this item for later
487 completed_io_
.push_back(item
);
489 DCHECK(item
.context
->handler
== item
.handler
);
490 item
.handler
->OnIOCompleted(item
.context
, item
.bytes_transfered
,
494 // The handler must be gone by now, just cleanup the mess.
500 // Asks the OS for another IO completion result.
501 bool MessagePumpForIO::GetIOItem(DWORD timeout
, IOItem
* item
) {
502 memset(item
, 0, sizeof(*item
));
504 OVERLAPPED
* overlapped
= NULL
;
507 AUTO_PROFILER_LABEL("MessagePumpForIO::GetIOItem::Wait", IDLE
);
508 #ifdef MOZ_GECKO_PROFILER
509 mozilla::Maybe
<mozilla::AutoProfilerThreadSleep
> profilerThreadSleep
;
511 profilerThreadSleep
.emplace();
514 success
= GetQueuedCompletionStatus(port_
.Get(), &item
->bytes_transfered
,
515 &key
, &overlapped
, timeout
);
518 if (!overlapped
) return false; // Nothing in the queue.
519 item
->error
= GetLastError();
520 item
->bytes_transfered
= 0;
523 item
->handler
= reinterpret_cast<IOHandler
*>(key
);
524 item
->context
= reinterpret_cast<IOContext
*>(overlapped
);
528 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem
& item
) {
529 if (this == reinterpret_cast<MessagePumpForIO
*>(item
.context
) &&
530 this == reinterpret_cast<MessagePumpForIO
*>(item
.handler
)) {
531 // This is our internal completion.
532 DCHECK(!item
.bytes_transfered
);
533 InterlockedExchange(&have_work_
, 0);
539 // Returns a completion item that was previously received.
540 bool MessagePumpForIO::MatchCompletedIOItem(IOHandler
* filter
, IOItem
* item
) {
541 DCHECK(!completed_io_
.empty());
542 for (std::list
<IOItem
>::iterator it
= completed_io_
.begin();
543 it
!= completed_io_
.end(); ++it
) {
544 if (!filter
|| it
->handler
== filter
) {
546 completed_io_
.erase(it
);