Fixed TSan annotation
authorkhizmax <khizmax@gmail.com>
Tue, 29 Nov 2016 14:56:22 +0000 (17:56 +0300)
committerkhizmax <khizmax@gmail.com>
Tue, 29 Nov 2016 14:56:22 +0000 (17:56 +0300)
cds/sync/spinlock.h

index 32b75ea..b1d9527 100644 (file)
@@ -130,7 +130,14 @@ namespace cds {
             bool try_lock() CDS_NOEXCEPT
             {
                 bool bCurrent = false;
             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 );
                 m_spin.compare_exchange_strong( bCurrent, true, atomics::memory_order_acquire, atomics::memory_order_relaxed );
+#           endif
 
                 CDS_DEBUG_ONLY(
                     if ( !bCurrent ) {
 
                 CDS_DEBUG_ONLY(
                     if ( !bCurrent ) {
@@ -149,10 +156,8 @@ namespace cds {
             {
                 backoff_strategy backoff;
                 while ( nTryCount-- ) {
             {
                 backoff_strategy backoff;
                 while ( nTryCount-- ) {
-                    if ( try_lock() ) {
-                        CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( &m_spin );
+                    if ( try_lock() )
                         return true;
                         return true;
-                    }
                     backoff();
                 }
                 return false;
                     backoff();
                 }
                 return false;
@@ -241,7 +246,15 @@ namespace cds {
             bool try_acquire() CDS_NOEXCEPT
             {
                 integral_type nCurrent = 0;
             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 );
                 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()()))
             }
 
             bool try_acquire( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( backoff_strategy()()))
@@ -249,10 +262,8 @@ namespace cds {
                 backoff_strategy bkoff;
 
                 while ( nTryCount-- ) {
                 backoff_strategy bkoff;
 
                 while ( nTryCount-- ) {
-                    if ( try_acquire() ) {
-                        CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( &m_spin );
+                    if ( try_acquire() )
                         return true;
                         return true;
-                    }
                     bkoff();
                 }
                 return false;
                     bkoff();
                 }
                 return false;