| 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/time.h" | 12 #include "base/time.h" |
| 12 | 13 |
| 13 namespace base { | 14 namespace base { |
| 14 | 15 |
| 15 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) | 16 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) |
| 16 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { | 17 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { |
| 17 // 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 |
| 18 // well make our stack reports more informative by crashing here. | 19 // well make our stack reports more informative by crashing here. |
| 19 CHECK(handle_); | 20 CHECK(handle_); |
| 20 } | 21 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 40 | 41 |
| 41 void WaitableEvent::Signal() { | 42 void WaitableEvent::Signal() { |
| 42 SetEvent(handle_); | 43 SetEvent(handle_); |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool WaitableEvent::IsSignaled() { | 46 bool WaitableEvent::IsSignaled() { |
| 46 return TimedWait(TimeDelta::FromMilliseconds(0)); | 47 return TimedWait(TimeDelta::FromMilliseconds(0)); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void WaitableEvent::Wait() { | 50 void WaitableEvent::Wait() { |
| 51 base::ThreadRestrictions::AssertWaitAllowed(); |
| 50 DWORD result = WaitForSingleObject(handle_, INFINITE); | 52 DWORD result = WaitForSingleObject(handle_, INFINITE); |
| 51 // It is most unexpected that this should ever fail. Help consumers learn | 53 // It is most unexpected that this should ever fail. Help consumers learn |
| 52 // about it if it should ever fail. | 54 // about it if it should ever fail. |
| 53 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 55 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
| 54 } | 56 } |
| 55 | 57 |
| 56 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 58 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
| 59 base::ThreadRestrictions::AssertWaitAllowed(); |
| 57 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); | 60 DCHECK(max_time >= TimeDelta::FromMicroseconds(0)); |
| 58 // Be careful here. TimeDelta has a precision of microseconds, but this API | 61 // Be careful here. TimeDelta has a precision of microseconds, but this API |
| 59 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? | 62 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? |
| 60 // It should be 6 to avoid returning too early. | 63 // It should be 6 to avoid returning too early. |
| 61 double timeout = ceil(max_time.InMillisecondsF()); | 64 double timeout = ceil(max_time.InMillisecondsF()); |
| 62 DWORD result = WaitForSingleObject(handle_, static_cast<DWORD>(timeout)); | 65 DWORD result = WaitForSingleObject(handle_, static_cast<DWORD>(timeout)); |
| 63 switch (result) { | 66 switch (result) { |
| 64 case WAIT_OBJECT_0: | 67 case WAIT_OBJECT_0: |
| 65 return true; | 68 return true; |
| 66 case WAIT_TIMEOUT: | 69 case WAIT_TIMEOUT: |
| 67 return false; | 70 return false; |
| 68 } | 71 } |
| 69 // It is most unexpected that this should ever fail. Help consumers learn | 72 // It is most unexpected that this should ever fail. Help consumers learn |
| 70 // about it if it should ever fail. | 73 // about it if it should ever fail. |
| 71 NOTREACHED() << "WaitForSingleObject failed"; | 74 NOTREACHED() << "WaitForSingleObject failed"; |
| 72 return false; | 75 return false; |
| 73 } | 76 } |
| 74 | 77 |
| 75 // static | 78 // static |
| 76 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { | 79 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { |
| 80 base::ThreadRestrictions::AssertWaitAllowed(); |
| 77 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | 81 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 78 CHECK_LE(count, MAXIMUM_WAIT_OBJECTS) | 82 CHECK_LE(count, MAXIMUM_WAIT_OBJECTS) |
| 79 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; | 83 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; |
| 80 | 84 |
| 81 for (size_t i = 0; i < count; ++i) | 85 for (size_t i = 0; i < count; ++i) |
| 82 handles[i] = events[i]->handle(); | 86 handles[i] = events[i]->handle(); |
| 83 | 87 |
| 84 // The cast is safe because count is small - see the CHECK above. | 88 // The cast is safe because count is small - see the CHECK above. |
| 85 DWORD result = | 89 DWORD result = |
| 86 WaitForMultipleObjects(static_cast<DWORD>(count), | 90 WaitForMultipleObjects(static_cast<DWORD>(count), |
| 87 handles, | 91 handles, |
| 88 FALSE, // don't wait for all the objects | 92 FALSE, // don't wait for all the objects |
| 89 INFINITE); // no timeout | 93 INFINITE); // no timeout |
| 90 if (result >= WAIT_OBJECT_0 + count) { | 94 if (result >= WAIT_OBJECT_0 + count) { |
| 91 NOTREACHED() << "WaitForMultipleObjects failed: " << GetLastError(); | 95 NOTREACHED() << "WaitForMultipleObjects failed: " << GetLastError(); |
| 92 return 0; | 96 return 0; |
| 93 } | 97 } |
| 94 | 98 |
| 95 return result - WAIT_OBJECT_0; | 99 return result - WAIT_OBJECT_0; |
| 96 } | 100 } |
| 97 | 101 |
| 98 } // namespace base | 102 } // namespace base |
| OLD | NEW |