mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_WAITABLE_EVENT_H_ |
| 6 | #define BASE_WAITABLE_EVENT_H_ |
| 7 | |
| 8 | #include "base/basictypes.h" |
| 9 | |
| 10 | #if defined(OS_WIN) |
| 11 | typedef void* HANDLE; |
| 12 | #else |
| 13 | #include "base/condition_variable.h" |
| 14 | #include "base/lock.h" |
| 15 | #endif |
| 16 | |
| 17 | class TimeDelta; |
| 18 | |
| 19 | namespace base { |
| 20 | |
| 21 | // A WaitableEvent can be a useful thread synchronization tool when you want to |
| 22 | // allow one thread to wait for another thread to finish some work. |
| 23 | // |
| 24 | // Use a WaitableEvent when you would otherwise use a Lock+ConditionVariable to |
| 25 | // protect a simple boolean value. However, if you find yourself using a |
| 26 | // WaitableEvent in conjunction with a Lock to wait for a more complex state |
| 27 | // change (e.g., for an item to be added to a queue), then you should probably |
| 28 | // be using a ConditionVariable instead of a WaitableEvent. |
| 29 | // |
| 30 | // NOTE: On Windows, this class provides a subset of the functionality afforded |
| 31 | // by a Windows event object. This is intentional. If you are writing Windows |
| 32 | // specific code and you need other features of a Windows event, then you might |
| 33 | // be better off just using an Windows event directly. |
| 34 | // |
| 35 | class WaitableEvent { |
| 36 | public: |
| 37 | // If manual_reset is true, then to set the event state to non-signaled, a |
| 38 | // consumer must call the Reset method. If this parameter is false, then the |
| 39 | // system automatically resets the event state to non-signaled after a single |
| 40 | // waiting thread has been released. |
| 41 | WaitableEvent(bool manual_reset, bool initially_signaled); |
| 42 | |
| 43 | // WARNING: Destroying a WaitableEvent while threads are waiting on it is not |
| 44 | // supported. Doing so will cause crashes or other instability. |
| 45 | ~WaitableEvent(); |
| 46 | |
| 47 | // Put the event in the un-signaled state. |
| 48 | void Reset(); |
| 49 | |
| 50 | // Put the event in the signaled state. Causing any thread blocked on Wait |
| 51 | // to be woken up. |
| 52 | void Signal(); |
| 53 | |
| 54 | // Returns true if the event is in the signaled state, else false. If this |
| 55 | // is not a manual reset event, then this test will cause a reset. |
| 56 | bool IsSignaled(); |
| 57 | |
| 58 | // Wait indefinitely for the event to be signaled. Returns true if the event |
| 59 | // was signaled, else false is returned to indicate that waiting failed. |
| 60 | bool Wait(); |
| 61 | |
| 62 | // Wait up until max_time has passed for the event to be signaled. Returns |
| 63 | // true if the event was signaled. If this method returns false, then it |
| 64 | // does not necessarily mean that max_time was exceeded. |
| 65 | bool TimedWait(const TimeDelta& max_time); |
| 66 | |
| 67 | private: |
| 68 | #if defined(OS_WIN) |
| 69 | HANDLE event_; |
| 70 | #else |
| 71 | Lock lock_; // Needs to be listed first so it will be constructed first. |
| 72 | ConditionVariable cvar_; |
| 73 | bool signaled_; |
| 74 | bool manual_reset_; |
| 75 | #endif |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(WaitableEvent); |
| 78 | }; |
| 79 | |
| 80 | } // namespace base |
| 81 | |
| 82 | #endif // BASE_WAITABLE_EVENT_H_ |
| 83 | |