Files
allwpilib/networktables/cpp/include/OSAL/Synchronized.h
peter mitrano 29d029fa61 merged from frcsim branch
verified to work on real robots
adds sim eclipse plugins, fixed JavaGazebo, made wpilibC++Sim build on windows
 - Java and C++ simulation robot programs run on windows
 - simulation eclipse plugin delivers models and gazebo plugins
 - Java Gazebo now respects GAZEBO_IP variables and can work across networks
 - hal and network tables win32 hacked to work on windows
 - smart dashboard broken on windows due to network tables hacks
 - wpilibC++Sim, gz_msgs, and frcsim_gazebo_plugins build with CMake
 - removed constexpr for cross platform compatibility
 - msgs generated using .protos as a part of build process
 - some spare and unused cmake/pom files deleted
 - simulation ubuntu debians removed entirely
 - refactored CMake project flags and macros
 - updated to match non-sim C++ API
 - fixed and updated documentation
 - servo added to simulation

Change-Id: Ia702ff0f1fee10d77f543810ad88f56696443b05
2015-08-18 10:39:25 -04:00

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