2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_INTRUSIVE_OPTIMISTIC_QUEUE_H
32 #define CDSLIB_INTRUSIVE_OPTIMISTIC_QUEUE_H
34 #include <type_traits>
35 #include <cds/intrusive/details/base.h>
36 #include <cds/algo/atomic.h>
37 #include <cds/gc/default_gc.h>
39 namespace cds { namespace intrusive {
41 /// OptimisticQueue related definitions
42 /** @ingroup cds_intrusive_helper
44 namespace optimistic_queue {
46 /// Optimistic queue node
49 - \p GC - garbage collector
50 - \p Tag - a \ref cds_intrusive_hook_tag "tag"
52 template <class GC, typename Tag = opt::none>
55 typedef GC gc ; ///< Garbage collector
56 typedef Tag tag ; ///< tag
58 typedef typename gc::template atomic_ref<node> atomic_node_ptr ; ///< atomic pointer
60 atomic_node_ptr m_pNext ; ///< Pointer to next node
61 atomic_node_ptr m_pPrev ; ///< Pointer to previous node
63 CDS_CONSTEXPR node() CDS_NOEXCEPT
71 typedef cds::gc::default_gc gc;
72 typedef opt::none tag;
77 template < typename HookType, typename... Options>
80 typedef typename opt::make_options< default_hook, Options...>::type options;
81 typedef typename options::gc gc;
82 typedef typename options::tag tag;
83 typedef node<gc, tag> node_type;
84 typedef HookType hook_type;
91 - opt::gc - garbage collector used.
92 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
94 template < typename... Options >
95 struct base_hook: public hook< opt::base_hook_tag, Options... >
100 \p MemberOffset specifies offset in bytes of \ref node member into your structure.
101 Use \p offsetof macro to define \p MemberOffset
104 - opt::gc - garbage collector used.
105 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
107 template < size_t MemberOffset, typename... Options >
108 struct member_hook: public hook< opt::member_hook_tag, Options... >
111 static const size_t c_nMemberOffset = MemberOffset;
117 \p NodeTraits defines type traits for node.
118 See \ref node_traits for \p NodeTraits interface description
121 - opt::gc - garbage collector used.
122 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
124 template <typename NodeTraits, typename... Options >
125 struct traits_hook: public hook< opt::traits_hook_tag, Options... >
128 typedef NodeTraits node_traits;
133 template <typename Node>
134 struct link_checker {
136 typedef Node node_type;
139 /// Checks if the link fields of node \p pNode is \p nullptr
141 An asserting is generated if \p pNode link fields is not \p nullptr
143 static void is_empty( const node_type * pNode )
145 assert( pNode->m_pNext.load( atomics::memory_order_relaxed ) == nullptr );
146 assert( pNode->m_pPrev.load( atomics::memory_order_relaxed ) == nullptr );
151 /// Metafunction for selecting appropriate link checking policy
152 template < typename Node, opt::link_check_type LinkType >
153 struct get_link_checker
156 typedef intrusive::opt::v::empty_link_checker<Node> type;
161 template < typename Node >
162 struct get_link_checker< Node, opt::always_check_link >
164 typedef link_checker<Node> type;
166 template < typename Node >
167 struct get_link_checker< Node, opt::debug_check_link >
170 typedef link_checker<Node> type;
172 typedef intrusive::opt::v::empty_link_checker<Node> type;
177 /// OptimisticQueue internal statistics. May be used for debugging or profiling
179 Template argument \p Counter defines type of counter.
180 Default is \p cds::atomicity::event_counter, that is weak, i.e. it is not guaranteed
181 strict event counting.
182 You may use stronger type of counter like as \p cds::atomicity::item_counter,
183 or even integral type, for example, \p int.
185 template <typename Counter = cds::atomicity::event_counter >
188 typedef Counter counter_type; ///< Counter type
190 counter_type m_EnqueueCount; ///< Enqueue call count
191 counter_type m_DequeueCount; ///< Dequeue call count
192 counter_type m_EnqueueRace; ///< Count of enqueue race conditions encountered
193 counter_type m_DequeueRace; ///< Count of dequeue race conditions encountered
194 counter_type m_AdvanceTailError; ///< Count of "advance tail failed" events
195 counter_type m_BadTail; ///< Count of events "Tail is not pointed to the last item in the queue"
196 counter_type m_FixListCount; ///< Count of fix list event
197 counter_type m_EmptyDequeue; ///< Count of dequeue from empty queue
199 /// Register enqueue call
200 void onEnqueue() { ++m_EnqueueCount; }
201 /// Register dequeue call
202 void onDequeue() { ++m_DequeueCount; }
203 /// Register enqueue race event
204 void onEnqueueRace() { ++m_EnqueueRace; }
205 /// Register dequeue race event
206 void onDequeueRace() { ++m_DequeueRace; }
207 /// Register "advance tail failed" event
208 void onAdvanceTailFailed() { ++m_AdvanceTailError; }
209 /// Register event "Tail is not pointed to last item in the queue"
210 void onBadTail() { ++m_BadTail; }
211 /// Register fix list event
212 void onFixList() { ++m_FixListCount; }
213 /// Register dequeuing from empty queue
214 void onEmptyDequeue() { ++m_EmptyDequeue; }
219 m_EnqueueCount.reset();
220 m_DequeueCount.reset();
221 m_EnqueueRace.reset();
222 m_DequeueRace.reset();
223 m_AdvanceTailError.reset();
225 m_FixListCount.reset();
226 m_EmptyDequeue.reset();
229 stat& operator +=( stat const& s )
231 m_EnqueueCount += s.m_EnqueueCount.get();
232 m_DequeueCount += s.m_DequeueCount.get();
233 m_EnqueueRace += s.m_EnqueueRace.get();
234 m_DequeueRace += s.m_DequeueRace.get();
235 m_AdvanceTailError += s.m_AdvanceTailError.get();
236 m_BadTail += s.m_BadTail.get();
237 m_FixListCount += s.m_FixListCount.get();
238 m_EmptyDequeue += s.m_EmptyDequeue.get();
245 /// Dummy OptimisticQueue statistics - no counting is performed. Support interface like \ref optimistic_queue::stat
249 void onEnqueue() const {}
250 void onDequeue() const {}
251 void onEnqueueRace() const {}
252 void onDequeueRace() const {}
253 void onAdvanceTailFailed() const {}
254 void onBadTail() const {}
255 void onFixList() const {}
256 void onEmptyDequeue() const {}
259 empty_stat& operator +=( empty_stat const& )
266 /// OptimisticQueue default type traits
269 /// Back-off strategy
270 typedef cds::backoff::empty back_off;
272 /// Hook, possible types are \p optimistic_queue::base_hook, \p optimistic_queue::member_hook, \p optimistic_queue::traits_hook
273 typedef optimistic_queue::base_hook<> hook;
275 /// The functor used for dispose removed items. Default is \p opt::v::empty_disposer. This option is used for dequeuing
276 typedef opt::v::empty_disposer disposer;
278 /// Item counting feature; by default, disabled. Use \p cds::atomicity::item_counter to enable item counting
279 typedef cds::atomicity::empty_item_counter item_counter;
281 /// C++ memory ordering model
283 Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
284 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
286 typedef opt::v::relaxed_ordering memory_model;
288 /// Internal statistics (by default, disabled)
290 Possible option value are: \p optimistic_queue::stat, \p optimistic_queue::empty_stat (the default),
291 user-provided class that supports \p %optimistic_queue::stat interface.
293 typedef optimistic_queue::empty_stat stat;
295 /// Link checking, see \p cds::opt::link_checker
296 static CDS_CONSTEXPR const opt::link_check_type link_checker = opt::debug_check_link;
298 /// Padding for internal critical atomic data. Default is \p opt::cache_line_padding
299 enum { padding = opt::cache_line_padding };
302 /// Metafunction converting option list to \p optimistic_queue::traits
304 Supported \p Options are:
306 - \p opt::hook - hook used. Possible hooks are: \p optimistic_queue::base_hook, \p optimistic_queue::member_hook, \p optimistic_queue::traits_hook.
307 If the option is not specified, \p %optimistic_queue::base_hook<> is used.
308 - \p opt::back_off - back-off strategy used, default is \p cds::backoff::empty.
309 - \p opt::disposer - the functor used for dispose removed items. Default is \p opt::v::empty_disposer. This option is used
311 - \p opt::link_checker - the type of node's link fields checking. Default is \p opt::debug_check_link
312 - \p opt::item_counter - the type of item counting feature. Default is \p cds::atomicity::empty_item_counter (item counting disabled)
313 To enable item counting use \p cds::atomicity::item_counter
314 - \p opt::stat - the type to gather internal statistics.
315 Possible statistics types are: \p optimistic_queue::stat, \p optimistic_queue::empty_stat,
316 user-provided class that supports \p %optimistic_queue::stat interface.
317 Default is \p %optimistic_queue::empty_stat (internal statistics disabled).
318 - \p opt::padding - padding for internal critical atomic data. Default is \p opt::cache_line_padding
319 - \p opt::memory_model - C++ memory ordering model. Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
320 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
322 Example: declare \p %OptimisticQueue with item counting and internal statistics
324 typedef cds::intrusive::OptimisticQueue< cds::gc::HP, Foo,
325 typename cds::intrusive::optimistic_queue::make_traits<
326 cds::intrusive::opt:hook< cds::intrusive::optimistic_queue::base_hook< cds::opt::gc<cds:gc::HP> >>,
327 cds::opt::item_counte< cds::atomicity::item_counter >,
328 cds::opt::stat< cds::intrusive::optimistic_queue::stat<> >
333 template <typename... Options>
335 # ifdef CDS_DOXYGEN_INVOKED
336 typedef implementation_defined type; ///< Metafunction result
338 typedef typename cds::opt::make_options<
339 typename cds::opt::find_type_traits< traits, Options... >::type
344 } // namespace optimistic_queue
346 /// Optimistic intruive lock-free queue
347 /** @ingroup cds_intrusive_queue
348 Implementation of Ladan-Mozes & Shavit optimistic queue algorithm.
349 [2008] Edya Ladan-Mozes, Nir Shavit "An Optimistic Approach to Lock-Free FIFO Queues"
352 - \p GC - garbage collector type: \p gc::HP, \p gc::DHP
353 - \p T - type of value to be stored in the queue. A value of type \p T must be derived from \p optimistic_queue::node for \p optimistic_queue::base_hook,
354 or it should have a member of type \p %optimistic_queue::node for \p optimistic_queue::member_hook,
355 or it should be convertible to \p %optimistic_queue::node for \p optimistic_queue::traits_hook.
356 - \p Traits - queue traits, default is \p optimistic_queue::traits. You can use \p optimistic_queue::make_traits
357 metafunction to make your traits or just derive your traits from \p %optimistic_queue::traits:
359 struct myTraits: public cds::intrusive::optimistic_queue::traits {
360 typedef cds::intrusive::optimistic_queue::stat<> stat;
361 typedef cds::atomicity::item_counter item_counter;
363 typedef cds::intrusive::OptimisticQueue< cds::gc::HP, Foo, myTraits > myQueue;
365 // Equivalent make_traits example:
366 typedef cds::intrusive::OptimisticQueue< cds::gc::HP, Foo,
367 typename cds::intrusive::optimistic_queue::make_traits<
368 cds::opt::stat< cds::intrusive::optimistic_queue::stat<> >,
369 cds::opt::item_counter< cds::atomicity::item_counter >
374 Garbage collecting schema \p GC must be consistent with the optimistic_queue::node GC.
376 \par About item disposing
377 The optimistic queue algo has a key feature: even if the queue is empty it contains one item that is "dummy" one from
378 the standpoint of the algo. See \p dequeue() function for explanation.
382 #include <cds/gc/hp.h>
383 #include <cds/intrusive/optimistic_queue.h>
385 namespace ci = cds::inrtusive;
386 typedef cds::gc::HP hp_gc;
388 // Optimistic queue with Hazard Pointer garbage collector, base hook + item counter:
389 struct Foo: public ci::optimistic_queue::node< hp_gc >
395 typedef ci::OptimisticQueue< hp_gc,
397 typename ci::optimistic_queue::make_traits<
399 ci::optimistic_queue::base_hook< ci::opt::gc< hp_gc > >
401 ,cds::opt::item_counter< cds::atomicity::item_counter >
405 // Optimistic queue with Hazard Pointer garbage collector, member hook, no item counter:
410 ci::optimistic_queue::node< hp_gc > hMember;
413 typedef ci::OptimisticQueue< hp_gc,
415 typename ci::optimistic_queue::make_traits<
417 ci::optimistic_queue::member_hook<
418 offsetof(Bar, hMember)
419 ,ci::opt::gc< hp_gc >
426 template <typename GC, typename T, typename Traits = optimistic_queue::traits >
427 class OptimisticQueue
430 typedef GC gc; ///< Garbage collector
431 typedef T value_type; ///< type of value to be stored in the queue
432 typedef Traits traits; ///< Queue traits
434 typedef typename traits::hook hook; ///< hook type
435 typedef typename hook::node_type node_type; ///< node type
436 typedef typename traits::disposer disposer; ///< disposer used
437 typedef typename get_node_traits< value_type, node_type, hook>::type node_traits; ///< node traits
438 typedef typename optimistic_queue::get_link_checker< node_type, traits::link_checker >::type link_checker; ///< link checker
439 typedef typename traits::back_off back_off; ///< back-off strategy
440 typedef typename traits::item_counter item_counter; ///< Item counting policy used
441 typedef typename traits::memory_model memory_model;///< Memory ordering. See cds::opt::memory_model option
442 typedef typename traits::stat stat; ///< Internal statistics policy used
444 /// Rebind template arguments
445 template <typename GC2, typename T2, typename Traits2>
447 typedef OptimisticQueue< GC2, T2, Traits2 > other ; ///< Rebinding result
452 typedef typename node_type::atomic_node_ptr atomic_node_ptr;
454 // GC and node_type::gc must be the same
455 static_assert((std::is_same<gc, typename node_type::gc>::value), "GC and node_type::gc must be the same");
458 atomic_node_ptr m_pTail; ///< Pointer to tail node
460 typename opt::details::apply_padding< atomic_node_ptr, traits::padding >::padding_type pad1_;
462 atomic_node_ptr m_pHead; ///< Pointer to head node
464 typename opt::details::apply_padding< atomic_node_ptr, traits::padding >::padding_type pad2_;
466 node_type m_Dummy ; ///< dummy node
468 typename opt::details::apply_padding< atomic_node_ptr, traits::padding >::padding_type pad3_;
470 item_counter m_ItemCounter ; ///< Item counter
471 stat m_Stat ; ///< Internal statistics
473 static CDS_CONSTEXPR const size_t c_nHazardPtrCount = 5 ; ///< Count of hazard pointer required for the algorithm
477 static void clear_links( node_type * pNode )
479 pNode->m_pNext.store( nullptr, memory_model::memory_order_release );
480 pNode->m_pPrev.store( nullptr, memory_model::memory_order_release );
483 struct dequeue_result {
484 typename gc::template GuardArray<3> guards;
490 bool do_dequeue( dequeue_result& res )
494 node_type * pFirstNodePrev;
497 while ( true ) { // Try till success or empty
498 pHead = res.guards.protect( 0, m_pHead, [](node_type * p) -> value_type * {return node_traits::to_value_ptr(p);});
499 pTail = res.guards.protect( 1, m_pTail, [](node_type * p) -> value_type * {return node_traits::to_value_ptr(p);});
500 assert( pHead != nullptr );
501 pFirstNodePrev = res.guards.protect( 2, pHead->m_pPrev, [](node_type * p) -> value_type * {return node_traits::to_value_ptr(p);});
503 if ( pHead == m_pHead.load(memory_model::memory_order_acquire)) {
504 if ( pTail != pHead ) {
505 if ( pFirstNodePrev == nullptr
506 || pFirstNodePrev->m_pNext.load(memory_model::memory_order_acquire) != pHead )
508 fix_list( pTail, pHead );
511 if ( m_pHead.compare_exchange_weak( pHead, pFirstNodePrev, memory_model::memory_order_acquire, atomics::memory_order_relaxed )) {
517 // the queue is empty
518 m_Stat.onEmptyDequeue();
523 m_Stat.onDequeueRace();
531 res.pNext = pFirstNodePrev;
536 /// Helper function for optimistic queue. Corrects \p prev pointer of queue's nodes if it is needed
537 void fix_list( node_type * pTail, node_type * pHead )
539 // pTail and pHead are already guarded
541 node_type * pCurNode;
542 node_type * pCurNodeNext;
544 typename gc::template GuardArray<2> guards;
547 while ( pCurNode != pHead ) { // While not at head
548 pCurNodeNext = guards.protect(0, pCurNode->m_pNext, [](node_type * p) -> value_type * { return node_traits::to_value_ptr(p);});
549 if ( pHead != m_pHead.load(memory_model::memory_order_acquire))
551 pCurNodeNext->m_pPrev.store( pCurNode, memory_model::memory_order_release );
552 guards.assign( 1, node_traits::to_value_ptr( pCurNode = pCurNodeNext ));
558 void dispose_result( dequeue_result& res )
560 dispose_node( res.pHead );
563 void dispose_node( node_type * p )
565 assert( p != nullptr );
567 if ( p != &m_Dummy ) {
568 struct internal_disposer
570 void operator ()( value_type * p )
572 assert( p != nullptr );
574 OptimisticQueue::clear_links( node_traits::to_node_ptr( *p ) );
578 gc::template retire<internal_disposer>( node_traits::to_value_ptr(p) );
585 /// Constructor creates empty queue
587 : m_pTail( &m_Dummy )
588 , m_pHead( &m_Dummy )
594 node_type * pHead = m_pHead.load(memory_model::memory_order_relaxed);
595 CDS_DEBUG_ONLY( node_type * pTail = m_pTail.load(memory_model::memory_order_relaxed); )
596 CDS_DEBUG_ONLY( assert( pHead == pTail ); )
597 assert( pHead != nullptr );
599 m_pHead.store( nullptr, memory_model::memory_order_relaxed );
600 m_pTail.store( nullptr, memory_model::memory_order_relaxed );
602 dispose_node( pHead );
605 /// @anchor cds_intrusive_OptimisticQueue_enqueue Enqueues \p data in lock-free manner. Always return \a true
606 bool enqueue( value_type& val )
608 node_type * pNew = node_traits::to_node_ptr( val );
609 link_checker::is_empty( pNew );
611 typename gc::template GuardArray<2> guards;
614 guards.assign( 1, &val );
615 node_type * pTail = guards.protect( 0, m_pTail, [](node_type * p) -> value_type * {return node_traits::to_value_ptr(p);} ); // Read the tail
617 pNew->m_pNext.store( pTail, memory_model::memory_order_release );
618 if ( m_pTail.compare_exchange_strong( pTail, pNew, memory_model::memory_order_release, atomics::memory_order_relaxed )) { // Try to CAS the tail
619 pTail->m_pPrev.store( pNew, memory_model::memory_order_release ); // Success, write prev
622 break; // Enqueue done!
624 guards.assign( 0, node_traits::to_value_ptr( pTail )); // pTail has been changed by CAS above
625 m_Stat.onEnqueueRace();
631 /// Dequeues a value from the queue
632 /** @anchor cds_intrusive_OptimisticQueue_dequeue
633 If the queue is empty the function returns \p nullptr
636 The queue algorithm has following feature: when \p dequeue is called,
637 the item returning is still queue's top, and previous top is disposed:
640 before dequeuing Dequeue after dequeuing
641 +------------------+ +------------------+
642 Top ->| Item 1 | -> Dispose Item 1 | Item 2 | <- Top
643 +------------------+ +------------------+
644 | Item 2 | -> Return Item 2 | ... |
649 \p dequeue function returns Item 2, that becomes new top of queue, and calls
650 the disposer for Item 1, that was queue's top on function entry.
651 Thus, you cannot manually delete item returned because it is still included in
652 item sequence and it has valuable link field that must not be zeroed.
653 The item may be deleted only in disposer call.
655 value_type * dequeue()
658 if ( do_dequeue( res )) {
659 dispose_result( res );
661 return node_traits::to_value_ptr( *res.pNext );
666 /// Synonym for \p enqueue()
667 bool push( value_type& val )
669 return enqueue( val );
672 /// Synonym for \p dequeue()
678 /// Checks if the queue is empty
681 return m_pTail.load(memory_model::memory_order_relaxed) == m_pHead.load(memory_model::memory_order_relaxed);
686 The function repeatedly calls \ref dequeue until it returns \p nullptr.
687 The disposer defined in template \p Traits is called for each item
688 that can be safely disposed.
693 while ( (pv = dequeue()) != nullptr );
696 /// Returns queue's item count
698 The value returned depends on \p optimistic_queue::traits::item_counter.
699 For \p atomicity::empty_item_counter, this function always returns 0.
701 @note Even if you use real item counter and it returns 0, this fact is not mean that the queue
702 is empty. To check queue emptyness use \p empty() method.
706 return m_ItemCounter.value();
709 /// Returns refernce to internal statistics
710 const stat& statistics() const
716 }} // namespace cds::intrusive
718 #endif // #ifndef CDSLIB_INTRUSIVE_OPTIMISTIC_QUEUE_H