[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/waitable_event.h" |
| 6 | |
| 7 | #include <math.h> |
| 8 | #include <windows.h> |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/time.h" |
| 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 16 | : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 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. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 19 | CHECK(handle_); |
| 20 | } |
| 21 | |
| 22 | WaitableEvent::WaitableEvent(HANDLE handle) |
| 23 | : handle_(handle) { |
| 24 | CHECK(handle) << "Tried to create WaitableEvent from NULL handle"; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | WaitableEvent::~WaitableEvent() { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 28 | CloseHandle(handle_); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 05f007c2 | 2009-03-06 01:21:30 | [diff] [blame] | 31 | HANDLE WaitableEvent::Release() { |
| 32 | HANDLE rv = handle_; |
| 33 | handle_ = INVALID_HANDLE_VALUE; |
| 34 | return rv; |
| 35 | } |
| 36 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 37 | void WaitableEvent::Reset() { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 38 | ResetEvent(handle_); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void WaitableEvent::Signal() { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 42 | SetEvent(handle_); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool WaitableEvent::IsSignaled() { |
| 46 | return TimedWait(TimeDelta::FromMilliseconds(0)); |
| 47 | } |
| 48 | |
| 49 | bool WaitableEvent::Wait() { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 50 | DWORD result = WaitForSingleObject(handle_, INFINITE); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 51 | // It is most unexpected that this should ever fail. Help consumers learn |
| 52 | // about it if it should ever fail. |
| 53 | DCHECK(result == WAIT_OBJECT_0) << "WaitForSingleObject failed"; |
| 54 | return result == WAIT_OBJECT_0; |
| 55 | } |
| 56 | |
| 57 | bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
| 58 | DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); |
| 59 | // Be careful here. TimeDelta has a precision of microseconds, but this API |
| 60 | // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? |
| 61 | // It should be 6 to avoid returning too early. |
| 62 | double timeout = ceil(max_time.InMillisecondsF()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 63 | DWORD result = WaitForSingleObject(handle_, static_cast<DWORD>(timeout)); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 64 | switch (result) { |
| 65 | case WAIT_OBJECT_0: |
| 66 | return true; |
| 67 | case WAIT_TIMEOUT: |
| 68 | return false; |
| 69 | } |
| 70 | // It is most unexpected that this should ever fail. Help consumers learn |
| 71 | // about it if it should ever fail. |
| 72 | NOTREACHED() << "WaitForSingleObject failed"; |
| 73 | return false; |
| 74 | } |
| 75 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 76 | // static |
| 77 | size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { |
| 78 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 79 | CHECK(count <= MAXIMUM_WAIT_OBJECTS) |
| 80 | << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; |
| 81 | |
| 82 | for (size_t i = 0; i < count; ++i) |
| 83 | handles[i] = events[i]->handle(); |
| 84 | |
| 85 | DWORD result = |
| 86 | WaitForMultipleObjects(count, handles, |
| 87 | FALSE, // don't wait for all the objects |
| 88 | INFINITE); // no timeout |
| 89 | if (result < WAIT_OBJECT_0 || result >= WAIT_OBJECT_0 + count) { |
| 90 | NOTREACHED() << "WaitForMultipleObjects failed: " << GetLastError(); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | return result - WAIT_OBJECT_0; |
| 95 | } |
| 96 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 97 | } // namespace base |