blob: 458ca6e7210c61b9a55c319f4c0321c555873360 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
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
9Semaphore::Semaphore(uint32_t count) {
10 m_count = count;
11}
12
13void Semaphore::give() {
14 std::lock_guard<priority_mutex> lock(m_mutex);
15 ++m_count;
16 m_condition.notify_one();
17}
18
19void 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
25bool 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}