blob: 956dc6c60aed34e9e8b3398fbe2a79f0ce4bf3b7 [file] [log] [blame]
Brian Silvermand41b4422013-09-01 14:02:33 -07001#ifndef AOS_COMMON_CONDITION_H_
2#define AOS_COMMON_CONDITION_H_
3
Brian Silvermand41b4422013-09-01 14:02:33 -07004#include "aos/common/mutex.h"
Brian Silverman08661c72013-09-01 17:24:38 -07005#include "aos/atom_code/ipc_lib/aos_sync.h"
Brian Silvermand41b4422013-09-01 14:02:33 -07006
7namespace aos {
8
9// A condition variable (IPC mechanism where 1 process/task can notify all
10// others that are waiting for something to happen).
Brian Silverman08661c72013-09-01 17:24:38 -070011// This implementation will LOG(FATAL) if anything weird happens.
Brian Silvermand41b4422013-09-01 14:02:33 -070012//
Brian Silverman08661c72013-09-01 17:24:38 -070013// Multiple condition variables may be associated with the same mutex but
14// exactly 1 mutex must be associated with each condition variable.
Brian Silvermand41b4422013-09-01 14:02:33 -070015class Condition {
16 public:
Brian Silverman08661c72013-09-01 17:24:38 -070017 // m is the mutex that will be associated with this condition variable.
18 explicit Condition(Mutex *m);
Brian Silvermand41b4422013-09-01 14:02:33 -070019
Brian Silverman08661c72013-09-01 17:24:38 -070020 // Waits for the condition variable to be signalled, atomically unlocking m at
21 // the same time. The mutex associated with this condition variable must be
22 // locked when this is called and will be locked when this method returns.
23 void Wait();
24
25 // Signals at most 1 other process currently Wait()ing on this condition
26 // variable. Calling this does not require the mutex associated with this
27 // condition variable to be locked.
28 // One of the processes with the highest priority level will be woken if there
29 // are multiple ones.
30 void Signal();
31 // Wakes all processes that are currently Wait()ing on this condition
32 // variable. Calling this does not require the mutex associated with this
33 // condition variable to be locked.
34 void Broadcast();
Brian Silvermand41b4422013-09-01 14:02:33 -070035
36 private:
Brian Silverman08661c72013-09-01 17:24:38 -070037 condition_variable impl_;
38 Mutex *m_;
Brian Silvermand41b4422013-09-01 14:02:33 -070039};
40
41} // namespace aos
42
43#endif // AOS_COMMON_CONDITION_H_