Fixed TSan annotation
[libcds.git] / cds / sync / spinlock.h
index 1708bba0c4abc8f2a37b5209877cdc943d8495cb..b1d952781261efc507ae6a2a6d66ff5fa8939cb7 100644 (file)
@@ -130,7 +130,14 @@ namespace cds {
             bool try_lock() CDS_NOEXCEPT
             {
                 bool bCurrent = false;
+
+#           ifdef CDS_THREAD_SANITIZER_ENABLED
+                if ( m_spin.compare_exchange_strong( bCurrent, true, atomics::memory_order_acquire, atomics::memory_order_relaxed )) {
+                    CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( &m_spin );
+                }
+#           else
                 m_spin.compare_exchange_strong( bCurrent, true, atomics::memory_order_acquire, atomics::memory_order_relaxed );
+#           endif
 
                 CDS_DEBUG_ONLY(
                     if ( !bCurrent ) {
@@ -145,11 +152,11 @@ namespace cds {
                 Returns \p true if locking is succeeded
                 otherwise (if the spin is already locked) returns \p false
             */
-            bool try_lock( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( backoff_strategy()()) )
+            bool try_lock( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( backoff_strategy()()))
             {
                 backoff_strategy backoff;
                 while ( nTryCount-- ) {
-                    if ( try_lock())
+                    if ( try_lock() )
                         return true;
                     backoff();
                 }
@@ -182,6 +189,7 @@ namespace cds {
                 CDS_DEBUG_ONLY( m_dbgOwnerId = OS::c_NullThreadId; )
 
                 m_spin.store( false, atomics::memory_order_release );
+                CDS_TSAN_ANNOTATE_MUTEX_RELEASED( &m_spin );
             }
         };
 
@@ -238,7 +246,15 @@ namespace cds {
             bool try_acquire() CDS_NOEXCEPT
             {
                 integral_type nCurrent = 0;
+#           ifdef CDS_THREAD_SANITIZER_ENABLED
+                if ( m_spin.compare_exchange_weak( nCurrent, 1, atomics::memory_order_acquire, atomics::memory_order_relaxed )) {
+                    CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( &m_spin );
+                    return true;
+                }
+                return false;
+#           else
                 return m_spin.compare_exchange_weak( nCurrent, 1, atomics::memory_order_acquire, atomics::memory_order_relaxed );
+#           endif
             }
 
             bool try_acquire( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( backoff_strategy()()))
@@ -246,7 +262,7 @@ namespace cds {
                 backoff_strategy bkoff;
 
                 while ( nTryCount-- ) {
-                    if ( try_acquire())
+                    if ( try_acquire() )
                         return true;
                     bkoff();
                 }
@@ -340,13 +356,14 @@ namespace cds {
             /// Unlock the spin-lock. Return \p true if the current thread is owner of spin-lock \p false otherwise
             bool unlock() CDS_NOEXCEPT
             {
-                if ( is_taken( OS::get_current_thread_id()) ) {
+                if ( is_taken( OS::get_current_thread_id())) {
                     integral_type n = m_spin.load( atomics::memory_order_relaxed );
                     if ( n > 1 )
                         m_spin.store( n - 1, atomics::memory_order_relaxed );
                     else {
                         free();
                         m_spin.store( 0, atomics::memory_order_release );
+                        CDS_TSAN_ANNOTATE_MUTEX_RELEASED( &m_spin );
                     }
                     return true;
                 }
@@ -356,7 +373,7 @@ namespace cds {
             /// Change the owner of locked spin-lock. May be called by thread that is owner of the spin-lock
             bool change_owner( OS::ThreadId newOwnerId ) CDS_NOEXCEPT
             {
-                if ( is_taken( OS::get_current_thread_id()) ) {
+                if ( is_taken( OS::get_current_thread_id())) {
                     assert( newOwnerId != OS::c_NullThreadId );
                     m_OwnerId = newOwnerId;
                     return true;