1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/message_loop/message_pump_win.h"
9 #include "base/debug/trace_event.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "base/process/memory.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/win/wrapped_window_proc.h"
20 enum MessageLoopProblems
{
22 COMPLETION_POST_ERROR
,
24 MESSAGE_LOOP_PROBLEM_MAX
,
29 static const wchar_t kWndClassFormat
[] = L
"Chrome_MessagePumpWindow_%p";
31 // Message sent to get an additional time slice for pumping (processing) another
32 // task (a series of such messages creates a continuous task pump).
33 static const int kMsgHaveWork
= WM_USER
+ 1;
35 //-----------------------------------------------------------------------------
36 // MessagePumpWin public:
38 void MessagePumpWin::AddObserver(MessagePumpObserver
* observer
) {
39 observers_
.AddObserver(observer
);
42 void MessagePumpWin::RemoveObserver(MessagePumpObserver
* observer
) {
43 observers_
.RemoveObserver(observer
);
46 void MessagePumpWin::WillProcessMessage(const MSG
& msg
) {
47 FOR_EACH_OBSERVER(MessagePumpObserver
, observers_
, WillProcessEvent(msg
));
50 void MessagePumpWin::DidProcessMessage(const MSG
& msg
) {
51 FOR_EACH_OBSERVER(MessagePumpObserver
, observers_
, DidProcessEvent(msg
));
54 void MessagePumpWin::RunWithDispatcher(
55 Delegate
* delegate
, MessagePumpDispatcher
* dispatcher
) {
57 s
.delegate
= delegate
;
58 s
.dispatcher
= dispatcher
;
59 s
.should_quit
= false;
60 s
.run_depth
= state_
? state_
->run_depth
+ 1 : 1;
62 RunState
* previous_state
= state_
;
67 state_
= previous_state
;
70 void MessagePumpWin::Quit() {
72 state_
->should_quit
= true;
75 //-----------------------------------------------------------------------------
76 // MessagePumpWin protected:
78 int MessagePumpWin::GetCurrentDelay() const {
79 if (delayed_work_time_
.is_null())
82 // Be careful here. TimeDelta has a precision of microseconds, but we want a
83 // value in milliseconds. If there are 5.5ms left, should the delay be 5 or
84 // 6? It should be 6 to avoid executing delayed work too early.
86 ceil((delayed_work_time_
- TimeTicks::Now()).InMillisecondsF());
88 // If this value is negative, then we need to run delayed work soon.
89 int delay
= static_cast<int>(timeout
);
96 //-----------------------------------------------------------------------------
97 // MessagePumpForUI public:
99 MessagePumpForUI::MessagePumpForUI()
101 message_filter_(new MessageFilter
) {
105 MessagePumpForUI::~MessagePumpForUI() {
106 DestroyWindow(message_hwnd_
);
107 UnregisterClass(MAKEINTATOM(atom_
),
108 GetModuleFromAddress(&WndProcThunk
));
111 void MessagePumpForUI::ScheduleWork() {
112 if (InterlockedExchange(&have_work_
, 1))
113 return; // Someone else continued the pumping.
115 // Make sure the MessagePump does some work for us.
116 BOOL ret
= PostMessage(message_hwnd_
, kMsgHaveWork
,
117 reinterpret_cast<WPARAM
>(this), 0);
119 return; // There was room in the Window Message queue.
121 // We have failed to insert a have-work message, so there is a chance that we
122 // will starve tasks/timers while sitting in a nested message loop. Nested
123 // loops only look at Windows Message queues, and don't look at *our* task
124 // queues, etc., so we might not get a time slice in such. :-(
125 // We could abort here, but the fear is that this failure mode is plausibly
126 // common (queue is full, of about 2000 messages), so we'll do a near-graceful
127 // recovery. Nested loops are pretty transient (we think), so this will
128 // probably be recoverable.
129 InterlockedExchange(&have_work_
, 0); // Clarify that we didn't really insert.
130 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", MESSAGE_POST_ERROR
,
131 MESSAGE_LOOP_PROBLEM_MAX
);
134 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks
& delayed_work_time
) {
136 // We would *like* to provide high resolution timers. Windows timers using
137 // SetTimer() have a 10ms granularity. We have to use WM_TIMER as a wakeup
138 // mechanism because the application can enter modal windows loops where it
139 // is not running our MessageLoop; the only way to have our timers fire in
140 // these cases is to post messages there.
142 // To provide sub-10ms timers, we process timers directly from our run loop.
143 // For the common case, timers will be processed there as the run loop does
144 // its normal work. However, we *also* set the system timer so that WM_TIMER
145 // events fire. This mops up the case of timers not being able to work in
146 // modal message loops. It is possible for the SetTimer to pop and have no
147 // pending timers, because they could have already been processed by the
150 // We use a single SetTimer corresponding to the timer that will expire
151 // soonest. As new timers are created and destroyed, we update SetTimer.
152 // Getting a spurrious SetTimer event firing is benign, as we'll just be
153 // processing an empty timer queue.
155 delayed_work_time_
= delayed_work_time
;
157 int delay_msec
= GetCurrentDelay();
158 DCHECK_GE(delay_msec
, 0);
159 if (delay_msec
< USER_TIMER_MINIMUM
)
160 delay_msec
= USER_TIMER_MINIMUM
;
162 // Create a WM_TIMER event that will wake us up to check for any pending
163 // timers (in case we are running within a nested, external sub-pump).
164 BOOL ret
= SetTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this),
168 // If we can't set timers, we are in big trouble... but cross our fingers for
170 // TODO(jar): If we don't see this error, use a CHECK() here instead.
171 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", SET_TIMER_ERROR
,
172 MESSAGE_LOOP_PROBLEM_MAX
);
175 void MessagePumpForUI::PumpOutPendingPaintMessages() {
176 // If we are being called outside of the context of Run, then don't try to do
181 // Create a mini-message-pump to force immediate processing of only Windows
182 // WM_PAINT messages. Don't provide an infinite loop, but do enough peeking
183 // to get the job done. Actual common max is 4 peeks, but we'll be a little
185 const int kMaxPeekCount
= 20;
187 for (peek_count
= 0; peek_count
< kMaxPeekCount
; ++peek_count
) {
189 if (!PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
| PM_QS_PAINT
))
191 ProcessMessageHelper(msg
);
192 if (state_
->should_quit
) // Handle WM_QUIT.
195 // Histogram what was really being used, to help to adjust kMaxPeekCount.
196 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count
);
199 //-----------------------------------------------------------------------------
200 // MessagePumpForUI private:
203 LRESULT CALLBACK
MessagePumpForUI::WndProcThunk(
204 HWND hwnd
, UINT message
, WPARAM wparam
, LPARAM lparam
) {
207 reinterpret_cast<MessagePumpForUI
*>(wparam
)->HandleWorkMessage();
210 reinterpret_cast<MessagePumpForUI
*>(wparam
)->HandleTimerMessage();
213 return DefWindowProc(hwnd
, message
, wparam
, lparam
);
216 void MessagePumpForUI::DoRunLoop() {
217 // IF this was just a simple PeekMessage() loop (servicing all possible work
218 // queues), then Windows would try to achieve the following order according
219 // to MSDN documentation about PeekMessage with no filter):
222 // * Sent messages (again)
223 // * WM_PAINT messages
224 // * WM_TIMER messages
226 // Summary: none of the above classes is starved, and sent messages has twice
227 // the chance of being processed (i.e., reduced service time).
230 // If we do any work, we may create more messages etc., and more work may
231 // possibly be waiting in another task group. When we (for example)
232 // ProcessNextWindowsMessage(), there is a good chance there are still more
233 // messages waiting. On the other hand, when any of these methods return
234 // having done no work, then it is pretty unlikely that calling them again
235 // quickly will find any work to do. Finally, if they all say they had no
236 // work, then it is a good time to consider sleeping (waiting) for more
239 bool more_work_is_plausible
= ProcessNextWindowsMessage();
240 if (state_
->should_quit
)
243 more_work_is_plausible
|= state_
->delegate
->DoWork();
244 if (state_
->should_quit
)
247 more_work_is_plausible
|=
248 state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
249 // If we did not process any delayed work, then we can assume that our
250 // existing WM_TIMER if any will fire when delayed work should run. We
251 // don't want to disturb that timer if it is already in flight. However,
252 // if we did do all remaining delayed work, then lets kill the WM_TIMER.
253 if (more_work_is_plausible
&& delayed_work_time_
.is_null())
254 KillTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this));
255 if (state_
->should_quit
)
258 if (more_work_is_plausible
)
261 more_work_is_plausible
= state_
->delegate
->DoIdleWork();
262 if (state_
->should_quit
)
265 if (more_work_is_plausible
)
268 WaitForWork(); // Wait (sleep) until we have work to do again.
272 void MessagePumpForUI::InitMessageWnd() {
273 // Generate a unique window class name.
274 string16 class_name
= StringPrintf(kWndClassFormat
, this);
276 HINSTANCE instance
= GetModuleFromAddress(&WndProcThunk
);
278 wc
.cbSize
= sizeof(wc
);
279 wc
.lpfnWndProc
= base::win::WrappedWindowProc
<WndProcThunk
>;
280 wc
.hInstance
= instance
;
281 wc
.lpszClassName
= class_name
.c_str();
282 atom_
= RegisterClassEx(&wc
);
285 message_hwnd_
= CreateWindow(MAKEINTATOM(atom_
), 0, 0, 0, 0, 0, 0,
286 HWND_MESSAGE
, 0, instance
, 0);
287 DCHECK(message_hwnd_
);
290 void MessagePumpForUI::WaitForWork() {
291 // Wait until a message is available, up to the time needed by the timer
292 // manager to fire the next set of timers.
293 int delay
= GetCurrentDelay();
294 if (delay
< 0) // Negative value means no timers waiting.
298 result
= MsgWaitForMultipleObjectsEx(0, NULL
, delay
, QS_ALLINPUT
,
299 MWMO_INPUTAVAILABLE
);
301 if (WAIT_OBJECT_0
== result
) {
302 // A WM_* message is available.
303 // If a parent child relationship exists between windows across threads
304 // then their thread inputs are implicitly attached.
305 // This causes the MsgWaitForMultipleObjectsEx API to return indicating
306 // that messages are ready for processing (Specifically, mouse messages
307 // intended for the child window may appear if the child window has
309 // The subsequent PeekMessages call may fail to return any messages thus
310 // causing us to enter a tight loop at times.
311 // The WaitMessage call below is a workaround to give the child window
312 // some time to process its input messages.
314 DWORD queue_status
= GetQueueStatus(QS_MOUSE
);
315 if (HIWORD(queue_status
) & QS_MOUSE
&&
316 !PeekMessage(&msg
, NULL
, WM_MOUSEFIRST
, WM_MOUSELAST
, PM_NOREMOVE
)) {
322 DCHECK_NE(WAIT_FAILED
, result
) << GetLastError();
325 void MessagePumpForUI::HandleWorkMessage() {
326 // If we are being called outside of the context of Run, then don't try to do
327 // any work. This could correspond to a MessageBox call or something of that
330 // Since we handled a kMsgHaveWork message, we must still update this flag.
331 InterlockedExchange(&have_work_
, 0);
335 // Let whatever would have run had we not been putting messages in the queue
336 // run now. This is an attempt to make our dummy message not starve other
337 // messages that may be in the Windows message queue.
338 ProcessPumpReplacementMessage();
340 // Now give the delegate a chance to do some work. He'll let us know if he
341 // needs to do more work.
342 if (state_
->delegate
->DoWork())
346 void MessagePumpForUI::HandleTimerMessage() {
347 KillTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this));
349 // If we are being called outside of the context of Run, then don't do
350 // anything. This could correspond to a MessageBox call or something of
355 state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
356 if (!delayed_work_time_
.is_null()) {
357 // A bit gratuitous to set delayed_work_time_ again, but oh well.
358 ScheduleDelayedWork(delayed_work_time_
);
362 bool MessagePumpForUI::ProcessNextWindowsMessage() {
363 // If there are sent messages in the queue then PeekMessage internally
364 // dispatches the message and returns false. We return true in this
365 // case to ensure that the message loop peeks again instead of calling
366 // MsgWaitForMultipleObjectsEx again.
367 bool sent_messages_in_queue
= false;
368 DWORD queue_status
= GetQueueStatus(QS_SENDMESSAGE
);
369 if (HIWORD(queue_status
) & QS_SENDMESSAGE
)
370 sent_messages_in_queue
= true;
373 if (message_filter_
->DoPeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
374 return ProcessMessageHelper(msg
);
376 return sent_messages_in_queue
;
379 bool MessagePumpForUI::ProcessMessageHelper(const MSG
& msg
) {
380 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper",
381 "message", msg
.message
);
382 if (WM_QUIT
== msg
.message
) {
383 // Repost the QUIT message so that it will be retrieved by the primary
384 // GetMessage() loop.
385 state_
->should_quit
= true;
386 PostQuitMessage(static_cast<int>(msg
.wParam
));
390 // While running our main message pump, we discard kMsgHaveWork messages.
391 if (msg
.message
== kMsgHaveWork
&& msg
.hwnd
== message_hwnd_
)
392 return ProcessPumpReplacementMessage();
394 if (CallMsgFilter(const_cast<MSG
*>(&msg
), kMessageFilterCode
))
397 WillProcessMessage(msg
);
399 if (!message_filter_
->ProcessMessage(msg
)) {
400 if (state_
->dispatcher
) {
401 if (!state_
->dispatcher
->Dispatch(msg
))
402 state_
->should_quit
= true;
404 TranslateMessage(&msg
);
405 DispatchMessage(&msg
);
409 DidProcessMessage(msg
);
413 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
414 // When we encounter a kMsgHaveWork message, this method is called to peek
415 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The
416 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though
417 // a continuous stream of such messages are posted. This method carefully
418 // peeks a message while there is no chance for a kMsgHaveWork to be pending,
419 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to
420 // possibly be posted), and finally dispatches that peeked replacement. Note
421 // that the re-post of kMsgHaveWork may be asynchronous to this thread!!
423 bool have_message
= false;
425 // We should not process all window messages if we are in the context of an
426 // OS modal loop, i.e. in the context of a windows API call like MessageBox.
427 // This is to ensure that these messages are peeked out by the OS modal loop.
428 if (MessageLoop::current()->os_modal_loop()) {
429 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above.
430 have_message
= PeekMessage(&msg
, NULL
, WM_PAINT
, WM_PAINT
, PM_REMOVE
) ||
431 PeekMessage(&msg
, NULL
, WM_TIMER
, WM_TIMER
, PM_REMOVE
);
433 have_message
= !!message_filter_
->DoPeekMessage(&msg
, NULL
, 0, 0,
437 DCHECK(!have_message
|| kMsgHaveWork
!= msg
.message
||
438 msg
.hwnd
!= message_hwnd_
);
440 // Since we discarded a kMsgHaveWork message, we must update the flag.
441 int old_have_work
= InterlockedExchange(&have_work_
, 0);
442 DCHECK(old_have_work
);
444 // We don't need a special time slice if we didn't have_message to process.
448 // Guarantee we'll get another time slice in the case where we go into native
449 // windows code. This ScheduleWork() may hurt performance a tiny bit when
450 // tasks appear very infrequently, but when the event queue is busy, the
451 // kMsgHaveWork events get (percentage wise) rarer and rarer.
453 return ProcessMessageHelper(msg
);
456 void MessagePumpForUI::SetMessageFilter(
457 scoped_ptr
<MessageFilter
> message_filter
) {
458 message_filter_
= message_filter
.Pass();
461 //-----------------------------------------------------------------------------
462 // MessagePumpForIO public:
464 MessagePumpForIO::MessagePumpForIO() {
465 port_
.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE
, NULL
, NULL
, 1));
466 DCHECK(port_
.IsValid());
469 void MessagePumpForIO::ScheduleWork() {
470 if (InterlockedExchange(&have_work_
, 1))
471 return; // Someone else continued the pumping.
473 // Make sure the MessagePump does some work for us.
474 BOOL ret
= PostQueuedCompletionStatus(port_
, 0,
475 reinterpret_cast<ULONG_PTR
>(this),
476 reinterpret_cast<OVERLAPPED
*>(this));
478 return; // Post worked perfectly.
480 // See comment in MessagePumpForUI::ScheduleWork() for this error recovery.
481 InterlockedExchange(&have_work_
, 0); // Clarify that we didn't succeed.
482 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", COMPLETION_POST_ERROR
,
483 MESSAGE_LOOP_PROBLEM_MAX
);
486 void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks
& delayed_work_time
) {
487 // We know that we can't be blocked right now since this method can only be
488 // called on the same thread as Run, so we only need to update our record of
489 // how long to sleep when we do sleep.
490 delayed_work_time_
= delayed_work_time
;
493 void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle
,
494 IOHandler
* handler
) {
495 ULONG_PTR key
= HandlerToKey(handler
, true);
496 HANDLE port
= CreateIoCompletionPort(file_handle
, port_
, key
, 1);
500 bool MessagePumpForIO::RegisterJobObject(HANDLE job_handle
,
501 IOHandler
* handler
) {
502 // Job object notifications use the OVERLAPPED pointer to carry the message
503 // data. Mark the completion key correspondingly, so we will not try to
504 // convert OVERLAPPED* to IOContext*.
505 ULONG_PTR key
= HandlerToKey(handler
, false);
506 JOBOBJECT_ASSOCIATE_COMPLETION_PORT info
;
507 info
.CompletionKey
= reinterpret_cast<void*>(key
);
508 info
.CompletionPort
= port_
;
509 return SetInformationJobObject(job_handle
,
510 JobObjectAssociateCompletionPortInformation
,
512 sizeof(info
)) != FALSE
;
515 //-----------------------------------------------------------------------------
516 // MessagePumpForIO private:
518 void MessagePumpForIO::DoRunLoop() {
520 // If we do any work, we may create more messages etc., and more work may
521 // possibly be waiting in another task group. When we (for example)
522 // WaitForIOCompletion(), there is a good chance there are still more
523 // messages waiting. On the other hand, when any of these methods return
524 // having done no work, then it is pretty unlikely that calling them
525 // again quickly will find any work to do. Finally, if they all say they
526 // had no work, then it is a good time to consider sleeping (waiting) for
529 bool more_work_is_plausible
= state_
->delegate
->DoWork();
530 if (state_
->should_quit
)
533 more_work_is_plausible
|= WaitForIOCompletion(0, NULL
);
534 if (state_
->should_quit
)
537 more_work_is_plausible
|=
538 state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
539 if (state_
->should_quit
)
542 if (more_work_is_plausible
)
545 more_work_is_plausible
= state_
->delegate
->DoIdleWork();
546 if (state_
->should_quit
)
549 if (more_work_is_plausible
)
552 WaitForWork(); // Wait (sleep) until we have work to do again.
556 // Wait until IO completes, up to the time needed by the timer manager to fire
557 // the next set of timers.
558 void MessagePumpForIO::WaitForWork() {
559 // We do not support nested IO message loops. This is to avoid messy
560 // recursion problems.
561 DCHECK_EQ(1, state_
->run_depth
) << "Cannot nest an IO message loop!";
563 int timeout
= GetCurrentDelay();
564 if (timeout
< 0) // Negative value means no timers waiting.
567 WaitForIOCompletion(timeout
, NULL
);
570 bool MessagePumpForIO::WaitForIOCompletion(DWORD timeout
, IOHandler
* filter
) {
572 if (completed_io_
.empty() || !MatchCompletedIOItem(filter
, &item
)) {
573 // We have to ask the system for another IO completion.
574 if (!GetIOItem(timeout
, &item
))
577 if (ProcessInternalIOItem(item
))
581 // If |item.has_valid_io_context| is false then |item.context| does not point
582 // to a context structure, and so should not be dereferenced, although it may
583 // still hold valid non-pointer data.
584 if (!item
.has_valid_io_context
|| item
.context
->handler
) {
585 if (filter
&& item
.handler
!= filter
) {
586 // Save this item for later
587 completed_io_
.push_back(item
);
589 DCHECK(!item
.has_valid_io_context
||
590 (item
.context
->handler
== item
.handler
));
591 WillProcessIOEvent();
592 item
.handler
->OnIOCompleted(item
.context
, item
.bytes_transfered
,
597 // The handler must be gone by now, just cleanup the mess.
603 // Asks the OS for another IO completion result.
604 bool MessagePumpForIO::GetIOItem(DWORD timeout
, IOItem
* item
) {
605 memset(item
, 0, sizeof(*item
));
606 ULONG_PTR key
= NULL
;
607 OVERLAPPED
* overlapped
= NULL
;
608 if (!GetQueuedCompletionStatus(port_
.Get(), &item
->bytes_transfered
, &key
,
609 &overlapped
, timeout
)) {
611 return false; // Nothing in the queue.
612 item
->error
= GetLastError();
613 item
->bytes_transfered
= 0;
616 item
->handler
= KeyToHandler(key
, &item
->has_valid_io_context
);
617 item
->context
= reinterpret_cast<IOContext
*>(overlapped
);
621 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem
& item
) {
622 if (this == reinterpret_cast<MessagePumpForIO
*>(item
.context
) &&
623 this == reinterpret_cast<MessagePumpForIO
*>(item
.handler
)) {
624 // This is our internal completion.
625 DCHECK(!item
.bytes_transfered
);
626 InterlockedExchange(&have_work_
, 0);
632 // Returns a completion item that was previously received.
633 bool MessagePumpForIO::MatchCompletedIOItem(IOHandler
* filter
, IOItem
* item
) {
634 DCHECK(!completed_io_
.empty());
635 for (std::list
<IOItem
>::iterator it
= completed_io_
.begin();
636 it
!= completed_io_
.end(); ++it
) {
637 if (!filter
|| it
->handler
== filter
) {
639 completed_io_
.erase(it
);
646 void MessagePumpForIO::AddIOObserver(IOObserver
*obs
) {
647 io_observers_
.AddObserver(obs
);
650 void MessagePumpForIO::RemoveIOObserver(IOObserver
*obs
) {
651 io_observers_
.RemoveObserver(obs
);
654 void MessagePumpForIO::WillProcessIOEvent() {
655 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, WillProcessIOEvent());
658 void MessagePumpForIO::DidProcessIOEvent() {
659 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, DidProcessIOEvent());
663 ULONG_PTR
MessagePumpForIO::HandlerToKey(IOHandler
* handler
,
664 bool has_valid_io_context
) {
665 ULONG_PTR key
= reinterpret_cast<ULONG_PTR
>(handler
);
667 // |IOHandler| is at least pointer-size aligned, so the lowest two bits are
668 // always cleared. We use the lowest bit to distinguish completion keys with
669 // and without the associated |IOContext|.
670 DCHECK((key
& 1) == 0);
672 // Mark the completion key as context-less.
673 if (!has_valid_io_context
)
679 MessagePumpForIO::IOHandler
* MessagePumpForIO::KeyToHandler(
681 bool* has_valid_io_context
) {
682 *has_valid_io_context
= ((key
& 1) == 0);
683 return reinterpret_cast<IOHandler
*>(key
& ~static_cast<ULONG_PTR
>(1));