| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_ | 5 #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_ |
| 6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_ | 6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 | 10 |
| 11 #if defined(OS_WIN) | |
| 12 #include "base/win/scoped_handle.h" | |
| 13 #endif | |
| 14 | |
| 15 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
| 16 #include <list> | 12 #include <list> |
| 17 #include <utility> | 13 #include <utility> |
| 18 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 19 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 20 #endif | 16 #endif |
| 21 | 17 |
| 22 namespace base { | 18 namespace base { |
| 23 | 19 |
| 24 class TimeDelta; | 20 class TimeDelta; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 39 // specific code and you need other features of a Windows event, then you might | 35 // specific code and you need other features of a Windows event, then you might |
| 40 // be better off just using an Windows event directly. | 36 // be better off just using an Windows event directly. |
| 41 class BASE_EXPORT WaitableEvent { | 37 class BASE_EXPORT WaitableEvent { |
| 42 public: | 38 public: |
| 43 // If manual_reset is true, then to set the event state to non-signaled, a | 39 // If manual_reset is true, then to set the event state to non-signaled, a |
| 44 // consumer must call the Reset method. If this parameter is false, then the | 40 // consumer must call the Reset method. If this parameter is false, then the |
| 45 // system automatically resets the event state to non-signaled after a single | 41 // system automatically resets the event state to non-signaled after a single |
| 46 // waiting thread has been released. | 42 // waiting thread has been released. |
| 47 WaitableEvent(bool manual_reset, bool initially_signaled); | 43 WaitableEvent(bool manual_reset, bool initially_signaled); |
| 48 | 44 |
| 49 #if defined(OS_WIN) | |
| 50 // Create a WaitableEvent from an Event HANDLE which has already been | |
| 51 // created. This objects takes ownership of the HANDLE and will close it when | |
| 52 // deleted. | |
| 53 explicit WaitableEvent(win::ScopedHandle event_handle); | |
| 54 #endif | |
| 55 | |
| 56 ~WaitableEvent(); | 45 ~WaitableEvent(); |
| 57 | 46 |
| 58 // Put the event in the un-signaled state. | 47 // Put the event in the un-signaled state. |
| 59 void Reset(); | 48 void Reset(); |
| 60 | 49 |
| 61 // Put the event in the signaled state. Causing any thread blocked on Wait | 50 // Put the event in the signaled state. Causing any thread blocked on Wait |
| 62 // to be woken up. | 51 // to be woken up. |
| 63 void Signal(); | 52 void Signal(); |
| 64 | 53 |
| 65 // Returns true if the event is in the signaled state, else false. If this | 54 // Returns true if the event is in the signaled state, else false. If this |
| (...skipping 10 matching lines...) Expand all Loading... |
| 76 // delete e; | 65 // delete e; |
| 77 void Wait(); | 66 void Wait(); |
| 78 | 67 |
| 79 // Wait up until max_time has passed for the event to be signaled. Returns | 68 // Wait up until max_time has passed for the event to be signaled. Returns |
| 80 // true if the event was signaled. If this method returns false, then it | 69 // true if the event was signaled. If this method returns false, then it |
| 81 // does not necessarily mean that max_time was exceeded. | 70 // does not necessarily mean that max_time was exceeded. |
| 82 // | 71 // |
| 83 // TimedWait can synchronise its own destruction like |Wait|. | 72 // TimedWait can synchronise its own destruction like |Wait|. |
| 84 bool TimedWait(const TimeDelta& max_time); | 73 bool TimedWait(const TimeDelta& max_time); |
| 85 | 74 |
| 86 #if defined(OS_WIN) | |
| 87 HANDLE handle() const { return handle_.Get(); } | |
| 88 #endif | |
| 89 | |
| 90 // Wait, synchronously, on multiple events. | 75 // Wait, synchronously, on multiple events. |
| 91 // waitables: an array of WaitableEvent pointers | 76 // waitables: an array of WaitableEvent pointers |
| 92 // count: the number of elements in @waitables | 77 // count: the number of elements in @waitables |
| 93 // | 78 // |
| 94 // returns: the index of a WaitableEvent which has been signaled. | 79 // returns: the index of a WaitableEvent which has been signaled. |
| 95 // | 80 // |
| 96 // You MUST NOT delete any of the WaitableEvent objects while this wait is | 81 // You MUST NOT delete any of the WaitableEvent objects while this wait is |
| 97 // happening, however WaitMany's return "happens after" the |Signal| call | 82 // happening, however WaitMany's return "happens after" the |Signal| call |
| 98 // that caused it has completed, like |Wait|. | 83 // that caused it has completed, like |Wait|. |
| 99 static size_t WaitMany(WaitableEvent** waitables, size_t count); | 84 static size_t WaitMany(WaitableEvent** waitables, size_t count); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 126 // comments in ~Handle for why. | 111 // comments in ~Handle for why. |
| 127 virtual bool Compare(void* tag) = 0; | 112 virtual bool Compare(void* tag) = 0; |
| 128 | 113 |
| 129 protected: | 114 protected: |
| 130 virtual ~Waiter() {} | 115 virtual ~Waiter() {} |
| 131 }; | 116 }; |
| 132 | 117 |
| 133 private: | 118 private: |
| 134 friend class WaitableEventWatcher; | 119 friend class WaitableEventWatcher; |
| 135 | 120 |
| 136 #if defined(OS_WIN) | |
| 137 win::ScopedHandle handle_; | |
| 138 #else | |
| 139 // On Windows, one can close a HANDLE which is currently being waited on. The | 121 // On Windows, one can close a HANDLE which is currently being waited on. The |
| 140 // MSDN documentation says that the resulting behaviour is 'undefined', but | 122 // MSDN documentation says that the resulting behaviour is 'undefined', but |
| 141 // it doesn't crash. However, if we were to include the following members | 123 // it doesn't crash. However, if we were to include the following members |
| 142 // directly then, on POSIX, one couldn't use WaitableEventWatcher to watch an | 124 // directly then, on POSIX, one couldn't use WaitableEventWatcher to watch an |
| 143 // event which gets deleted. This mismatch has bitten us several times now, | 125 // event which gets deleted. This mismatch has bitten us several times now, |
| 144 // so we have a kernel of the WaitableEvent, which is reference counted. | 126 // so we have a kernel of the WaitableEvent, which is reference counted. |
| 145 // WaitableEventWatchers may then take a reference and thus match the Windows | 127 // WaitableEventWatchers may then take a reference and thus match the Windows |
| 146 // behaviour. | 128 // behaviour. |
| 147 struct WaitableEventKernel : | 129 struct WaitableEventKernel : |
| 148 public RefCountedThreadSafe<WaitableEventKernel> { | 130 public RefCountedThreadSafe<WaitableEventKernel> { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 169 // second element is the index of the WaitableEvent in the original, | 151 // second element is the index of the WaitableEvent in the original, |
| 170 // unsorted, array. | 152 // unsorted, array. |
| 171 static size_t EnqueueMany(WaiterAndIndex* waitables, | 153 static size_t EnqueueMany(WaiterAndIndex* waitables, |
| 172 size_t count, Waiter* waiter); | 154 size_t count, Waiter* waiter); |
| 173 | 155 |
| 174 bool SignalAll(); | 156 bool SignalAll(); |
| 175 bool SignalOne(); | 157 bool SignalOne(); |
| 176 void Enqueue(Waiter* waiter); | 158 void Enqueue(Waiter* waiter); |
| 177 | 159 |
| 178 scoped_refptr<WaitableEventKernel> kernel_; | 160 scoped_refptr<WaitableEventKernel> kernel_; |
| 179 #endif | |
| 180 | 161 |
| 181 DISALLOW_COPY_AND_ASSIGN(WaitableEvent); | 162 DISALLOW_COPY_AND_ASSIGN(WaitableEvent); |
| 182 }; | 163 }; |
| 183 | 164 |
| 184 } // namespace base | 165 } // namespace base |
| 185 | 166 |
| 186 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_ | 167 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_ |
| OLD | NEW |