Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <condition_variable> |
| 13 | |
| 14 | #include "HAL/cpp/priority_mutex.h" |
| 15 | #include "support/deprecated.h" |
| 16 | |
| 17 | class WPI_DEPRECATED( |
| 18 | "Semaphore scheduled for removal in 2018. Recommended to replace with a " |
| 19 | "std::mutex and std::condition_variable") Semaphore { |
| 20 | public: |
| 21 | explicit Semaphore(int32_t count = 0); |
| 22 | Semaphore(Semaphore&&); |
| 23 | Semaphore& operator=(Semaphore&&); |
| 24 | |
| 25 | void give(); |
| 26 | void take(); |
| 27 | |
| 28 | // @return true if semaphore was locked successfully. false if not. |
| 29 | bool tryTake(); |
| 30 | |
| 31 | static const int32_t kNoWait = 0; |
| 32 | static const int32_t kWaitForever = -1; |
| 33 | |
| 34 | static const int32_t kEmpty = 0; |
| 35 | static const int32_t kFull = 1; |
| 36 | |
| 37 | private: |
| 38 | priority_mutex m_mutex; |
| 39 | std::condition_variable_any m_condition; |
| 40 | int32_t m_count = 0; |
| 41 | }; |