mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
61 lines
1.8 KiB
C
61 lines
1.8 KiB
C
|
|
/*----------------------------------------------------------------------------*/
|
||
|
|
/* 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. */
|
||
|
|
/*----------------------------------------------------------------------------*/
|
||
|
|
|
||
|
|
#ifndef NT_SYNCHRONIZED_H
|
||
|
|
#define NT_SYNCHRONIZED_H
|
||
|
|
|
||
|
|
#define NT_CRITICAL_REGION(s) { NTSynchronized _sync(s);
|
||
|
|
#define NT_END_REGION }
|
||
|
|
|
||
|
|
#include <pthread.h>
|
||
|
|
|
||
|
|
class NTReentrantSemaphore
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit NTReentrantSemaphore(){
|
||
|
|
pthread_mutexattr_init(&mta);
|
||
|
|
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
|
||
|
|
pthread_mutex_init(&m_semaphore, &mta);
|
||
|
|
};
|
||
|
|
~NTReentrantSemaphore(){
|
||
|
|
pthread_mutex_unlock(&m_semaphore);
|
||
|
|
pthread_mutex_destroy(&m_semaphore);
|
||
|
|
};
|
||
|
|
void take(){
|
||
|
|
pthread_mutex_lock(&m_semaphore);
|
||
|
|
};
|
||
|
|
void give(){
|
||
|
|
pthread_mutex_unlock(&m_semaphore);
|
||
|
|
};
|
||
|
|
private:
|
||
|
|
pthread_mutexattr_t mta;
|
||
|
|
pthread_mutex_t m_semaphore;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Provide easy support for critical regions.
|
||
|
|
* A critical region is an area of code that is always executed under mutual exclusion. Only
|
||
|
|
* one task can be executing this code at any time. The idea is that code that manipulates data
|
||
|
|
* that is shared between two or more tasks has to be prevented from executing at the same time
|
||
|
|
* otherwise a race condition is possible when both tasks try to update the data. Typically
|
||
|
|
* semaphores are used to ensure only single task access to the data.
|
||
|
|
* Synchronized objects are a simple wrapper around semaphores to help ensure that semaphores
|
||
|
|
* are always signaled (semGive) after a wait (semTake).
|
||
|
|
*/
|
||
|
|
class NTSynchronized
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit NTSynchronized(NTReentrantSemaphore&);
|
||
|
|
//TODO remove vxworks SEM_ID support
|
||
|
|
virtual ~NTSynchronized();
|
||
|
|
private:
|
||
|
|
NTReentrantSemaphore& m_semaphore;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
#endif
|