blob: 56349e3294ee3192ac58d8557050a47d04a920bd [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2015-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
7
8#include "HAL/cpp/Semaphore.hpp"
9
10Semaphore::Semaphore(uint32_t count) {
11 m_count = count;
12}
13
14void Semaphore::give() {
15 std::lock_guard<priority_mutex> lock(m_mutex);
16 ++m_count;
17 m_condition.notify_one();
18}
19
20void 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
26bool 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}