3 #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_RCU_H
4 #define CDSLIB_CONTAINER_SPLIT_LIST_MAP_RCU_H
6 #include <cds/container/split_list_set_rcu.h>
7 #include <cds/details/binary_functor_wrapper.h>
9 namespace cds { namespace container {
11 /// Split-ordered list map (template specialization for \ref cds_urcu_desc "RCU")
12 /** @ingroup cds_nonintrusive_map
13 \anchor cds_nonintrusive_SplitListMap_rcu
15 Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
16 - [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"
17 - [2008] Nir Shavit "The Art of Multiprocessor Programming"
19 See intrusive::SplitListSet for a brief description of the split-list algorithm.
22 - \p RCU - one of \ref cds_urcu_gc "RCU type"
23 - \p Key - key type to be stored in the map
24 - \p Value - value type to be stored in the map
25 - \p Traits - type traits, default is \p split_list::traits. Instead of declaring \p %split_list::traits -based
26 struct you may apply option-based notation with \p split_list::make_traits metafunction.
30 The class supports a forward unordered iterator (\ref iterator and \ref const_iterator).
31 You may iterate over split-list map items only under RCU lock.
32 Only in this case the iterator is thread-safe since
33 while RCU is locked any map's item cannot be reclaimed.
34 The requirement of RCU lock during iterating means that deletion of the elements
37 @warning The iterator object cannot be passed between threads.
38 Due to concurrent nature of split-list map it is not guarantee that you can iterate
39 all elements in the map: any concurrent deletion can exclude the element
40 pointed by the iterator from the map, and your iteration can be terminated
41 before end of the map. Therefore, such iteration is more suitable for debugging purposes
43 The iterator class supports the following minimalistic interface:
50 iterator( iterator const& s);
52 value_type * operator ->() const;
53 value_type& operator *() const;
56 iterator& operator ++();
59 iterator& operator = (const iterator& src);
61 bool operator ==(iterator const& i ) const;
62 bool operator !=(iterator const& i ) const;
65 Note, the iterator object returned by \ref end, \p cend member functions points to \p nullptr and should not be dereferenced.
69 You should decide what garbage collector you want, and what ordered list you want to use. Split-ordered list
70 is original data structure based on an ordered list. Suppose, you want construct split-list map based on \p cds::urcu::general_buffered<> GC
71 and \p MichaelList as ordered list implementation. Your map should map \p int key to \p std::string value.
72 So, you beginning your program with following include:
74 #include <cds/urcu/general_buffered.h>
75 #include <cds/container/michael_list_rcu.h>
76 #include <cds/container/split_list_map_rcu.h>
78 namespace cc = cds::container;
80 The inclusion order is important:
81 - first, include one of \ref cds_urcu_gc "RCU implementation" (<tt>cds/urcu/general_buffered.h</tt> in our case)
82 - second, include the header of ordered-list implementation (for this example, <tt>cds/container/michael_list_rcu.h</tt>),
83 - then, the header for RCU-based split-list map <tt>cds/container/split_list_map_rcu.h</tt>.
85 Now, you should declare traits for split-list map. The main parts of traits are a hash functor for the map key and a comparing functor for ordered list.
86 We use \p std::hash<int> and \p std::less<int>.
88 The second attention: instead of using \p %MichaelList in \p %SplitListMap traits we use a tag \p ds::contaner::michael_list_tag
89 for the Michael's list.
90 The split-list requires significant support from underlying ordered list class and it is not good idea to dive you
91 into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.
94 // SplitListMap traits
95 struct foo_set_traits: public cc::split_list::traits
97 typedef cc::michael_list_tag ordered_list ; // what type of ordered list we want to use
98 typedef std::hash<int> hash ; // hash functor for the key stored in split-list map
100 // Type traits for our MichaelList class
101 struct ordered_list_traits: public cc::michael_list::traits
103 typedef std::less<int> less ; // use our std::less predicate as comparator to order list nodes
108 Now you are ready to declare our map class based on \p %SplitListMap:
110 typedef cc::SplitListMap< cds::urcu::gc<cds::urcu::general_buffered<> >, int, std::string, foo_set_traits > int_string_map;
113 You may use the modern option-based declaration instead of classic traits-based one:
115 typedef cc::SplitListMap<
116 cds::urcu::gc<cds::urcu::general_buffered<> > // RCU type
118 ,std::string // value type
119 ,cc::split_list::make_traits< // metafunction to build split-list traits
120 cc::split_list::ordered_list<cc::michael_list_tag> // tag for underlying ordered list implementation
121 ,cc::opt::hash< std::hash<int> > // hash functor
122 ,cc::split_list::ordered_list_traits< // ordered list traits desired
123 cc::michael_list::make_traits< // metafunction to build lazy list traits
124 cc::opt::less< std::less<int> > // less-based compare functor
130 In case of option-based declaration using \p split_list::make_traits metafunction the struct \p foo_set_traits is not required.
132 Now, the map of type \p int_string_map is ready to use in your program.
134 Note that in this example we show only mandatory \p traits parts, optional ones is the default and they are inherited
135 from cds::container::split_list::traits.
136 There are many other useful options for deep tuning the split-list and ordered-list containers.
142 #ifdef CDS_DOXYGEN_INVOKED
143 class Traits = split_list::traits
148 class SplitListMap< cds::urcu::gc< RCU >, Key, Value, Traits >:
149 protected container::SplitListSet<
150 cds::urcu::gc< RCU >,
151 std::pair<Key const, Value>,
152 split_list::details::wrap_map_traits<Key, Value, Traits>
156 typedef container::SplitListSet<
157 cds::urcu::gc< RCU >,
158 std::pair<Key const, Value>,
159 split_list::details::wrap_map_traits<Key, Value, Traits>
164 typedef cds::urcu::gc< RCU > gc; ///< Garbage collector
165 typedef Key key_type; ///< key type
166 typedef Value mapped_type; ///< type of value to be stored in the map
167 typedef Traits traits; ///< Map traits
169 typedef std::pair<key_type const, mapped_type> value_type; ///< key-value pair type
170 typedef typename base_class::ordered_list ordered_list; ///< Underlying ordered list class
171 typedef typename base_class::key_comparator key_comparator; ///< key comparison functor
173 typedef typename base_class::hash hash; ///< Hash functor for \ref key_type
174 typedef typename base_class::item_counter item_counter; ///< Item counter type
175 typedef typename base_class::stat stat; ///< Internal statistics
177 typedef typename base_class::rcu_lock rcu_lock; ///< RCU scoped lock
178 typedef typename base_class::exempt_ptr exempt_ptr; ///< pointer to extracted node
179 /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
180 static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal;
181 typedef typename base_class::raw_ptr raw_ptr; ///< type of \p get() return value
184 typedef cds::container::split_list::implementation_tag implementation_tag;
189 typedef typename base_class::maker::traits::key_accessor key_accessor;
194 typedef typename base_class::iterator iterator;
196 /// Const forward iterator
197 typedef typename base_class::const_iterator const_iterator;
199 /// Returns a forward iterator addressing the first element in a map
201 For empty map \code begin() == end() \endcode
205 return base_class::begin();
208 /// Returns an iterator that addresses the location succeeding the last element in a map
210 Do not use the value returned by <tt>end</tt> function to access any item.
211 The returned value can be used only to control reaching the end of the map.
212 For empty map \code begin() == end() \endcode
216 return base_class::end();
219 /// Returns a forward const iterator addressing the first element in a map
221 const_iterator begin() const
223 return base_class::begin();
225 const_iterator cbegin() const
227 return base_class::cbegin();
231 /// Returns an const iterator that addresses the location succeeding the last element in a map
233 const_iterator end() const
235 return base_class::end();
237 const_iterator cend() const
239 return base_class::cend();
244 /// Initializes split-ordered map of default capacity
246 The default capacity is defined in bucket table constructor.
247 See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_bucket_table
248 which selects by \p split_list::dynamic_bucket_table option.
254 /// Initializes split-ordered map
256 size_t nItemCount ///< estimated average item count
257 , size_t nLoadFactor = 1 ///< load factor - average item count per bucket. Small integer up to 10, default is 1.
259 : base_class( nItemCount, nLoadFactor )
263 /// Inserts new node with key and default value
265 The function creates a node with \p key and the default value, and then inserts the node created into the map.
268 - The \p key_type should be constructible from value of type \p K.
269 - The \p mapped_type should be default-constructible.
271 The function applies RCU lock internally.
273 Returns \p true if inserting successful, \p false otherwise.
275 template <typename K>
276 bool insert( K const& key )
278 //TODO: pass arguments by reference (make_pair makes copy)
279 return base_class::insert( std::make_pair( key, mapped_type() ) );
284 The function creates a node with copy of \p val value
285 and then inserts the node into the map.
288 - The \p key_type should be constructible from \p key of type \p K.
289 - The \p mapped_type should be constructible from \p val of type \p V.
291 The function applies RCU lock internally.
293 Returns \p true if \p val is inserted into the map, \p false otherwise.
295 template <typename K, typename V>
296 bool insert( K const& key, V const& val )
298 //TODO: pass arguments by reference (make_pair makes copy)
299 return base_class::insert( std::make_pair(key, val) );
302 /// Inserts new node and initialize it by a functor
304 This function inserts new node with key \p key and if inserting is successful then it calls
305 \p func functor with signature
308 void operator()( value_type& item );
312 The argument \p item of user-defined functor \p func is the reference
313 to the map's item inserted:
314 - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
315 - <tt>item.second</tt> is a reference to item's value that may be changed.
317 It should be keep in mind that concurrent modifications of \p <tt>item.second</tt> in \p func body
318 should be careful. You shouldf guarantee that during changing item's value in \p func no any other changes
319 could be made on this \p item by concurrent threads.
321 \p func is called only if inserting is successful.
323 The function allows to split creating of new item into two part:
324 - create item from \p key;
325 - insert new item into the map;
326 - if inserting is successful, initialize the value of item by calling \p func functor
328 This can be useful if complete initialization of object of \p mapped_type is heavyweight and
329 it is preferable that the initialization should be completed only if inserting is successful.
331 The function applies RCU lock internally.
333 template <typename K, typename Func>
334 bool insert_with( K const& key, Func func )
336 //TODO: pass arguments by reference (make_pair makes copy)
337 return base_class::insert( std::make_pair( key, mapped_type() ), func );
340 /// For key \p key inserts data of type \p mapped_type created in-place from \p args
342 \p key_type should be constructible from type \p K
344 The function applies RCU lock internally.
346 Returns \p true if inserting successful, \p false otherwise.
348 template <typename K, typename... Args>
349 bool emplace( K&& key, Args&&... args )
351 return base_class::emplace( std::forward<K>(key), std::move(mapped_type(std::forward<Args>(args)...)));
354 /// Ensures that the \p key exists in the map
356 The operation performs inserting or changing data with lock-free manner.
358 If the \p key not found in the map, then the new item created from \p key
359 is inserted into the map; in this case the \p key_type should be
360 constructible from type \p K.
361 Otherwise, the functor \p func is called with item found.
362 The functor \p Func signature is:
365 void operator()( bool bNew, value_type& item );
369 - \p bNew - \p true if the item has been inserted, \p false otherwise
370 - \p item - item of the list
372 The functor may change any fields of the \p item.second that is \p mapped_type;
373 however, \p func must guarantee that during changing no any other modifications
374 could be made on this item by concurrent threads.
376 The function applies RCU lock internally.
378 Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
379 \p second is true if new item has been added or \p false if the item with \p key
380 already is in the list.
382 @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" as the ordered list see \ref cds_intrusive_item_creating "insert item troubleshooting".
383 \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level
386 template <typename K, typename Func>
387 std::pair<bool, bool> ensure( K const& key, Func func )
389 //TODO: pass arguments by reference (make_pair makes copy)
390 return base_class::ensure( std::make_pair( key, mapped_type() ),
391 [&func](bool bNew, value_type& item, value_type const& /*val*/) {
396 /// Deletes \p key from the map
397 /** \anchor cds_nonintrusive_SplitListMap_rcu_erase_val
399 RCU \p synchronize method can be called. RCU should not be locked.
401 Return \p true if \p key is found and deleted, \p false otherwise
403 template <typename K>
404 bool erase( K const& key )
406 return base_class::erase( key );
409 /// Deletes the item from the map using \p pred predicate for searching
411 The function is an analog of \ref cds_nonintrusive_SplitListMap_rcu_erase_val "erase(K const&)"
412 but \p pred is used for key comparing.
413 \p Less functor has the interface like \p std::less.
414 \p Less must imply the same element order as the comparator used for building the map.
416 template <typename K, typename Less>
417 bool erase_with( K const& key, Less pred )
420 return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
423 /// Deletes \p key from the map
424 /** \anchor cds_nonintrusive_SplitListMap_rcu_erase_func
426 The function searches an item with key \p key, calls \p f functor
427 and deletes the item. If \p key is not found, the functor is not called.
429 The functor \p Func interface is:
432 void operator()(value_type& item) { ... }
436 RCU \p synchronize method can be called. RCU should not be locked.
438 Return \p true if key is found and deleted, \p false otherwise
440 template <typename K, typename Func>
441 bool erase( K const& key, Func f )
443 return base_class::erase( key, f );
446 /// Deletes the item from the map using \p pred predicate for searching
448 The function is an analog of \ref cds_nonintrusive_SplitListMap_rcu_erase_func "erase(K const&, Func)"
449 but \p pred is used for key comparing.
450 \p Less functor has the interface like \p std::less.
451 \p Less must imply the same element order as the comparator used for building the map.
453 template <typename K, typename Less, typename Func>
454 bool erase_with( K const& key, Less pred, Func f )
457 return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>(), f );
460 /// Extracts an item from the map
461 /** \anchor cds_nonintrusive_SplitListMap_rcu_extract
462 The function searches an item with key equal to \p key in the map,
463 unlinks it from the map, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
464 If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
466 Depends on ordered list you should or should not lock RCU before calling of this function:
467 - for the set based on \ref cds_intrusive_MichaelList_rcu "MichaelList" RCU should not be locked
468 - for the set based on \ref cds_intrusive_LazyList_rcu "LazyList" RCU should be locked
469 See ordered list implementation for details.
472 typedef cds::urcu::gc< general_buffered<> > rcu;
474 // Split-list set based on MichaelList by default
475 typedef cds::container::SplitListMap< rcu, int, Foo > splitlist_map;
477 splitlist_map theMap;
480 typename splitlist_map::exempt_ptr p;
482 // For MichaelList we should not lock RCU
484 // Now, you can apply extract function
485 p = theMap.extract( 10 )
487 // do something with p
491 // We may safely release p here
492 // release() passes the pointer to RCU reclamation cycle
496 template <typename K>
497 exempt_ptr extract( K const& key )
499 return base_class::extract( key );
502 /// Extracts an item from the map using \p pred predicate for searching
504 The function is an analog of \p extract(K const&) but \p pred is used for key comparing.
505 \p Less functor has the interface like \p std::less.
506 \p pred must imply the same element order as the comparator used for building the map.
508 template <typename K, typename Less>
509 exempt_ptr extract_with( K const& key, Less pred )
512 return base_class::extract_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
515 /// Finds the key \p key
516 /** \anchor cds_nonintrusive_SplitListMap_rcu_find_cfunc
518 The function searches the item with key equal to \p key and calls the functor \p f for item found.
519 The interface of \p Func functor is:
522 void operator()( value_type& item );
525 where \p item is the item found.
527 The functor may change \p item.second. Note that the functor is only guarantee
528 that \p item cannot be disposed during functor is executing.
529 The functor does not serialize simultaneous access to the map's \p item. If such access is
530 possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
532 The function applies RCU lock internally.
534 The function returns \p true if \p key is found, \p false otherwise.
536 template <typename K, typename Func>
537 bool find( K const& key, Func f )
539 return base_class::find( key, [&f](value_type& pair, K const&){ f( pair ); } );
542 /// Finds the key \p key using \p pred predicate for searching
544 The function is an analog of \ref cds_nonintrusive_SplitListMap_rcu_find_cfunc "find(K const&, Func)"
545 but \p pred is used for key comparing.
546 \p Less functor has the interface like \p std::less.
547 \p Less must imply the same element order as the comparator used for building the map.
549 template <typename K, typename Less, typename Func>
550 bool find_with( K const& key, Less pred, Func f )
553 return base_class::find_with( key,
554 cds::details::predicate_wrapper<value_type, Less, key_accessor>(),
555 [&f](value_type& pair, K const&){ f( pair ); } );
558 /// Finds the key \p key
559 /** \anchor cds_nonintrusive_SplitListMap_rcu_find_val
561 The function searches the item with key equal to \p key
562 and returns \p true if it is found, and \p false otherwise.
564 The function applies RCU lock internally.
566 template <typename K>
567 bool find( K const& key )
569 return base_class::find( key );
572 /// Finds the key \p key using \p pred predicate for searching
574 The function is an analog of \ref cds_nonintrusive_SplitListMap_rcu_find_val "find(K const&)"
575 but \p pred is used for key comparing.
576 \p Less functor has the interface like \p std::less.
577 \p Less must imply the same element order as the comparator used for building the map.
579 template <typename K, typename Less>
580 bool find_with( K const& key, Less pred )
583 return base_class::find_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
586 /// Finds \p key and return the item found
587 /** \anchor cds_intrusive_SplitListMap_rcu_get
588 The function searches the item with key equal to \p key and returns the pointer to item found.
589 If \p key is not found it returns empty \p raw_ptr.
591 Note the compare functor should accept a parameter of type \p K that can be not the same as \p value_type.
593 RCU should be locked before call of this function.
594 Returned item is valid only while RCU is locked:
596 typedef cds::urcu::gc< general_buffered<> > rcu;
597 typedef cds::container::SplitListMap< rcu, int, Foo > splitlist_map;
598 splitlist_map theMap;
602 typename splitlist_map::rcu_lock lock;
604 typename splitlist_map::raw_ptr pVal = theMap.get( 5 );
609 // Unlock RCU by rcu_lock destructor
610 // pVal can be retired by disposer at any time after RCU has been unlocked
614 template <typename K>
615 raw_ptr get( K const& key )
617 return base_class::get( key );
620 /// Finds \p key with predicate specified and return the item found
622 The function is an analog of \ref cds_intrusive_SplitListMap_rcu_get "get(K const&)"
623 but \p pred is used for comparing the keys.
625 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p K
627 \p pred must imply the same element order as the comparator used for building the map.
629 template <typename K, typename Less>
630 raw_ptr get_with( K const& key, Less pred )
633 return base_class::get_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
636 /// Clears the map (not atomic)
642 /// Checks if the map is empty
644 Emptiness is checked by item counting: if item count is zero then the map is empty.
645 Thus, the correct item counting is an important part of the map implementation.
649 return base_class::empty();
652 /// Returns item count in the map
655 return base_class::size();
658 /// Returns internal statistics
659 stat const& statistics() const
661 return base_class::statistics();
665 }} // namespace cds::container
667 #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_RCU_H