mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
artf4156: Replaced synchronization primitives with C++11 equivalents
Change-Id: I90da739347e875efda2a29dd5484b6dda3cd4753
This commit is contained in:
committed by
James Kuszmaul
parent
7f5ee01d3e
commit
3f59f3472a
@@ -7,8 +7,9 @@
|
||||
#include "HAL/cpp/Resource.hpp"
|
||||
#include "HAL/Errors.hpp"
|
||||
#include <cstddef>
|
||||
#include "HAL/cpp/priority_mutex.h"
|
||||
|
||||
ReentrantSemaphore Resource::m_createLock;
|
||||
priority_recursive_mutex Resource::m_createLock;
|
||||
|
||||
/**
|
||||
* Allocate storage for a new instance of Resource.
|
||||
@@ -17,7 +18,7 @@ ReentrantSemaphore Resource::m_createLock;
|
||||
*/
|
||||
Resource::Resource(uint32_t elements)
|
||||
{
|
||||
Synchronized sync(m_createLock);
|
||||
std::unique_lock<priority_recursive_mutex> sync(m_createLock);
|
||||
m_size = elements;
|
||||
m_isAllocated = new bool[m_size];
|
||||
for (uint32_t i=0; i < m_size; i++)
|
||||
@@ -38,7 +39,7 @@ Resource::Resource(uint32_t elements)
|
||||
*/
|
||||
/*static*/ void Resource::CreateResourceObject(Resource **r, uint32_t elements)
|
||||
{
|
||||
Synchronized sync(m_createLock);
|
||||
std::unique_lock<priority_recursive_mutex> sync(m_createLock);
|
||||
if (*r == NULL)
|
||||
{
|
||||
*r = new Resource(elements);
|
||||
@@ -61,7 +62,7 @@ Resource::~Resource()
|
||||
*/
|
||||
uint32_t Resource::Allocate(const char *resourceDesc)
|
||||
{
|
||||
Synchronized sync(m_allocateLock);
|
||||
std::unique_lock<priority_recursive_mutex> sync(m_allocateLock);
|
||||
for (uint32_t i=0; i < m_size; i++)
|
||||
{
|
||||
if (!m_isAllocated[i])
|
||||
@@ -81,7 +82,7 @@ uint32_t Resource::Allocate(const char *resourceDesc)
|
||||
*/
|
||||
uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
|
||||
{
|
||||
Synchronized sync(m_allocateLock);
|
||||
std::unique_lock<priority_recursive_mutex> sync(m_allocateLock);
|
||||
if (index >= m_size)
|
||||
{
|
||||
// TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
|
||||
@@ -104,7 +105,7 @@ uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
|
||||
*/
|
||||
void Resource::Free(uint32_t index)
|
||||
{
|
||||
Synchronized sync(m_allocateLock);
|
||||
std::unique_lock<priority_recursive_mutex> sync(m_allocateLock);
|
||||
if (index == ~0ul) return;
|
||||
if (index >= m_size)
|
||||
{
|
||||
|
||||
34
hal/lib/Athena/cpp/Semaphore.cpp
Normal file
34
hal/lib/Athena/cpp/Semaphore.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "HAL/cpp/Semaphore.hpp"
|
||||
|
||||
Semaphore::Semaphore(uint32_t count) {
|
||||
m_count = count;
|
||||
}
|
||||
|
||||
void Semaphore::give() {
|
||||
std::unique_lock<priority_mutex> lock(m_mutex);
|
||||
++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() {
|
||||
std::unique_lock<priority_mutex> lock(m_mutex);
|
||||
if (m_count) {
|
||||
--m_count;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2008. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "HAL/cpp/Synchronized.hpp"
|
||||
#include "HAL/Semaphore.hpp"
|
||||
|
||||
/**
|
||||
* Synchronized class deals with critical regions.
|
||||
* Declare a Synchronized object at the beginning of a block. That will take the semaphore.
|
||||
* When the code exits from the block it will call the destructor which will give the semaphore.
|
||||
* This ensures that no matter how the block is exited, the semaphore will always be released.
|
||||
* Use the CRITICAL_REGION(SEM_ID) and END_REGION macros to make the code look cleaner (see header file)
|
||||
* @param semaphore The semaphore controlling this critical region.
|
||||
*/
|
||||
Synchronized::Synchronized(MUTEX_ID semaphore)
|
||||
{
|
||||
m_mutex = semaphore;
|
||||
m_semaphore = NULL;
|
||||
takeMutex(m_mutex);
|
||||
}
|
||||
|
||||
Synchronized::Synchronized(SEMAPHORE_ID semaphore)
|
||||
{
|
||||
m_mutex = NULL;
|
||||
m_semaphore = semaphore;
|
||||
takeSemaphore(m_semaphore);
|
||||
}
|
||||
|
||||
Synchronized::Synchronized(ReentrantSemaphore& semaphore)
|
||||
{
|
||||
m_mutex = semaphore.m_semaphore;
|
||||
m_semaphore = NULL;
|
||||
takeMutex(m_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* This destructor unlocks the semaphore.
|
||||
*/
|
||||
Synchronized::~Synchronized()
|
||||
{
|
||||
if (m_mutex != NULL) {
|
||||
giveMutex(m_mutex);
|
||||
} else {
|
||||
giveSemaphore(m_semaphore);
|
||||
}
|
||||
}
|
||||
33
hal/lib/Athena/cpp/priority_mutex.cpp
Normal file
33
hal/lib/Athena/cpp/priority_mutex.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "HAL/cpp/priority_mutex.h"
|
||||
|
||||
void priority_recursive_mutex::lock() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
}
|
||||
|
||||
void priority_recursive_mutex::unlock() {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
bool priority_recursive_mutex::try_lock() noexcept {
|
||||
return !pthread_mutex_trylock(&m_mutex);
|
||||
}
|
||||
|
||||
pthread_mutex_t* priority_recursive_mutex::native_handle() {
|
||||
return &m_mutex;
|
||||
}
|
||||
|
||||
void priority_mutex::lock() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
}
|
||||
|
||||
void priority_mutex::unlock() {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
bool priority_mutex::try_lock() noexcept {
|
||||
return !pthread_mutex_trylock(&m_mutex);
|
||||
}
|
||||
|
||||
pthread_mutex_t* priority_mutex::native_handle() {
|
||||
return &m_mutex;
|
||||
}
|
||||
Reference in New Issue
Block a user