1 // Copyright (C) 2008-2013 Tim Blechmann
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_LOCKFREE_STACK_HPP_INCLUDED
8 #define BOOST_LOCKFREE_STACK_HPP_INCLUDED
10 #include <boost/assert.hpp>
11 #include <boost/checked_delete.hpp>
12 #include <boost/core/allocator_access.hpp>
13 #include <boost/core/no_exceptions_support.hpp>
14 #include <boost/integer_traits.hpp>
15 #include <boost/static_assert.hpp>
16 #include <boost/tuple/tuple.hpp>
17 #include <boost/type_traits/is_copy_constructible.hpp>
19 #include <boost/lockfree/detail/atomic.hpp>
20 #include <boost/lockfree/detail/copy_payload.hpp>
21 #include <boost/lockfree/detail/freelist.hpp>
22 #include <boost/lockfree/detail/parameter.hpp>
23 #include <boost/lockfree/detail/tagged_ptr.hpp>
25 #include <boost/lockfree/lockfree_forward.hpp>
27 #ifdef BOOST_HAS_PRAGMA_ONCE
35 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
36 boost::parameter::optional<tag::capacity>
41 /** The stack class provides a multi-writer/multi-reader stack, pushing and popping is lock-free,
42 * construction/destruction has to be synchronized. It uses a freelist for memory management,
43 * freed nodes are pushed to the freelist and not returned to the OS before the stack is destroyed.
47 * - \c boost::lockfree::fixed_sized<>, defaults to \c boost::lockfree::fixed_sized<false> <br>
48 * Can be used to completely disable dynamic memory allocations during push in order to ensure lockfree behavior.<br>
49 * If the data structure is configured as fixed-sized, the internal nodes are stored inside an array and they are addressed
50 * by array indexing. This limits the possible size of the stack to the number of elements that can be addressed by the index
51 * type (usually 2**16-2), but on platforms that lack double-width compare-and-exchange instructions, this is the best way
52 * to achieve lock-freedom.
54 * - \c boost::lockfree::capacity<>, optional <br>
55 * If this template argument is passed to the options, the size of the stack is set at compile-time. <br>
56 * It this option implies \c fixed_sized<true>
58 * - \c boost::lockfree::allocator<>, defaults to \c boost::lockfree::allocator<std::allocator<void>> <br>
59 * Specifies the allocator that is used for the internal freelist
62 * - T must have a copy constructor
64 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
65 template <typename T, class A0, class A1, class A2>
67 template <typename T, typename ...Options>
72 #ifndef BOOST_DOXYGEN_INVOKED
73 BOOST_STATIC_ASSERT(boost::is_copy_constructible<T>::value);
75 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
76 typedef typename detail::stack_signature::bind<A0, A1, A2>::type bound_args;
78 typedef typename detail::stack_signature::bind<Options...>::type bound_args;
81 static const bool has_capacity = detail::extract_capacity<bound_args>::has_capacity;
82 static const size_t capacity = detail::extract_capacity<bound_args>::capacity;
83 static const bool fixed_sized = detail::extract_fixed_sized<bound_args>::value;
84 static const bool node_based = !(has_capacity || fixed_sized);
85 static const bool compile_time_sized = has_capacity;
93 typedef typename detail::select_tagged_handle<node, node_based>::handle_type handle_t;
98 typedef typename detail::extract_allocator<bound_args, node>::type node_allocator;
99 typedef typename detail::select_freelist<node, node_allocator, compile_time_sized, fixed_sized, capacity>::type pool_t;
100 typedef typename pool_t::tagged_node_handle tagged_node_handle;
102 // check compile-time capacity
103 BOOST_STATIC_ASSERT((mpl::if_c<has_capacity,
104 mpl::bool_<capacity - 1 < boost::integer_traits<boost::uint16_t>::const_max>,
108 struct implementation_defined
110 typedef node_allocator allocator;
111 typedef std::size_t size_type;
116 BOOST_DELETED_FUNCTION(stack(stack const&))
117 BOOST_DELETED_FUNCTION(stack& operator= (stack const&))
120 typedef T value_type;
121 typedef typename implementation_defined::allocator allocator;
122 typedef typename implementation_defined::size_type size_type;
125 * \return true, if implementation is lock-free.
127 * \warning It only checks, if the top stack node and the freelist can be modified in a lock-free manner.
128 * On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics,
129 * there is no possibility to provide a completely accurate implementation, because one would need to test
130 * every internal node, which is impossible if further nodes will be allocated from the operating system.
133 bool is_lock_free (void) const
135 return tos.is_lock_free() && pool.is_lock_free();
138 /** Construct a fixed-sized stack
140 * \pre Must specify a capacity<> argument
143 pool(node_allocator(), capacity)
145 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
146 // this function and this function may be compiled even when it isn't being used.
147 BOOST_ASSERT(has_capacity);
151 /** Construct a fixed-sized stack with a custom allocator
153 * \pre Must specify a capacity<> argument
155 template <typename U>
156 explicit stack(typename boost::allocator_rebind<node_allocator, U>::type const & alloc):
157 pool(alloc, capacity)
159 BOOST_STATIC_ASSERT(has_capacity);
163 /** Construct a fixed-sized stack with a custom allocator
165 * \pre Must specify a capacity<> argument
167 explicit stack(allocator const & alloc):
168 pool(alloc, capacity)
170 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
171 // this function and this function may be compiled even when it isn't being used.
172 BOOST_ASSERT(has_capacity);
176 /** Construct a variable-sized stack
178 * Allocate n nodes initially for the freelist
180 * \pre Must \b not specify a capacity<> argument
182 explicit stack(size_type n):
183 pool(node_allocator(), n)
185 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
186 // this function and this function may be compiled even when it isn't being used.
187 BOOST_ASSERT(!has_capacity);
191 /** Construct a variable-sized stack with a custom allocator
193 * Allocate n nodes initially for the freelist
195 * \pre Must \b not specify a capacity<> argument
197 template <typename U>
198 stack(size_type n, typename boost::allocator_rebind<node_allocator, U>::type const & alloc):
201 BOOST_STATIC_ASSERT(!has_capacity);
205 /** Allocate n nodes for freelist
207 * \pre only valid if no capacity<> argument given
208 * \note thread-safe, may block if memory allocator blocks
211 void reserve(size_type n)
213 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
214 // this function and this function may be compiled even when it isn't being used.
215 BOOST_ASSERT(!has_capacity);
216 pool.template reserve<true>(n);
219 /** Allocate n nodes for freelist
221 * \pre only valid if no capacity<> argument given
222 * \note not thread-safe, may block if memory allocator blocks
225 void reserve_unsafe(size_type n)
227 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
228 // this function and this function may be compiled even when it isn't being used.
229 BOOST_ASSERT(!has_capacity);
230 pool.template reserve<false>(n);
233 /** Destroys stack, free all nodes from freelist.
235 * \note not thread-safe
241 while(unsynchronized_pop(dummy))
246 #ifndef BOOST_DOXYGEN_INVOKED
247 void initialize(void)
249 tos.store(tagged_node_handle(pool.null_handle(), 0));
252 void link_nodes_atomic(node * new_top_node, node * end_node)
254 tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
256 tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
257 end_node->next = pool.get_handle(old_tos);
259 if (tos.compare_exchange_weak(old_tos, new_tos))
264 void link_nodes_unsafe(node * new_top_node, node * end_node)
266 tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
268 tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
269 end_node->next = pool.get_handle(old_tos);
271 tos.store(new_tos, memory_order_relaxed);
274 template <bool Threadsafe, bool Bounded, typename ConstIterator>
275 tuple<node*, node*> prepare_node_list(ConstIterator begin, ConstIterator end, ConstIterator & ret)
277 ConstIterator it = begin;
278 node * end_node = pool.template construct<Threadsafe, Bounded>(*it++);
279 if (end_node == NULL) {
281 return make_tuple<node*, node*>(NULL, NULL);
284 node * new_top_node = end_node;
285 end_node->next = NULL;
289 for (; it != end; ++it) {
290 node * newnode = pool.template construct<Threadsafe, Bounded>(*it);
293 newnode->next = new_top_node;
294 new_top_node = newnode;
296 } BOOST_CATCH (...) {
297 for (node * current_node = new_top_node; current_node != NULL;) {
298 node * next = current_node->next;
299 pool.template destruct<Threadsafe>(current_node);
307 return make_tuple(new_top_node, end_node);
312 /** Pushes object t to the stack.
314 * \post object will be pushed to the stack, if internal node can be allocated
315 * \returns true, if the push operation is successful.
317 * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
318 * from the OS. This may not be lock-free.
319 * \throws if memory allocator throws
321 bool push(T const & v)
323 return do_push<false>(v);
326 /** Pushes object t to the stack.
328 * \post object will be pushed to the stack, if internal node can be allocated
329 * \returns true, if the push operation is successful.
331 * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
333 bool bounded_push(T const & v)
335 return do_push<true>(v);
338 #ifndef BOOST_DOXYGEN_INVOKED
340 template <bool Bounded>
341 bool do_push(T const & v)
343 node * newnode = pool.template construct<true, Bounded>(v);
347 link_nodes_atomic(newnode, newnode);
351 template <bool Bounded, typename ConstIterator>
352 ConstIterator do_push(ConstIterator begin, ConstIterator end)
358 tie(new_top_node, end_node) = prepare_node_list<true, Bounded>(begin, end, ret);
360 link_nodes_atomic(new_top_node, end_node);
368 /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
370 * \return iterator to the first element, which has not been pushed
372 * \note Operation is applied atomically
373 * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
374 * from the OS. This may not be lock-free.
375 * \throws if memory allocator throws
377 template <typename ConstIterator>
378 ConstIterator push(ConstIterator begin, ConstIterator end)
380 return do_push<false, ConstIterator>(begin, end);
383 /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
385 * \return iterator to the first element, which has not been pushed
387 * \note Operation is applied atomically
388 * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
389 * \throws if memory allocator throws
391 template <typename ConstIterator>
392 ConstIterator bounded_push(ConstIterator begin, ConstIterator end)
394 return do_push<true, ConstIterator>(begin, end);
398 /** Pushes object t to the stack.
400 * \post object will be pushed to the stack, if internal node can be allocated
401 * \returns true, if the push operation is successful.
403 * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
404 * from the OS. This may not be lock-free.
405 * \throws if memory allocator throws
407 bool unsynchronized_push(T const & v)
409 node * newnode = pool.template construct<false, false>(v);
413 link_nodes_unsafe(newnode, newnode);
417 /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
419 * \return iterator to the first element, which has not been pushed
421 * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
422 * from the OS. This may not be lock-free.
423 * \throws if memory allocator throws
425 template <typename ConstIterator>
426 ConstIterator unsynchronized_push(ConstIterator begin, ConstIterator end)
432 tie(new_top_node, end_node) = prepare_node_list<false, false>(begin, end, ret);
434 link_nodes_unsafe(new_top_node, end_node);
440 /** Pops object from stack.
442 * \post if pop operation is successful, object will be copied to ret.
443 * \returns true, if the pop operation is successful, false if stack was empty.
445 * \note Thread-safe and non-blocking
453 /** Pops object from stack.
455 * \pre type T must be convertible to U
456 * \post if pop operation is successful, object will be copied to ret.
457 * \returns true, if the pop operation is successful, false if stack was empty.
459 * \note Thread-safe and non-blocking
462 template <typename U>
465 BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
466 detail::consume_via_copy<U> consumer(ret);
468 return consume_one(consumer);
472 /** Pops object from stack.
474 * \post if pop operation is successful, object will be copied to ret.
475 * \returns true, if the pop operation is successful, false if stack was empty.
477 * \note Not thread-safe, but non-blocking
480 bool unsynchronized_pop(T & ret)
482 return unsynchronized_pop<T>(ret);
485 /** Pops object from stack.
487 * \pre type T must be convertible to U
488 * \post if pop operation is successful, object will be copied to ret.
489 * \returns true, if the pop operation is successful, false if stack was empty.
491 * \note Not thread-safe, but non-blocking
494 template <typename U>
495 bool unsynchronized_pop(U & ret)
497 BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
498 tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
499 node * old_tos_pointer = pool.get_pointer(old_tos);
501 if (!pool.get_pointer(old_tos))
504 node * new_tos_ptr = pool.get_pointer(old_tos_pointer->next);
505 tagged_node_handle new_tos(pool.get_handle(new_tos_ptr), old_tos.get_next_tag());
507 tos.store(new_tos, memory_order_relaxed);
508 detail::copy_payload(old_tos_pointer->v, ret);
509 pool.template destruct<false>(old_tos);
513 /** consumes one element via a functor
515 * pops one element from the stack and applies the functor on this object
517 * \returns true, if one element was consumed
519 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
521 template <typename Functor>
522 bool consume_one(Functor & f)
524 tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
527 node * old_tos_pointer = pool.get_pointer(old_tos);
528 if (!old_tos_pointer)
531 tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
533 if (tos.compare_exchange_weak(old_tos, new_tos)) {
534 f(old_tos_pointer->v);
535 pool.template destruct<true>(old_tos);
541 /// \copydoc boost::lockfree::stack::consume_one(Functor & rhs)
542 template <typename Functor>
543 bool consume_one(Functor const & f)
545 tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
548 node * old_tos_pointer = pool.get_pointer(old_tos);
549 if (!old_tos_pointer)
552 tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
554 if (tos.compare_exchange_weak(old_tos, new_tos)) {
555 f(old_tos_pointer->v);
556 pool.template destruct<true>(old_tos);
562 /** consumes all elements via a functor
564 * sequentially pops all elements from the stack and applies the functor on each object
566 * \returns number of elements that are consumed
568 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
570 template <typename Functor>
571 size_t consume_all(Functor & f)
573 size_t element_count = 0;
574 while (consume_one(f))
577 return element_count;
580 /// \copydoc boost::lockfree::stack::consume_all(Functor & rhs)
581 template <typename Functor>
582 size_t consume_all(Functor const & f)
584 size_t element_count = 0;
585 while (consume_one(f))
588 return element_count;
591 /** consumes all elements via a functor
593 * atomically pops all elements from the stack and applies the functor on each object
595 * \returns number of elements that are consumed
597 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
599 template <typename Functor>
600 size_t consume_all_atomic(Functor & f)
602 size_t element_count = 0;
603 tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
606 node * old_tos_pointer = pool.get_pointer(old_tos);
607 if (!old_tos_pointer)
610 tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
612 if (tos.compare_exchange_weak(old_tos, new_tos))
616 tagged_node_handle nodes_to_consume = old_tos;
619 node * node_pointer = pool.get_pointer(nodes_to_consume);
623 node * next_node = pool.get_pointer(node_pointer->next);
626 pool.template destruct<true>(nodes_to_consume);
630 tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
631 pool.template destruct<true>(nodes_to_consume);
632 nodes_to_consume = next;
635 return element_count;
638 /// \copydoc boost::lockfree::stack::consume_all_atomic(Functor & rhs)
639 template <typename Functor>
640 size_t consume_all_atomic(Functor const & f)
642 size_t element_count = 0;
643 tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
646 node * old_tos_pointer = pool.get_pointer(old_tos);
647 if (!old_tos_pointer)
650 tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
652 if (tos.compare_exchange_weak(old_tos, new_tos))
656 tagged_node_handle nodes_to_consume = old_tos;
659 node * node_pointer = pool.get_pointer(nodes_to_consume);
663 node * next_node = pool.get_pointer(node_pointer->next);
666 pool.template destruct<true>(nodes_to_consume);
670 tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
671 pool.template destruct<true>(nodes_to_consume);
672 nodes_to_consume = next;
675 return element_count;
678 /** consumes all elements via a functor
680 * atomically pops all elements from the stack and applies the functor on each object in reversed order
682 * \returns number of elements that are consumed
684 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
686 template <typename Functor>
687 size_t consume_all_atomic_reversed(Functor & f)
689 size_t element_count = 0;
690 tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
693 node * old_tos_pointer = pool.get_pointer(old_tos);
694 if (!old_tos_pointer)
697 tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
699 if (tos.compare_exchange_weak(old_tos, new_tos))
703 tagged_node_handle nodes_to_consume = old_tos;
705 node * last_node_pointer = NULL;
706 tagged_node_handle nodes_in_reversed_order;
708 node * node_pointer = pool.get_pointer(nodes_to_consume);
709 node * next_node = pool.get_pointer(node_pointer->next);
711 node_pointer->next = pool.get_handle(last_node_pointer);
712 last_node_pointer = node_pointer;
715 nodes_in_reversed_order = nodes_to_consume;
719 tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
720 nodes_to_consume = next;
724 node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
728 node * next_node = pool.get_pointer(node_pointer->next);
731 pool.template destruct<true>(nodes_in_reversed_order);
735 tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
736 pool.template destruct<true>(nodes_in_reversed_order);
737 nodes_in_reversed_order = next;
740 return element_count;
743 /// \copydoc boost::lockfree::stack::consume_all_atomic_reversed(Functor & rhs)
744 template <typename Functor>
745 size_t consume_all_atomic_reversed(Functor const & f)
747 size_t element_count = 0;
748 tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
751 node * old_tos_pointer = pool.get_pointer(old_tos);
752 if (!old_tos_pointer)
755 tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
757 if (tos.compare_exchange_weak(old_tos, new_tos))
761 tagged_node_handle nodes_to_consume = old_tos;
763 node * last_node_pointer = NULL;
764 tagged_node_handle nodes_in_reversed_order;
766 node * node_pointer = pool.get_pointer(nodes_to_consume);
767 node * next_node = pool.get_pointer(node_pointer->next);
769 node_pointer->next = pool.get_handle(last_node_pointer);
770 last_node_pointer = node_pointer;
773 nodes_in_reversed_order = nodes_to_consume;
777 tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
778 nodes_to_consume = next;
782 node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
786 node * next_node = pool.get_pointer(node_pointer->next);
789 pool.template destruct<true>(nodes_in_reversed_order);
793 tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
794 pool.template destruct<true>(nodes_in_reversed_order);
795 nodes_in_reversed_order = next;
798 return element_count;
801 * \return true, if stack is empty.
803 * \note It only guarantees that at some point during the execution of the function the stack has been empty.
804 * It is rarely practical to use this value in program logic, because the stack can be modified by other threads.
806 bool empty(void) const
808 return pool.get_pointer(tos.load()) == NULL;
812 #ifndef BOOST_DOXYGEN_INVOKED
813 detail::atomic<tagged_node_handle> tos;
815 static const int padding_size = BOOST_LOCKFREE_CACHELINE_BYTES - sizeof(tagged_node_handle);
816 char padding[padding_size];
822 } /* namespace lockfree */
823 } /* namespace boost */
825 #endif /* BOOST_LOCKFREE_STACK_HPP_INCLUDED */