annotate DEPENDENCIES/generic/include/boost/lockfree/queue.hpp @ 16:2665513ce2d3

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