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" | |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
14 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
15 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
16 #include "base/time/time.h" | 15 #include "base/time/time.h" |
17 | 16 |
18 namespace base { | 17 namespace base { |
19 | 18 |
20 WaitableEvent::WaitableEvent(ResetPolicy reset_policy, | 19 WaitableEvent::WaitableEvent(ResetPolicy reset_policy, |
21 InitialState initial_state) | 20 InitialState initial_state) |
22 : handle_(CreateEvent(nullptr, | 21 : handle_(CreateEvent(nullptr, |
(...skipping 18 matching lines...) Expand all Loading... |
41 | 40 |
42 void WaitableEvent::Signal() { | 41 void WaitableEvent::Signal() { |
43 SetEvent(handle_.Get()); | 42 SetEvent(handle_.Get()); |
44 } | 43 } |
45 | 44 |
46 bool WaitableEvent::IsSignaled() { | 45 bool WaitableEvent::IsSignaled() { |
47 return TimedWait(TimeDelta()); | 46 return TimedWait(TimeDelta()); |
48 } | 47 } |
49 | 48 |
50 void WaitableEvent::Wait() { | 49 void WaitableEvent::Wait() { |
51 // Record the event that this thread is blocking upon (for hang diagnosis). | |
52 base::debug::ScopedEventWaitActivity event_activity(this); | |
53 | |
54 base::ThreadRestrictions::AssertWaitAllowed(); | 50 base::ThreadRestrictions::AssertWaitAllowed(); |
55 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); | 51 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); |
56 // It is most unexpected that this should ever fail. Help consumers learn | 52 // It is most unexpected that this should ever fail. Help consumers learn |
57 // about it if it should ever fail. | 53 // about it if it should ever fail. |
58 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 54 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
59 } | 55 } |
60 | 56 |
61 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 57 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
62 // Record the event that this thread is blocking upon (for hang diagnosis). | |
63 base::debug::ScopedEventWaitActivity event_activity(this); | |
64 | |
65 base::ThreadRestrictions::AssertWaitAllowed(); | 58 base::ThreadRestrictions::AssertWaitAllowed(); |
66 DCHECK_GE(max_time, TimeDelta()); | 59 DCHECK_GE(max_time, TimeDelta()); |
67 // Truncate the timeout to milliseconds. The API specifies that this method | 60 // Truncate the timeout to milliseconds. The API specifies that this method |
68 // 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 |
69 // is the maximum time that a caller is willing to wait. | 62 // is the maximum time that a caller is willing to wait. |
70 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); | 63 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); |
71 | 64 |
72 DWORD result = WaitForSingleObject(handle_.Get(), timeout); | 65 DWORD result = WaitForSingleObject(handle_.Get(), timeout); |
73 switch (result) { | 66 switch (result) { |
74 case WAIT_OBJECT_0: | 67 case WAIT_OBJECT_0: |
75 return true; | 68 return true; |
76 case WAIT_TIMEOUT: | 69 case WAIT_TIMEOUT: |
77 return false; | 70 return false; |
78 } | 71 } |
79 // 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 |
80 // about it if it should ever fail. | 73 // about it if it should ever fail. |
81 NOTREACHED() << "WaitForSingleObject failed"; | 74 NOTREACHED() << "WaitForSingleObject failed"; |
82 return false; | 75 return false; |
83 } | 76 } |
84 | 77 |
85 // static | 78 // static |
86 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { | 79 size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) { |
87 DCHECK(count) << "Cannot wait on no events"; | |
88 | |
89 // Record an event (the first) that this thread is blocking upon. | |
90 base::debug::ScopedEventWaitActivity event_activity(events[0]); | |
91 | |
92 base::ThreadRestrictions::AssertWaitAllowed(); | 80 base::ThreadRestrictions::AssertWaitAllowed(); |
93 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | 81 HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
94 CHECK_LE(count, static_cast<size_t>(MAXIMUM_WAIT_OBJECTS)) | 82 CHECK_LE(count, static_cast<size_t>(MAXIMUM_WAIT_OBJECTS)) |
95 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; | 83 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany"; |
96 | 84 |
97 for (size_t i = 0; i < count; ++i) | 85 for (size_t i = 0; i < count; ++i) |
98 handles[i] = events[i]->handle(); | 86 handles[i] = events[i]->handle(); |
99 | 87 |
100 // 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. |
101 DWORD result = | 89 DWORD result = |
102 WaitForMultipleObjects(static_cast<DWORD>(count), | 90 WaitForMultipleObjects(static_cast<DWORD>(count), |
103 handles, | 91 handles, |
104 FALSE, // don't wait for all the objects | 92 FALSE, // don't wait for all the objects |
105 INFINITE); // no timeout | 93 INFINITE); // no timeout |
106 if (result >= WAIT_OBJECT_0 + count) { | 94 if (result >= WAIT_OBJECT_0 + count) { |
107 DPLOG(FATAL) << "WaitForMultipleObjects failed"; | 95 DPLOG(FATAL) << "WaitForMultipleObjects failed"; |
108 return 0; | 96 return 0; |
109 } | 97 } |
110 | 98 |
111 return result - WAIT_OBJECT_0; | 99 return result - WAIT_OBJECT_0; |
112 } | 100 } |
113 | 101 |
114 } // namespace base | 102 } // namespace base |
OLD | NEW |