Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2015-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "HAL/cpp/Semaphore.hpp" |
| 9 | |
| 10 | Semaphore::Semaphore(uint32_t count) { |
| 11 | m_count = count; |
| 12 | } |
| 13 | |
| 14 | void Semaphore::give() { |
| 15 | std::lock_guard<priority_mutex> lock(m_mutex); |
| 16 | ++m_count; |
| 17 | m_condition.notify_one(); |
| 18 | } |
| 19 | |
| 20 | void Semaphore::take() { |
| 21 | std::unique_lock<priority_mutex> lock(m_mutex); |
| 22 | m_condition.wait(lock, [this] { return m_count; } ); |
| 23 | --m_count; |
| 24 | } |
| 25 | |
| 26 | bool Semaphore::tryTake() { |
| 27 | std::lock_guard<priority_mutex> lock(m_mutex); |
| 28 | if (m_count) { |
| 29 | --m_count; |
| 30 | return true; |
| 31 | } |
| 32 | else { |
| 33 | return false; |
| 34 | } |
| 35 | } |