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 <math.h> | |
8 #include <windows.h> | 7 #include <windows.h> |
9 | 8 |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/numerics/safe_conversions.h" |
11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) | 16 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) |
17 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { | 17 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { |
18 // We're probably going to crash anyways if this is ever NULL, so we might as | 18 // We're probably going to crash anyways if this is ever NULL, so we might as |
19 // well make our stack reports more informative by crashing here. | 19 // well make our stack reports more informative by crashing here. |
20 CHECK(handle_.IsValid()); | 20 CHECK(handle_.IsValid()); |
21 } | 21 } |
22 | 22 |
23 WaitableEvent::WaitableEvent(win::ScopedHandle handle) | 23 WaitableEvent::WaitableEvent(win::ScopedHandle handle) |
24 : handle_(handle.Pass()) { | 24 : handle_(handle.Pass()) { |
25 CHECK(handle_.IsValid()) << "Tried to create WaitableEvent from NULL handle"; | 25 CHECK(handle_.IsValid()) << "Tried to create WaitableEvent from NULL handle"; |
26 } | 26 } |
27 | 27 |
28 WaitableEvent::~WaitableEvent() { | 28 WaitableEvent::~WaitableEvent() { |
29 } | 29 } |
30 | 30 |
31 void WaitableEvent::Reset() { | 31 void WaitableEvent::Reset() { |
32 ResetEvent(handle_.Get()); | 32 ResetEvent(handle_.Get()); |
33 } | 33 } |
34 | 34 |
35 void WaitableEvent::Signal() { | 35 void WaitableEvent::Signal() { |
36 SetEvent(handle_.Get()); | 36 SetEvent(handle_.Get()); |
37 } | 37 } |
38 | 38 |
39 bool WaitableEvent::IsSignaled() { | 39 bool WaitableEvent::IsSignaled() { |
40 return TimedWait(TimeDelta::FromMilliseconds(0)); | 40 return TimedWait(TimeDelta()); |
41 } | 41 } |
42 | 42 |
43 void WaitableEvent::Wait() { | 43 void WaitableEvent::Wait() { |
44 base::ThreadRestrictions::AssertWaitAllowed(); | 44 base::ThreadRestrictions::AssertWaitAllowed(); |
45 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); | 45 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); |
46 // It is most unexpected that this should ever fail. Help consumers learn | 46 // It is most unexpected that this should ever fail. Help consumers learn |
47 // about it if it should ever fail. | 47 // about it if it should ever fail. |
48 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 48 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
49 } | 49 } |
50 | 50 |
51 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 51 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
52 base::ThreadRestrictions::AssertWaitAllowed(); | 52 base::ThreadRestrictions::AssertWaitAllowed(); |
53 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); | 53 DCHECK_GE(max_time, TimeDelta()); |
54 // Be careful here. TimeDelta has a precision of microseconds, but this API | 54 // Truncate the timeout to milliseconds. The API specifies that this method |
55 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? | 55 // can return in less than |max_time| (when returning false), as the argument |
56 // It should be 6 to avoid returning too early. | 56 // is the maximum time that a caller is willing to wait. |
57 double timeout = ceil(max_time.InMillisecondsF()); | 57 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); |
58 DWORD result = WaitForSingleObject(handle_.Get(), | 58 |
59 static_cast<DWORD>(timeout)); | 59 DWORD result = WaitForSingleObject(handle_.Get(), timeout); |
60 switch (result) { | 60 switch (result) { |
61 case WAIT_OBJECT_0: | 61 case WAIT_OBJECT_0: |
62 return true; | 62 return true; |
63 case WAIT_TIMEOUT: | 63 case WAIT_TIMEOUT: |
64 return false; | 64 return false; |
65 } | 65 } |
66 // It is most unexpected that this should ever fail. Help consumers learn | 66 // It is most unexpected that this should ever fail. Help consumers learn |
67 // about it if it should ever fail. | 67 // about it if it should ever fail. |
68 NOTREACHED() << "WaitForSingleObject failed"; | 68 NOTREACHED() << "WaitForSingleObject failed"; |
69 return false; | 69 return false; |
(...skipping 17 matching lines...) Expand all Loading... |
87 INFINITE); // no timeout | 87 INFINITE); // no timeout |
88 if (result >= WAIT_OBJECT_0 + count) { | 88 if (result >= WAIT_OBJECT_0 + count) { |
89 DPLOG(FATAL) << "WaitForMultipleObjects failed"; | 89 DPLOG(FATAL) << "WaitForMultipleObjects failed"; |
90 return 0; | 90 return 0; |
91 } | 91 } |
92 | 92 |
93 return result - WAIT_OBJECT_0; | 93 return result - WAIT_OBJECT_0; |
94 } | 94 } |
95 | 95 |
96 } // namespace base | 96 } // namespace base |
OLD | NEW |