Chromium Code Reviews| 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 <windows.h> | 7 #include <windows.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/debug/activity_tracker.h" | |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 | 19 |
| 19 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) | 20 WaitableEvent::WaitableEvent(bool manual_reset, bool signaled) |
| 20 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { | 21 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) { |
| 21 // We're probably going to crash anyways if this is ever NULL, so we might as | 22 // We're probably going to crash anyways if this is ever NULL, so we might as |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 38 void WaitableEvent::Signal() { | 39 void WaitableEvent::Signal() { |
| 39 SetEvent(handle_.Get()); | 40 SetEvent(handle_.Get()); |
| 40 } | 41 } |
| 41 | 42 |
| 42 bool WaitableEvent::IsSignaled() { | 43 bool WaitableEvent::IsSignaled() { |
| 43 return TimedWait(TimeDelta()); | 44 return TimedWait(TimeDelta()); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void WaitableEvent::Wait() { | 47 void WaitableEvent::Wait() { |
| 47 base::ThreadRestrictions::AssertWaitAllowed(); | 48 base::ThreadRestrictions::AssertWaitAllowed(); |
| 49 base::debug::ScopedEventActivity event_activity(this); | |
| 48 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); | 50 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); |
| 49 // It is most unexpected that this should ever fail. Help consumers learn | 51 // It is most unexpected that this should ever fail. Help consumers learn |
| 50 // about it if it should ever fail. | 52 // about it if it should ever fail. |
| 51 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 53 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
| 52 } | 54 } |
| 53 | 55 |
| 54 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 56 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
| 55 base::ThreadRestrictions::AssertWaitAllowed(); | 57 base::ThreadRestrictions::AssertWaitAllowed(); |
| 58 base::debug::ScopedEventActivity event_activity(this); | |
| 56 DCHECK_GE(max_time, TimeDelta()); | 59 DCHECK_GE(max_time, TimeDelta()); |
| 57 // Truncate the timeout to milliseconds. The API specifies that this method | 60 // Truncate the timeout to milliseconds. The API specifies that this method |
| 58 // can return in less than |max_time| (when returning false), as the argument | 61 // can return in less than |max_time| (when returning false), as the argument |
| 59 // is the maximum time that a caller is willing to wait. | 62 // is the maximum time that a caller is willing to wait. |
| 60 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); | 63 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); |
| 61 | 64 |
| 62 DWORD result = WaitForSingleObject(handle_.Get(), timeout); | 65 DWORD result = WaitForSingleObject(handle_.Get(), 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) { |
| 77 base::ThreadRestrictions::AssertWaitAllowed(); | 80 base::ThreadRestrictions::AssertWaitAllowed(); |
| 81 base::debug::ScopedEventActivity event_activity(events[0]); | |
|
manzagop (departed)
2016/06/01 21:59:41
A comment for this?
bcwhite
2016/06/02 16:18:16
Done.
| |
| 78 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | 82 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 79 CHECK_LE(count, static_cast<size_t>(MAXIMUM_WAIT_OBJECTS)) | 83 CHECK_LE(count, static_cast<size_t>(MAXIMUM_WAIT_OBJECTS)) |
| 80 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; | 84 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; |
| 81 | 85 |
| 82 for (size_t i = 0; i < count; ++i) | 86 for (size_t i = 0; i < count; ++i) |
| 83 handles[i] = events[i]->handle(); | 87 handles[i] = events[i]->handle(); |
| 84 | 88 |
| 85 // The cast is safe because count is small - see the CHECK above. | 89 // The cast is safe because count is small - see the CHECK above. |
| 86 DWORD result = | 90 DWORD result = |
| 87 WaitForMultipleObjects(static_cast<DWORD>(count), | 91 WaitForMultipleObjects(static_cast<DWORD>(count), |
| 88 handles, | 92 handles, |
| 89 FALSE, // don't wait for all the objects | 93 FALSE, // don't wait for all the objects |
| 90 INFINITE); // no timeout | 94 INFINITE); // no timeout |
| 91 if (result >= WAIT_OBJECT_0 + count) { | 95 if (result >= WAIT_OBJECT_0 + count) { |
| 92 DPLOG(FATAL) << "WaitForMultipleObjects failed"; | 96 DPLOG(FATAL) << "WaitForMultipleObjects failed"; |
| 93 return 0; | 97 return 0; |
| 94 } | 98 } |
| 95 | 99 |
| 96 return result - WAIT_OBJECT_0; | 100 return result - WAIT_OBJECT_0; |
| 97 } | 101 } |
| 98 | 102 |
| 99 } // namespace base | 103 } // namespace base |
| OLD | NEW |