Fixed TSan annotation for spin-lock
[libcds.git] / cds / sync / spinlock.h
index b1d952781261efc507ae6a2a6d66ff5fa8939cb7..72941c14cce531f9c744aec8b04f5f3d13f3edc7 100644 (file)
@@ -80,6 +80,7 @@ namespace cds {
                 :m_dbgOwnerId( OS::c_NullThreadId )
 #    endif
             {
+                CDS_TSAN_ANNOTATE_MUTEX_CREATE( &m_spin );
                 m_spin.store( false, atomics::memory_order_relaxed );
             }
 
@@ -92,7 +93,12 @@ namespace cds {
                 : m_dbgOwnerId( bLocked ? cds::OS::get_current_thread_id() : cds::OS::c_NullThreadId )
 #    endif
             {
+                CDS_TSAN_ANNOTATE_MUTEX_CREATE( &m_spin );
                 m_spin.store( bLocked, atomics::memory_order_relaxed );
+#           ifdef CDS_THREAD_SANITIZER_ENABLED
+                if ( bLocked )
+                    CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( &m_spin );
+#           endif
             }
 
             /// Dummy copy constructor
@@ -106,12 +112,15 @@ namespace cds {
 #   ifdef CDS_DEBUG
                 , m_dbgOwnerId( cds::OS::c_NullThreadId )
 #   endif
-            {}
+            {
+                CDS_TSAN_ANNOTATE_MUTEX_CREATE( &m_spin );
+            }
 
             /// Destructor. On debug time it checks whether spin-lock is free
             ~spin_lock()
             {
                 assert( !m_spin.load( atomics::memory_order_relaxed ));
+                CDS_TSAN_ANNOTATE_MUTEX_DESTROY( &m_spin );
             }
 
             /// Check if the spin is locked
@@ -188,8 +197,8 @@ namespace cds {
                 assert( m_dbgOwnerId == OS::get_current_thread_id());
                 CDS_DEBUG_ONLY( m_dbgOwnerId = OS::c_NullThreadId; )
 
-                m_spin.store( false, atomics::memory_order_release );
                 CDS_TSAN_ANNOTATE_MUTEX_RELEASED( &m_spin );
+                m_spin.store( false, atomics::memory_order_release );
             }
         };
 
@@ -285,7 +294,9 @@ namespace cds {
             reentrant_spin_lock() CDS_NOEXCEPT
                 : m_spin(0)
                 , m_OwnerId( OS::c_NullThreadId )
-            {}
+            {
+                CDS_TSAN_ANNOTATE_MUTEX_CREATE( &m_spin );
+            }
 
             /// Dummy copy constructor
             /**
@@ -296,13 +307,16 @@ namespace cds {
             reentrant_spin_lock( const reentrant_spin_lock<Integral, Backoff>& ) CDS_NOEXCEPT
                 : m_spin(0)
                 , m_OwnerId( OS::c_NullThreadId )
-            {}
+            {
+                CDS_TSAN_ANNOTATE_MUTEX_CREATE( &m_spin );
+            }
 
-            /// Construct object for specified state
+            /// Construct object in specified state
             explicit reentrant_spin_lock( bool bLocked )
                 : m_spin(0)
                 , m_OwnerId( OS::c_NullThreadId )
             {
+                CDS_TSAN_ANNOTATE_MUTEX_CREATE( &m_spin );
                 if ( bLocked )
                     lock();
             }
@@ -362,8 +376,8 @@ namespace cds {
                         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 );
+                        m_spin.store( 0, atomics::memory_order_release );
                     }
                     return true;
                 }