blob: 521c816b7caae5805351a62349d037c6a215846f [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2015-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#include "HAL/cpp/Semaphore.h"
9
10Semaphore::Semaphore(int32_t count) { m_count = count; }
11
12void Semaphore::give() {
13 std::lock_guard<priority_mutex> lock(m_mutex);
14 ++m_count;
15 m_condition.notify_one();
16}
17
18void Semaphore::take() {
19 std::unique_lock<priority_mutex> lock(m_mutex);
20 m_condition.wait(lock, [this] { return m_count; });
21 --m_count;
22}
23
24bool Semaphore::tryTake() {
25 std::lock_guard<priority_mutex> lock(m_mutex);
26 if (m_count) {
27 --m_count;
28 return true;
29 } else {
30 return false;
31 }
32}