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> | 7 #include <math.h> |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.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_); | 20 CHECK(handle_.IsValid()); |
21 } | 21 } |
22 | 22 |
23 WaitableEvent::WaitableEvent(HANDLE handle) | 23 WaitableEvent::WaitableEvent(HANDLE handle) |
24 : handle_(handle) { | 24 : handle_(handle) { |
25 CHECK(handle) << "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 CloseHandle(handle_); | |
30 } | 29 } |
31 | 30 |
32 HANDLE WaitableEvent::Release() { | 31 HANDLE WaitableEvent::Release() { |
33 HANDLE rv = handle_; | 32 return handle_.Take(); |
34 handle_ = INVALID_HANDLE_VALUE; | |
35 return rv; | |
36 } | 33 } |
37 | 34 |
38 void WaitableEvent::Reset() { | 35 void WaitableEvent::Reset() { |
39 ResetEvent(handle_); | 36 ResetEvent(handle_.Get()); |
40 } | 37 } |
41 | 38 |
42 void WaitableEvent::Signal() { | 39 void WaitableEvent::Signal() { |
43 SetEvent(handle_); | 40 SetEvent(handle_.Get()); |
44 } | 41 } |
45 | 42 |
46 bool WaitableEvent::IsSignaled() { | 43 bool WaitableEvent::IsSignaled() { |
47 return TimedWait(TimeDelta::FromMilliseconds(0)); | 44 return TimedWait(TimeDelta::FromMilliseconds(0)); |
48 } | 45 } |
49 | 46 |
50 void WaitableEvent::Wait() { | 47 void WaitableEvent::Wait() { |
51 base::ThreadRestrictions::AssertWaitAllowed(); | 48 base::ThreadRestrictions::AssertWaitAllowed(); |
52 DWORD result = WaitForSingleObject(handle_, INFINITE); | 49 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); |
53 // It is most unexpected that this should ever fail. Help consumers learn | 50 // It is most unexpected that this should ever fail. Help consumers learn |
54 // about it if it should ever fail. | 51 // about it if it should ever fail. |
55 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 52 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
56 } | 53 } |
57 | 54 |
58 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 55 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
59 base::ThreadRestrictions::AssertWaitAllowed(); | 56 base::ThreadRestrictions::AssertWaitAllowed(); |
60 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); | 57 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); |
61 // Be careful here. TimeDelta has a precision of microseconds, but this API | 58 // Be careful here. TimeDelta has a precision of microseconds, but this API |
62 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? | 59 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? |
63 // It should be 6 to avoid returning too early. | 60 // It should be 6 to avoid returning too early. |
64 double timeout = ceil(max_time.InMillisecondsF()); | 61 double timeout = ceil(max_time.InMillisecondsF()); |
65 DWORD result = WaitForSingleObject(handle_, static_cast<DWORD>(timeout)); | 62 DWORD result = WaitForSingleObject(handle_.Get(), |
| 63 static_cast<DWORD>(timeout)); |
66 switch (result) { | 64 switch (result) { |
67 case WAIT_OBJECT_0: | 65 case WAIT_OBJECT_0: |
68 return true; | 66 return true; |
69 case WAIT_TIMEOUT: | 67 case WAIT_TIMEOUT: |
70 return false; | 68 return false; |
71 } | 69 } |
72 // It is most unexpected that this should ever fail. Help consumers learn | 70 // It is most unexpected that this should ever fail. Help consumers learn |
73 // about it if it should ever fail. | 71 // about it if it should ever fail. |
74 NOTREACHED() << "WaitForSingleObject failed"; | 72 NOTREACHED() << "WaitForSingleObject failed"; |
75 return false; | 73 return false; |
(...skipping 17 matching lines...) Expand all Loading... |
93 INFINITE); // no timeout | 91 INFINITE); // no timeout |
94 if (result >= WAIT_OBJECT_0 + count) { | 92 if (result >= WAIT_OBJECT_0 + count) { |
95 DPLOG(FATAL) << "WaitForMultipleObjects failed"; | 93 DPLOG(FATAL) << "WaitForMultipleObjects failed"; |
96 return 0; | 94 return 0; |
97 } | 95 } |
98 | 96 |
99 return result - WAIT_OBJECT_0; | 97 return result - WAIT_OBJECT_0; |
100 } | 98 } |
101 | 99 |
102 } // namespace base | 100 } // namespace base |
OLD | NEW |