| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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_WAITABLE_EVENT_H_ | 5 #ifndef BASE_WAITABLE_EVENT_H_ |
| 6 #define BASE_WAITABLE_EVENT_H_ | 6 #define BASE_WAITABLE_EVENT_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 | 9 |
| 10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| 11 #include <windows.h> | 11 #include <windows.h> |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
| 15 #include <list> | 15 #include <list> |
| 16 #include <utility> | 16 #include <utility> |
| 17 #include "base/condition_variable.h" | 17 #include "base/condition_variable.h" |
| 18 #include "base/lock.h" | 18 #include "base/lock.h" |
| 19 #include "base/ref_counted.h" |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 | 25 |
| 25 // This replaces INFINITE from Win32 | 26 // This replaces INFINITE from Win32 |
| 26 static const int kNoTimeout = -1; | 27 static const int kNoTimeout = -1; |
| 27 | 28 |
| 28 class TimeDelta; | 29 class TimeDelta; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 53 #if defined(OS_WIN) | 54 #if defined(OS_WIN) |
| 54 // Create a WaitableEvent from an Event HANDLE which has already been | 55 // Create a WaitableEvent from an Event HANDLE which has already been |
| 55 // created. This objects takes ownership of the HANDLE and will close it when | 56 // created. This objects takes ownership of the HANDLE and will close it when |
| 56 // deleted. | 57 // deleted. |
| 57 explicit WaitableEvent(HANDLE event_handle); | 58 explicit WaitableEvent(HANDLE event_handle); |
| 58 | 59 |
| 59 // Releases ownership of the handle from this object. | 60 // Releases ownership of the handle from this object. |
| 60 HANDLE Release(); | 61 HANDLE Release(); |
| 61 #endif | 62 #endif |
| 62 | 63 |
| 63 // WARNING: Destroying a WaitableEvent while threads are waiting on it is not | |
| 64 // supported. Doing so will cause crashes or other instability. | |
| 65 ~WaitableEvent(); | 64 ~WaitableEvent(); |
| 66 | 65 |
| 67 // Put the event in the un-signaled state. | 66 // Put the event in the un-signaled state. |
| 68 void Reset(); | 67 void Reset(); |
| 69 | 68 |
| 70 // Put the event in the signaled state. Causing any thread blocked on Wait | 69 // Put the event in the signaled state. Causing any thread blocked on Wait |
| 71 // to be woken up. | 70 // to be woken up. |
| 72 void Signal(); | 71 void Signal(); |
| 73 | 72 |
| 74 // Returns true if the event is in the signaled state, else false. If this | 73 // Returns true if the event is in the signaled state, else false. If this |
| (...skipping 11 matching lines...) Expand all Loading... |
| 86 | 85 |
| 87 #if defined(OS_WIN) | 86 #if defined(OS_WIN) |
| 88 HANDLE handle() const { return handle_; } | 87 HANDLE handle() const { return handle_; } |
| 89 #endif | 88 #endif |
| 90 | 89 |
| 91 // Wait, synchronously, on multiple events. | 90 // Wait, synchronously, on multiple events. |
| 92 // waitables: an array of WaitableEvent pointers | 91 // waitables: an array of WaitableEvent pointers |
| 93 // count: the number of elements in @waitables | 92 // count: the number of elements in @waitables |
| 94 // | 93 // |
| 95 // returns: the index of a WaitableEvent which has been signaled. | 94 // returns: the index of a WaitableEvent which has been signaled. |
| 95 // |
| 96 // You MUST NOT delete any of the WaitableEvent objects while this wait is |
| 97 // happening. |
| 96 static size_t WaitMany(WaitableEvent** waitables, size_t count); | 98 static size_t WaitMany(WaitableEvent** waitables, size_t count); |
| 97 | 99 |
| 98 // For asynchronous waiting, see WaitableEventWatcher | 100 // For asynchronous waiting, see WaitableEventWatcher |
| 99 | 101 |
| 100 // This is a private helper class. It's here because it's used by friends of | 102 // This is a private helper class. It's here because it's used by friends of |
| 101 // this class (such as WaitableEventWatcher) to be able to enqueue elements | 103 // this class (such as WaitableEventWatcher) to be able to enqueue elements |
| 102 // of the wait-list | 104 // of the wait-list |
| 103 class Waiter { | 105 class Waiter { |
| 104 public: | 106 public: |
| 105 // Signal the waiter to wake up. | 107 // Signal the waiter to wake up. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 123 // comments in ~Handle for why. | 125 // comments in ~Handle for why. |
| 124 virtual bool Compare(void* tag) = 0; | 126 virtual bool Compare(void* tag) = 0; |
| 125 }; | 127 }; |
| 126 | 128 |
| 127 private: | 129 private: |
| 128 friend class WaitableEventWatcher; | 130 friend class WaitableEventWatcher; |
| 129 | 131 |
| 130 #if defined(OS_WIN) | 132 #if defined(OS_WIN) |
| 131 HANDLE handle_; | 133 HANDLE handle_; |
| 132 #else | 134 #else |
| 135 // On Windows, one can close a HANDLE which is currently being waited on. The |
| 136 // MSDN documentation says that the resulting behaviour is 'undefined', but |
| 137 // it doesn't crash. However, if we were to include the following members |
| 138 // directly then, on POSIX, one couldn't use WaitableEventWatcher to watch an |
| 139 // event which gets deleted. This mismatch has bitten us several times now, |
| 140 // so we have a kernel of the WaitableEvent, which is reference counted. |
| 141 // WaitableEventWatchers may then take a reference and thus match the Windows |
| 142 // behaviour. |
| 143 struct WaitableEventKernel : |
| 144 public RefCountedThreadSafe<WaitableEventKernel> { |
| 145 public: |
| 146 WaitableEventKernel(bool manual_reset, bool initially_signaled) |
| 147 : manual_reset_(manual_reset), |
| 148 signaled_(initially_signaled) { |
| 149 } |
| 150 |
| 151 bool Dequeue(Waiter* waiter, void* tag); |
| 152 |
| 153 Lock lock_; |
| 154 bool signaled_; |
| 155 const bool manual_reset_; |
| 156 std::list<Waiter*> waiters_; |
| 157 }; |
| 158 |
| 159 scoped_refptr<WaitableEventKernel> kernel_; |
| 160 |
| 133 bool SignalAll(); | 161 bool SignalAll(); |
| 134 bool SignalOne(); | 162 bool SignalOne(); |
| 135 void Enqueue(Waiter* waiter); | 163 void Enqueue(Waiter* waiter); |
| 136 bool Dequeue(Waiter* waiter, void* tag); | |
| 137 | 164 |
| 138 // When dealing with arrays of WaitableEvent*, we want to sort by the address | 165 // When dealing with arrays of WaitableEvent*, we want to sort by the address |
| 139 // of the WaitableEvent in order to have a globally consistent locking order. | 166 // of the WaitableEvent in order to have a globally consistent locking order. |
| 140 // In that case we keep them, in sorted order, in an array of pairs where the | 167 // In that case we keep them, in sorted order, in an array of pairs where the |
| 141 // second element is the index of the WaitableEvent in the original, | 168 // second element is the index of the WaitableEvent in the original, |
| 142 // unsorted, array. | 169 // unsorted, array. |
| 143 typedef std::pair<WaitableEvent*, size_t> WaiterAndIndex; | 170 typedef std::pair<WaitableEvent*, size_t> WaiterAndIndex; |
| 144 static size_t EnqueueMany(WaiterAndIndex* waitables, | 171 static size_t EnqueueMany(WaiterAndIndex* waitables, |
| 145 size_t count, Waiter* waiter); | 172 size_t count, Waiter* waiter); |
| 146 | |
| 147 Lock lock_; | |
| 148 bool signaled_; | |
| 149 const bool manual_reset_; | |
| 150 std::list<Waiter*> waiters_; | |
| 151 #endif | 173 #endif |
| 152 | 174 |
| 153 DISALLOW_COPY_AND_ASSIGN(WaitableEvent); | 175 DISALLOW_COPY_AND_ASSIGN(WaitableEvent); |
| 154 }; | 176 }; |
| 155 | 177 |
| 156 } // namespace base | 178 } // namespace base |
| 157 | 179 |
| 158 #endif // BASE_WAITABLE_EVENT_H_ | 180 #endif // BASE_WAITABLE_EVENT_H_ |
| OLD | NEW |