| 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/waitable_event.h" | 5 #include "base/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/time.h" | 11 #include "base/time.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 | 14 |
| 15 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) | 15 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) |
| 16 : event_(CreateEvent(NULL, manual_reset, signaled, NULL)) { | 16 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { |
| 17 // We're probably going to crash anyways if this is ever NULL, so we might as | 17 // We're probably going to crash anyways if this is ever NULL, so we might as |
| 18 // well make our stack reports more informative by crashing here. | 18 // well make our stack reports more informative by crashing here. |
| 19 CHECK(event_); | 19 CHECK(handle_); |
| 20 } |
| 21 |
| 22 WaitableEvent::WaitableEvent(HANDLE handle) |
| 23 : handle_(handle) { |
| 24 CHECK(handle) << "Tried to create WaitableEvent from NULL handle"; |
| 20 } | 25 } |
| 21 | 26 |
| 22 WaitableEvent::~WaitableEvent() { | 27 WaitableEvent::~WaitableEvent() { |
| 23 CloseHandle(event_); | 28 CloseHandle(handle_); |
| 24 } | 29 } |
| 25 | 30 |
| 26 void WaitableEvent::Reset() { | 31 void WaitableEvent::Reset() { |
| 27 ResetEvent(event_); | 32 ResetEvent(handle_); |
| 28 } | 33 } |
| 29 | 34 |
| 30 void WaitableEvent::Signal() { | 35 void WaitableEvent::Signal() { |
| 31 SetEvent(event_); | 36 SetEvent(handle_); |
| 32 } | 37 } |
| 33 | 38 |
| 34 bool WaitableEvent::IsSignaled() { | 39 bool WaitableEvent::IsSignaled() { |
| 35 return TimedWait(TimeDelta::FromMilliseconds(0)); | 40 return TimedWait(TimeDelta::FromMilliseconds(0)); |
| 36 } | 41 } |
| 37 | 42 |
| 38 bool WaitableEvent::Wait() { | 43 bool WaitableEvent::Wait() { |
| 39 DWORD result = WaitForSingleObject(event_, INFINITE); | 44 DWORD result = WaitForSingleObject(handle_, INFINITE); |
| 40 // It is most unexpected that this should ever fail. Help consumers learn | 45 // It is most unexpected that this should ever fail. Help consumers learn |
| 41 // about it if it should ever fail. | 46 // about it if it should ever fail. |
| 42 DCHECK(result == WAIT_OBJECT_0) << "WaitForSingleObject failed"; | 47 DCHECK(result == WAIT_OBJECT_0) << "WaitForSingleObject failed"; |
| 43 return result == WAIT_OBJECT_0; | 48 return result == WAIT_OBJECT_0; |
| 44 } | 49 } |
| 45 | 50 |
| 46 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 51 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
| 47 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); | 52 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); |
| 48 // Be careful here. TimeDelta has a precision of microseconds, but this API | 53 // Be careful here. TimeDelta has a precision of microseconds, but this API |
| 49 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? | 54 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? |
| 50 // It should be 6 to avoid returning too early. | 55 // It should be 6 to avoid returning too early. |
| 51 double timeout = ceil(max_time.InMillisecondsF()); | 56 double timeout = ceil(max_time.InMillisecondsF()); |
| 52 DWORD result = WaitForSingleObject(event_, static_cast<DWORD>(timeout)); | 57 DWORD result = WaitForSingleObject(handle_, static_cast<DWORD>(timeout)); |
| 53 switch (result) { | 58 switch (result) { |
| 54 case WAIT_OBJECT_0: | 59 case WAIT_OBJECT_0: |
| 55 return true; | 60 return true; |
| 56 case WAIT_TIMEOUT: | 61 case WAIT_TIMEOUT: |
| 57 return false; | 62 return false; |
| 58 } | 63 } |
| 59 // It is most unexpected that this should ever fail. Help consumers learn | 64 // It is most unexpected that this should ever fail. Help consumers learn |
| 60 // about it if it should ever fail. | 65 // about it if it should ever fail. |
| 61 NOTREACHED() << "WaitForSingleObject failed"; | 66 NOTREACHED() << "WaitForSingleObject failed"; |
| 62 return false; | 67 return false; |
| 63 } | 68 } |
| 64 | 69 |
| 70 // static |
| 71 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { |
| 72 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 73 CHECK(count <= MAXIMUM_WAIT_OBJECTS) |
| 74 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; |
| 75 |
| 76 for (size_t i = 0; i < count; ++i) |
| 77 handles[i] = events[i]->handle(); |
| 78 |
| 79 DWORD result = |
| 80 WaitForMultipleObjects(count, handles, |
| 81 FALSE, // don't wait for all the objects |
| 82 INFINITE); // no timeout |
| 83 if (result < WAIT_OBJECT_0 || result >= WAIT_OBJECT_0 + count) { |
| 84 NOTREACHED() << "WaitForMultipleObjects failed: " << GetLastError(); |
| 85 return 0; |
| 86 } |
| 87 |
| 88 return result - WAIT_OBJECT_0; |
| 89 } |
| 90 |
| 65 } // namespace base | 91 } // namespace base |
| OLD | NEW |