1 // lock-free queue from
2 // Michael, M. M. and Scott, M. L.,
3 // "simple, fast and practical non-blocking and blocking concurrent queue algorithms"
5 // Copyright (C) 2008-2013 Tim Blechmann
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_LOCKFREE_FIFO_HPP_INCLUDED
12 #define BOOST_LOCKFREE_FIFO_HPP_INCLUDED
14 #include <boost/assert.hpp>
15 #include <boost/static_assert.hpp>
16 #include <boost/core/allocator_access.hpp>
17 #include <boost/type_traits/has_trivial_assign.hpp>
18 #include <boost/type_traits/has_trivial_destructor.hpp>
19 #include <boost/config.hpp> // for BOOST_LIKELY & BOOST_ALIGNMENT
21 #include <boost/lockfree/detail/atomic.hpp>
22 #include <boost/lockfree/detail/copy_payload.hpp>
23 #include <boost/lockfree/detail/freelist.hpp>
24 #include <boost/lockfree/detail/parameter.hpp>
25 #include <boost/lockfree/detail/tagged_ptr.hpp>
27 #include <boost/lockfree/lockfree_forward.hpp>
29 #ifdef BOOST_HAS_PRAGMA_ONCE
36 #pragma warning(disable: 4324) // structure was padded due to __declspec(align())
39 #if defined(BOOST_INTEL) && (BOOST_INTEL_CXX_VERSION > 1000)
41 #pragma warning(disable:488) // template parameter unused in declaring parameter types,
42 // gets erronously triggered the queue constructor which
43 // takes an allocator of another type and rebinds it
52 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
53 boost::parameter::optional<tag::capacity>
56 } /* namespace detail */
59 /** The queue class provides a multi-writer/multi-reader queue, pushing and popping is lock-free,
60 * construction/destruction has to be synchronized. It uses a freelist for memory management,
61 * freed nodes are pushed to the freelist and not returned to the OS before the queue is destroyed.
64 * - \ref boost::lockfree::fixed_sized, defaults to \c boost::lockfree::fixed_sized<false> \n
65 * Can be used to completely disable dynamic memory allocations during push in order to ensure lockfree behavior. \n
66 * If the data structure is configured as fixed-sized, the internal nodes are stored inside an array and they are addressed
67 * by array indexing. This limits the possible size of the queue to the number of elements that can be addressed by the index
68 * type (usually 2**16-2), but on platforms that lack double-width compare-and-exchange instructions, this is the best way
69 * to achieve lock-freedom.
71 * - \ref boost::lockfree::capacity, optional \n
72 * If this template argument is passed to the options, the size of the queue is set at compile-time.\n
73 * This option implies \c fixed_sized<true>
75 * - \ref boost::lockfree::allocator, defaults to \c boost::lockfree::allocator<std::allocator<void>> \n
76 * Specifies the allocator that is used for the internal freelist
79 * - T must have a copy constructor
80 * - T must have a trivial assignment operator
81 * - T must have a trivial destructor
84 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
85 template <typename T, class A0, class A1, class A2>
87 template <typename T, typename ...Options>
92 #ifndef BOOST_DOXYGEN_INVOKED
94 #ifdef BOOST_HAS_TRIVIAL_DESTRUCTOR
95 BOOST_STATIC_ASSERT((boost::has_trivial_destructor<T>::value));
98 #ifdef BOOST_HAS_TRIVIAL_ASSIGN
99 BOOST_STATIC_ASSERT((boost::has_trivial_assign<T>::value));
102 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
103 typedef typename detail::queue_signature::bind<A0, A1, A2>::type bound_args;
105 typedef typename detail::queue_signature::bind<Options...>::type bound_args;
108 static const bool has_capacity = detail::extract_capacity<bound_args>::has_capacity;
109 static const size_t capacity = detail::extract_capacity<bound_args>::capacity + 1; // the queue uses one dummy node
110 static const bool fixed_sized = detail::extract_fixed_sized<bound_args>::value;
111 static const bool node_based = !(has_capacity || fixed_sized);
112 static const bool compile_time_sized = has_capacity;
114 struct BOOST_ALIGNMENT(BOOST_LOCKFREE_CACHELINE_BYTES) node
116 typedef typename detail::select_tagged_handle<node, node_based>::tagged_handle_type tagged_node_handle;
117 typedef typename detail::select_tagged_handle<node, node_based>::handle_type handle_type;
119 node(T const & v, handle_type null_handle):
122 /* increment tag to avoid ABA problem */
123 tagged_node_handle old_next = next.load(memory_order_relaxed);
124 tagged_node_handle new_next (null_handle, old_next.get_next_tag());
125 next.store(new_next, memory_order_release);
128 node (handle_type null_handle):
129 next(tagged_node_handle(null_handle, 0))
135 atomic<tagged_node_handle> next;
139 typedef typename detail::extract_allocator<bound_args, node>::type node_allocator;
140 typedef typename detail::select_freelist<node, node_allocator, compile_time_sized, fixed_sized, capacity>::type pool_t;
141 typedef typename pool_t::tagged_node_handle tagged_node_handle;
142 typedef typename detail::select_tagged_handle<node, node_based>::handle_type handle_type;
144 void initialize(void)
146 node * n = pool.template construct<true, false>(pool.null_handle());
147 tagged_node_handle dummy_node(pool.get_handle(n), 0);
148 head_.store(dummy_node, memory_order_relaxed);
149 tail_.store(dummy_node, memory_order_release);
152 struct implementation_defined
154 typedef node_allocator allocator;
155 typedef std::size_t size_type;
160 BOOST_DELETED_FUNCTION(queue(queue const&))
161 BOOST_DELETED_FUNCTION(queue& operator= (queue const&))
164 typedef T value_type;
165 typedef typename implementation_defined::allocator allocator;
166 typedef typename implementation_defined::size_type size_type;
169 * \return true, if implementation is lock-free.
171 * \warning It only checks, if the queue head and tail nodes and the freelist can be modified in a lock-free manner.
172 * On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics, there is
173 * no possibility to provide a completely accurate implementation, because one would need to test every internal
174 * node, which is impossible if further nodes will be allocated from the operating system.
176 bool is_lock_free (void) const
178 return head_.is_lock_free() && tail_.is_lock_free() && pool.is_lock_free();
181 /** Construct a fixed-sized queue
183 * \pre Must specify a capacity<> argument
186 head_(tagged_node_handle(0, 0)),
187 tail_(tagged_node_handle(0, 0)),
188 pool(node_allocator(), capacity)
190 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
191 // this function and this function may be compiled even when it isn't being used.
192 BOOST_ASSERT(has_capacity);
196 /** Construct a fixed-sized queue with a custom allocator
198 * \pre Must specify a capacity<> argument
200 template <typename U>
201 explicit queue(typename boost::allocator_rebind<node_allocator, U>::type const & alloc):
202 head_(tagged_node_handle(0, 0)),
203 tail_(tagged_node_handle(0, 0)),
204 pool(alloc, capacity)
206 BOOST_STATIC_ASSERT(has_capacity);
210 /** Construct a fixed-sized queue with a custom allocator
212 * \pre Must specify a capacity<> argument
214 explicit queue(allocator const & alloc):
215 head_(tagged_node_handle(0, 0)),
216 tail_(tagged_node_handle(0, 0)),
217 pool(alloc, capacity)
219 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
220 // this function and this function may be compiled even when it isn't being used.
221 BOOST_ASSERT(has_capacity);
225 /** Construct a variable-sized queue
227 * Allocate n nodes initially for the freelist
229 * \pre Must \b not specify a capacity<> argument
231 explicit queue(size_type n):
232 head_(tagged_node_handle(0, 0)),
233 tail_(tagged_node_handle(0, 0)),
234 pool(node_allocator(), n + 1)
236 // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
237 // this function and this function may be compiled even when it isn't being used.
238 BOOST_ASSERT(!has_capacity);
242 /** Construct a variable-sized queue with a custom allocator
244 * Allocate n nodes initially for the freelist
246 * \pre Must \b not specify a capacity<> argument
248 template <typename U>
249 queue(size_type n, typename boost::allocator_rebind<node_allocator, U>::type const & alloc):
250 head_(tagged_node_handle(0, 0)),
251 tail_(tagged_node_handle(0, 0)),
254 BOOST_STATIC_ASSERT(!has_capacity);
258 /** \copydoc boost::lockfree::stack::reserve
260 void reserve(size_type n)
262 pool.template reserve<true>(n);
265 /** \copydoc boost::lockfree::stack::reserve_unsafe
267 void reserve_unsafe(size_type n)
269 pool.template reserve<false>(n);
272 /** Destroys queue, free all nodes from freelist.
277 while(unsynchronized_pop(dummy))
280 pool.template destruct<false>(head_.load(memory_order_relaxed));
283 /** Check if the queue is empty
285 * \return true, if the queue is empty, false otherwise
286 * \note The result is only accurate, if no other thread modifies the queue. Therefore it is rarely practical to use this
287 * value in program logic.
289 bool empty(void) const
291 return pool.get_handle(head_.load()) == pool.get_handle(tail_.load());
294 /** Pushes object t to the queue.
296 * \post object will be pushed to the queue, if internal node can be allocated
297 * \returns true, if the push operation is successful.
299 * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
300 * from the OS. This may not be lock-free.
302 bool push(T const & t)
304 return do_push<false>(t);
307 /** Pushes object t to the queue.
309 * \post object will be pushed to the queue, if internal node can be allocated
310 * \returns true, if the push operation is successful.
312 * \note Thread-safe and non-blocking. If internal memory pool is exhausted, operation will fail
313 * \throws if memory allocator throws
315 bool bounded_push(T const & t)
317 return do_push<true>(t);
322 #ifndef BOOST_DOXYGEN_INVOKED
323 template <bool Bounded>
324 bool do_push(T const & t)
326 node * n = pool.template construct<true, Bounded>(t, pool.null_handle());
327 handle_type node_handle = pool.get_handle(n);
333 tagged_node_handle tail = tail_.load(memory_order_acquire);
334 node * tail_node = pool.get_pointer(tail);
335 tagged_node_handle next = tail_node->next.load(memory_order_acquire);
336 node * next_ptr = pool.get_pointer(next);
338 tagged_node_handle tail2 = tail_.load(memory_order_acquire);
339 if (BOOST_LIKELY(tail == tail2)) {
341 tagged_node_handle new_tail_next(node_handle, next.get_next_tag());
342 if ( tail_node->next.compare_exchange_weak(next, new_tail_next) ) {
343 tagged_node_handle new_tail(node_handle, tail.get_next_tag());
344 tail_.compare_exchange_strong(tail, new_tail);
349 tagged_node_handle new_tail(pool.get_handle(next_ptr), tail.get_next_tag());
350 tail_.compare_exchange_strong(tail, new_tail);
359 /** Pushes object t to the queue.
361 * \post object will be pushed to the queue, if internal node can be allocated
362 * \returns true, if the push operation is successful.
364 * \note Not Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
365 * from the OS. This may not be lock-free.
366 * \throws if memory allocator throws
368 bool unsynchronized_push(T const & t)
370 node * n = pool.template construct<false, false>(t, pool.null_handle());
376 tagged_node_handle tail = tail_.load(memory_order_relaxed);
377 tagged_node_handle next = tail->next.load(memory_order_relaxed);
378 node * next_ptr = next.get_ptr();
381 tail->next.store(tagged_node_handle(n, next.get_next_tag()), memory_order_relaxed);
382 tail_.store(tagged_node_handle(n, tail.get_next_tag()), memory_order_relaxed);
386 tail_.store(tagged_node_handle(next_ptr, tail.get_next_tag()), memory_order_relaxed);
390 /** Pops object from queue.
392 * \post if pop operation is successful, object will be copied to ret.
393 * \returns true, if the pop operation is successful, false if queue was empty.
395 * \note Thread-safe and non-blocking
402 /** Pops object from queue.
404 * \pre type U must be constructible by T and copyable, or T must be convertible to U
405 * \post if pop operation is successful, object will be copied to ret.
406 * \returns true, if the pop operation is successful, false if queue was empty.
408 * \note Thread-safe and non-blocking
410 template <typename U>
414 tagged_node_handle head = head_.load(memory_order_acquire);
415 node * head_ptr = pool.get_pointer(head);
417 tagged_node_handle tail = tail_.load(memory_order_acquire);
418 tagged_node_handle next = head_ptr->next.load(memory_order_acquire);
419 node * next_ptr = pool.get_pointer(next);
421 tagged_node_handle head2 = head_.load(memory_order_acquire);
422 if (BOOST_LIKELY(head == head2)) {
423 if (pool.get_handle(head) == pool.get_handle(tail)) {
427 tagged_node_handle new_tail(pool.get_handle(next), tail.get_next_tag());
428 tail_.compare_exchange_strong(tail, new_tail);
432 /* this check is not part of the original algorithm as published by michael and scott
434 * however we reuse the tagged_ptr part for the freelist and clear the next part during node
435 * allocation. we can observe a null-pointer here.
438 detail::copy_payload(next_ptr->data, ret);
440 tagged_node_handle new_head(pool.get_handle(next), head.get_next_tag());
441 if (head_.compare_exchange_weak(head, new_head)) {
442 pool.template destruct<true>(head);
450 /** Pops object from queue.
452 * \post if pop operation is successful, object will be copied to ret.
453 * \returns true, if the pop operation is successful, false if queue was empty.
455 * \note Not thread-safe, but non-blocking
458 bool unsynchronized_pop (T & ret)
460 return unsynchronized_pop<T>(ret);
463 /** Pops object from queue.
465 * \pre type U must be constructible by T and copyable, or T must be convertible to U
466 * \post if pop operation is successful, object will be copied to ret.
467 * \returns true, if the pop operation is successful, false if queue was empty.
469 * \note Not thread-safe, but non-blocking
472 template <typename U>
473 bool unsynchronized_pop (U & ret)
476 tagged_node_handle head = head_.load(memory_order_relaxed);
477 node * head_ptr = pool.get_pointer(head);
478 tagged_node_handle tail = tail_.load(memory_order_relaxed);
479 tagged_node_handle next = head_ptr->next.load(memory_order_relaxed);
480 node * next_ptr = pool.get_pointer(next);
482 if (pool.get_handle(head) == pool.get_handle(tail)) {
486 tagged_node_handle new_tail(pool.get_handle(next), tail.get_next_tag());
487 tail_.store(new_tail);
490 /* this check is not part of the original algorithm as published by michael and scott
492 * however we reuse the tagged_ptr part for the freelist and clear the next part during node
493 * allocation. we can observe a null-pointer here.
496 detail::copy_payload(next_ptr->data, ret);
497 tagged_node_handle new_head(pool.get_handle(next), head.get_next_tag());
498 head_.store(new_head);
499 pool.template destruct<false>(head);
505 /** consumes one element via a functor
507 * pops one element from the queue and applies the functor on this object
509 * \returns true, if one element was consumed
511 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
513 template <typename Functor>
514 bool consume_one(Functor & f)
517 bool success = pop(element);
524 /// \copydoc boost::lockfree::queue::consume_one(Functor & rhs)
525 template <typename Functor>
526 bool consume_one(Functor const & f)
529 bool success = pop(element);
536 /** consumes all elements via a functor
538 * sequentially pops all elements from the queue and applies the functor on each object
540 * \returns number of elements that are consumed
542 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
544 template <typename Functor>
545 size_t consume_all(Functor & f)
547 size_t element_count = 0;
548 while (consume_one(f))
551 return element_count;
554 /// \copydoc boost::lockfree::queue::consume_all(Functor & rhs)
555 template <typename Functor>
556 size_t consume_all(Functor const & f)
558 size_t element_count = 0;
559 while (consume_one(f))
562 return element_count;
566 #ifndef BOOST_DOXYGEN_INVOKED
567 atomic<tagged_node_handle> head_;
568 static const int padding_size = BOOST_LOCKFREE_CACHELINE_BYTES - sizeof(tagged_node_handle);
569 char padding1[padding_size];
570 atomic<tagged_node_handle> tail_;
571 char padding2[padding_size];
577 } /* namespace lockfree */
578 } /* namespace boost */
580 #if defined(BOOST_INTEL) && (BOOST_INTEL_CXX_VERSION > 1000)
584 #if defined(_MSC_VER)
588 #endif /* BOOST_LOCKFREE_FIFO_HPP_INCLUDED */