Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 1 | #ifndef AOS_IPC_LIB_EVENT_H_ |
| 2 | #define AOS_IPC_LIB_EVENT_H_ |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 3 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 4 | #include "aos/ipc_lib/aos_sync.h" |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 5 | #include "aos/time/time.h" |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 6 | |
| 7 | namespace aos { |
| 8 | |
| 9 | // An abstraction of an event which is easy to implement for Linux and in other |
| 10 | // environments. |
| 11 | // On Linux at least, this is definitely safe for passing through C code with |
| 12 | // memcpy etc. |
| 13 | // |
| 14 | // An event is either "set" or "unset". Any thread can transition it between |
| 15 | // these two states and other threads can wait for an unset->set transition. |
| 16 | // This is not a particularly powerful synchronization primitive, but it can be |
| 17 | // much easier to use than more complicated ones in some situations. The name is |
| 18 | // taken from Python's implementation of the same thing. |
| 19 | // |
| 20 | // An event is equivalent to a semaphore which is either set to 0 or infinity. |
| 21 | // It is also equivalent to a condition variable with the monitored condition |
| 22 | // being "is it set or not". |
| 23 | // |
| 24 | // IMPORTANT: You can NOT use this to successfully replace a standard condition |
| 25 | // variable in most cases. When the condition being monitored changes separately |
| 26 | // from the actual state of the condition variable/event, there WILL be race |
| 27 | // conditions if you try to use this class. |
| 28 | // |
| 29 | // It is undefined behavior to destroy an Event while there are current |
| 30 | // Wait()ers. |
| 31 | class Event { |
| 32 | public: |
| 33 | // Creates an unset event. |
| 34 | Event(); |
| 35 | // There must be no waiters when an Event is destroyed. |
| 36 | ~Event() = default; |
| 37 | |
| 38 | // Waits for the event to be set. Returns immediately if it is already set. |
| 39 | void Wait(); |
| 40 | |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 41 | // Waits for the event to be set or until timeout has elapsed. Returns |
| 42 | // immediately if it is already set. |
| 43 | // Returns true if the event was Set or false if the timeout expired. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 44 | bool WaitTimeout(monotonic_clock::duration timeout); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 45 | |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 46 | // Wakes up all Wait()ers and sets the event (atomically). |
| 47 | void Set(); |
| 48 | // Unsets the event so future Wait() callers will block instead of returning |
| 49 | // immediately. |
| 50 | // Returns true if the event was previously set. |
| 51 | bool Clear(); |
| 52 | |
| 53 | private: |
| 54 | aos_futex impl_; |
| 55 | }; |
| 56 | |
| 57 | } // namespace aos |
| 58 | |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 59 | #endif // AOS_IPC_LIB_EVENT_H_ |