From: khizmax Date: Tue, 29 Nov 2016 20:22:27 +0000 (+0300) Subject: Fixed TSan annotation for spin-lock X-Git-Tag: v2.2.0~34 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=libcds.git;a=commitdiff_plain;h=5bda951272b5026c1f9089400c68bd21aa8be113 Fixed TSan annotation for spin-lock --- diff --git a/cds/compiler/feature_tsan.h b/cds/compiler/feature_tsan.h index 9852773e..193cfb6e 100644 --- a/cds/compiler/feature_tsan.h +++ b/cds/compiler/feature_tsan.h @@ -52,7 +52,11 @@ CDS_TSAN_ANNOTATE_IGNORE_READS_END # define CDS_TSAN_ANNOTATE_NEW_MEMORY( addr, sz ) AnnotateNewMemory( (char *) __FILE__, __LINE__, reinterpret_cast(addr), sz ) +# define CDS_TSAN_ANNOTATE_MUTEX_CREATE( addr ) AnnotateRWLockCreate( __FILE__, __LINE__, reinterpret_cast(addr)) +# define CDS_TSAN_ANNOTATE_MUTEX_DESTROY( addr ) AnnotateRWLockdESTROY( __FILE__, __LINE__, reinterpret_cast(addr)) + // must be called after actual acquire # define CDS_TSAN_ANNOTATE_MUTEX_ACQUIRED( addr ) AnnotateRWLockAcquired( __FILE__, __LINE__, reinterpret_cast(addr), 1 ) + // must be called before actual release # define CDS_TSAN_ANNOTATE_MUTEX_RELEASED( addr ) AnnotateRWLockReleased( __FILE__, __LINE__, reinterpret_cast(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 ) diff --git a/cds/sync/spinlock.h b/cds/sync/spinlock.h index b1d95278..72941c14 100644 --- a/cds/sync/spinlock.h +++ b/cds/sync/spinlock.h @@ -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& ) 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; }