17#ifndef AGX_THREADSYNCHRONIZATION_H
18#define AGX_THREADSYNCHRONIZATION_H
32#include <condition_variable>
45# pragma warning( disable : 4275 )
46# pragma warning( disable : 4702)
52#define AGX_MEASURE_LOCK_TIME 0
57 using Mutex = std::mutex;
76 void block(
int numThreads);
83 std::condition_variable m_condition;
119 bool block(
unsigned long timeout_ms);
135 std::condition_variable m_condition;
190 unsigned int m_numReaders;
191 unsigned int m_numWriters;
204 : m_mutex(), m_condition(), m_wave(0), m_numWaiting(0), m_barrierCount( numThreads )
215 #if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
219 #if AGX_MEASURE_LOCK_TIME
221 thread->startWaitTimer();
225 std::unique_lock<std::mutex> lock(m_mutex);
229 if ( numThreads != 0 )
230 m_barrierCount = numThreads;
232 if ( ++m_numWaiting == m_barrierCount ) {
238 m_condition.notify_all();
241 m_condition.wait( lock, [
this,wave](){
return m_wave != wave; } );
245 #if AGX_MEASURE_LOCK_TIME
246 thread->stopWaitTimer();
253 m_mutex(), m_condition(), m_released( false )
259 #if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
263 #if AGX_MEASURE_LOCK_TIME
265 thread->startWaitTimer();
268 std::unique_lock<std::mutex> lock( m_mutex );
269 m_condition.wait( lock, [
this]{
return m_released;} );
272 #if AGX_MEASURE_LOCK_TIME
273 thread->stopWaitTimer();
280 #if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
284 #if AGX_MEASURE_LOCK_TIME
286 thread->startWaitTimer();
289 std::unique_lock<std::mutex> lock(m_mutex);
290 bool status = m_condition.wait_for( lock, std::chrono::milliseconds( timeout_ms ), [
this]{
return m_released;} );
295 #if AGX_MEASURE_LOCK_TIME
296 thread->stopWaitTimer();
305 #if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
309 std::lock_guard<std::mutex> lock(m_mutex);
313 m_condition.notify_all();
319 #if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
323 std::lock_guard<std::mutex> lock(m_mutex);
Barrier synchronization primitive.
Barrier(const Barrier &)=delete
Barrier & operator=(const Barrier &other)=delete
Block synchronization primitive.
void reset()
Reset the block.
Block()
Default constructor.
Block(const Block &)=delete
No copy-constructor.
void release()
Release the block.
Block & operator=(const Block &other)=delete
No assignment operator.
void block()
Block until released from another thread.
ReaderWriterLock provides shared access for readers and exclusive access for writers.
ReaderWriterLock(const ReaderWriterLock &)=delete
ReaderWriterLock()
Constructor.
void unlock_shared()
Releases the shared access.
void lock()
Acquire exclusive access.
void unlock()
Relese the exclusive access.
ReaderWriterLock & operator=(const ReaderWriterLock &)=delete
void lock_shared()
Acquire the shared lock.
agx::Thread is a representation of an OS specific implementation of a computational thread.
static Thread * getCurrentThread()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::condition_variable Condition
std::recursive_mutex ReentrantMutex