OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_pump_win.h" | 5 #include "base/message_loop/message_pump_win.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <math.h> | 8 #include <math.h> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/process/memory.h" | 12 #include "base/process/memory.h" |
13 #include "base/profiler/scoped_tracker.h" | 13 #include "base/profiler/scoped_tracker.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/threading/thread.h" | |
15 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
16 #include "base/win/wrapped_window_proc.h" | 17 #include "base/win/wrapped_window_proc.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 enum MessageLoopProblems { | 23 enum MessageLoopProblems { |
23 MESSAGE_POST_ERROR, | 24 MESSAGE_POST_ERROR, |
24 COMPLETION_POST_ERROR, | 25 COMPLETION_POST_ERROR, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 // super-long delay. | 82 // super-long delay. |
82 return timeout < 0 ? 0 : | 83 return timeout < 0 ? 0 : |
83 (timeout > std::numeric_limits<int>::max() ? | 84 (timeout > std::numeric_limits<int>::max() ? |
84 std::numeric_limits<int>::max() : static_cast<int>(timeout)); | 85 std::numeric_limits<int>::max() : static_cast<int>(timeout)); |
85 } | 86 } |
86 | 87 |
87 //----------------------------------------------------------------------------- | 88 //----------------------------------------------------------------------------- |
88 // MessagePumpForUI public: | 89 // MessagePumpForUI public: |
89 | 90 |
90 MessagePumpForUI::MessagePumpForUI() | 91 MessagePumpForUI::MessagePumpForUI() |
91 : atom_(0) { | 92 : atom_(0), |
93 ui_worker_thread_signal_(false, false) { | |
92 InitMessageWnd(); | 94 InitMessageWnd(); |
95 ui_worker_thread_.reset(new base::Thread("UI Pump Worker thread")); | |
96 ui_worker_thread_->Start(); | |
97 ui_worker_thread_->task_runner()->PostTask( | |
98 FROM_HERE, | |
99 base::Bind(&MessagePumpForUI::DoWorkerThreadRunLoop, | |
100 base::Unretained(this))); | |
93 } | 101 } |
94 | 102 |
95 MessagePumpForUI::~MessagePumpForUI() { | 103 MessagePumpForUI::~MessagePumpForUI() { |
96 DestroyWindow(message_hwnd_); | 104 DestroyWindow(message_hwnd_); |
97 UnregisterClass(MAKEINTATOM(atom_), | 105 UnregisterClass(MAKEINTATOM(atom_), |
98 GetModuleFromAddress(&WndProcThunk)); | 106 GetModuleFromAddress(&WndProcThunk)); |
107 | |
108 ::QueueUserAPC( | |
109 reinterpret_cast<PAPCFUNC>(&MessagePumpForUI::ShutdownWorkerThread), | |
110 ui_worker_thread_->thread_handle().platform_handle(), NULL); | |
111 ui_worker_thread_->Stop(); | |
99 } | 112 } |
100 | 113 |
101 void MessagePumpForUI::ScheduleWork() { | 114 void MessagePumpForUI::ScheduleWork() { |
102 if (InterlockedExchange(&have_work_, 1)) | 115 ui_worker_thread_signal_.Signal(); |
103 return; // Someone else continued the pumping. | 116 return; |
104 | |
105 // Make sure the MessagePump does some work for us. | |
106 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, | |
107 reinterpret_cast<WPARAM>(this), 0); | |
108 if (ret) | |
109 return; // There was room in the Window Message queue. | |
110 | |
111 // We have failed to insert a have-work message, so there is a chance that we | |
112 // will starve tasks/timers while sitting in a nested message loop. Nested | |
113 // loops only look at Windows Message queues, and don't look at *our* task | |
114 // queues, etc., so we might not get a time slice in such. :-( | |
115 // We could abort here, but the fear is that this failure mode is plausibly | |
116 // common (queue is full, of about 2000 messages), so we'll do a near-graceful | |
117 // recovery. Nested loops are pretty transient (we think), so this will | |
118 // probably be recoverable. | |
119 InterlockedExchange(&have_work_, 0); // Clarify that we didn't really insert. | |
120 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", MESSAGE_POST_ERROR, | |
121 MESSAGE_LOOP_PROBLEM_MAX); | |
122 } | 117 } |
123 | 118 |
124 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { | 119 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
125 delayed_work_time_ = delayed_work_time; | 120 delayed_work_time_ = delayed_work_time; |
126 RescheduleTimer(); | 121 RescheduleTimer(); |
127 } | 122 } |
128 | 123 |
129 //----------------------------------------------------------------------------- | 124 //----------------------------------------------------------------------------- |
130 // MessagePumpForUI private: | 125 // MessagePumpForUI private: |
131 | 126 |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
428 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 423 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
429 "440919 MessagePumpForUI::ProcessMessageHelper6")); | 424 "440919 MessagePumpForUI::ProcessMessageHelper6")); |
430 | 425 |
431 DispatchMessage(&msg); | 426 DispatchMessage(&msg); |
432 } | 427 } |
433 | 428 |
434 return true; | 429 return true; |
435 } | 430 } |
436 | 431 |
437 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 432 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
438 // When we encounter a kMsgHaveWork message, this method is called to peek | |
439 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The | |
440 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though | |
441 // a continuous stream of such messages are posted. This method carefully | |
442 // peeks a message while there is no chance for a kMsgHaveWork to be pending, | |
443 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to | |
444 // possibly be posted), and finally dispatches that peeked replacement. Note | |
445 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! | |
446 | |
447 bool have_message = false; | |
448 MSG msg; | |
449 // We should not process all window messages if we are in the context of an | |
450 // OS modal loop, i.e. in the context of a windows API call like MessageBox. | |
451 // This is to ensure that these messages are peeked out by the OS modal loop. | |
452 if (MessageLoop::current()->os_modal_loop()) { | |
453 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. | |
454 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || | |
455 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); | |
456 } else { | |
457 have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; | |
458 } | |
459 | |
460 DCHECK(!have_message || kMsgHaveWork != msg.message || | |
461 msg.hwnd != message_hwnd_); | |
462 | |
463 // Since we discarded a kMsgHaveWork message, we must update the flag. | 433 // Since we discarded a kMsgHaveWork message, we must update the flag. |
464 int old_have_work = InterlockedExchange(&have_work_, 0); | 434 int old_have_work = InterlockedExchange(&have_work_, 0); |
465 DCHECK(old_have_work); | 435 DCHECK(old_have_work); |
436 return true; | |
437 } | |
466 | 438 |
467 // We don't need a special time slice if we didn't have_message to process. | 439 void MessagePumpForUI::DoWorkerThreadRunLoop() { |
468 if (!have_message) | 440 base::win::ScopedHandle timer(::CreateWaitableTimer(NULL, FALSE, NULL)); |
469 return false; | 441 LARGE_INTEGER due_time = {0}; |
442 // 3 milliseconds. | |
443 due_time.QuadPart = -30000; | |
444 BOOL ret = ::SetWaitableTimer(timer.Get(), &due_time, 3, NULL, NULL, FALSE); | |
445 CHECK(ret); | |
470 | 446 |
471 // Guarantee we'll get another time slice in the case where we go into native | 447 while (TRUE) { |
472 // windows code. This ScheduleWork() may hurt performance a tiny bit when | 448 HANDLE objects[] = {ui_worker_thread_signal_.handle(), timer.Get()}; |
cpu_(ooo_6.6-7.5)
2015/06/02 00:17:27
I don't think you need the ui_worker_thread_signal
ananta
2015/06/03 01:27:30
We are using the ui_worker_thread_signal_ to avoid
| |
473 // tasks appear very infrequently, but when the event queue is busy, the | 449 DWORD ret = WaitForMultipleObjectsEx(arraysize(objects), objects, TRUE, |
474 // kMsgHaveWork events get (percentage wise) rarer and rarer. | 450 INFINITE, TRUE); |
475 ScheduleWork(); | 451 // The only APC this thread could receive is the Shutdown APC. |
476 return ProcessMessageHelper(msg); | 452 if (ret == WAIT_IO_COMPLETION) |
453 return; | |
454 | |
455 if (InterlockedExchange(&have_work_, 1)) | |
456 continue; // Someone else continued the pumping. | |
457 | |
458 // Make sure the MessagePump does some work for us. | |
459 BOOL posted = PostMessage(message_hwnd_, kMsgHaveWork, | |
460 reinterpret_cast<WPARAM>(this), | |
461 0); | |
462 if (!posted) { | |
463 // We have failed to insert a have-work message, so there is a chance | |
464 // that we will starve tasks/timers while sitting in a nested message | |
465 // loop. Nested loops only look at Windows Message queues, and don't | |
466 // look at *our* task queues, etc., so we might not get a time slice in | |
467 // such. :-( | |
468 // We could abort here, but the fear is that this failure mode is | |
469 // plausibly common (queue is full, of about 2000 messages), so we'll | |
470 // do a near-graceful recovery. Nested loops are pretty transient | |
471 // (we think), so this will probably be recoverable. | |
472 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", | |
473 MESSAGE_POST_ERROR, | |
474 MESSAGE_LOOP_PROBLEM_MAX); | |
475 } | |
cpu_(ooo_6.6-7.5)
2015/06/02 00:17:27
the original code seems to reset have_work_ here,
ananta
2015/06/03 01:27:29
Removed the check for have_work in this function.
| |
476 } | |
477 } | |
478 | |
479 // static | |
480 void CALLBACK MessagePumpForUI::ShutdownWorkerThread(ULONG_PTR param) { | |
481 return; | |
cpu_(ooo_6.6-7.5)
2015/06/02 00:17:27
have a comment in 481 explaining why the empty fun
ananta
2015/06/03 01:27:29
Done.
| |
477 } | 482 } |
478 | 483 |
479 //----------------------------------------------------------------------------- | 484 //----------------------------------------------------------------------------- |
480 // MessagePumpForIO public: | 485 // MessagePumpForIO public: |
481 | 486 |
482 MessagePumpForIO::MessagePumpForIO() { | 487 MessagePumpForIO::MessagePumpForIO() { |
483 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); | 488 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); |
484 DCHECK(port_.IsValid()); | 489 DCHECK(port_.IsValid()); |
485 } | 490 } |
486 | 491 |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
698 | 703 |
699 // static | 704 // static |
700 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 705 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
701 ULONG_PTR key, | 706 ULONG_PTR key, |
702 bool* has_valid_io_context) { | 707 bool* has_valid_io_context) { |
703 *has_valid_io_context = ((key & 1) == 0); | 708 *has_valid_io_context = ((key & 1) == 0); |
704 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 709 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
705 } | 710 } |
706 | 711 |
707 } // namespace base | 712 } // namespace base |
OLD | NEW |