Chromium Code Reviews| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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) { |
| 92 InitMessageWnd(); | 93 InitMessageWnd(); |
| 94 | |
| 95 ui_worker_thread_timer_.Set(::CreateWaitableTimer(NULL, FALSE, NULL)); | |
| 96 ui_worker_thread_.reset(new base::Thread("UI Pump Worker thread")); | |
| 97 ui_worker_thread_->Start(); | |
| 98 ui_worker_thread_->task_runner()->PostTask( | |
| 99 FROM_HERE, | |
| 100 base::Bind(&MessagePumpForUI::DoWorkerThreadRunLoop, | |
| 101 base::Unretained(this))); | |
|
cpu_(ooo_6.6-7.5)
2015/06/03 00:57:11
indent in 101 looks funny
ananta
2015/06/03 01:27:30
Done.
| |
| 93 } | 102 } |
| 94 | 103 |
| 95 MessagePumpForUI::~MessagePumpForUI() { | 104 MessagePumpForUI::~MessagePumpForUI() { |
| 96 DestroyWindow(message_hwnd_); | 105 DestroyWindow(message_hwnd_); |
| 97 UnregisterClass(MAKEINTATOM(atom_), | 106 UnregisterClass(MAKEINTATOM(atom_), |
| 98 GetModuleFromAddress(&WndProcThunk)); | 107 GetModuleFromAddress(&WndProcThunk)); |
| 108 | |
| 109 ::QueueUserAPC( | |
| 110 reinterpret_cast<PAPCFUNC>(&MessagePumpForUI::ShutdownWorkerThread), | |
| 111 ui_worker_thread_->thread_handle().platform_handle(), NULL); | |
| 112 ui_worker_thread_->Stop(); | |
| 99 } | 113 } |
| 100 | 114 |
| 101 void MessagePumpForUI::ScheduleWork() { | 115 void MessagePumpForUI::ScheduleWork() { |
| 102 if (InterlockedExchange(&have_work_, 1)) | 116 LARGE_INTEGER due_time = {0}; |
| 103 return; // Someone else continued the pumping. | 117 // 3 milliseconds. |
|
cpu_(ooo_6.6-7.5)
2015/06/03 00:57:11
make a full sentence, like "set the timer to fire
ananta
2015/06/03 01:27:30
Done.
| |
| 104 | 118 due_time.QuadPart = -30000; |
| 105 // Make sure the MessagePump does some work for us. | 119 BOOL ret = ::SetWaitableTimer(ui_worker_thread_timer_.Get(), &due_time, 3, |
| 106 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, | 120 NULL, NULL, FALSE); |
| 107 reinterpret_cast<WPARAM>(this), 0); | 121 CHECK(ret); |
| 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 } | 122 } |
| 123 | 123 |
| 124 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { | 124 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
| 125 delayed_work_time_ = delayed_work_time; | 125 delayed_work_time_ = delayed_work_time; |
| 126 RescheduleTimer(); | 126 RescheduleTimer(); |
| 127 } | 127 } |
| 128 | 128 |
| 129 //----------------------------------------------------------------------------- | 129 //----------------------------------------------------------------------------- |
| 130 // MessagePumpForUI private: | 130 // MessagePumpForUI private: |
| 131 | 131 |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 428 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 428 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 429 "440919 MessagePumpForUI::ProcessMessageHelper6")); | 429 "440919 MessagePumpForUI::ProcessMessageHelper6")); |
| 430 | 430 |
| 431 DispatchMessage(&msg); | 431 DispatchMessage(&msg); |
| 432 } | 432 } |
| 433 | 433 |
| 434 return true; | 434 return true; |
| 435 } | 435 } |
| 436 | 436 |
| 437 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 437 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
| 438 // When we encounter a kMsgHaveWork message, this method is called to peek | 438 // Since we discarded a kMsgHaveWork message, we must update the flag. |
| 439 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The | 439 InterlockedExchange(&have_work_, 0); |
| 440 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though | 440 return true; |
| 441 // a continuous stream of such messages are posted. This method carefully | 441 } |
| 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 | 442 |
| 447 bool have_message = false; | 443 void MessagePumpForUI::DoWorkerThreadRunLoop() { |
| 448 MSG msg; | 444 DCHECK(ui_worker_thread_timer_.Get()); |
| 449 // We should not process all window messages if we are in the context of an | 445 while (TRUE) { |
| 450 // OS modal loop, i.e. in the context of a windows API call like MessageBox. | 446 DWORD ret = WaitForSingleObjectEx( |
| 451 // This is to ensure that these messages are peeked out by the OS modal loop. | 447 ui_worker_thread_timer_.Get(), INFINITE, TRUE); |
| 452 if (MessageLoop::current()->os_modal_loop()) { | 448 // The only APC this thread could receive is the Shutdown APC. |
| 453 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. | 449 if (ret == WAIT_IO_COMPLETION) |
| 454 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || | 450 return; |
| 455 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); | 451 |
| 456 } else { | 452 // Make sure the MessagePump does some work for us. |
| 457 have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; | 453 BOOL posted = PostMessage(message_hwnd_, kMsgHaveWork, |
| 454 reinterpret_cast<WPARAM>(this), | |
| 455 0); | |
| 456 if (!posted) { | |
| 457 // We have failed to insert a have-work message, so there is a chance | |
| 458 // that we will starve tasks/timers while sitting in a nested message | |
| 459 // loop. Nested loops only look at Windows Message queues, and don't | |
| 460 // look at *our* task queues, etc., so we might not get a time slice in | |
| 461 // such. :-( | |
| 462 // We could abort here, but the fear is that this failure mode is | |
| 463 // plausibly common (queue is full, of about 2000 messages), so we'll | |
| 464 // do a near-graceful recovery. Nested loops are pretty transient | |
| 465 // (we think), so this will probably be recoverable. | |
| 466 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", | |
| 467 MESSAGE_POST_ERROR, | |
| 468 MESSAGE_LOOP_PROBLEM_MAX); | |
| 469 } | |
| 458 } | 470 } |
| 471 } | |
| 459 | 472 |
| 460 DCHECK(!have_message || kMsgHaveWork != msg.message || | 473 // static |
| 461 msg.hwnd != message_hwnd_); | 474 void CALLBACK MessagePumpForUI::ShutdownWorkerThread(ULONG_PTR param) { |
| 462 | 475 // This function is empty because we only use the fact that an APC was posted |
| 463 // Since we discarded a kMsgHaveWork message, we must update the flag. | 476 // to the worker thread to shut it down. |
| 464 int old_have_work = InterlockedExchange(&have_work_, 0); | 477 return; |
| 465 DCHECK(old_have_work); | |
| 466 | |
| 467 // We don't need a special time slice if we didn't have_message to process. | |
| 468 if (!have_message) | |
| 469 return false; | |
| 470 | |
| 471 // Guarantee we'll get another time slice in the case where we go into native | |
| 472 // windows code. This ScheduleWork() may hurt performance a tiny bit when | |
| 473 // tasks appear very infrequently, but when the event queue is busy, the | |
| 474 // kMsgHaveWork events get (percentage wise) rarer and rarer. | |
| 475 ScheduleWork(); | |
| 476 return ProcessMessageHelper(msg); | |
| 477 } | 478 } |
| 478 | 479 |
| 479 //----------------------------------------------------------------------------- | 480 //----------------------------------------------------------------------------- |
| 480 // MessagePumpForIO public: | 481 // MessagePumpForIO public: |
| 481 | 482 |
| 482 MessagePumpForIO::MessagePumpForIO() { | 483 MessagePumpForIO::MessagePumpForIO() { |
| 483 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); | 484 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); |
| 484 DCHECK(port_.IsValid()); | 485 DCHECK(port_.IsValid()); |
| 485 } | 486 } |
| 486 | 487 |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 698 | 699 |
| 699 // static | 700 // static |
| 700 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 701 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
| 701 ULONG_PTR key, | 702 ULONG_PTR key, |
| 702 bool* has_valid_io_context) { | 703 bool* has_valid_io_context) { |
| 703 *has_valid_io_context = ((key & 1) == 0); | 704 *has_valid_io_context = ((key & 1) == 0); |
| 704 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 705 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
| 705 } | 706 } |
| 706 | 707 |
| 707 } // namespace base | 708 } // namespace base |
| OLD | NEW |