Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2015. 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 $(WIND_BASE)/WPILib. */ |
| 5 | /*----------------------------------------------------------------------------*/ |
| 6 | |
| 7 | #include "HAL/cpp/Semaphore.hpp" |
| 8 | |
| 9 | Semaphore::Semaphore(uint32_t count) { |
| 10 | m_count = count; |
| 11 | } |
| 12 | |
| 13 | void Semaphore::give() { |
| 14 | std::lock_guard<priority_mutex> lock(m_mutex); |
| 15 | ++m_count; |
| 16 | m_condition.notify_one(); |
| 17 | } |
| 18 | |
| 19 | void Semaphore::take() { |
| 20 | std::unique_lock<priority_mutex> lock(m_mutex); |
| 21 | m_condition.wait(lock, [this] { return m_count; } ); |
| 22 | --m_count; |
| 23 | } |
| 24 | |
| 25 | bool Semaphore::tryTake() { |
| 26 | std::lock_guard<priority_mutex> lock(m_mutex); |
| 27 | if (m_count) { |
| 28 | --m_count; |
| 29 | return true; |
| 30 | } |
| 31 | else { |
| 32 | return false; |
| 33 | } |
| 34 | } |