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 | |
| 4 | #ifdef __VXWORKS__ |
| 5 | #include <semLib.h> |
| 6 | #endif |
| 7 | |
| 8 | #include "aos/aos_core.h" |
| 9 | #include "aos/common/mutex.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | // A condition variable (IPC mechanism where 1 process/task can notify all |
| 14 | // others that are waiting for something to happen). |
| 15 | // There are implementations for both the atom and the cRIO. |
| 16 | // They both LOG(FATAL) if anything weird happens. |
| 17 | // |
| 18 | // A condition is either set or unset, and multiple processes/tasks can wait on |
| 19 | // one for it to be set. |
| 20 | class Condition { |
| 21 | public: |
| 22 | // Creates an unset condition. |
| 23 | Condition(); |
| 24 | #ifdef __VXWORKS__ |
| 25 | // Will not make sure that it is either set or unset. |
| 26 | ~Condition(); |
| 27 | #endif |
| 28 | // Waits for the condition to be set. Will return true immediately if it is |
| 29 | // already set. |
| 30 | // Returns false if returning before a confirmed condition set, although doing |
| 31 | // anything very useful with the return value is difficult because the |
| 32 | // condition may have been set (and possibly even unset again) between the |
| 33 | // time when the system call to block returned and this function returns. |
| 34 | bool Wait(); |
| 35 | // Waits for the next Set(), regardless of whether or not the condition is |
| 36 | // currently set. |
| 37 | // Same return value as Wait(). |
| 38 | bool WaitNext(); |
| 39 | |
| 40 | // Sets the condition. Any processes/tasks that are currently Wait()ing will |
| 41 | // continue. |
| 42 | // All implementations will wake all waiting processes/tasks at once so that |
| 43 | // the highest priority one(s) will run before others like usual. |
| 44 | void Set(); |
| 45 | // Unsets the condition. |
| 46 | void Unset(); |
| 47 | |
| 48 | private: |
| 49 | #ifdef __VXWORKS__ |
| 50 | // Always empty. Used to make tasks wait and then gets flushed to unblock all |
| 51 | // of them. |
| 52 | SEM_ID wait_; |
| 53 | // Whether or not the conditon is set. |
| 54 | bool set_; |
| 55 | #else |
| 56 | mutex impl_; |
| 57 | #endif |
| 58 | }; |
| 59 | |
| 60 | } // namespace aos |
| 61 | |
| 62 | #endif // AOS_COMMON_CONDITION_H_ |