From da87fd4e551268c9f32a9ba67d5b5da80f698960 Mon Sep 17 00:00:00 2001 From: Jens Lang Date: Mon, 26 Nov 2012 22:58:27 +0100 Subject: [PATCH] RemotePlugin: use atomic operation for lock Use GCC's builtin atomic add/subtract operation for incrementing/ decrementing the recursive lock variable. This is needed to avoid race conditions and is much faster than using mutexes etc. Signed-off-by: Tobias Doerffel --- include/RemotePlugin.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/include/RemotePlugin.h b/include/RemotePlugin.h index 40c8fee4a..347536b4b 100644 --- a/include/RemotePlugin.h +++ b/include/RemotePlugin.h @@ -284,7 +284,7 @@ public: // recursive lock inline void lock() { - if( !isInvalid() && ++m_lockDepth == 1 ) + if( !isInvalid() && __sync_add_and_fetch( &m_lockDepth, 1 ) == 1 ) { #ifdef USE_QT_SEMAPHORES m_dataSem.acquire(); @@ -297,16 +297,13 @@ public: // recursive unlock inline void unlock() { - if( m_lockDepth > 0 ) + if( __sync_sub_and_fetch( &m_lockDepth, 1) <= 0 ) { - if( --m_lockDepth == 0 ) - { #ifdef USE_QT_SEMAPHORES - m_dataSem.release(); + m_dataSem.release(); #else - sem_post( m_dataSem ); + sem_post( m_dataSem ); #endif - } } } @@ -484,7 +481,7 @@ private: sem_t * m_dataSem; sem_t * m_messageSem; #endif - int m_lockDepth; + volatile int m_lockDepth; } ;