annotate DEPENDENCIES/generic/include/boost/lockfree/queue.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // lock-free queue from
Chris@16 2 // Michael, M. M. and Scott, M. L.,
Chris@16 3 // "simple, fast and practical non-blocking and blocking concurrent queue algorithms"
Chris@16 4 //
Chris@16 5 // Copyright (C) 2008-2013 Tim Blechmann
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 8 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 9 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 10
Chris@16 11 #ifndef BOOST_LOCKFREE_FIFO_HPP_INCLUDED
Chris@16 12 #define BOOST_LOCKFREE_FIFO_HPP_INCLUDED
Chris@16 13
Chris@16 14 #include <boost/assert.hpp>
Chris@16 15 #include <boost/static_assert.hpp>
Chris@16 16 #include <boost/type_traits/has_trivial_assign.hpp>
Chris@16 17 #include <boost/type_traits/has_trivial_destructor.hpp>
Chris@16 18
Chris@16 19 #include <boost/lockfree/detail/atomic.hpp>
Chris@16 20 #include <boost/lockfree/detail/copy_payload.hpp>
Chris@16 21 #include <boost/lockfree/detail/freelist.hpp>
Chris@16 22 #include <boost/lockfree/detail/parameter.hpp>
Chris@16 23 #include <boost/lockfree/detail/tagged_ptr.hpp>
Chris@16 24
Chris@101 25 #ifdef BOOST_HAS_PRAGMA_ONCE
Chris@101 26 #pragma once
Chris@101 27 #endif
Chris@101 28
Chris@101 29
Chris@16 30 #if defined(_MSC_VER)
Chris@16 31 #pragma warning(push)
Chris@16 32 #pragma warning(disable: 4324) // structure was padded due to __declspec(align())
Chris@16 33 #endif
Chris@16 34
Chris@16 35
Chris@16 36 namespace boost {
Chris@16 37 namespace lockfree {
Chris@16 38 namespace detail {
Chris@16 39
Chris@16 40 typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
Chris@16 41 boost::parameter::optional<tag::capacity>
Chris@16 42 > queue_signature;
Chris@16 43
Chris@16 44 } /* namespace detail */
Chris@16 45
Chris@16 46
Chris@16 47 /** The queue class provides a multi-writer/multi-reader queue, pushing and popping is lock-free,
Chris@16 48 * construction/destruction has to be synchronized. It uses a freelist for memory management,
Chris@16 49 * freed nodes are pushed to the freelist and not returned to the OS before the queue is destroyed.
Chris@16 50 *
Chris@16 51 * \b Policies:
Chris@16 52 * - \ref boost::lockfree::fixed_sized, defaults to \c boost::lockfree::fixed_sized<false> \n
Chris@16 53 * Can be used to completely disable dynamic memory allocations during push in order to ensure lockfree behavior. \n
Chris@16 54 * If the data structure is configured as fixed-sized, the internal nodes are stored inside an array and they are addressed
Chris@16 55 * by array indexing. This limits the possible size of the queue to the number of elements that can be addressed by the index
Chris@16 56 * type (usually 2**16-2), but on platforms that lack double-width compare-and-exchange instructions, this is the best way
Chris@16 57 * to achieve lock-freedom.
Chris@16 58 *
Chris@16 59 * - \ref boost::lockfree::capacity, optional \n
Chris@16 60 * If this template argument is passed to the options, the size of the queue is set at compile-time.\n
Chris@16 61 * It this option implies \c fixed_sized<true>
Chris@16 62 *
Chris@16 63 * - \ref boost::lockfree::allocator, defaults to \c boost::lockfree::allocator<std::allocator<void>> \n
Chris@16 64 * Specifies the allocator that is used for the internal freelist
Chris@16 65 *
Chris@16 66 * \b Requirements:
Chris@16 67 * - T must have a copy constructor
Chris@16 68 * - T must have a trivial assignment operator
Chris@16 69 * - T must have a trivial destructor
Chris@16 70 *
Chris@16 71 * */
Chris@16 72 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 73 template <typename T,
Chris@16 74 class A0 = boost::parameter::void_,
Chris@16 75 class A1 = boost::parameter::void_,
Chris@16 76 class A2 = boost::parameter::void_>
Chris@16 77 #else
Chris@16 78 template <typename T, ...Options>
Chris@16 79 #endif
Chris@16 80 class queue
Chris@16 81 {
Chris@16 82 private:
Chris@16 83 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 84
Chris@16 85 #ifdef BOOST_HAS_TRIVIAL_DESTRUCTOR
Chris@16 86 BOOST_STATIC_ASSERT((boost::has_trivial_destructor<T>::value));
Chris@16 87 #endif
Chris@16 88
Chris@16 89 #ifdef BOOST_HAS_TRIVIAL_ASSIGN
Chris@16 90 BOOST_STATIC_ASSERT((boost::has_trivial_assign<T>::value));
Chris@16 91 #endif
Chris@16 92
Chris@16 93 typedef typename detail::queue_signature::bind<A0, A1, A2>::type bound_args;
Chris@16 94
Chris@16 95 static const bool has_capacity = detail::extract_capacity<bound_args>::has_capacity;
Chris@16 96 static const size_t capacity = detail::extract_capacity<bound_args>::capacity + 1; // the queue uses one dummy node
Chris@16 97 static const bool fixed_sized = detail::extract_fixed_sized<bound_args>::value;
Chris@16 98 static const bool node_based = !(has_capacity || fixed_sized);
Chris@16 99 static const bool compile_time_sized = has_capacity;
Chris@16 100
Chris@16 101 struct BOOST_LOCKFREE_CACHELINE_ALIGNMENT node
Chris@16 102 {
Chris@16 103 typedef typename detail::select_tagged_handle<node, node_based>::tagged_handle_type tagged_node_handle;
Chris@16 104 typedef typename detail::select_tagged_handle<node, node_based>::handle_type handle_type;
Chris@16 105
Chris@16 106 node(T const & v, handle_type null_handle):
Chris@16 107 data(v)//, next(tagged_node_handle(0, 0))
Chris@16 108 {
Chris@16 109 /* increment tag to avoid ABA problem */
Chris@16 110 tagged_node_handle old_next = next.load(memory_order_relaxed);
Chris@16 111 tagged_node_handle new_next (null_handle, old_next.get_next_tag());
Chris@16 112 next.store(new_next, memory_order_release);
Chris@16 113 }
Chris@16 114
Chris@16 115 node (handle_type null_handle):
Chris@16 116 next(tagged_node_handle(null_handle, 0))
Chris@16 117 {}
Chris@16 118
Chris@16 119 node(void)
Chris@16 120 {}
Chris@16 121
Chris@16 122 atomic<tagged_node_handle> next;
Chris@16 123 T data;
Chris@16 124 };
Chris@16 125
Chris@16 126 typedef typename detail::extract_allocator<bound_args, node>::type node_allocator;
Chris@16 127 typedef typename detail::select_freelist<node, node_allocator, compile_time_sized, fixed_sized, capacity>::type pool_t;
Chris@16 128 typedef typename pool_t::tagged_node_handle tagged_node_handle;
Chris@16 129 typedef typename detail::select_tagged_handle<node, node_based>::handle_type handle_type;
Chris@16 130
Chris@16 131 void initialize(void)
Chris@16 132 {
Chris@16 133 node * n = pool.template construct<true, false>(pool.null_handle());
Chris@16 134 tagged_node_handle dummy_node(pool.get_handle(n), 0);
Chris@16 135 head_.store(dummy_node, memory_order_relaxed);
Chris@16 136 tail_.store(dummy_node, memory_order_release);
Chris@16 137 }
Chris@16 138
Chris@16 139 struct implementation_defined
Chris@16 140 {
Chris@16 141 typedef node_allocator allocator;
Chris@16 142 typedef std::size_t size_type;
Chris@16 143 };
Chris@16 144
Chris@16 145 #endif
Chris@16 146
Chris@101 147 BOOST_DELETED_FUNCTION(queue(queue const&))
Chris@101 148 BOOST_DELETED_FUNCTION(queue& operator= (queue const&))
Chris@16 149
Chris@16 150 public:
Chris@16 151 typedef T value_type;
Chris@16 152 typedef typename implementation_defined::allocator allocator;
Chris@16 153 typedef typename implementation_defined::size_type size_type;
Chris@16 154
Chris@16 155 /**
Chris@16 156 * \return true, if implementation is lock-free.
Chris@16 157 *
Chris@16 158 * \warning It only checks, if the queue head and tail nodes and the freelist can be modified in a lock-free manner.
Chris@16 159 * On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics, there is
Chris@16 160 * no possibility to provide a completely accurate implementation, because one would need to test every internal
Chris@16 161 * node, which is impossible if further nodes will be allocated from the operating system.
Chris@16 162 * */
Chris@16 163 bool is_lock_free (void) const
Chris@16 164 {
Chris@16 165 return head_.is_lock_free() && tail_.is_lock_free() && pool.is_lock_free();
Chris@16 166 }
Chris@16 167
Chris@16 168 //! Construct queue
Chris@16 169 // @{
Chris@16 170 queue(void):
Chris@16 171 head_(tagged_node_handle(0, 0)),
Chris@16 172 tail_(tagged_node_handle(0, 0)),
Chris@16 173 pool(node_allocator(), capacity)
Chris@16 174 {
Chris@16 175 BOOST_ASSERT(has_capacity);
Chris@16 176 initialize();
Chris@16 177 }
Chris@16 178
Chris@16 179 template <typename U>
Chris@16 180 explicit queue(typename node_allocator::template rebind<U>::other const & alloc):
Chris@16 181 head_(tagged_node_handle(0, 0)),
Chris@16 182 tail_(tagged_node_handle(0, 0)),
Chris@16 183 pool(alloc, capacity)
Chris@16 184 {
Chris@16 185 BOOST_STATIC_ASSERT(has_capacity);
Chris@16 186 initialize();
Chris@16 187 }
Chris@16 188
Chris@16 189 explicit queue(allocator const & alloc):
Chris@16 190 head_(tagged_node_handle(0, 0)),
Chris@16 191 tail_(tagged_node_handle(0, 0)),
Chris@16 192 pool(alloc, capacity)
Chris@16 193 {
Chris@16 194 BOOST_ASSERT(has_capacity);
Chris@16 195 initialize();
Chris@16 196 }
Chris@16 197 // @}
Chris@16 198
Chris@16 199 //! Construct queue, allocate n nodes for the freelist.
Chris@16 200 // @{
Chris@16 201 explicit queue(size_type n):
Chris@16 202 head_(tagged_node_handle(0, 0)),
Chris@16 203 tail_(tagged_node_handle(0, 0)),
Chris@16 204 pool(node_allocator(), n + 1)
Chris@16 205 {
Chris@16 206 BOOST_ASSERT(!has_capacity);
Chris@16 207 initialize();
Chris@16 208 }
Chris@16 209
Chris@16 210 template <typename U>
Chris@16 211 queue(size_type n, typename node_allocator::template rebind<U>::other const & alloc):
Chris@16 212 head_(tagged_node_handle(0, 0)),
Chris@16 213 tail_(tagged_node_handle(0, 0)),
Chris@16 214 pool(alloc, n + 1)
Chris@16 215 {
Chris@16 216 BOOST_STATIC_ASSERT(!has_capacity);
Chris@16 217 initialize();
Chris@16 218 }
Chris@16 219 // @}
Chris@16 220
Chris@16 221 /** \copydoc boost::lockfree::stack::reserve
Chris@16 222 * */
Chris@16 223 void reserve(size_type n)
Chris@16 224 {
Chris@16 225 pool.template reserve<true>(n);
Chris@16 226 }
Chris@16 227
Chris@16 228 /** \copydoc boost::lockfree::stack::reserve_unsafe
Chris@16 229 * */
Chris@16 230 void reserve_unsafe(size_type n)
Chris@16 231 {
Chris@16 232 pool.template reserve<false>(n);
Chris@16 233 }
Chris@16 234
Chris@16 235 /** Destroys queue, free all nodes from freelist.
Chris@16 236 * */
Chris@16 237 ~queue(void)
Chris@16 238 {
Chris@16 239 T dummy;
Chris@16 240 while(unsynchronized_pop(dummy))
Chris@16 241 {}
Chris@16 242
Chris@16 243 pool.template destruct<false>(head_.load(memory_order_relaxed));
Chris@16 244 }
Chris@16 245
Chris@16 246 /** Check if the queue is empty
Chris@16 247 *
Chris@16 248 * \return true, if the queue is empty, false otherwise
Chris@16 249 * \note The result is only accurate, if no other thread modifies the queue. Therefore it is rarely practical to use this
Chris@16 250 * value in program logic.
Chris@16 251 * */
Chris@101 252 bool empty(void) const
Chris@16 253 {
Chris@16 254 return pool.get_handle(head_.load()) == pool.get_handle(tail_.load());
Chris@16 255 }
Chris@16 256
Chris@16 257 /** Pushes object t to the queue.
Chris@16 258 *
Chris@16 259 * \post object will be pushed to the queue, if internal node can be allocated
Chris@16 260 * \returns true, if the push operation is successful.
Chris@16 261 *
Chris@16 262 * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
Chris@16 263 * from the OS. This may not be lock-free.
Chris@16 264 * */
Chris@16 265 bool push(T const & t)
Chris@16 266 {
Chris@16 267 return do_push<false>(t);
Chris@16 268 }
Chris@16 269
Chris@16 270 /** Pushes object t to the queue.
Chris@16 271 *
Chris@16 272 * \post object will be pushed to the queue, if internal node can be allocated
Chris@16 273 * \returns true, if the push operation is successful.
Chris@16 274 *
Chris@16 275 * \note Thread-safe and non-blocking. If internal memory pool is exhausted, operation will fail
Chris@16 276 * \throws if memory allocator throws
Chris@16 277 * */
Chris@16 278 bool bounded_push(T const & t)
Chris@16 279 {
Chris@16 280 return do_push<true>(t);
Chris@16 281 }
Chris@16 282
Chris@16 283
Chris@16 284 private:
Chris@16 285 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 286 template <bool Bounded>
Chris@16 287 bool do_push(T const & t)
Chris@16 288 {
Chris@16 289 using detail::likely;
Chris@16 290
Chris@16 291 node * n = pool.template construct<true, Bounded>(t, pool.null_handle());
Chris@16 292 handle_type node_handle = pool.get_handle(n);
Chris@16 293
Chris@16 294 if (n == NULL)
Chris@16 295 return false;
Chris@16 296
Chris@16 297 for (;;) {
Chris@16 298 tagged_node_handle tail = tail_.load(memory_order_acquire);
Chris@16 299 node * tail_node = pool.get_pointer(tail);
Chris@16 300 tagged_node_handle next = tail_node->next.load(memory_order_acquire);
Chris@16 301 node * next_ptr = pool.get_pointer(next);
Chris@16 302
Chris@16 303 tagged_node_handle tail2 = tail_.load(memory_order_acquire);
Chris@16 304 if (likely(tail == tail2)) {
Chris@16 305 if (next_ptr == 0) {
Chris@16 306 tagged_node_handle new_tail_next(node_handle, next.get_next_tag());
Chris@16 307 if ( tail_node->next.compare_exchange_weak(next, new_tail_next) ) {
Chris@16 308 tagged_node_handle new_tail(node_handle, tail.get_next_tag());
Chris@16 309 tail_.compare_exchange_strong(tail, new_tail);
Chris@16 310 return true;
Chris@16 311 }
Chris@16 312 }
Chris@16 313 else {
Chris@16 314 tagged_node_handle new_tail(pool.get_handle(next_ptr), tail.get_next_tag());
Chris@16 315 tail_.compare_exchange_strong(tail, new_tail);
Chris@16 316 }
Chris@16 317 }
Chris@16 318 }
Chris@16 319 }
Chris@16 320 #endif
Chris@16 321
Chris@16 322 public:
Chris@16 323
Chris@16 324 /** Pushes object t to the queue.
Chris@16 325 *
Chris@16 326 * \post object will be pushed to the queue, if internal node can be allocated
Chris@16 327 * \returns true, if the push operation is successful.
Chris@16 328 *
Chris@16 329 * \note Not Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
Chris@16 330 * from the OS. This may not be lock-free.
Chris@16 331 * \throws if memory allocator throws
Chris@16 332 * */
Chris@16 333 bool unsynchronized_push(T const & t)
Chris@16 334 {
Chris@16 335 node * n = pool.template construct<false, false>(t, pool.null_handle());
Chris@16 336
Chris@16 337 if (n == NULL)
Chris@16 338 return false;
Chris@16 339
Chris@16 340 for (;;) {
Chris@16 341 tagged_node_handle tail = tail_.load(memory_order_relaxed);
Chris@16 342 tagged_node_handle next = tail->next.load(memory_order_relaxed);
Chris@16 343 node * next_ptr = next.get_ptr();
Chris@16 344
Chris@16 345 if (next_ptr == 0) {
Chris@16 346 tail->next.store(tagged_node_handle(n, next.get_next_tag()), memory_order_relaxed);
Chris@16 347 tail_.store(tagged_node_handle(n, tail.get_next_tag()), memory_order_relaxed);
Chris@16 348 return true;
Chris@16 349 }
Chris@16 350 else
Chris@16 351 tail_.store(tagged_node_handle(next_ptr, tail.get_next_tag()), memory_order_relaxed);
Chris@16 352 }
Chris@16 353 }
Chris@16 354
Chris@16 355 /** Pops object from queue.
Chris@16 356 *
Chris@16 357 * \post if pop operation is successful, object will be copied to ret.
Chris@16 358 * \returns true, if the pop operation is successful, false if queue was empty.
Chris@16 359 *
Chris@16 360 * \note Thread-safe and non-blocking
Chris@16 361 * */
Chris@16 362 bool pop (T & ret)
Chris@16 363 {
Chris@16 364 return pop<T>(ret);
Chris@16 365 }
Chris@16 366
Chris@16 367 /** Pops object from queue.
Chris@16 368 *
Chris@16 369 * \pre type U must be constructible by T and copyable, or T must be convertible to U
Chris@16 370 * \post if pop operation is successful, object will be copied to ret.
Chris@16 371 * \returns true, if the pop operation is successful, false if queue was empty.
Chris@16 372 *
Chris@16 373 * \note Thread-safe and non-blocking
Chris@16 374 * */
Chris@16 375 template <typename U>
Chris@16 376 bool pop (U & ret)
Chris@16 377 {
Chris@16 378 using detail::likely;
Chris@16 379 for (;;) {
Chris@16 380 tagged_node_handle head = head_.load(memory_order_acquire);
Chris@16 381 node * head_ptr = pool.get_pointer(head);
Chris@16 382
Chris@16 383 tagged_node_handle tail = tail_.load(memory_order_acquire);
Chris@16 384 tagged_node_handle next = head_ptr->next.load(memory_order_acquire);
Chris@16 385 node * next_ptr = pool.get_pointer(next);
Chris@16 386
Chris@16 387 tagged_node_handle head2 = head_.load(memory_order_acquire);
Chris@16 388 if (likely(head == head2)) {
Chris@16 389 if (pool.get_handle(head) == pool.get_handle(tail)) {
Chris@16 390 if (next_ptr == 0)
Chris@16 391 return false;
Chris@16 392
Chris@16 393 tagged_node_handle new_tail(pool.get_handle(next), tail.get_next_tag());
Chris@16 394 tail_.compare_exchange_strong(tail, new_tail);
Chris@16 395
Chris@16 396 } else {
Chris@16 397 if (next_ptr == 0)
Chris@16 398 /* this check is not part of the original algorithm as published by michael and scott
Chris@16 399 *
Chris@16 400 * however we reuse the tagged_ptr part for the freelist and clear the next part during node
Chris@16 401 * allocation. we can observe a null-pointer here.
Chris@16 402 * */
Chris@16 403 continue;
Chris@16 404 detail::copy_payload(next_ptr->data, ret);
Chris@16 405
Chris@16 406 tagged_node_handle new_head(pool.get_handle(next), head.get_next_tag());
Chris@16 407 if (head_.compare_exchange_weak(head, new_head)) {
Chris@16 408 pool.template destruct<true>(head);
Chris@16 409 return true;
Chris@16 410 }
Chris@16 411 }
Chris@16 412 }
Chris@16 413 }
Chris@16 414 }
Chris@16 415
Chris@16 416 /** Pops object from queue.
Chris@16 417 *
Chris@16 418 * \post if pop operation is successful, object will be copied to ret.
Chris@16 419 * \returns true, if the pop operation is successful, false if queue was empty.
Chris@16 420 *
Chris@16 421 * \note Not thread-safe, but non-blocking
Chris@16 422 *
Chris@16 423 * */
Chris@16 424 bool unsynchronized_pop (T & ret)
Chris@16 425 {
Chris@16 426 return unsynchronized_pop<T>(ret);
Chris@16 427 }
Chris@16 428
Chris@16 429 /** Pops object from queue.
Chris@16 430 *
Chris@16 431 * \pre type U must be constructible by T and copyable, or T must be convertible to U
Chris@16 432 * \post if pop operation is successful, object will be copied to ret.
Chris@16 433 * \returns true, if the pop operation is successful, false if queue was empty.
Chris@16 434 *
Chris@16 435 * \note Not thread-safe, but non-blocking
Chris@16 436 *
Chris@16 437 * */
Chris@16 438 template <typename U>
Chris@16 439 bool unsynchronized_pop (U & ret)
Chris@16 440 {
Chris@16 441 for (;;) {
Chris@16 442 tagged_node_handle head = head_.load(memory_order_relaxed);
Chris@16 443 node * head_ptr = pool.get_pointer(head);
Chris@16 444 tagged_node_handle tail = tail_.load(memory_order_relaxed);
Chris@16 445 tagged_node_handle next = head_ptr->next.load(memory_order_relaxed);
Chris@16 446 node * next_ptr = pool.get_pointer(next);
Chris@16 447
Chris@16 448 if (pool.get_handle(head) == pool.get_handle(tail)) {
Chris@16 449 if (next_ptr == 0)
Chris@16 450 return false;
Chris@16 451
Chris@16 452 tagged_node_handle new_tail(pool.get_handle(next), tail.get_next_tag());
Chris@16 453 tail_.store(new_tail);
Chris@16 454 } else {
Chris@16 455 if (next_ptr == 0)
Chris@16 456 /* this check is not part of the original algorithm as published by michael and scott
Chris@16 457 *
Chris@16 458 * however we reuse the tagged_ptr part for the freelist and clear the next part during node
Chris@16 459 * allocation. we can observe a null-pointer here.
Chris@16 460 * */
Chris@16 461 continue;
Chris@16 462 detail::copy_payload(next_ptr->data, ret);
Chris@16 463 tagged_node_handle new_head(pool.get_handle(next), head.get_next_tag());
Chris@16 464 head_.store(new_head);
Chris@16 465 pool.template destruct<false>(head);
Chris@16 466 return true;
Chris@16 467 }
Chris@16 468 }
Chris@16 469 }
Chris@16 470
Chris@16 471 /** consumes one element via a functor
Chris@16 472 *
Chris@16 473 * pops one element from the queue and applies the functor on this object
Chris@16 474 *
Chris@16 475 * \returns true, if one element was consumed
Chris@16 476 *
Chris@16 477 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
Chris@16 478 * */
Chris@16 479 template <typename Functor>
Chris@16 480 bool consume_one(Functor & f)
Chris@16 481 {
Chris@16 482 T element;
Chris@16 483 bool success = pop(element);
Chris@16 484 if (success)
Chris@16 485 f(element);
Chris@16 486
Chris@16 487 return success;
Chris@16 488 }
Chris@16 489
Chris@16 490 /// \copydoc boost::lockfree::queue::consume_one(Functor & rhs)
Chris@16 491 template <typename Functor>
Chris@16 492 bool consume_one(Functor const & f)
Chris@16 493 {
Chris@16 494 T element;
Chris@16 495 bool success = pop(element);
Chris@16 496 if (success)
Chris@16 497 f(element);
Chris@16 498
Chris@16 499 return success;
Chris@16 500 }
Chris@16 501
Chris@16 502 /** consumes all elements via a functor
Chris@16 503 *
Chris@16 504 * sequentially pops all elements from the queue and applies the functor on each object
Chris@16 505 *
Chris@16 506 * \returns number of elements that are consumed
Chris@16 507 *
Chris@16 508 * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
Chris@16 509 * */
Chris@16 510 template <typename Functor>
Chris@16 511 size_t consume_all(Functor & f)
Chris@16 512 {
Chris@16 513 size_t element_count = 0;
Chris@16 514 while (consume_one(f))
Chris@16 515 element_count += 1;
Chris@16 516
Chris@16 517 return element_count;
Chris@16 518 }
Chris@16 519
Chris@16 520 /// \copydoc boost::lockfree::queue::consume_all(Functor & rhs)
Chris@16 521 template <typename Functor>
Chris@16 522 size_t consume_all(Functor const & f)
Chris@16 523 {
Chris@16 524 size_t element_count = 0;
Chris@16 525 while (consume_one(f))
Chris@16 526 element_count += 1;
Chris@16 527
Chris@16 528 return element_count;
Chris@16 529 }
Chris@16 530
Chris@16 531 private:
Chris@16 532 #ifndef BOOST_DOXYGEN_INVOKED
Chris@16 533 atomic<tagged_node_handle> head_;
Chris@16 534 static const int padding_size = BOOST_LOCKFREE_CACHELINE_BYTES - sizeof(tagged_node_handle);
Chris@16 535 char padding1[padding_size];
Chris@16 536 atomic<tagged_node_handle> tail_;
Chris@16 537 char padding2[padding_size];
Chris@16 538
Chris@16 539 pool_t pool;
Chris@16 540 #endif
Chris@16 541 };
Chris@16 542
Chris@16 543 } /* namespace lockfree */
Chris@16 544 } /* namespace boost */
Chris@16 545
Chris@16 546 #if defined(_MSC_VER)
Chris@16 547 #pragma warning(pop)
Chris@16 548 #endif
Chris@16 549
Chris@16 550 #endif /* BOOST_LOCKFREE_FIFO_HPP_INCLUDED */