Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
| 4 | #include <condition_variable> |
| 5 | |
| 6 | #include "HAL/cpp/priority_mutex.h" |
| 7 | |
| 8 | class Semaphore { |
| 9 | public: |
| 10 | explicit Semaphore(uint32_t count = 0); |
| 11 | Semaphore(Semaphore&&); |
| 12 | Semaphore& operator=(Semaphore&&); |
| 13 | |
| 14 | void give(); |
| 15 | void take(); |
| 16 | |
| 17 | // @return true if semaphore was locked successfully. false if not. |
| 18 | bool tryTake(); |
| 19 | |
| 20 | static const int32_t kNoWait = 0; |
| 21 | static const int32_t kWaitForever = -1; |
| 22 | |
| 23 | static const uint32_t kEmpty = 0; |
| 24 | static const uint32_t kFull = 1; |
| 25 | |
| 26 | private: |
| 27 | priority_mutex m_mutex; |
| 28 | std::condition_variable_any m_condition; |
| 29 | uint32_t m_count = 0; |
| 30 | }; |