Refactoring: cds/lock/spinlock.h
authorkhizmax <libcds.dev@gmail.com>
Tue, 30 Sep 2014 19:27:30 +0000 (23:27 +0400)
committerkhizmax <libcds.dev@gmail.com>
Tue, 30 Sep 2014 19:27:30 +0000 (23:27 +0400)
cds/lock/spinlock.h
projects/Win/vc12/cds.vcxproj
projects/Win/vc12/cds.vcxproj.filters
tests/unit/alloc/random.cpp
tests/unit/lock/nolock.h
tests/unit/lock/win32_lock.h

index 1b657fc6fef6a89bacaeb3ac1635a1ec87c47c04..e4248a5fbe2222fa86c7ec9069ee2a1cf03377b2 100644 (file)
@@ -46,7 +46,7 @@ namespace cds {
             Template parameters:
                 - @p Backoff    backoff strategy. Used when spin lock is locked
         */
-        template <class Backoff >
+        template <typename Backoff >
         class Spinlock
         {
         public:
@@ -54,7 +54,7 @@ namespace cds {
         private:
             atomics::atomic<bool>    m_spin  ;       ///< Spin
 #    ifdef CDS_DEBUG
-            typename OS::ThreadId       m_dbgOwnerId        ;       ///< Owner thread id (only for debug mode)
+            typename OS::ThreadId    m_dbgOwnerId        ;       ///< Owner thread id (only for debug mode)
 #    endif
 
         public:
@@ -73,7 +73,7 @@ namespace cds {
             */
             Spinlock( bool bLocked ) CDS_NOEXCEPT
 #    ifdef CDS_DEBUG
-                :m_dbgOwnerId( bLocked ? OS::getCurrentThreadId() : OS::c_NullThreadId )
+                : m_dbgOwnerId( bLocked ? OS::getCurrentThreadId() : OS::c_NullThreadId )
 #    endif
             {
                 m_spin.store( bLocked, atomics::memory_order_relaxed );
@@ -112,12 +112,6 @@ namespace cds {
                 Debug version: deadlock can be detected
             */
             bool try_lock() CDS_NOEXCEPT
-            {
-                return tryLock();
-            }
-
-            /// Try to lock the object (synonym for \ref try_lock)
-            bool tryLock() CDS_NOEXCEPT
             {
                 bool bCurrent = false;
                 m_spin.compare_exchange_strong( bCurrent, true, atomics::memory_order_acquire, atomics::memory_order_relaxed );
@@ -135,17 +129,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
+            bool try_lock( unsigned int nTryCount ) CDS_NOEXCEPT( noexept( backoff_strategy()() ) )
             {
-                return tryLock( nTryCount );
-            }
-
-            /// Try to lock the object (synonym for \ref try_lock)
-            bool tryLock( unsigned int nTryCount ) CDS_NOEXCEPT
-            {
-                Backoff backoff;
+                backoff_strategy backoff;
                 while ( nTryCount-- ) {
-                    if ( tryLock() )
+                    if ( try_lock() )
                         return true;
                     backoff();
                 }
@@ -153,15 +141,15 @@ namespace cds {
             }
 
             /// Lock the spin-lock. Waits infinitely while spin-lock is locked. Debug version: deadlock may be detected
-            void lock() CDS_NOEXCEPT
+            void lock() CDS_NOEXCEPT(noexept( backoff_strategy()() ))
             {
-                Backoff backoff;
+                backoff_strategy backoff;
 
                 // Deadlock detected
                 assert( m_dbgOwnerId != OS::getCurrentThreadId() );
 
                 // TATAS algorithm
-                while ( !tryLock() ) {
+                while ( !try_lock() ) {
                     while ( m_spin.load( atomics::memory_order_relaxed ) ) {
                         backoff();
                     }
@@ -203,11 +191,11 @@ namespace cds {
 
         private:
             atomics::atomic<integral_type>   m_spin      ; ///< spin-lock atomic
-            thread_id                           m_OwnerId   ; ///< Owner thread id. If spin-lock is not locked it usually equals to OS::c_NullThreadId
+            thread_id                        m_OwnerId   ; ///< Owner thread id. If spin-lock is not locked it usually equals to OS::c_NullThreadId
 
         private:
             //@cond
-            void beOwner( thread_id tid ) CDS_NOEXCEPT
+            void take( thread_id tid ) CDS_NOEXCEPT
             {
                 m_OwnerId = tid;
             }
@@ -217,43 +205,43 @@ namespace cds {
                 m_OwnerId = OS::c_NullThreadId;
             }
 
-            bool isOwned( thread_id tid ) const CDS_NOEXCEPT
+            bool is_taken( thread_id tid ) const CDS_NOEXCEPT
             {
                 return m_OwnerId == tid;
             }
 
-            bool    tryLockOwned( thread_id tid ) CDS_NOEXCEPT
+            bool try_taken_lock( thread_id tid ) CDS_NOEXCEPT
             {
-                if ( isOwned( tid )) {
+                if ( is_taken( tid )) {
                     m_spin.fetch_add( 1, atomics::memory_order_relaxed );
                     return true;
                 }
                 return false;
             }
 
-            bool tryAcquireLock() CDS_NOEXCEPT
+            bool try_acquire() CDS_NOEXCEPT
             {
                 integral_type nCurrent = 0;
                 return m_spin.compare_exchange_weak( nCurrent, 1, atomics::memory_order_acquire, atomics::memory_order_relaxed );
             }
 
-            bool tryAcquireLock( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( backoff_strategy()() ))
+            bool try_acquire( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( backoff_strategy()() ))
             {
                 backoff_strategy bkoff;
 
                 while ( nTryCount-- ) {
-                    if ( tryAcquireLock() )
+                    if ( try_acquire() )
                         return true;
                     bkoff();
                 }
                 return false;
             }
 
-            void acquireLock() CDS_NOEXCEPT_( noexcept( backoff_strategy()() ))
+            void acquire() CDS_NOEXCEPT_( noexcept( backoff_strategy()() ))
             {
                 // TATAS algorithm
                 backoff_strategy bkoff;
-                while ( !tryAcquireLock() ) {
+                while ( !try_acquire() ) {
                     while ( m_spin.load( atomics::memory_order_relaxed ) )
                         bkoff();
                 }
@@ -294,76 +282,49 @@ namespace cds {
             */
             bool is_locked() const CDS_NOEXCEPT
             {
-                return !( m_spin.load( atomics::memory_order_relaxed ) == 0 || isOwned( cds::OS::getCurrentThreadId() ));
+                return !( m_spin.load( atomics::memory_order_relaxed ) == 0 || is_taken( cds::OS::getCurrentThreadId() ));
             }
 
             /// Try to lock the spin-lock (synonym for \ref try_lock)
-            bool tryLock() CDS_NOEXCEPT
+            bool try_lock() CDS_NOEXCEPT
             {
                 thread_id tid = OS::getCurrentThreadId();
-                if ( tryLockOwned( tid ) )
+                if ( try_taken_lock( tid ) )
                     return true;
-                if ( tryAcquireLock()) {
-                    beOwner( tid );
+                if ( try_acquire()) {
+                    take( tid );
                     return true;
                 }
                 return false;
             }
 
-            /// Try to lock the spin-lock. If spin-lock is free the current thread owns it. Return @p true if locking is success
-            bool try_lock() CDS_NOEXCEPT
-            {
-                return tryLock();
-            }
-
-            /// Try to lock the object (synonym for \ref try_lock)
-            bool tryLock( unsigned int nTryCount )
-#       if !( (CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 40600 && CDS_COMPILER_VERSION < 40700) || (CDS_COMPILER == CDS_COMPILER_CLANG && CDS_COMPILER_VERSION < 30100) )
-                // GCC 4.6, clang 3.0 error in noexcept expression:
-                // cannot call member function \91bool cds::lock::ReentrantSpinT<Integral, Backoff>::tryAcquireLock(unsigned int) without object
-                CDS_NOEXCEPT_( noexcept( tryAcquireLock(nTryCount) ))
-#       endif
+            /// Try to lock the object
+            bool try_lock( unsigned int nTryCount ) CDS_NOEXCEPT_( noexcept( try_acquire( nTryCount ) ) )
             {
                 thread_id tid = OS::getCurrentThreadId();
-                if ( tryLockOwned( tid ) )
+                if ( try_taken_lock( tid ) )
                     return true;
-                if ( tryAcquireLock( nTryCount )) {
-                    beOwner( tid );
+                if ( try_acquire( nTryCount )) {
+                    take( tid );
                     return true;
                 }
                 return false;
             }
 
-            /// Try to lock the object.
-            /**
-                If the spin-lock is locked the method repeats attempts to own spin-lock up to @p nTryCount times.
-                Between attempts @p backoff() is called.
-                Return @p true if current thread owns the lock @p false otherwise
-            */
-            bool try_lock( unsigned int nTryCount )
-#       if !( (CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 40600 && CDS_COMPILER_VERSION < 40700) || (CDS_COMPILER == CDS_COMPILER_CLANG && CDS_COMPILER_VERSION < 30100) )
-                // GCC 4.6, clang 3.0 error in noexcept expression:
-                // cannot call member function \91bool cds::lock::ReentrantSpinT<Integral, Backoff>::tryLock(unsigned int) without object
-                CDS_NOEXCEPT_( noexcept( tryLock(nTryCount) ))
-#       endif
-            {
-                return tryLock( nTryCount );
-            }
-
             /// Lock the object waits if it is busy
             void lock() CDS_NOEXCEPT
             {
                 thread_id tid = OS::getCurrentThreadId();
-                if ( !tryLockOwned( tid ) ) {
-                    acquireLock();
-                    beOwner( tid );
+                if ( !try_taken_lock( tid ) ) {
+                    acquire();
+                    take( tid );
                 }
             }
 
             /// Unlock the spin-lock. Return @p true if the current thread is owner of spin-lock @p false otherwise
             bool unlock() CDS_NOEXCEPT
             {
-                if ( isOwned( OS::getCurrentThreadId() ) ) {
+                if ( is_taken( OS::getCurrentThreadId() ) ) {
                     integral_type n = m_spin.load( atomics::memory_order_relaxed );
                     if ( n > 1 )
                         m_spin.store( n - 1, atomics::memory_order_relaxed );
@@ -377,9 +338,9 @@ namespace cds {
             }
 
             /// Change the owner of locked spin-lock. May be called by thread that is owner of the spin-lock
-            bool changeOwner( OS::ThreadId newOwnerId ) CDS_NOEXCEPT
+            bool change_owner( OS::ThreadId newOwnerId ) CDS_NOEXCEPT
             {
-                if ( isOwned( OS::getCurrentThreadId() ) ) {
+                if ( is_taken( OS::getCurrentThreadId() ) ) {
                     assert( newOwnerId != OS::c_NullThreadId );
                     m_OwnerId = newOwnerId;
                     return true;
@@ -389,10 +350,10 @@ namespace cds {
         };
 
         /// Recursive spin-lock based on atomic32u_t
-        typedef ReentrantSpinT<atomic32u_t, backoff::LockDefault>   ReentrantSpin32;
+        typedef ReentrantSpinT<uint32_t, backoff::LockDefault>   ReentrantSpin32;
 
         /// Recursive spin-lock based on atomic64u_t type
-        typedef ReentrantSpinT<atomic64u_t, backoff::LockDefault>   ReentrantSpin64;
+        typedef ReentrantSpinT<uint64_t, backoff::LockDefault>   ReentrantSpin64;
 
         /// Recursive spin-lock based on atomic32_t type
         typedef ReentrantSpin32                                     ReentrantSpin;
index 471ae9eab3e0358420c2ee57904350e982f6f12c..cea98ac6b197cad16b32c59e74c3e81d5548425f 100644 (file)
     <ClInclude Include="..\..\..\cds\details\bounded_array.h" />\r
     <ClInclude Include="..\..\..\cds\details\comparator.h" />\r
     <ClInclude Include="..\..\..\cds\details\defs.h" />\r
-    <ClInclude Include="..\..\..\cds\details\functor_wrapper.h" />\r
     <ClInclude Include="..\..\..\cds\details\is_aligned.h" />\r
     <ClInclude Include="..\..\..\cds\details\make_const_type.h" />\r
     <ClInclude Include="..\..\..\cds\details\marked_ptr.h" />\r
index 358e40ba7fa355706be73897c34da01638947046..74f37bfe4755c8444b40baea68f70588d45f3e43 100644 (file)
     <ClInclude Include="..\..\..\cds\details\defs.h">\r
       <Filter>Header Files\cds\details</Filter>\r
     </ClInclude>\r
-    <ClInclude Include="..\..\..\cds\details\functor_wrapper.h">\r
-      <Filter>Header Files\cds\details</Filter>\r
-    </ClInclude>\r
     <ClInclude Include="..\..\..\cds\gc\guarded_ptr.h">\r
       <Filter>Header Files\cds\gc</Filter>\r
     </ClInclude>\r
index 05034f8e9c94b050db6f815fb357794f3ede7f0d..3e6a6b416c124710211acb4d5ba9a47a7382706d 100644 (file)
@@ -82,7 +82,7 @@ namespace memory {
                 for ( size_t nPass = 0; nPass < s_nPassPerThread; ) {
                     size_t nIdx = m_rndGen( size_t(0), s_nDataSize - 1 );
                     Item & item = arr.at(nIdx);
-                    if ( item.m_access.tryLock() ) {
+                    if ( item.m_access.try_lock() ) {
                         if ( item.m_pszBlock ) {
                             m_Alloc.deallocate( item.m_pszBlock, 1 );
                             item.m_pszBlock = nullptr;
index d986cf93e57511c6a1fa50ba88d28f159c2b97c0..0fa37703295232f11719e54e1f58363eab307876 100644 (file)
@@ -9,7 +9,7 @@ namespace lock {
     public:
         void lock()     {}
         void unlock()   {}
-        bool tryLock()  { return true; }
+        bool try_lock()  { return true; }
     };
 }
 
index 197c67cc9511a07469b53217a912d209ba6a35dc..ff3ddb60b99a64bd2326ff6e4ae8448d392cdb20 100644 (file)
@@ -20,7 +20,7 @@ namespace lock {
 
             void lock()     { ::EnterCriticalSection( &m_cs ) ; }
             void unlock()   { ::LeaveCriticalSection( &m_cs)  ; }
-            bool tryLock()  { return ::TryEnterCriticalSection( &m_cs ) != 0 ; }
+            bool try_lock()  { return ::TryEnterCriticalSection( &m_cs ) != 0 ; }
         };
 
         class Mutex {
@@ -32,7 +32,7 @@ namespace lock {
 
             void lock()     { ::WaitForSingleObject( m_hMutex, INFINITE ); }
             void unlock()   { ::ReleaseMutex( m_hMutex ); }
-            bool tryLock()  { return ::WaitForSingleObject( m_hMutex, 0) == WAIT_OBJECT_0; }
+            bool try_lock()  { return ::WaitForSingleObject( m_hMutex, 0) == WAIT_OBJECT_0; }
         };
 
     } // namespace win