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" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 // super-long delay. | 86 // super-long delay. |
87 return timeout < 0 ? 0 : | 87 return timeout < 0 ? 0 : |
88 (timeout > std::numeric_limits<int>::max() ? | 88 (timeout > std::numeric_limits<int>::max() ? |
89 std::numeric_limits<int>::max() : static_cast<int>(timeout)); | 89 std::numeric_limits<int>::max() : static_cast<int>(timeout)); |
90 } | 90 } |
91 | 91 |
92 //----------------------------------------------------------------------------- | 92 //----------------------------------------------------------------------------- |
93 // MessagePumpForUI public: | 93 // MessagePumpForUI public: |
94 | 94 |
95 MessagePumpForUI::MessagePumpForUI() | 95 MessagePumpForUI::MessagePumpForUI() |
96 : atom_(0) { | 96 : atom_(0), |
97 force_fallback_timer_for_tasks_(0) { | |
97 InitMessageWnd(); | 98 InitMessageWnd(); |
98 | 99 |
99 ui_worker_thread_timer_.Set(::CreateWaitableTimer(NULL, FALSE, NULL)); | 100 ui_worker_thread_timer_.Set(::CreateWaitableTimer(NULL, FALSE, NULL)); |
100 ui_worker_thread_.reset(new base::Thread("UI Pump Worker thread")); | 101 ui_worker_thread_.reset(new base::Thread("UI Pump Worker thread")); |
101 ui_worker_thread_->Start(); | 102 ui_worker_thread_->Start(); |
102 ui_worker_thread_->WaitUntilThreadStarted(); | 103 ui_worker_thread_->WaitUntilThreadStarted(); |
103 ui_worker_thread_->task_runner()->PostTask( | 104 ui_worker_thread_->task_runner()->PostTask( |
104 FROM_HERE, | 105 FROM_HERE, |
105 base::Bind(&MessagePumpForUI::DoWorkerThreadRunLoop, | 106 base::Bind(&MessagePumpForUI::DoWorkerThreadRunLoop, |
106 base::Unretained(this))); | 107 base::Unretained(this))); |
(...skipping 11 matching lines...) Expand all Loading... | |
118 } | 119 } |
119 | 120 |
120 void MessagePumpForUI::ScheduleWork() { | 121 void MessagePumpForUI::ScheduleWork() { |
121 // If we have a regular posted task at the head of queue then we need to | 122 // If we have a regular posted task at the head of queue then we need to |
122 // process it quickly. | 123 // process it quickly. |
123 if (state_ && state_->delegate->GetNewlyAddedTaskDelay().is_null()) { | 124 if (state_ && state_->delegate->GetNewlyAddedTaskDelay().is_null()) { |
124 // Make sure the MessagePump does some work for us. | 125 // Make sure the MessagePump does some work for us. |
125 PostWorkMessage(); | 126 PostWorkMessage(); |
126 return; | 127 return; |
127 } | 128 } |
129 | |
130 ::InterlockedExchange(&force_fallback_timer_for_tasks_, 1); | |
128 // Set a one shot timer to fire after 3 milliseconds. The actual resolution | 131 // Set a one shot timer to fire after 3 milliseconds. The actual resolution |
129 // of the timer is dependent on timeBeginPeriod being called. | 132 // of the timer is dependent on timeBeginPeriod being called. |
130 SetWakeupTimer(kDefaultUIWorkerThreadWakeupTimerMs); | 133 SetWakeupTimer(kDefaultUIWorkerThreadWakeupTimerMs); |
131 } | 134 } |
132 | 135 |
133 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { | 136 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
134 delayed_work_time_ = delayed_work_time; | 137 delayed_work_time_ = delayed_work_time; |
135 RescheduleTimer(); | 138 RescheduleTimer(); |
136 } | 139 } |
137 | 140 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 } | 421 } |
419 | 422 |
420 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 423 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
421 // Since we discarded a kMsgHaveWork message, we must update the flag. | 424 // Since we discarded a kMsgHaveWork message, we must update the flag. |
422 InterlockedExchange(&have_work_, 0); | 425 InterlockedExchange(&have_work_, 0); |
423 return true; | 426 return true; |
424 } | 427 } |
425 | 428 |
426 void MessagePumpForUI::DoWorkerThreadRunLoop() { | 429 void MessagePumpForUI::DoWorkerThreadRunLoop() { |
427 DCHECK(ui_worker_thread_timer_.Get()); | 430 DCHECK(ui_worker_thread_timer_.Get()); |
431 | |
428 while (TRUE) { | 432 while (TRUE) { |
429 DWORD ret = WaitForSingleObjectEx( | 433 DWORD ret = WaitForSingleObjectEx( |
430 ui_worker_thread_timer_.Get(), INFINITE, TRUE); | 434 ui_worker_thread_timer_.Get(), INFINITE, TRUE); |
431 // The only APC this thread could receive is the Shutdown APC. | 435 // The only APC this thread could receive is the Shutdown APC. |
432 if (ret == WAIT_IO_COMPLETION) | 436 if (ret == WAIT_IO_COMPLETION) |
433 return; | 437 return; |
434 | 438 |
435 // Make sure the MessagePump does some work for us. | 439 // Make sure the MessagePump does some work for us. |
436 PostWorkMessage(); | 440 PostWorkMessage(); |
437 | 441 |
438 // Set a one shot timer to process pending delayed tasks if any in the | 442 // Set a one shot timer to process pending delayed tasks if any in the |
439 // queue. The actual resolution of the timer is dependent on the | 443 // queue. The actual resolution of the timer is dependent on the |
440 // timeBeginPeriod API being called. | 444 // timeBeginPeriod API being called. |
brucedawson
2015/06/19 21:09:01
"is dependent on the current global timer precisio
ananta
2015/06/19 22:19:10
Done.
| |
441 SetWakeupTimer(kDefaultUIWorkerThreadWakeupTimerMs); | 445 if (::InterlockedExchange(&force_fallback_timer_for_tasks_, 0)) |
446 SetWakeupTimer(kDefaultUIWorkerThreadWakeupTimerMs); | |
442 } | 447 } |
443 } | 448 } |
444 | 449 |
445 // static | 450 // static |
446 void CALLBACK MessagePumpForUI::ShutdownWorkerThread(ULONG_PTR param) { | 451 void CALLBACK MessagePumpForUI::ShutdownWorkerThread(ULONG_PTR param) { |
447 // This function is empty because we only use the fact that an APC was posted | 452 // This function is empty because we only use the fact that an APC was posted |
448 // to the worker thread to shut it down. | 453 // to the worker thread to shut it down. |
449 return; | 454 return; |
450 } | 455 } |
451 | 456 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 | 706 |
702 // static | 707 // static |
703 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 708 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
704 ULONG_PTR key, | 709 ULONG_PTR key, |
705 bool* has_valid_io_context) { | 710 bool* has_valid_io_context) { |
706 *has_valid_io_context = ((key & 1) == 0); | 711 *has_valid_io_context = ((key & 1) == 0); |
707 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 712 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
708 } | 713 } |
709 | 714 |
710 } // namespace base | 715 } // namespace base |
OLD | NEW |