OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/synchronization/waitable_event.h" | 5 #include "base/synchronization/waitable_event.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | |
10 #include <utility> | 11 #include <utility> |
11 | 12 |
12 #include "base/debug/activity_tracker.h" | 13 #include "base/debug/activity_tracker.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 | 20 |
(...skipping 17 matching lines...) Expand all Loading... | |
37 | 38 |
38 void WaitableEvent::Reset() { | 39 void WaitableEvent::Reset() { |
39 ResetEvent(handle_.Get()); | 40 ResetEvent(handle_.Get()); |
40 } | 41 } |
41 | 42 |
42 void WaitableEvent::Signal() { | 43 void WaitableEvent::Signal() { |
43 SetEvent(handle_.Get()); | 44 SetEvent(handle_.Get()); |
44 } | 45 } |
45 | 46 |
46 bool WaitableEvent::IsSignaled() { | 47 bool WaitableEvent::IsSignaled() { |
47 return TimedWait(TimeDelta()); | 48 DWORD result = WaitForSingleObject(handle_.Get(), 0); |
49 switch (result) { | |
50 case WAIT_OBJECT_0: | |
51 return true; | |
52 case WAIT_TIMEOUT: | |
53 return false; | |
54 } | |
55 | |
56 // It is most unexpected that this should ever fail. Help consumers learn | |
57 // about it if it should ever fail. | |
58 NOTREACHED() << "WaitForSingleObject failed"; | |
59 return false; | |
48 } | 60 } |
49 | 61 |
50 void WaitableEvent::Wait() { | 62 void WaitableEvent::Wait() { |
51 // Record the event that this thread is blocking upon (for hang diagnosis). | 63 // Record the event that this thread is blocking upon (for hang diagnosis). |
52 base::debug::ScopedEventWaitActivity event_activity(this); | 64 base::debug::ScopedEventWaitActivity event_activity(this); |
53 | 65 |
54 base::ThreadRestrictions::AssertWaitAllowed(); | 66 base::ThreadRestrictions::AssertWaitAllowed(); |
55 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); | 67 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); |
56 // It is most unexpected that this should ever fail. Help consumers learn | 68 // It is most unexpected that this should ever fail. Help consumers learn |
57 // about it if it should ever fail. | 69 // about it if it should ever fail. |
58 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 70 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
59 } | 71 } |
60 | 72 |
61 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 73 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
danakj
2016/10/21 22:01:22
nit: can you name timedeltas with _delta suffix an
stanisc
2016/11/11 03:50:48
Done.
| |
74 DCHECK_GE(max_time, TimeDelta()); | |
75 if (max_time.is_zero()) | |
76 return IsSignaled(); | |
77 | |
62 // Record the event that this thread is blocking upon (for hang diagnosis). | 78 // Record the event that this thread is blocking upon (for hang diagnosis). |
63 base::debug::ScopedEventWaitActivity event_activity(this); | 79 base::debug::ScopedEventWaitActivity event_activity(this); |
80 base::ThreadRestrictions::AssertWaitAllowed(); | |
danakj
2016/10/21 22:01:22
can you put assertions first? (just above event_ac
stanisc
2016/11/11 03:50:48
Done.
| |
64 | 81 |
65 DCHECK_GE(max_time, TimeDelta()); | 82 constexpr TimeDelta min_delay = TimeDelta::FromMilliseconds(1); |
66 if (!max_time.is_zero()) | 83 TimeTicks target_time = TimeTicks::Now() + max_time; |
danakj
2016/10/21 22:01:22
It's possible this was a bad suggestion because it
stanisc
2016/10/21 23:05:25
Yeah, that is unfortunate and redundant that the c
danakj
2016/10/21 23:15:26
I was wondering about an overload also, this seems
| |
67 base::ThreadRestrictions::AssertWaitAllowed(); | 84 TimeDelta delay = max_time; |
68 | 85 |
69 // Truncate the timeout to milliseconds. The API specifies that this method | 86 do { |
70 // can return in less than |max_time| (when returning false), as the argument | 87 // On Windows, waiting for less than 1 ms results in WaitForSingleObject |
71 // is the maximum time that a caller is willing to wait. | 88 // returning promptly which may result in the caller code spinning. |
72 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); | 89 // We need to ensure that we specify at least the minimally possible 1 ms |
73 | 90 // delay unless the initial timeout was exactly zero. |
74 DWORD result = WaitForSingleObject(handle_.Get(), timeout); | 91 delay = std::max(delay, min_delay); |
danakj
2016/10/21 22:01:22
I think you can just write TimeDelta::FromMillisec
stanisc
2016/11/11 03:50:48
Done.
| |
75 switch (result) { | 92 // Truncate the timeout to milliseconds. |
76 case WAIT_OBJECT_0: | 93 DWORD timeout = saturated_cast<DWORD>(delay.InMilliseconds()); |
77 return true; | 94 DWORD result = WaitForSingleObject(handle_.Get(), timeout); |
78 case WAIT_TIMEOUT: | 95 switch (result) { |
79 return false; | 96 case WAIT_OBJECT_0: |
80 } | 97 return true; |
81 // It is most unexpected that this should ever fail. Help consumers learn | 98 case WAIT_TIMEOUT: |
82 // about it if it should ever fail. | 99 // TimedWait can time out earlier than the specified |timeout| on |
83 NOTREACHED() << "WaitForSingleObject failed"; | 100 // Windows. To make this consistent with the posix implementation we |
101 // should guarantee that TimedWait doesn't return earlier than the | |
102 // specified |max_time| and wait again for the remaining time. | |
103 delay = target_time - TimeTicks::Now(); | |
104 break; | |
105 default: | |
106 // It is most unexpected that this should ever fail. Help consumers | |
107 // learn about it if it should ever fail. | |
108 NOTREACHED() << "WaitForSingleObject failed"; | |
danakj
2016/10/21 22:01:22
This falls into the NOTREACHED+return antipattern.
stanisc
2016/11/11 03:50:48
Good suggestion. Fixed.
| |
109 return false; | |
110 } | |
111 } while (delay > TimeDelta()); | |
84 return false; | 112 return false; |
85 } | 113 } |
86 | 114 |
87 // static | 115 // static |
88 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { | 116 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { |
89 DCHECK(count) << "Cannot wait on no events"; | 117 DCHECK(count) << "Cannot wait on no events"; |
90 | 118 |
91 // Record an event (the first) that this thread is blocking upon. | 119 // Record an event (the first) that this thread is blocking upon. |
92 base::debug::ScopedEventWaitActivity event_activity(events[0]); | 120 base::debug::ScopedEventWaitActivity event_activity(events[0]); |
93 | 121 |
(...skipping 13 matching lines...) Expand all Loading... | |
107 INFINITE); // no timeout | 135 INFINITE); // no timeout |
108 if (result >= WAIT_OBJECT_0 + count) { | 136 if (result >= WAIT_OBJECT_0 + count) { |
109 DPLOG(FATAL) << "WaitForMultipleObjects failed"; | 137 DPLOG(FATAL) << "WaitForMultipleObjects failed"; |
110 return 0; | 138 return 0; |
111 } | 139 } |
112 | 140 |
113 return result - WAIT_OBJECT_0; | 141 return result - WAIT_OBJECT_0; |
114 } | 142 } |
115 | 143 |
116 } // namespace base | 144 } // namespace base |
OLD | NEW |