Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 1 | #ifndef AOS_COMMON_CONDITION_H_ |
| 2 | #define AOS_COMMON_CONDITION_H_ |
| 3 | |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 4 | #include "aos/common/mutex.h" |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 5 | #include "aos/atom_code/ipc_lib/aos_sync.h" |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 6 | |
| 7 | namespace 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 11 | // This implementation will LOG(FATAL) if anything weird happens. |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 12 | // |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 13 | // Multiple condition variables may be associated with the same mutex but |
| 14 | // exactly 1 mutex must be associated with each condition variable. |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 15 | class Condition { |
| 16 | public: |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 17 | // 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 19 | explicit Condition(Mutex *m); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 20 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 21 | // 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 29 | // One of the processes with the highest priority level will be woken. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 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 Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 35 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 36 | // Retrieves the mutex associated with this condition variable. |
| 37 | Mutex *m() { return m_; } |
| 38 | |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 39 | private: |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame^] | 40 | mutex impl_; |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 41 | Mutex *m_; |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | } // namespace aos |
| 45 | |
| 46 | #endif // AOS_COMMON_CONDITION_H_ |