blob: d8440e89733fa910c35dca409b4d45bc87079aa2 [file] [log] [blame]
Brian Silverman7b266d92021-02-17 21:24:02 -08001#ifndef AOS_IPC_LIB_EVENT_H_
2#define AOS_IPC_LIB_EVENT_H_
Brian Silverman30608942015-04-08 19:16:46 -04003
John Park398c74a2018-10-20 21:17:39 -07004#include "aos/ipc_lib/aos_sync.h"
Brian Silverman7b266d92021-02-17 21:24:02 -08005#include "aos/time/time.h"
Brian Silvermanf5f34902015-03-29 17:57:59 -04006
7namespace 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.
31class 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 Silverman30608942015-04-08 19:16:46 -040041 // 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 Schuhf2a50ba2016-12-24 16:16:26 -080044 bool WaitTimeout(monotonic_clock::duration timeout);
Brian Silverman30608942015-04-08 19:16:46 -040045
Brian Silvermanf5f34902015-03-29 17:57:59 -040046 // 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 Silverman7b266d92021-02-17 21:24:02 -080059#endif // AOS_IPC_LIB_EVENT_H_