X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=cds%2Flock%2Farray.h;h=ff8db6b877557002f7f9a007dc5e77bbca866891;hb=65590d04529c4ad7f586703e7875a94a5cbe2a9e;hp=ce96cc0b23caef1216273888edf0aa3ae76952a5;hpb=7d15399a4d18ae2061ddb01656d85dbc940ff915;p=libcds.git diff --git a/cds/lock/array.h b/cds/lock/array.h index ce96cc0b..ff8db6b8 100644 --- a/cds/lock/array.h +++ b/cds/lock/array.h @@ -1,12 +1,11 @@ -//$$CDS-header$$ +//$$CDS-header$$-2 #ifndef __CDS_LOCK_ARRAY_H #define __CDS_LOCK_ARRAY_H +#include //unique_lock #include -#include -#include - +#include #include namespace cds { namespace lock { @@ -66,12 +65,10 @@ namespace cds { namespace lock { : m_nMask( src.m_nMask ) {} -# ifdef CDS_RVALUE_SUPPORT /// Move constructor pow2_select_policy( pow2_select_policy&& src ) : m_nMask( src.m_nMask ) {} -# endif /// Returns nWhat & (nPow2 - 1) size_t operator()( size_t nWhat, size_t ) const @@ -142,11 +139,11 @@ namespace cds { namespace lock { // Only for internal use!!! array() - : m_arrLocks( null_ptr() ) + : m_arrLocks( nullptr ) , m_nCapacity(0) {} array( select_cell_policy const& policy ) - : m_arrLocks( null_ptr() ) + : m_arrLocks( nullptr ) , m_nCapacity(0) , m_SelectCellPolicy( policy ) {} @@ -160,7 +157,7 @@ namespace cds { namespace lock { array( size_t nCapacity ///< [in] Array size ) - : m_arrLocks( null_ptr() ) + : m_arrLocks( nullptr ) , m_nCapacity( nCapacity ) { m_arrLocks = create_lock_array( nCapacity ); @@ -174,14 +171,13 @@ namespace cds { namespace lock { size_t nCapacity, ///< [in] Array size select_cell_policy const& policy ///< Cell selection policy (copy-constructible) ) - : m_arrLocks( null_ptr() ) + : m_arrLocks( nullptr ) , m_nCapacity( nCapacity ) , m_SelectCellPolicy( policy ) { m_arrLocks = create_lock_array( m_nCapacity ); } -# ifdef CDS_RVALUE_SUPPORT /// Constructs array of lock and move cell selection policy /** Allocates the array and initializes all locks as unlocked. @@ -190,13 +186,12 @@ namespace cds { namespace lock { size_t nCapacity, ///< [in] Array size select_cell_policy&& policy ///< Cell selection policy (move-constructible) ) - : m_arrLocks( null_ptr() ) + : m_arrLocks( nullptr ) , m_nCapacity( nCapacity ) , m_SelectCellPolicy( std::forward( policy )) { m_arrLocks = create_lock_array( m_nCapacity ); } -# endif /// Destructs array of locks and frees used memory ~array() @@ -216,8 +211,7 @@ namespace cds { namespace lock { { size_t nCell = m_SelectCellPolicy( hint, size() ); assert( nCell < size() ); - lock_type& l = m_arrLocks[ nCell ]; - l.lock(); + m_arrLocks[nCell].lock(); return nCell; } @@ -233,8 +227,7 @@ namespace cds { namespace lock { { size_t nCell = m_SelectCellPolicy( hint, size() ); assert( nCell < size() ); - lock_type& l = m_arrLocks[ nCell ]; - if ( l.try_lock() ) + if ( m_arrLocks[nCell].try_lock() ) return nCell; return c_nUnspecifiedCell; } @@ -250,7 +243,7 @@ namespace cds { namespace lock { void lock_all() { lock_type * pLock = m_arrLocks; - for ( size_t i = 0; i < size(); ++i, ++pLock ) + for ( lock_type * pEnd = m_arrLocks + size(); pLock != pEnd; ++pLock ) pLock->lock(); } @@ -258,7 +251,7 @@ namespace cds { namespace lock { void unlock_all() { lock_type * pLock = m_arrLocks; - for ( size_t i = 0; i < size(); ++i, ++pLock ) + for ( lock_type * pEnd = m_arrLocks + size(); pLock != pEnd; ++pLock ) pLock->unlock(); } @@ -279,38 +272,44 @@ namespace cds { namespace lock { } }; +}} // namespace cds::lock + +//@cond +namespace std { + /// Specialization \ref scoped_lock for lock::array template - class scoped_lock< cds::lock::array< Lock, SelectPolicy, Alloc > >: public cds::details::noncopyable + class unique_lock< cds::lock::array< Lock, SelectPolicy, Alloc > > { public: - typedef cds::lock::array< Lock, SelectPolicy, Alloc > lock_array_type ; ///< Lock array type + typedef cds::lock::array< Lock, SelectPolicy, Alloc > lock_array_type; ///< Lock array type private: - //@cond lock_array_type& m_arrLocks; size_t m_nLockGuarded; - static const size_t c_nLockAll = ~size_t(0); - //@endcond + static const size_t c_nLockAll = ~size_t( 0 ); public: /// Onws the lock array \p arrLocks and locks a cell determined by \p hint parameter template - scoped_lock( lock_array_type& arrLocks, Q const& hint ) + unique_lock( lock_array_type& arrLocks, Q const& hint ) : m_arrLocks( arrLocks ) - , m_nLockGuarded( arrLocks.lock( hint )) + , m_nLockGuarded( arrLocks.lock( hint ) ) {} /// Locks all from \p arrLocks array - scoped_lock( lock_array_type& arrLocks ) + unique_lock( lock_array_type& arrLocks ) : m_arrLocks( arrLocks ) , m_nLockGuarded( c_nLockAll ) { arrLocks.lock_all(); } - ~scoped_lock() + unique_lock() = delete; + unique_lock( unique_lock const& ) = delete; + + ~unique_lock() { if ( m_nLockGuarded == c_nLockAll ) m_arrLocks.unlock_all(); @@ -318,7 +317,7 @@ namespace cds { namespace lock { m_arrLocks.unlock( m_nLockGuarded ); } }; - -}} // namespace cds::lock +} // namespace std +//@endcond #endif // #ifndef __CDS_LOCK_ARRAY_H