X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=cds%2Fintrusive%2Fstriped_set.h;h=30d41fcd985385470f49344948f33d11483501dd;hb=59cb651402874a22500cab3ec586565b48f76059;hp=8445a1119c8f498e7edb9d5d28592fa71c7d5b0e;hpb=4b5852dc7e861bb26c6af06c2a1e058f87f703ae;p=libcds.git diff --git a/cds/intrusive/striped_set.h b/cds/intrusive/striped_set.h index 8445a111..30d41fcd 100644 --- a/cds/intrusive/striped_set.h +++ b/cds/intrusive/striped_set.h @@ -1,4 +1,32 @@ -//$$CDS-header$$ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ #ifndef CDSLIB_INTRUSIVE_STRIPED_SET_H #define CDSLIB_INTRUSIVE_STRIPED_SET_H @@ -64,8 +92,8 @@ namespace cds { namespace intrusive { Note that the choose of resizing policy depends of \p Container type: for sequential containers like \p boost::intrusive::list the right policy can significantly improve performance. For other, non-sequential types of \p Container (like a \p boost::intrusive::set) the resizing policy is not so important. - - \p cds::opt::buffer - a buffer type used only for \p boost::intrusive::unordered_set. - Default is cds::opt::v::static_buffer< cds::any_type, 256 > . + - \p cds::opt::buffer - an initialized buffer type used only for \p boost::intrusive::unordered_set. + Default is cds::opt::v::initialized_static_buffer< cds::any_type, 256 > . \p opt::compare or \p opt::less options are used in some \p Container class for ordering. \p %opt::compare option has the highest priority: if \p %opt::compare is specified, \p %opt::less is not used. @@ -167,7 +195,8 @@ namespace cds { namespace intrusive { You should provide two different hash function \p h1 and \p h2 - one for \p boost::intrusive::unordered_set and other for \p %StripedSet. For the best result, \p h1 and \p h2 must be orthogonal i.e. h1(X) != h2(X) for any value \p X - The option \p opt::buffer is used for \p boost::intrusive::bucket_traits. Default is cds::opt::v::static_buffer< cds::any_type, 256 > . + The option \p opt::buffer is used for \p boost::intrusive::bucket_traits. + Default is cds::opt::v::initialized_static_buffer< cds::any_type, 256 > . The resizing policy should correlate with the buffer capacity. The default resizing policy is cds::container::striped_set::load_factor_resizing<256> what gives load factor 1 for default bucket buffer that is the best for \p boost::intrusive::unordered_set. @@ -297,10 +326,10 @@ namespace cds { namespace intrusive { typedef cds::details::Allocator< bucket_type, allocator_type > bucket_allocator; ///< bucket allocator type based on allocator_type protected: - bucket_type * m_Buckets ; ///< Bucket table - size_t m_nBucketMask ; ///< Bucket table size - 1. m_nBucketMask + 1 should be power of two. - item_counter m_ItemCounter ; ///< Item counter - hash m_Hash ; ///< Hash functor + bucket_type * m_Buckets; ///< Bucket table + atomics::atomic m_nBucketMask; ///< Bucket table size - 1. m_nBucketMask + 1 should be power of two. + item_counter m_ItemCounter; ///< Item counter + hash m_Hash; ///< Hash functor mutex_policy m_MutexPolicy ; ///< Mutex policy resizing_policy m_ResizingPolicy; ///< Resizing policy @@ -325,7 +354,7 @@ namespace cds { namespace intrusive { void alloc_bucket_table( size_t nSize ) { assert( cds::beans::is_power2( nSize )); - m_nBucketMask = nSize - 1; + m_nBucketMask.store( nSize - 1, atomics::memory_order_release ); m_Buckets = bucket_allocator().NewArray( nSize ); } @@ -342,7 +371,7 @@ namespace cds { namespace intrusive { bucket_type * bucket( size_t nHash ) const CDS_NOEXCEPT { - return m_Buckets + (nHash & m_nBucketMask); + return m_Buckets + (nHash & m_nBucketMask.load( atomics::memory_order_relaxed )); } template @@ -380,7 +409,7 @@ namespace cds { namespace intrusive { for ( bucket_iterator it = pCur->begin(); it != itEnd; it = itNext ) { itNext = it; ++itNext; - bucket( m_Hash( *it ) )->move_item( *pCur, it ); + bucket( m_Hash( *it ))->move_item( *pCur, it ); } pCur->clear(); } @@ -392,12 +421,11 @@ namespace cds { namespace intrusive { void resize() { - size_t nOldCapacity = bucket_count(); - size_t volatile& refBucketMask = m_nBucketMask; + size_t nOldCapacity = bucket_count( atomics::memory_order_acquire ); scoped_resize_lock al( m_MutexPolicy ); - if ( al.success() ) { - if ( nOldCapacity != refBucketMask + 1 ) { + if ( al.success()) { + if ( nOldCapacity != bucket_count( atomics::memory_order_acquire )) { // someone resized already return; } @@ -412,21 +440,21 @@ namespace cds { namespace intrusive { /// Default ctor. The initial capacity is 16. StripedSet() : m_Buckets( nullptr ) - , m_nBucketMask( c_nMinimalCapacity - 1 ) - , m_MutexPolicy( c_nMinimalCapacity ) + , m_nBucketMask( c_nMinimalCapacity - 1 ) + , m_MutexPolicy( c_nMinimalCapacity ) { - alloc_bucket_table( m_nBucketMask + 1 ); + alloc_bucket_table( bucket_count()); } /// Ctor with initial capacity specified StripedSet( size_t nCapacity ///< Initial size of bucket table and lock array. Must be power of two, the minimum is 16. ) - : m_Buckets( nullptr ) - , m_nBucketMask( calc_init_capacity(nCapacity) - 1 ) - , m_MutexPolicy( m_nBucketMask + 1 ) + : m_Buckets( nullptr ) + , m_nBucketMask( calc_init_capacity(nCapacity) - 1 ) + , m_MutexPolicy( bucket_count()) { - alloc_bucket_table( m_nBucketMask + 1 ); + alloc_bucket_table( bucket_count()); } /// Ctor with resizing policy (copy semantics) @@ -439,10 +467,10 @@ namespace cds { namespace intrusive { ) : m_Buckets( nullptr ) , m_nBucketMask( ( nCapacity ? calc_init_capacity(nCapacity) : c_nMinimalCapacity ) - 1 ) - , m_MutexPolicy( m_nBucketMask + 1 ) + , m_MutexPolicy( bucket_count()) , m_ResizingPolicy( resizingPolicy ) { - alloc_bucket_table( m_nBucketMask + 1 ); + alloc_bucket_table( bucket_count()); } /// Ctor with resizing policy (move semantics) @@ -456,16 +484,16 @@ namespace cds { namespace intrusive { ) : m_Buckets( nullptr ) , m_nBucketMask( ( nCapacity ? calc_init_capacity(nCapacity) : c_nMinimalCapacity ) - 1 ) - , m_MutexPolicy( m_nBucketMask + 1 ) - , m_ResizingPolicy( std::forward( resizingPolicy ) ) + , m_MutexPolicy( bucket_count()) + , m_ResizingPolicy( std::forward( resizingPolicy )) { - alloc_bucket_table( m_nBucketMask + 1 ); + alloc_bucket_table( bucket_count()); } /// Destructor destroys internal data ~StripedSet() { - free_bucket_table( m_Buckets, m_nBucketMask + 1 ); + free_bucket_table( m_Buckets, bucket_count()); } public: @@ -533,7 +561,7 @@ namespace cds { namespace intrusive { The functor may change non-key fields of the \p item. - Returns std::pair where \p first is \p true if operation is successfull, + Returns std::pair where \p first is \p true if operation is successful, \p second is \p true if new item has been added or \p false if the item with \p val already is in the set. */ @@ -849,8 +877,14 @@ namespace cds { namespace intrusive { */ size_t bucket_count() const { - return m_nBucketMask + 1; + return m_nBucketMask.load( atomics::memory_order_relaxed ) + 1; + } + //@cond + size_t bucket_count( atomics::memory_order load_mo ) const + { + return m_nBucketMask.load( load_mo ) + 1; } + //@endcond /// Returns lock array size size_t lock_count() const