2015-06-25 01:54:20 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2015-2016. All Rights Reserved. */
|
2015-06-25 01:54:20 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2015-06-25 01:54:20 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "HAL/cpp/Semaphore.hpp"
|
|
|
|
|
|
|
|
|
|
Semaphore::Semaphore(uint32_t count) {
|
|
|
|
|
m_count = count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Semaphore::give() {
|
2015-09-01 16:47:57 -07:00
|
|
|
std::lock_guard<priority_mutex> lock(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
++m_count;
|
|
|
|
|
m_condition.notify_one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Semaphore::take() {
|
|
|
|
|
std::unique_lock<priority_mutex> lock(m_mutex);
|
|
|
|
|
m_condition.wait(lock, [this] { return m_count; } );
|
|
|
|
|
--m_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Semaphore::tryTake() {
|
2015-09-01 16:47:57 -07:00
|
|
|
std::lock_guard<priority_mutex> lock(m_mutex);
|
2015-06-25 01:54:20 -07:00
|
|
|
if (m_count) {
|
|
|
|
|
--m_count;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|