X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=cds%2Fcontainer%2Fimpl%2Fmichael_kvlist.h;h=f0c766a62e4648014568b4c7b6c55d4555a1d617;hb=6e2784d24f15873dc5b971499276cc8e4147fd46;hp=0043330db1112479d69668341e18eda4e788ddcd;hpb=325051d5af141257437148a4ded3c20b15c4d91a;p=libcds.git diff --git a/cds/container/impl/michael_kvlist.h b/cds/container/impl/michael_kvlist.h index 0043330d..f0c766a6 100644 --- a/cds/container/impl/michael_kvlist.h +++ b/cds/container/impl/michael_kvlist.h @@ -4,12 +4,11 @@ #define __CDS_CONTAINER_IMPL_MICHAEL_KVLIST_H #include -#include // ref #include namespace cds { namespace container { - /// Michael's ordered list (key-value pair) + /// Michael's ordered list fo key-value pair /** @ingroup cds_nonintrusive_list \anchor cds_nonintrusive_MichaelKVList_gc @@ -18,16 +17,17 @@ namespace cds { namespace container { constant key and alterable value. Usually, ordered single-linked list is used as a building block for the hash table implementation. - The complexity of searching is O(N). + The complexity of searching is O(N) where \p N is the item count in the list, not in the + hash table. Template arguments: - \p GC - garbage collector used - \p Key - key type of an item stored in the list. It should be copy-constructible - \p Value - value type stored in a list - - \p Traits - type traits, default is michael_list::type_traits + - \p Traits - type traits, default is \p michael_list::traits - It is possible to declare option-based list with cds::container::michael_list::make_traits metafunction istead of \p Traits template - argument. For example, the following traits-based declaration of gc::HP Michael's list + It is possible to declare option-based list with \p cds::container::michael_list::make_traits metafunction istead of \p Traits template + argument. For example, the following traits-based declaration of \p gc::HP Michael's list \code #include // Declare comparator for the item @@ -38,8 +38,8 @@ namespace cds { namespace container { } }; - // Declare type_traits - struct my_traits: public cds::container::michael_list::type_traits + // Declare traits + struct my_traits: public cds::container::michael_list::traits { typedef my_compare compare; }; @@ -47,7 +47,6 @@ namespace cds { namespace container { // Declare traits-based list typedef cds::container::MichaelKVList< cds::gc::HP, int, int, my_traits > traits_based_list; \endcode - is equivalent for the following option-based list \code #include @@ -62,16 +61,6 @@ namespace cds { namespace container { > option_based_list; \endcode - Template argument list \p Options of cds::container::michael_list::make_traits metafunction are: - - opt::compare - key comparison functor. No default functor is provided. - If the option is not specified, the opt::less is used. - - opt::less - specifies binary predicate used for key comparison. Default is \p std::less. - - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::empty is used. - - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting. - - opt::allocator - the allocator used for creating and freeing list's item. Default is \ref CDS_DEFAULT_ALLOCATOR macro. - - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default) - or opt::v::sequential_consistent (sequentially consisnent memory model). - \par Usage There are different specializations of this template for each garbage collecting schema used. You should include appropriate .h-file depending on GC you are using: @@ -85,7 +74,7 @@ namespace cds { namespace container { typename Key, typename Value, #ifdef CDS_DOXYGEN_INVOKED - typename Traits = michael_list::type_traits + typename Traits = michael_list::traits #else typename Traits #endif @@ -98,8 +87,8 @@ namespace cds { namespace container { #endif { //@cond - typedef details::make_michael_kvlist< GC, Key, Value, Traits > options; - typedef typename options::type base_class; + typedef details::make_michael_kvlist< GC, Key, Value, Traits > maker; + typedef typename maker::type base_class; //@endcond public: @@ -108,24 +97,24 @@ namespace cds { namespace container { typedef Value mapped_type ; ///< Type of value stored in the list typedef std::pair value_type ; ///< key/value pair stored in the list #else - typedef typename options::key_type key_type; - typedef typename options::value_type mapped_type; - typedef typename options::pair_type value_type; + typedef typename maker::key_type key_type; + typedef typename maker::value_type mapped_type; + typedef typename maker::pair_type value_type; #endif - typedef typename base_class::gc gc ; ///< Garbage collector used - typedef typename base_class::back_off back_off ; ///< Back-off strategy used - typedef typename options::allocator_type allocator_type ; ///< Allocator type used for allocate/deallocate the nodes - typedef typename base_class::item_counter item_counter ; ///< Item counting policy used - typedef typename options::key_comparator key_comparator ; ///< key comparison functor - typedef typename base_class::memory_model memory_model ; ///< Memory ordering. See cds::opt::memory_model option + typedef typename base_class::gc gc; ///< Garbage collector used + typedef typename base_class::back_off back_off; ///< Back-off strategy used + typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes + typedef typename base_class::item_counter item_counter; ///< Item counting policy used + typedef typename maker::key_comparator key_comparator; ///< key comparison functor + typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option protected: //@cond - typedef typename base_class::value_type node_type; - typedef typename options::cxx_allocator cxx_allocator; - typedef typename options::node_deallocator node_deallocator; - typedef typename options::type_traits::compare intrusive_key_comparator; + typedef typename base_class::value_type node_type; + typedef typename maker::cxx_allocator cxx_allocator; + typedef typename maker::node_deallocator node_deallocator; + typedef typename maker::intrusive_traits::compare intrusive_key_comparator; typedef typename base_class::atomic_node_ptr head_type; //@endcond @@ -257,7 +246,7 @@ namespace cds { namespace container { The forward iterator for Michael's list has some features: - it has no post-increment operator - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator. - For some GC (gc::HP, gc::HRC), a guard is limited resource per thread, so an exception (or assertion) "no free guard" + For some GC (\p gc::HP), a guard is limited resource per thread, so an exception (or assertion) "no free guard" may be thrown if a limit of guard count per thread is exceeded. - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data. - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent @@ -310,7 +299,7 @@ namespace cds { namespace container { { return const_iterator( head() ); } - const_iterator cbegin() + const_iterator cbegin() const { return const_iterator( head() ); } @@ -322,7 +311,7 @@ namespace cds { namespace container { { return const_iterator(); } - const_iterator cend() + const_iterator cend() const { return const_iterator(); } @@ -350,9 +339,9 @@ namespace cds { namespace container { The function creates a node with \p key and default value, and then inserts the node created into the list. Preconditions: - - The \ref key_type should be constructible from value of type \p K. - In trivial case, \p K is equal to \ref key_type. - - The \ref mapped_type should be default-constructible. + - The \p key_type should be constructible from value of type \p K. + In trivial case, \p K is equal to \p key_type. + - The \p mapped_type should be default-constructible. Returns \p true if inserting successful, \p false otherwise. */ @@ -367,8 +356,8 @@ namespace cds { namespace container { The function creates a node with \p key and value \p val, and then inserts the node created into the list. Preconditions: - - The \ref key_type should be constructible from \p key of type \p K. - - The \ref mapped_type should be constructible from \p val of type \p V. + - The \p key_type should be constructible from \p key of type \p K. + - The \p mapped_type should be constructible from \p val of type \p V. Returns \p true if inserting successful, \p false otherwise. */ @@ -392,21 +381,22 @@ namespace cds { namespace container { \endcode The argument \p item of user-defined functor \p func is the reference - to the list's item inserted. item.second is a reference to item's value that may be changed. + to the item inserted. item.second is a reference to item's value that may be changed. User-defined functor \p func should guarantee that during changing item's value no any other changes could be made on this list's item by concurrent threads. - The user-defined functor can be passed by reference using \p std::ref - and it is called only if inserting is successful. + The user-defined functor is called only if inserting is successful. - The key_type should be constructible from value of type \p K. + The \p key_type should be constructible from value of type \p K. The function allows to split creating of new item into two part: - - create item from \p key; - - insert new item into the list; + - create a new item from \p key; + - insert the new item into the list; - if inserting is successful, initialize the value of item by calling \p func functor This can be useful if complete initialization of object of \p mapped_type is heavyweight and it is preferable that the initialization should be completed only if inserting is successful. + + @warning See \ref cds_intrusive_item_creating "insert item troubleshooting" */ template bool insert_key( const K& key, Func func ) @@ -419,7 +409,7 @@ namespace cds { namespace container { The operation performs inserting or changing data with lock-free manner. If the \p key not found in the list, then the new item created from \p key - is inserted into the list (note that in this case the \ref key_type should be + is inserted into the list (note that in this case the \p key_type should be copy-constructible from type \p K). Otherwise, the functor \p func is called with item found. The functor \p Func may be a function with signature: @@ -437,15 +427,15 @@ namespace cds { namespace container { - \p bNew - \p true if the item has been inserted, \p false otherwise - \p item - item of the list - The functor may change any fields of the \p item.second that is \ref mapped_type; + The functor may change any fields of the \p item.second of \p mapped_type; however, \p func must guarantee that during changing no any other modifications could be made on this item by concurrent threads. - You may pass \p func argument by reference using \p std::ref - Returns std::pair where \p first is true if operation is successfull, \p second is true if new item has been added or \p false if the item with \p key already is in the list. + + @warning See \ref cds_intrusive_item_creating "insert item troubleshooting" */ template std::pair ensure( const K& key, Func f ) @@ -453,8 +443,11 @@ namespace cds { namespace container { return ensure_at( head(), key, f ); } - /// Inserts data of type \ref mapped_type constructed with std::forward(args)... + /// Inserts a new node using move semantics /** + \p key_type field of new item is constructed from \p key argument, + \p mapped_type field is done from \p args. + Returns \p true if inserting successful, \p false otherwise. */ template @@ -484,7 +477,8 @@ namespace cds { namespace container { template bool erase_with( K const& key, Less pred ) { - return erase_at( head(), key, typename options::template less_wrapper::type() ); + CDS_UNUSED( pred ); + return erase_at( head(), key, typename maker::template less_wrapper::type() ); } /// Deletes \p key from the list @@ -498,7 +492,6 @@ namespace cds { namespace container { void operator()(value_type& val) { ... } }; \endcode - The functor may be passed by reference with boost:ref Return \p true if key is found and deleted, \p false otherwise @@ -520,7 +513,8 @@ namespace cds { namespace container { template bool erase_with( K const& key, Less pred, Func f ) { - return erase_at( head(), key, typename options::template less_wrapper::type(), f ); + CDS_UNUSED( pred ); + return erase_at( head(), key, typename maker::template less_wrapper::type(), f ); } /// Extracts the item from the list with specified \p key @@ -569,7 +563,8 @@ namespace cds { namespace container { template bool extract_with( guarded_ptr& dest, K const& key, Less pred ) { - return extract_at( head(), dest.guard(), key, typename options::template less_wrapper::type() ); + CDS_UNUSED( pred ); + return extract_at( head(), dest.guard(), key, typename maker::template less_wrapper::type() ); } /// Finds the key \p key @@ -593,7 +588,8 @@ namespace cds { namespace container { template bool find_with( Q const& key, Less pred ) { - return find_at( head(), key, typename options::template less_wrapper::type() ); + CDS_UNUSED( pred ); + return find_at( head(), key, typename maker::template less_wrapper::type() ); } /// Finds the key \p key and performs an action with it @@ -607,8 +603,6 @@ namespace cds { namespace container { \endcode where \p item is the item found. - You may pass \p f argument by reference using \p std::ref - The functor may change item.second that is reference to value of node. Note that the function is only guarantee that \p item cannot be deleted during functor is executing. The function does not serialize simultaneous access to the list \p item. If such access is @@ -632,7 +626,8 @@ namespace cds { namespace container { template bool find_with( Q const& key, Less pred, Func f ) { - return find_at( head(), key, typename options::template less_wrapper::type(), f ); + CDS_UNUSED( pred ); + return find_at( head(), key, typename maker::template less_wrapper::type(), f ); } /// Finds the \p key and return the item found @@ -683,7 +678,8 @@ namespace cds { namespace container { template bool get_with( guarded_ptr& ptr, K const& key, Less pred ) { - return get_at( head(), ptr.guard(), key, typename options::template less_wrapper::type() ); + CDS_UNUSED( pred ); + return get_at( head(), ptr.guard(), key, typename maker::template less_wrapper::type() ); } /// Checks if the list is empty @@ -694,11 +690,11 @@ namespace cds { namespace container { /// Returns list's item count /** - The value returned depends on opt::item_counter option. For atomicity::empty_item_counter, + The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter, this function always returns 0. - Warning: even if you use real item counter and it returns 0, this fact is not mean that the list - is empty. To check list emptyness use \ref empty() method. + @note Even if you use real item counter and it returns 0, this fact is not mean that the list + is empty. To check list emptyness use \p empty() method. */ size_t size() const { @@ -706,9 +702,6 @@ namespace cds { namespace container { } /// Clears the list - /** - Post-condition: the list is empty - */ void clear() { base_class::clear();