OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_default.h" | 5 #include "base/message_loop/message_pump_default.h" |
6 | 6 |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "base/threading/thread_restrictions.h" | 8 #include "base/threading/thread_restrictions.h" |
11 #include "build/build_config.h" | 9 #include "build/build_config.h" |
12 | 10 |
13 #if defined(OS_MACOSX) | 11 #if defined(OS_MACOSX) |
14 #include "base/mac/scoped_nsautorelease_pool.h" | 12 #include "base/mac/scoped_nsautorelease_pool.h" |
15 #endif | 13 #endif |
16 | 14 |
17 namespace base { | 15 namespace base { |
18 | 16 |
(...skipping 28 matching lines...) Expand all Loading... | |
47 if (!keep_running_) | 45 if (!keep_running_) |
48 break; | 46 break; |
49 | 47 |
50 if (did_work) | 48 if (did_work) |
51 continue; | 49 continue; |
52 | 50 |
53 ThreadRestrictions::ScopedAllowWait allow_wait; | 51 ThreadRestrictions::ScopedAllowWait allow_wait; |
54 if (delayed_work_time_.is_null()) { | 52 if (delayed_work_time_.is_null()) { |
55 event_.Wait(); | 53 event_.Wait(); |
56 } else { | 54 } else { |
57 TimeDelta delay = delayed_work_time_ - TimeTicks::Now(); | 55 event_.TimedWaitUntil(delayed_work_time_); |
58 if (delay > TimeDelta()) { | |
59 #if defined(OS_WIN) | |
60 // TODO(stanisc): crbug.com/623223: Consider moving the OS_WIN specific | |
61 // logic into TimedWait implementation in waitable_event_win.cc. | |
62 | |
63 // crbug.com/487724: on Windows, waiting for less than 1 ms results in | |
64 // returning from TimedWait promptly and spinning | |
65 // MessagePumpDefault::Run loop for up to 1 ms - until it is time to | |
66 // run a delayed task. |min_delay| is the minimum possible wait to | |
67 // to avoid the spinning. | |
68 constexpr TimeDelta min_delay = TimeDelta::FromMilliseconds(1); | |
69 do { | |
70 delay = std::max(delay, min_delay); | |
71 if (event_.TimedWait(delay)) | |
72 break; | |
73 | |
74 // TimedWait can time out earlier than the specified |delay| on | |
75 // Windows. It doesn't make sense to run the outer loop in that case | |
76 // because there isn't going to be any new work. It is less overhead | |
77 // to just go back to wait. | |
78 // In practice this inner wait loop might have up to 3 iterations. | |
79 delay = delayed_work_time_ - TimeTicks::Now(); | |
80 } while (delay > TimeDelta()); | |
81 #else | |
82 event_.TimedWait(delay); | |
83 #endif | |
84 } else { | |
danakj
2016/11/16 22:57:29
Can u explain why the if and this else block are g
stanisc
2016/11/17 19:42:22
Good call!
I think it shouldn't matter with the ne
danakj
2016/11/29 02:46:45
Ok thanks. It's true that in order to reach this p
| |
85 // It looks like delayed_work_time_ indicates a time in the past, so we | |
86 // need to call DoDelayedWork now. | |
87 delayed_work_time_ = TimeTicks(); | |
88 } | |
89 } | 56 } |
90 // Since event_ is auto-reset, we don't need to do anything special here | 57 // Since event_ is auto-reset, we don't need to do anything special here |
91 // other than service each delegate method. | 58 // other than service each delegate method. |
92 } | 59 } |
93 | 60 |
94 keep_running_ = true; | 61 keep_running_ = true; |
95 } | 62 } |
96 | 63 |
97 void MessagePumpDefault::Quit() { | 64 void MessagePumpDefault::Quit() { |
98 keep_running_ = false; | 65 keep_running_ = false; |
99 } | 66 } |
100 | 67 |
101 void MessagePumpDefault::ScheduleWork() { | 68 void MessagePumpDefault::ScheduleWork() { |
102 // Since this can be called on any thread, we need to ensure that our Run | 69 // Since this can be called on any thread, we need to ensure that our Run |
103 // loop wakes up. | 70 // loop wakes up. |
104 event_.Signal(); | 71 event_.Signal(); |
105 } | 72 } |
106 | 73 |
107 void MessagePumpDefault::ScheduleDelayedWork( | 74 void MessagePumpDefault::ScheduleDelayedWork( |
108 const TimeTicks& delayed_work_time) { | 75 const TimeTicks& delayed_work_time) { |
109 // We know that we can't be blocked on Wait right now since this method can | 76 // We know that we can't be blocked on Wait right now since this method can |
110 // only be called on the same thread as Run, so we only need to update our | 77 // only be called on the same thread as Run, so we only need to update our |
111 // record of how long to sleep when we do sleep. | 78 // record of how long to sleep when we do sleep. |
112 delayed_work_time_ = delayed_work_time; | 79 delayed_work_time_ = delayed_work_time; |
113 } | 80 } |
114 | 81 |
115 } // namespace base | 82 } // namespace base |
OLD | NEW |