Fixed TSan annotation for spin-lock
authorkhizmax <libcds.dev@gmail.com>
Tue, 29 Nov 2016 20:22:27 +0000 (23:22 +0300)
committerkhizmax <libcds.dev@gmail.com>
Tue, 29 Nov 2016 20:22:27 +0000 (23:22 +0300)
cds/compiler/feature_tsan.h
cds/sync/spinlock.h

index 9852773e8f249b3fd33c21c538ecd6dac66bfc70..193cfb6efff6e62bcb8ae67dee0696850bda0643 100644 (file)
                                                     CDS_TSAN_ANNOTATE_IGNORE_READS_END
 #   define CDS_TSAN_ANNOTATE_NEW_MEMORY( addr, sz ) AnnotateNewMemory( (char *) __FILE__, __LINE__, reinterpret_cast<void *>(addr), sz )
 
+#   define CDS_TSAN_ANNOTATE_MUTEX_CREATE( addr )    AnnotateRWLockCreate( __FILE__, __LINE__, reinterpret_cast<void *>(addr))
+#   define CDS_TSAN_ANNOTATE_MUTEX_DESTROY( addr )   AnnotateRWLockdESTROY( __FILE__, __LINE__, reinterpret_cast<void *>(addr))
+        // must be called after actual acquire
 #   define CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( addr )  AnnotateRWLockAcquired( __FILE__, __LINE__, reinterpret_cast<void *>(addr), 1 )
+        // must be called before actual release
 #   define CDS_TSAN_ANNOTATE_MUTEX_RELEASED( addr )  AnnotateRWLockReleased( __FILE__, __LINE__, reinterpret_cast<void *>(addr), 1 )
 
     // provided by TSan
@@ -67,6 +71,8 @@
 
         void AnnotateNewMemory(char *f, int l, void * mem, size_t size);
 
+        void AnnotateRWLockCreate( char *f, int l, void* m )
+        void AnnotateRWLockDestroy( char *f, int l, void* m )
         void AnnotateRWLockAcquired( const char *f, int l, void *m, long is_w );
         void AnnotateRWLockReleased( const char *f, int l, void *m, long is_w );
     }
@@ -85,6 +91,8 @@
 
 #   define CDS_TSAN_ANNOTATE_NEW_MEMORY( addr, sz )
 
+#   define CDS_TSAN_ANNOTATE_MUTEX_CREATE( addr )
+#   define CDS_TSAN_ANNOTATE_MUTEX_DESTROY( addr )
 #   define CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( addr )
 #   define CDS_TSAN_ANNOTATE_MUTEX_RELEASED( addr )
 
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;
                 }