17 #ifndef AGX_THREADSYNCHRONIZATION_H
18 #define AGX_THREADSYNCHRONIZATION_H
20 #include <agx/config/AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING.h>
21 #include <agx/config/AGX_USE_SSE.h>
32 #include <condition_variable>
35 #include <xmmintrin.h>
36 #include <emmintrin.h>
44 # pragma warning(push)
45 # pragma warning( disable : 4275 ) // warning C4275: non dll-interface class
46 # pragma warning( disable : 4702) // warning C4702: unreachable code
52 #define AGX_MEASURE_LOCK_TIME 0
57 using Mutex = std::mutex;
76 void block(
int numThreads);
83 std::condition_variable m_condition;
107 Block& operator=(
const Block& other ) =
delete;
119 bool block(
unsigned long timeout_ms);
135 std::condition_variable m_condition;
177 void unlock_shared();
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);
334 # pragma warning(pop)