blob: 346ae54c660615124a6ad7d0a8bc571b105e33dd [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 Silverman797e71e2013-09-06 17:29:39 -070017 // m is the mutex that will be associated with this condition variable. This
18 // object will hold on to a reference to it but does not take ownership.
Brian Silverman08661c72013-09-01 17:24:38 -070019 explicit Condition(Mutex *m);
Brian Silvermand41b4422013-09-01 14:02:33 -070020
Brian Silverman08661c72013-09-01 17:24:38 -070021 // Waits for the condition variable to be signalled, atomically unlocking m at
22 // the same time. The mutex associated with this condition variable must be
23 // locked when this is called and will be locked when this method returns.
24 void Wait();
25
26 // Signals at most 1 other process currently Wait()ing on this condition
27 // variable. Calling this does not require the mutex associated with this
28 // condition variable to be locked.
Brian Silverman797e71e2013-09-06 17:29:39 -070029 // One of the processes with the highest priority level will be woken.
Brian Silverman08661c72013-09-01 17:24:38 -070030 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
Brian Silverman797e71e2013-09-06 17:29:39 -070036 // Retrieves the mutex associated with this condition variable.
37 Mutex *m() { return m_; }
38
Brian Silvermand41b4422013-09-01 14:02:33 -070039 private:
Brian Silverman797e71e2013-09-06 17:29:39 -070040 mutex impl_;
Brian Silverman08661c72013-09-01 17:24:38 -070041 Mutex *m_;
Brian Silvermand41b4422013-09-01 14:02:33 -070042};
43
44} // namespace aos
45
46#endif // AOS_COMMON_CONDITION_H_