Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(586)

Side by Side Diff: base/message_loop/message_pump_win.cc

Issue 1156503005: Don't peek messages in the MessagePumpForUI class when we receive our kMsgHaveWork message. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use the IO message pump in the GPU process by default Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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)));
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 // Set the timer to fire every 3 milliseconds. The actual resolution of the
104 118 // timer is dependent on timeBeginPeriod being called.
105 // Make sure the MessagePump does some work for us. 119 due_time.QuadPart = -30000;
106 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, 120 BOOL ret = ::SetWaitableTimer(ui_worker_thread_timer_.Get(), &due_time, 3,
107 reinterpret_cast<WPARAM>(this), 0); 121 NULL, NULL, FALSE);
108 if (ret) 122 CHECK(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 } 123 }
123 124
124 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { 125 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
125 delayed_work_time_ = delayed_work_time; 126 delayed_work_time_ = delayed_work_time;
126 RescheduleTimer(); 127 RescheduleTimer();
127 } 128 }
128 129
129 //----------------------------------------------------------------------------- 130 //-----------------------------------------------------------------------------
130 // MessagePumpForUI private: 131 // MessagePumpForUI private:
131 132
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 FROM_HERE_WITH_EXPLICIT_FUNCTION( 429 FROM_HERE_WITH_EXPLICIT_FUNCTION(
429 "440919 MessagePumpForUI::ProcessMessageHelper6")); 430 "440919 MessagePumpForUI::ProcessMessageHelper6"));
430 431
431 DispatchMessage(&msg); 432 DispatchMessage(&msg);
432 } 433 }
433 434
434 return true; 435 return true;
435 } 436 }
436 437
437 bool MessagePumpForUI::ProcessPumpReplacementMessage() { 438 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
438 // When we encounter a kMsgHaveWork message, this method is called to peek 439 // 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 440 InterlockedExchange(&have_work_, 0);
440 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though 441 return true;
441 // a continuous stream of such messages are posted. This method carefully 442 }
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 443
447 bool have_message = false; 444 void MessagePumpForUI::DoWorkerThreadRunLoop() {
448 MSG msg; 445 DCHECK(ui_worker_thread_timer_.Get());
449 // We should not process all window messages if we are in the context of an 446 while (TRUE) {
450 // OS modal loop, i.e. in the context of a windows API call like MessageBox. 447 DWORD ret = WaitForSingleObjectEx(
451 // This is to ensure that these messages are peeked out by the OS modal loop. 448 ui_worker_thread_timer_.Get(), INFINITE, TRUE);
452 if (MessageLoop::current()->os_modal_loop()) { 449 // 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. 450 if (ret == WAIT_IO_COMPLETION)
454 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || 451 return;
455 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); 452
456 } else { 453 // Make sure the MessagePump does some work for us.
457 have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; 454 BOOL posted = PostMessage(message_hwnd_, kMsgHaveWork,
455 reinterpret_cast<WPARAM>(this),
456 0);
457 if (!posted) {
458 // We have failed to insert a have-work message, so there is a chance
459 // that we will starve tasks/timers while sitting in a nested message
460 // loop. Nested loops only look at Windows Message queues, and don't
461 // look at *our* task queues, etc., so we might not get a time slice in
462 // such. :-(
463 // We could abort here, but the fear is that this failure mode is
464 // plausibly common (queue is full, of about 2000 messages), so we'll
465 // do a near-graceful recovery. Nested loops are pretty transient
466 // (we think), so this will probably be recoverable.
467 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem",
468 MESSAGE_POST_ERROR,
469 MESSAGE_LOOP_PROBLEM_MAX);
470 }
458 } 471 }
472 }
459 473
460 DCHECK(!have_message || kMsgHaveWork != msg.message || 474 // static
461 msg.hwnd != message_hwnd_); 475 void CALLBACK MessagePumpForUI::ShutdownWorkerThread(ULONG_PTR param) {
462 476 // 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. 477 // to the worker thread to shut it down.
464 int old_have_work = InterlockedExchange(&have_work_, 0); 478 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 } 479 }
478 480
479 //----------------------------------------------------------------------------- 481 //-----------------------------------------------------------------------------
480 // MessagePumpForIO public: 482 // MessagePumpForIO public:
481 483
482 MessagePumpForIO::MessagePumpForIO() { 484 MessagePumpForIO::MessagePumpForIO() {
483 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); 485 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1));
484 DCHECK(port_.IsValid()); 486 DCHECK(port_.IsValid());
485 } 487 }
486 488
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 700
699 // static 701 // static
700 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( 702 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler(
701 ULONG_PTR key, 703 ULONG_PTR key,
702 bool* has_valid_io_context) { 704 bool* has_valid_io_context) {
703 *has_valid_io_context = ((key & 1) == 0); 705 *has_valid_io_context = ((key & 1) == 0);
704 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); 706 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1));
705 } 707 }
706 708
707 } // namespace base 709 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698