3 #ifndef CDSLIB_INTRUSIVE_TREIBER_STACK_H
4 #define CDSLIB_INTRUSIVE_TREIBER_STACK_H
7 #include <mutex> // unique_lock
8 #include <cds/intrusive/details/single_link_struct.h>
9 #include <cds/algo/elimination.h>
10 #include <cds/opt/buffer.h>
11 #include <cds/sync/spinlock.h>
12 #include <cds/details/type_padding.h>
14 namespace cds { namespace intrusive {
16 /// TreiberStack related definitions
17 /** @ingroup cds_intrusive_helper
19 namespace treiber_stack {
24 - GC - garbage collector used
25 - Tag - a \ref cds_intrusive_hook_tag "tag"
27 template <class GC, typename Tag = opt::none >
28 using node = cds::intrusive::single_link::node< GC, Tag >;
33 - opt::gc - garbage collector used.
34 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
36 template < typename... Options >
37 using base_hook = cds::intrusive::single_link::base_hook< Options...>;
41 \p MemberOffset specifies offset in bytes of \ref node member into your structure.
42 Use \p offsetof macro to define \p MemberOffset
45 - opt::gc - garbage collector used.
46 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
48 template < size_t MemberOffset, typename... Options >
49 using member_hook = cds::intrusive::single_link::member_hook< MemberOffset, Options... >;
53 \p NodeTraits defines type traits for node.
54 See \ref node_traits for \p NodeTraits interface description
57 - opt::gc - garbage collector used.
58 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
60 template <typename NodeTraits, typename... Options >
61 using traits_hook = cds::intrusive::single_link::traits_hook< NodeTraits, Options... >;
64 /// Operation id for the \ref cds_elimination_description "elimination back-off"
66 op_push, ///< push op id
70 /// Operation descriptor for the \ref cds_elimination_description "elimination back-off"
72 struct operation: public cds::algo::elimination::operation_desc
74 operation_id idOp; ///< Op id
75 T * pVal; ///< for push: pointer to argument; for pop: accepts a return value
76 atomics::atomic<unsigned int> nStatus; ///< Internal elimination status
85 /// Stack internal statistics. May be useful for debugging or profiling
87 Template argument \p Counter defines type of counter.
88 Default is cds::atomicity::event_counter.
89 You may use stronger type of counter like as cds::atomicity::item_counter,
90 or even an integral type, for example, \p int
92 template <typename Counter = cds::atomicity::event_counter >
95 typedef Counter counter_type ; ///< Counter type
97 counter_type m_PushCount ; ///< Push call count
98 counter_type m_PopCount ; ///< Pop call count
99 counter_type m_PushRace ; ///< Count of push race conditions encountered
100 counter_type m_PopRace ; ///< Count of pop race conditions encountered
101 counter_type m_ActivePushCollision ; ///< Count of active push collision for elimination back-off
102 counter_type m_ActivePopCollision ; ///< Count of active pop collision for elimination back-off
103 counter_type m_PassivePushCollision ; ///< Count of passive push collision for elimination back-off
104 counter_type m_PassivePopCollision ; ///< Count of passive pop collision for elimination back-off
105 counter_type m_EliminationFailed ; ///< Count of unsuccessful elimination back-off
108 void onPush() { ++m_PushCount; }
109 void onPop() { ++m_PopCount; }
110 void onPushRace() { ++m_PushRace; }
111 void onPopRace() { ++m_PopRace; }
112 void onActiveCollision( operation_id opId )
114 if ( opId == treiber_stack::op_push )
115 ++m_ActivePushCollision;
117 ++m_ActivePopCollision;
119 void onPassiveCollision( operation_id opId )
121 if ( opId == treiber_stack::op_push )
122 ++m_PassivePushCollision;
124 ++m_PassivePopCollision;
126 void onEliminationFailed()
128 ++m_EliminationFailed;
133 /// Empty (no overhead) stack statistics. Support interface like treiber_stack::stat
141 void onActiveCollision( operation_id ) {}
142 void onPassiveCollision( operation_id ) {}
143 void onEliminationFailed() {}
147 /// TreiberStack default type traits
150 /// Back-off strategy
151 typedef cds::backoff::Default back_off;
153 /// Hook, possible types are \p treiber_stack::base_hook, \p treiber_stack::member_hook, \p treiber_stack::traits_hook
154 typedef treiber_stack::base_hook<> hook;
156 /// The functor used for dispose removed items. Default is \p opt::v::empty_disposer. This option is used only in \p TreiberStack::clear() function
157 typedef opt::v::empty_disposer disposer;
159 /// Item counting feature; by default, disabled. Use \p cds::atomicity::item_counter to enable item counting
160 typedef cds::atomicity::empty_item_counter item_counter;
162 /// C++ memory ordering model
164 Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
165 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
167 typedef opt::v::relaxed_ordering memory_model;
169 /// Internal statistics (by default, disabled)
171 Possible option value are: \p treiber_stack::stat, \p treiber_stack::empty_stat (the default),
172 user-provided class that supports \p %treiber_stack::stat interface.
174 typedef treiber_stack::empty_stat stat;
176 /// Link checking, see \p cds::opt::link_checker
177 static CDS_CONSTEXPR const opt::link_check_type link_checker = opt::debug_check_link;
179 /** @name Elimination back-off traits
180 The following traits is used only if elimination enabled
184 /// Enable elimination back-off; by default, it is disabled
185 static CDS_CONSTEXPR const bool enable_elimination = false;
187 /// Back-off strategy to wait for elimination, default is cds::backoff::delay<>
188 typedef cds::backoff::delay<> elimination_backoff;
190 /// Buffer type for elimination array
192 Possible types are \p opt::v::static_buffer, \p opt::v::dynamic_buffer.
193 The buffer can be any size: \p Exp2 template parameter of those classes can be \p false.
194 The size should be selected empirically for your application and hardware, there are no common rules for that.
195 Default is <tt> %opt::v::static_buffer< any_type, 4 > </tt>.
197 typedef opt::v::static_buffer< int, 4 > buffer;
199 /// Random engine to generate a random position in elimination array
200 typedef opt::v::c_rand random_engine;
202 /// Lock type used in elimination, default is cds::sync::spin
203 typedef cds::sync::spin lock_type;
208 /// Metafunction converting option list to \p treiber_stack::traits
210 Supported \p Options are:
211 - opt::hook - hook used. Possible hooks are: \p treiber_stack::base_hook, \p treiber_stack::member_hook, \p treiber_stack::traits_hook.
212 If the option is not specified, \p %treiber_stack::base_hook<> is used.
213 - opt::back_off - back-off strategy used. If the option is not specified, the \p cds::backoff::Default is used.
214 - opt::disposer - the functor used for dispose removed items. Default is \p opt::v::empty_disposer. This option is used only
215 in \p TreiberStack::clear function.
216 - opt::link_checker - the type of node's link fields checking. Default is \ref opt::debug_check_link.
217 - opt::memory_model - C++ memory ordering model. Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
218 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
219 - opt::item_counter - the type of item counting feature. Default is \p cds::atomicity::empty_item_counter, i.e.
220 no item counting. Use \p cds::atomicity::item_counter to enable item counting.
221 - opt::stat - the type to gather internal statistics.
222 Possible option value are: \p treiber_stack::stat, \p treiber_stack::empty_stat (the default),
223 user-provided class that supports \p treiber_stack::stat interface.
224 - opt::enable_elimination - enable elimination back-off for the stack. Default value is \p false.
226 If elimination back-off is enabled, additional options can be specified:
227 - opt::buffer - a buffer type for elimination array, see \p opt::v::static_buffer, \p opt::v::dynamic_buffer.
228 The buffer can be any size: \p Exp2 template parameter of those classes can be \p false.
229 The size should be selected empirically for your application and hardware, there are no common rules for that.
230 Default is <tt> %opt::v::static_buffer< any_type, 4 > </tt>.
231 - opt::random_engine - a random engine to generate a random position in elimination array.
232 Default is \p opt::v::c_rand.
233 - opt::elimination_backoff - back-off strategy to wait for elimination, default is \p cds::backoff::delay<>
234 - opt::lock_type - a lock type used in elimination back-off, default is \p cds::sync::spin
236 Example: declare \p %TreiberStack with elimination enabled and internal statistics
238 typedef cds::intrusive::TreiberStack< cds::gc::HP, Foo,
239 typename cds::intrusive::treiber_stack::make_traits<
240 cds::opt::enable_elimination< true >,
241 cds::opt::stat< cds::intrusive::treiber_stack::stat<> >
246 template <typename... Options>
248 # ifdef CDS_DOXYGEN_INVOKED
249 typedef implementation_defined type; ///< Metafunction result
251 typedef typename cds::opt::make_options<
252 typename cds::opt::find_type_traits< traits, Options... >::type
262 template <bool EnableElimination, typename T, typename Traits>
263 class elimination_backoff;
265 template <typename T, typename Traits>
266 class elimination_backoff<false, T, Traits>
268 typedef typename Traits::back_off back_off;
272 elimination_backoff()
275 elimination_backoff( size_t )
283 template <typename Stat>
284 bool backoff(treiber_stack::operation< T >&, Stat& )
291 template <typename T, typename Traits>
292 class elimination_backoff<true, T, Traits>
294 typedef typename Traits::back_off back_off;
296 /// Back-off for elimination (usually delay)
297 typedef typename Traits::elimination_backoff elimination_backoff_type;
298 /// Lock type used in elimination back-off
299 typedef typename Traits::lock_type elimination_lock_type;
300 /// Random engine used in elimination back-off
301 typedef typename Traits::random_engine elimination_random_engine;
303 /// Per-thread elimination record
304 typedef cds::algo::elimination::record elimination_rec;
306 /// Collision array record
307 struct collision_array_record {
308 elimination_rec * pRec;
309 elimination_lock_type lock;
312 /// Collision array used in elimination-backoff; each item is optimized for cache-line size
313 typedef typename Traits::buffer::template rebind<
314 typename cds::details::type_padding<collision_array_record, cds::c_nCacheLineSize >::type
315 >::other collision_array;
317 /// Operation descriptor used in elimination back-off
318 typedef treiber_stack::operation< T > operation_desc;
320 /// Elimination back-off data
321 struct elimination_data {
322 elimination_random_engine randEngine; ///< random engine
323 collision_array collisions; ///< collision array
327 //TODO: check Traits::buffer must be static!
329 elimination_data( size_t nCollisionCapacity )
330 : collisions( nCollisionCapacity )
334 elimination_data m_Elimination;
336 enum operation_status {
342 typedef std::unique_lock< elimination_lock_type > slot_scoped_lock;
345 elimination_backoff()
347 m_Elimination.collisions.zeroize();
350 elimination_backoff( size_t nCollisionCapacity )
351 : m_Elimination( nCollisionCapacity )
353 m_Elimination.collisions.zeroize();
359 template <typename Stat>
360 bool backoff( operation_desc& op, Stat& stat )
362 elimination_backoff_type bkoff;
363 op.nStatus.store( op_busy, atomics::memory_order_relaxed );
365 elimination_rec * myRec = cds::algo::elimination::init_record( op );
367 collision_array_record& slot = m_Elimination.collisions[m_Elimination.randEngine() % m_Elimination.collisions.capacity()];
370 elimination_rec * himRec = slot.pRec;
372 operation_desc * himOp = static_cast<operation_desc *>( himRec->pOp );
374 if ( himOp->idOp != op.idOp ) {
375 if ( op.idOp == treiber_stack::op_push )
376 himOp->pVal = op.pVal;
378 op.pVal = himOp->pVal;
382 himOp->nStatus.store( op_collided, atomics::memory_order_release );
383 cds::algo::elimination::clear_record();
384 stat.onActiveCollision( op.idOp );
387 himOp->nStatus.store( op_free, atomics::memory_order_release );
393 // Wait for colliding operation
394 bkoff( [&op]() CDS_NOEXCEPT -> bool { return op.nStatus.load( atomics::memory_order_acquire ) != op_busy; } );
396 slot_scoped_lock l( slot.lock );
397 if ( slot.pRec == myRec )
401 bool bCollided = op.nStatus.load( atomics::memory_order_acquire ) == op_collided;
404 stat.onEliminationFailed();
406 stat.onPassiveCollision( op.idOp );
408 cds::algo::elimination::clear_record();
413 } // namespace details
415 } // namespace treiber_stack
417 /// Treiber intrusive stack
418 /** @ingroup cds_intrusive_stack
419 Intrusive implementation of well-known Treiber's stack algorithm:
420 - R. K. Treiber. Systems programming: Coping with parallelism. Technical Report RJ 5118, IBM Almaden Research Center, April 1986.
422 \ref cds_elimination_description "Elimination back-off technique" can be used optionally.
423 The idea of elimination algorithm is taken from:
424 - [2004] Danny Hendler, Nir Shavit, Lena Yerushalmi "A Scalable Lock-free Stack Algorithm"
426 The elimination algorithm uses a single elimination array as a back-off schema
427 on a shared lock-free stack. If the threads fail on the stack, they attempt to eliminate
428 on the array, and if they fail in eliminating, they attempt to access the stack again and so on.
430 @note Hendler's et al paper describes a lock-free implementation of elimination back-off which is quite complex.
431 The main difficulty is the managing life-time of elimination record.
432 Our implementation uses simplified lock-based (spin-based) approach which allows
433 the elimination record allocation on thread's stack.
434 This approach demonstrates sufficient performance under high load.
437 - \p GC - garbage collector type: \p gc::HP, \p gc::DHP.
438 Garbage collecting schema must be the same as \p treiber_stack::node GC.
439 - \p T - a type the stack contains. A value of type \p T must be derived
440 from \p treiber_stack::node for \p treiber_stack::base_hook,
441 or it should have a member of type \p %treiber_stack::node for \p treiber_stack::member_hook,
442 or it should be convertible to \p %treiber_stack::node for \p treiber_stack::traits_hook.
443 - \p Traits - stack traits, default is \p treiber_stack::traits. You can use \p treiber_stack::make_traits
444 metafunction to make your traits or just derive your traits from \p %treiber_stack::traits:
446 struct myTraits: public cds::intrusive::treiber_stack::traits {
447 typedef cds::intrusive::treiber_stack::stat<> stat;
449 typedef cds::intrusive::TreiberStack< cds::gc::HP, Foo, myTraits > myStack;
451 // Equivalent make_traits example:
452 typedef cds::intrusive::TreiberStack< cds::gc::HP, Foo,
453 typename cds::intrusive::treiber_stack::make_traits<
454 cds::opt::stat< cds::intrusive::treiber_stack::stat<> >
459 @note Be careful when you want destroy an item popped, see \ref cds_intrusive_item_destroying "Destroying items of intrusive containers".
461 @anchor cds_intrusive_TreiberStack_examples
464 Example of how to use \p treiber_stack::base_hook.
465 Your class that objects will be pushed on \p %TreiberStack should be based on \p treiber_stack::node class
467 #include <cds/intrusive/treiber_stack.h>
468 #include <cds/gc/hp.h>
470 namespace ci = cds::intrusive;
471 typedef cds::gc::HP gc;
473 struct myData: public ci::treiber_stack::node< gc >
479 typedef ci::TreiberStack< gc,
481 typename cds::intrusive::treiber_stack::make_traits<
482 ci::opt::hook< ci::treiber_stack::base_hook< gc > >
486 // Stack with elimination back-off enabled
487 typedef ci::TreiberStack< gc,
489 typename ci::treiber_stack::make_traits<
490 ci::opt::hook< ci::treiber_stack::base_hook< gc > >,
491 cds::opt::enable_elimination< true >
493 > elimination_stack_t;
496 Example of how to use \p treiber_stack::base_hook with different tags.
498 #include <cds/intrusive/treiber_stack.h>
499 #include <cds/gc/hp.h>
501 namespace ci = cds::intrusive;
502 typedef cds::gc::HP gc;
504 // It is not necessary to declare complete type for tags
509 : public ci::treiber_stack::node< gc, tag1 >
510 , public ci::treiber_stack::node< gc, tag2 >
515 typedef ci::TreiberStack< gc,
517 typename ci::treiber_stack::make_traits<
518 ci::opt::hook< ci::treiber_stack::base_hook< gc, tag1 > >
522 typedef ci::TreiberStack< gc,
524 typename ci::treiber_stack::make_traits<
525 ci::opt::hook< ci::treiber_stack::base_hook< gc, tag2 > >
529 // You may add myData objects into stack1_t and stack2_t independently
537 s2.push( i1 ) ; // i1 is now contained in s1 and s2.
541 p = s1.pop() ; // pop i1 from s1
542 p = s1.pop() ; // p == nullptr, s1 is empty
543 p = s2.pop() ; // pop i1 from s2
544 p = s2.pop() ; // pop i2 from s2
545 p = s2.pop() ; // p == nullptr, s2 is empty
549 Example of how to use \p treiber_stack::member_hook.
550 Your class should have a member of type \p treiber_stack::node
552 #include <stddef.h> // offsetof macro
553 #include <cds/intrusive/treiber_stack.h>
554 #include <cds/gc/hp.h>
556 namespace ci = cds::intrusive;
557 typedef cds::gc::HP gc;
562 ci::treiber_stack::node< gc > member_hook_;
566 typedef ci::TreiberStack< gc,
568 typename ci::treiber_stack::make_traits<
569 ci::opt::hook< ci::treiber_stack::member_hook< offsetof(myData, member_hook_), gc >>
577 typename Traits = treiber_stack::traits
582 /// Rebind template arguments
583 template <typename GC2, typename T2, typename Traits2>
585 typedef TreiberStack< GC2, T2, Traits2 > other ; ///< Rebinding result
589 typedef GC gc; ///< Garbage collector
590 typedef T value_type; ///< type of value stored in the stack
591 typedef Traits traits; ///< Stack traits
593 typedef typename traits::hook hook; ///< hook type
594 typedef typename hook::node_type node_type; ///< node type
595 typedef typename traits::disposer disposer; ///< disposer used
596 typedef typename get_node_traits< value_type, node_type, hook>::type node_traits ; ///< node traits
597 typedef typename single_link::get_link_checker< node_type, traits::link_checker >::type link_checker ; ///< link checker
598 typedef typename traits::memory_model memory_model; ///< Memory ordering. See \p cds::opt::memory_model option
599 typedef typename traits::item_counter item_counter; ///< Item counter class
600 typedef typename traits::stat stat; ///< Internal statistics
601 typedef typename traits::back_off back_off; ///< back-off strategy
603 public: // related to elimination back-off
605 /// Elimination back-off is enabled or not
606 static CDS_CONSTEXPR const bool enable_elimination = traits::enable_elimination;
607 /// back-off strategy used to wait for elimination
608 typedef typename traits::elimination_backoff elimination_backoff_type;
609 /// Lock type used in elimination back-off
610 typedef typename traits::lock_type elimination_lock_type;
611 /// Random engine used in elimination back-off
612 typedef typename traits::random_engine elimination_random_engine;
615 typename node_type::atomic_node_ptr m_Top; ///< Top of the stack
616 item_counter m_ItemCounter; ///< Item counter
617 stat m_stat; ///< Internal statistics
620 treiber_stack::details::elimination_backoff<enable_elimination, value_type, traits> m_Backoff;
622 typedef intrusive::node_to_value<TreiberStack> node_to_value;
623 typedef treiber_stack::operation< value_type > operation_desc;
625 // GC and node_type::gc must be the same
626 static_assert( std::is_same<gc, typename node_type::gc>::value, "GC and node_type::gc must be the same");
628 static_assert( !enable_elimination || std::is_same<typename elimination_random_engine::result_type, unsigned int>::value,
629 "Random engine result type must be unsigned int");
634 void clear_links( node_type * pNode ) CDS_NOEXCEPT
636 pNode->m_pNext.store( nullptr, memory_model::memory_order_relaxed );
639 template <bool EnableElimination>
640 struct elimination_backoff_impl;
644 /// Constructs empty stack
649 /// Constructs empty stack and initializes elimination back-off data
651 This form should be used if you use elimination back-off with dynamically allocated collision array, i.e
652 \p Options... contains cds::opt::buffer< cds::opt::v::dynamic_buffer >.
653 \p nCollisionCapacity parameter specifies the capacity of collision array.
655 TreiberStack( size_t nCollisionCapacity )
657 , m_Backoff( nCollisionCapacity )
660 /// \p %TreiberStack is not copy-constructible
661 TreiberStack( TreiberStack const& ) = delete;
663 /// Destructor calls \ref cds_intrusive_TreiberStack_clear "clear" member function
669 /// Push the item \p val on the stack
671 No copying is made since it is intrusive stack.
673 bool push( value_type& val )
675 node_type * pNew = node_traits::to_node_ptr( val );
676 link_checker::is_empty( pNew );
681 if ( enable_elimination ) {
682 op.idOp = treiber_stack::op_push;
686 node_type * t = m_Top.load(memory_model::memory_order_relaxed);
688 pNew->m_pNext.store( t, memory_model::memory_order_relaxed );
689 if ( m_Top.compare_exchange_weak( t, pNew, memory_model::memory_order_release, atomics::memory_order_relaxed )) { // #1 sync-with #2
696 if ( m_Backoff.backoff( op, m_stat ))
701 /// Pop an item from the stack
703 If stack is empty, returns \p nullptr.
704 The disposer is <b>not</b> called for popped item.
705 See \ref cds_intrusive_item_destroying "Destroying items of intrusive containers".
710 typename gc::Guard guard;
713 if ( enable_elimination ) {
714 op.idOp = treiber_stack::op_pop;
718 node_type * t = guard.protect( m_Top, node_to_value() );
720 return nullptr; // stack is empty
722 node_type * pNext = t->m_pNext.load(memory_model::memory_order_relaxed);
723 if ( m_Top.compare_exchange_weak( t, pNext, memory_model::memory_order_acquire, atomics::memory_order_relaxed )) { // #2
727 return node_traits::to_value_ptr( *t );
731 if ( m_Backoff.backoff( op, m_stat )) {
732 // may return nullptr if stack is empty
738 /// Check if stack is empty
741 // http://www.manning-sandbox.com/thread.jspa?threadID=46245&tstart=0
742 return m_Top.load( memory_model::memory_order_relaxed ) == nullptr;
746 /** @anchor cds_intrusive_TreiberStack_clear
747 For each removed item the disposer is called.
749 @note It is possible that after <tt>clear()</tt> the <tt>empty()</tt> returns \p false
750 if some other thread pushes an item into the stack during \p clear works
757 pTop = m_Top.load( memory_model::memory_order_relaxed );
758 if ( pTop == nullptr )
760 if ( m_Top.compare_exchange_weak( pTop, nullptr, memory_model::memory_order_acq_rel, atomics::memory_order_relaxed ) ) { // sync-with #1 and #2
761 m_ItemCounter.reset();
768 node_type * p = pTop;
769 pTop = p->m_pNext.load(memory_model::memory_order_relaxed);
771 gc::template retire<disposer>( node_traits::to_value_ptr( *p ) );
775 /// Returns stack's item count
777 The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
778 this function always returns 0.
780 @warning Even if you use real item counter and it returns 0, this fact is not mean that the stack
781 is empty. To check emptyness use \ref empty() method.
785 return m_ItemCounter.value();
788 /// Returns reference to internal statistics
789 stat const& statistics() const
795 }} // namespace cds::intrusive
797 #endif // #ifndef CDSLIB_INTRUSIVE_TREIBER_STACK_H