stack.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // Copyright (C) 2008-2013 Tim Blechmann
  2. //
  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)
  6. #ifndef BOOST_LOCKFREE_STACK_HPP_INCLUDED
  7. #define BOOST_LOCKFREE_STACK_HPP_INCLUDED
  8. #include <boost/assert.hpp>
  9. #include <boost/checked_delete.hpp>
  10. #include <boost/core/allocator_access.hpp>
  11. #include <boost/core/no_exceptions_support.hpp>
  12. #include <boost/integer_traits.hpp>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/tuple/tuple.hpp>
  15. #include <boost/type_traits/is_copy_constructible.hpp>
  16. #include <boost/lockfree/detail/atomic.hpp>
  17. #include <boost/lockfree/detail/copy_payload.hpp>
  18. #include <boost/lockfree/detail/freelist.hpp>
  19. #include <boost/lockfree/detail/parameter.hpp>
  20. #include <boost/lockfree/detail/tagged_ptr.hpp>
  21. #include <boost/lockfree/lockfree_forward.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. namespace lockfree {
  27. namespace detail {
  28. typedef parameter::parameters<boost::parameter::optional<tag::allocator>,
  29. boost::parameter::optional<tag::capacity>
  30. > stack_signature;
  31. }
  32. /** The stack class provides a multi-writer/multi-reader stack, pushing and popping is lock-free,
  33. * construction/destruction has to be synchronized. It uses a freelist for memory management,
  34. * freed nodes are pushed to the freelist and not returned to the OS before the stack is destroyed.
  35. *
  36. * \b Policies:
  37. *
  38. * - \c boost::lockfree::fixed_sized<>, defaults to \c boost::lockfree::fixed_sized<false> <br>
  39. * Can be used to completely disable dynamic memory allocations during push in order to ensure lockfree behavior.<br>
  40. * If the data structure is configured as fixed-sized, the internal nodes are stored inside an array and they are addressed
  41. * by array indexing. This limits the possible size of the stack to the number of elements that can be addressed by the index
  42. * type (usually 2**16-2), but on platforms that lack double-width compare-and-exchange instructions, this is the best way
  43. * to achieve lock-freedom.
  44. *
  45. * - \c boost::lockfree::capacity<>, optional <br>
  46. * If this template argument is passed to the options, the size of the stack is set at compile-time. <br>
  47. * It this option implies \c fixed_sized<true>
  48. *
  49. * - \c boost::lockfree::allocator<>, defaults to \c boost::lockfree::allocator<std::allocator<void>> <br>
  50. * Specifies the allocator that is used for the internal freelist
  51. *
  52. * \b Requirements:
  53. * - T must have a copy constructor
  54. * */
  55. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  56. template <typename T, class A0, class A1, class A2>
  57. #else
  58. template <typename T, typename ...Options>
  59. #endif
  60. class stack
  61. {
  62. private:
  63. #ifndef BOOST_DOXYGEN_INVOKED
  64. BOOST_STATIC_ASSERT(boost::is_copy_constructible<T>::value);
  65. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  66. typedef typename detail::stack_signature::bind<A0, A1, A2>::type bound_args;
  67. #else
  68. typedef typename detail::stack_signature::bind<Options...>::type bound_args;
  69. #endif
  70. static const bool has_capacity = detail::extract_capacity<bound_args>::has_capacity;
  71. static const size_t capacity = detail::extract_capacity<bound_args>::capacity;
  72. static const bool fixed_sized = detail::extract_fixed_sized<bound_args>::value;
  73. static const bool node_based = !(has_capacity || fixed_sized);
  74. static const bool compile_time_sized = has_capacity;
  75. struct node
  76. {
  77. node(T const & val):
  78. v(val)
  79. {}
  80. typedef typename detail::select_tagged_handle<node, node_based>::handle_type handle_t;
  81. handle_t next;
  82. const T v;
  83. };
  84. typedef typename detail::extract_allocator<bound_args, node>::type node_allocator;
  85. typedef typename detail::select_freelist<node, node_allocator, compile_time_sized, fixed_sized, capacity>::type pool_t;
  86. typedef typename pool_t::tagged_node_handle tagged_node_handle;
  87. // check compile-time capacity
  88. BOOST_STATIC_ASSERT((mpl::if_c<has_capacity,
  89. mpl::bool_<capacity - 1 < boost::integer_traits<boost::uint16_t>::const_max>,
  90. mpl::true_
  91. >::type::value));
  92. struct implementation_defined
  93. {
  94. typedef node_allocator allocator;
  95. typedef std::size_t size_type;
  96. };
  97. #endif
  98. BOOST_DELETED_FUNCTION(stack(stack const&))
  99. BOOST_DELETED_FUNCTION(stack& operator= (stack const&))
  100. public:
  101. typedef T value_type;
  102. typedef typename implementation_defined::allocator allocator;
  103. typedef typename implementation_defined::size_type size_type;
  104. /**
  105. * \return true, if implementation is lock-free.
  106. *
  107. * \warning It only checks, if the top stack node and the freelist can be modified in a lock-free manner.
  108. * On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics,
  109. * there is no possibility to provide a completely accurate implementation, because one would need to test
  110. * every internal node, which is impossible if further nodes will be allocated from the operating system.
  111. *
  112. * */
  113. bool is_lock_free (void) const
  114. {
  115. return tos.is_lock_free() && pool.is_lock_free();
  116. }
  117. /** Construct a fixed-sized stack
  118. *
  119. * \pre Must specify a capacity<> argument
  120. * */
  121. stack(void):
  122. pool(node_allocator(), capacity)
  123. {
  124. // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
  125. // this function and this function may be compiled even when it isn't being used.
  126. BOOST_ASSERT(has_capacity);
  127. initialize();
  128. }
  129. /** Construct a fixed-sized stack with a custom allocator
  130. *
  131. * \pre Must specify a capacity<> argument
  132. * */
  133. template <typename U>
  134. explicit stack(typename boost::allocator_rebind<node_allocator, U>::type const & alloc):
  135. pool(alloc, capacity)
  136. {
  137. BOOST_STATIC_ASSERT(has_capacity);
  138. initialize();
  139. }
  140. /** Construct a fixed-sized stack with a custom allocator
  141. *
  142. * \pre Must specify a capacity<> argument
  143. * */
  144. explicit stack(allocator const & alloc):
  145. pool(alloc, capacity)
  146. {
  147. // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
  148. // this function and this function may be compiled even when it isn't being used.
  149. BOOST_ASSERT(has_capacity);
  150. initialize();
  151. }
  152. /** Construct a variable-sized stack
  153. *
  154. * Allocate n nodes initially for the freelist
  155. *
  156. * \pre Must \b not specify a capacity<> argument
  157. * */
  158. explicit stack(size_type n):
  159. pool(node_allocator(), n)
  160. {
  161. // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
  162. // this function and this function may be compiled even when it isn't being used.
  163. BOOST_ASSERT(!has_capacity);
  164. initialize();
  165. }
  166. /** Construct a variable-sized stack with a custom allocator
  167. *
  168. * Allocate n nodes initially for the freelist
  169. *
  170. * \pre Must \b not specify a capacity<> argument
  171. * */
  172. template <typename U>
  173. stack(size_type n, typename boost::allocator_rebind<node_allocator, U>::type const & alloc):
  174. pool(alloc, n)
  175. {
  176. BOOST_STATIC_ASSERT(!has_capacity);
  177. initialize();
  178. }
  179. /** Allocate n nodes for freelist
  180. *
  181. * \pre only valid if no capacity<> argument given
  182. * \note thread-safe, may block if memory allocator blocks
  183. *
  184. * */
  185. void reserve(size_type n)
  186. {
  187. // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
  188. // this function and this function may be compiled even when it isn't being used.
  189. BOOST_ASSERT(!has_capacity);
  190. pool.template reserve<true>(n);
  191. }
  192. /** Allocate n nodes for freelist
  193. *
  194. * \pre only valid if no capacity<> argument given
  195. * \note not thread-safe, may block if memory allocator blocks
  196. *
  197. * */
  198. void reserve_unsafe(size_type n)
  199. {
  200. // Don't use BOOST_STATIC_ASSERT() here since it will be evaluated when compiling
  201. // this function and this function may be compiled even when it isn't being used.
  202. BOOST_ASSERT(!has_capacity);
  203. pool.template reserve<false>(n);
  204. }
  205. /** Destroys stack, free all nodes from freelist.
  206. *
  207. * \note not thread-safe
  208. *
  209. * */
  210. ~stack(void)
  211. {
  212. T dummy;
  213. while(unsynchronized_pop(dummy))
  214. {}
  215. }
  216. private:
  217. #ifndef BOOST_DOXYGEN_INVOKED
  218. void initialize(void)
  219. {
  220. tos.store(tagged_node_handle(pool.null_handle(), 0));
  221. }
  222. void link_nodes_atomic(node * new_top_node, node * end_node)
  223. {
  224. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  225. for (;;) {
  226. tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
  227. end_node->next = pool.get_handle(old_tos);
  228. if (tos.compare_exchange_weak(old_tos, new_tos))
  229. break;
  230. }
  231. }
  232. void link_nodes_unsafe(node * new_top_node, node * end_node)
  233. {
  234. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  235. tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
  236. end_node->next = pool.get_handle(old_tos);
  237. tos.store(new_tos, memory_order_relaxed);
  238. }
  239. template <bool Threadsafe, bool Bounded, typename ConstIterator>
  240. tuple<node*, node*> prepare_node_list(ConstIterator begin, ConstIterator end, ConstIterator & ret)
  241. {
  242. ConstIterator it = begin;
  243. node * end_node = pool.template construct<Threadsafe, Bounded>(*it++);
  244. if (end_node == NULL) {
  245. ret = begin;
  246. return make_tuple<node*, node*>(NULL, NULL);
  247. }
  248. node * new_top_node = end_node;
  249. end_node->next = NULL;
  250. BOOST_TRY {
  251. /* link nodes */
  252. for (; it != end; ++it) {
  253. node * newnode = pool.template construct<Threadsafe, Bounded>(*it);
  254. if (newnode == NULL)
  255. break;
  256. newnode->next = new_top_node;
  257. new_top_node = newnode;
  258. }
  259. } BOOST_CATCH (...) {
  260. for (node * current_node = new_top_node; current_node != NULL;) {
  261. node * next = current_node->next;
  262. pool.template destruct<Threadsafe>(current_node);
  263. current_node = next;
  264. }
  265. BOOST_RETHROW;
  266. }
  267. BOOST_CATCH_END
  268. ret = it;
  269. return make_tuple(new_top_node, end_node);
  270. }
  271. #endif
  272. public:
  273. /** Pushes object t to the stack.
  274. *
  275. * \post object will be pushed to the stack, if internal node can be allocated
  276. * \returns true, if the push operation is successful.
  277. *
  278. * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  279. * from the OS. This may not be lock-free.
  280. * \throws if memory allocator throws
  281. * */
  282. bool push(T const & v)
  283. {
  284. return do_push<false>(v);
  285. }
  286. /** Pushes object t to the stack.
  287. *
  288. * \post object will be pushed to the stack, if internal node can be allocated
  289. * \returns true, if the push operation is successful.
  290. *
  291. * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
  292. * */
  293. bool bounded_push(T const & v)
  294. {
  295. return do_push<true>(v);
  296. }
  297. #ifndef BOOST_DOXYGEN_INVOKED
  298. private:
  299. template <bool Bounded>
  300. bool do_push(T const & v)
  301. {
  302. node * newnode = pool.template construct<true, Bounded>(v);
  303. if (newnode == 0)
  304. return false;
  305. link_nodes_atomic(newnode, newnode);
  306. return true;
  307. }
  308. template <bool Bounded, typename ConstIterator>
  309. ConstIterator do_push(ConstIterator begin, ConstIterator end)
  310. {
  311. node * new_top_node;
  312. node * end_node;
  313. ConstIterator ret;
  314. tie(new_top_node, end_node) = prepare_node_list<true, Bounded>(begin, end, ret);
  315. if (new_top_node)
  316. link_nodes_atomic(new_top_node, end_node);
  317. return ret;
  318. }
  319. public:
  320. #endif
  321. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  322. *
  323. * \return iterator to the first element, which has not been pushed
  324. *
  325. * \note Operation is applied atomically
  326. * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  327. * from the OS. This may not be lock-free.
  328. * \throws if memory allocator throws
  329. */
  330. template <typename ConstIterator>
  331. ConstIterator push(ConstIterator begin, ConstIterator end)
  332. {
  333. return do_push<false, ConstIterator>(begin, end);
  334. }
  335. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  336. *
  337. * \return iterator to the first element, which has not been pushed
  338. *
  339. * \note Operation is applied atomically
  340. * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
  341. * \throws if memory allocator throws
  342. */
  343. template <typename ConstIterator>
  344. ConstIterator bounded_push(ConstIterator begin, ConstIterator end)
  345. {
  346. return do_push<true, ConstIterator>(begin, end);
  347. }
  348. /** Pushes object t to the stack.
  349. *
  350. * \post object will be pushed to the stack, if internal node can be allocated
  351. * \returns true, if the push operation is successful.
  352. *
  353. * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  354. * from the OS. This may not be lock-free.
  355. * \throws if memory allocator throws
  356. * */
  357. bool unsynchronized_push(T const & v)
  358. {
  359. node * newnode = pool.template construct<false, false>(v);
  360. if (newnode == 0)
  361. return false;
  362. link_nodes_unsafe(newnode, newnode);
  363. return true;
  364. }
  365. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  366. *
  367. * \return iterator to the first element, which has not been pushed
  368. *
  369. * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  370. * from the OS. This may not be lock-free.
  371. * \throws if memory allocator throws
  372. */
  373. template <typename ConstIterator>
  374. ConstIterator unsynchronized_push(ConstIterator begin, ConstIterator end)
  375. {
  376. node * new_top_node;
  377. node * end_node;
  378. ConstIterator ret;
  379. tie(new_top_node, end_node) = prepare_node_list<false, false>(begin, end, ret);
  380. if (new_top_node)
  381. link_nodes_unsafe(new_top_node, end_node);
  382. return ret;
  383. }
  384. /** Pops object from stack.
  385. *
  386. * \post if pop operation is successful, object will be copied to ret.
  387. * \returns true, if the pop operation is successful, false if stack was empty.
  388. *
  389. * \note Thread-safe and non-blocking
  390. *
  391. * */
  392. bool pop(T & ret)
  393. {
  394. return pop<T>(ret);
  395. }
  396. /** Pops object from stack.
  397. *
  398. * \pre type T must be convertible to U
  399. * \post if pop operation is successful, object will be copied to ret.
  400. * \returns true, if the pop operation is successful, false if stack was empty.
  401. *
  402. * \note Thread-safe and non-blocking
  403. *
  404. * */
  405. template <typename U>
  406. bool pop(U & ret)
  407. {
  408. BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
  409. detail::consume_via_copy<U> consumer(ret);
  410. return consume_one(consumer);
  411. }
  412. /** Pops object from stack.
  413. *
  414. * \post if pop operation is successful, object will be copied to ret.
  415. * \returns true, if the pop operation is successful, false if stack was empty.
  416. *
  417. * \note Not thread-safe, but non-blocking
  418. *
  419. * */
  420. bool unsynchronized_pop(T & ret)
  421. {
  422. return unsynchronized_pop<T>(ret);
  423. }
  424. /** Pops object from stack.
  425. *
  426. * \pre type T must be convertible to U
  427. * \post if pop operation is successful, object will be copied to ret.
  428. * \returns true, if the pop operation is successful, false if stack was empty.
  429. *
  430. * \note Not thread-safe, but non-blocking
  431. *
  432. * */
  433. template <typename U>
  434. bool unsynchronized_pop(U & ret)
  435. {
  436. BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
  437. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  438. node * old_tos_pointer = pool.get_pointer(old_tos);
  439. if (!pool.get_pointer(old_tos))
  440. return false;
  441. node * new_tos_ptr = pool.get_pointer(old_tos_pointer->next);
  442. tagged_node_handle new_tos(pool.get_handle(new_tos_ptr), old_tos.get_next_tag());
  443. tos.store(new_tos, memory_order_relaxed);
  444. detail::copy_payload(old_tos_pointer->v, ret);
  445. pool.template destruct<false>(old_tos);
  446. return true;
  447. }
  448. /** consumes one element via a functor
  449. *
  450. * pops one element from the stack and applies the functor on this object
  451. *
  452. * \returns true, if one element was consumed
  453. *
  454. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  455. * */
  456. template <typename Functor>
  457. bool consume_one(Functor & f)
  458. {
  459. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  460. for (;;) {
  461. node * old_tos_pointer = pool.get_pointer(old_tos);
  462. if (!old_tos_pointer)
  463. return false;
  464. tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
  465. if (tos.compare_exchange_weak(old_tos, new_tos)) {
  466. f(old_tos_pointer->v);
  467. pool.template destruct<true>(old_tos);
  468. return true;
  469. }
  470. }
  471. }
  472. /// \copydoc boost::lockfree::stack::consume_one(Functor & rhs)
  473. template <typename Functor>
  474. bool consume_one(Functor const & f)
  475. {
  476. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  477. for (;;) {
  478. node * old_tos_pointer = pool.get_pointer(old_tos);
  479. if (!old_tos_pointer)
  480. return false;
  481. tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
  482. if (tos.compare_exchange_weak(old_tos, new_tos)) {
  483. f(old_tos_pointer->v);
  484. pool.template destruct<true>(old_tos);
  485. return true;
  486. }
  487. }
  488. }
  489. /** consumes all elements via a functor
  490. *
  491. * sequentially pops all elements from the stack and applies the functor on each object
  492. *
  493. * \returns number of elements that are consumed
  494. *
  495. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  496. * */
  497. template <typename Functor>
  498. size_t consume_all(Functor & f)
  499. {
  500. size_t element_count = 0;
  501. while (consume_one(f))
  502. element_count += 1;
  503. return element_count;
  504. }
  505. /// \copydoc boost::lockfree::stack::consume_all(Functor & rhs)
  506. template <typename Functor>
  507. size_t consume_all(Functor const & f)
  508. {
  509. size_t element_count = 0;
  510. while (consume_one(f))
  511. element_count += 1;
  512. return element_count;
  513. }
  514. /** consumes all elements via a functor
  515. *
  516. * atomically pops all elements from the stack and applies the functor on each object
  517. *
  518. * \returns number of elements that are consumed
  519. *
  520. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  521. * */
  522. template <typename Functor>
  523. size_t consume_all_atomic(Functor & f)
  524. {
  525. size_t element_count = 0;
  526. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  527. for (;;) {
  528. node * old_tos_pointer = pool.get_pointer(old_tos);
  529. if (!old_tos_pointer)
  530. return 0;
  531. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  532. if (tos.compare_exchange_weak(old_tos, new_tos))
  533. break;
  534. }
  535. tagged_node_handle nodes_to_consume = old_tos;
  536. for(;;) {
  537. node * node_pointer = pool.get_pointer(nodes_to_consume);
  538. f(node_pointer->v);
  539. element_count += 1;
  540. node * next_node = pool.get_pointer(node_pointer->next);
  541. if (!next_node) {
  542. pool.template destruct<true>(nodes_to_consume);
  543. break;
  544. }
  545. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  546. pool.template destruct<true>(nodes_to_consume);
  547. nodes_to_consume = next;
  548. }
  549. return element_count;
  550. }
  551. /// \copydoc boost::lockfree::stack::consume_all_atomic(Functor & rhs)
  552. template <typename Functor>
  553. size_t consume_all_atomic(Functor const & f)
  554. {
  555. size_t element_count = 0;
  556. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  557. for (;;) {
  558. node * old_tos_pointer = pool.get_pointer(old_tos);
  559. if (!old_tos_pointer)
  560. return 0;
  561. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  562. if (tos.compare_exchange_weak(old_tos, new_tos))
  563. break;
  564. }
  565. tagged_node_handle nodes_to_consume = old_tos;
  566. for(;;) {
  567. node * node_pointer = pool.get_pointer(nodes_to_consume);
  568. f(node_pointer->v);
  569. element_count += 1;
  570. node * next_node = pool.get_pointer(node_pointer->next);
  571. if (!next_node) {
  572. pool.template destruct<true>(nodes_to_consume);
  573. break;
  574. }
  575. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  576. pool.template destruct<true>(nodes_to_consume);
  577. nodes_to_consume = next;
  578. }
  579. return element_count;
  580. }
  581. /** consumes all elements via a functor
  582. *
  583. * atomically pops all elements from the stack and applies the functor on each object in reversed order
  584. *
  585. * \returns number of elements that are consumed
  586. *
  587. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  588. * */
  589. template <typename Functor>
  590. size_t consume_all_atomic_reversed(Functor & f)
  591. {
  592. size_t element_count = 0;
  593. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  594. for (;;) {
  595. node * old_tos_pointer = pool.get_pointer(old_tos);
  596. if (!old_tos_pointer)
  597. return 0;
  598. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  599. if (tos.compare_exchange_weak(old_tos, new_tos))
  600. break;
  601. }
  602. tagged_node_handle nodes_to_consume = old_tos;
  603. node * last_node_pointer = NULL;
  604. tagged_node_handle nodes_in_reversed_order;
  605. for(;;) {
  606. node * node_pointer = pool.get_pointer(nodes_to_consume);
  607. node * next_node = pool.get_pointer(node_pointer->next);
  608. node_pointer->next = pool.get_handle(last_node_pointer);
  609. last_node_pointer = node_pointer;
  610. if (!next_node) {
  611. nodes_in_reversed_order = nodes_to_consume;
  612. break;
  613. }
  614. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  615. nodes_to_consume = next;
  616. }
  617. for(;;) {
  618. node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
  619. f(node_pointer->v);
  620. element_count += 1;
  621. node * next_node = pool.get_pointer(node_pointer->next);
  622. if (!next_node) {
  623. pool.template destruct<true>(nodes_in_reversed_order);
  624. break;
  625. }
  626. tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
  627. pool.template destruct<true>(nodes_in_reversed_order);
  628. nodes_in_reversed_order = next;
  629. }
  630. return element_count;
  631. }
  632. /// \copydoc boost::lockfree::stack::consume_all_atomic_reversed(Functor & rhs)
  633. template <typename Functor>
  634. size_t consume_all_atomic_reversed(Functor const & f)
  635. {
  636. size_t element_count = 0;
  637. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  638. for (;;) {
  639. node * old_tos_pointer = pool.get_pointer(old_tos);
  640. if (!old_tos_pointer)
  641. return 0;
  642. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  643. if (tos.compare_exchange_weak(old_tos, new_tos))
  644. break;
  645. }
  646. tagged_node_handle nodes_to_consume = old_tos;
  647. node * last_node_pointer = NULL;
  648. tagged_node_handle nodes_in_reversed_order;
  649. for(;;) {
  650. node * node_pointer = pool.get_pointer(nodes_to_consume);
  651. node * next_node = pool.get_pointer(node_pointer->next);
  652. node_pointer->next = pool.get_handle(last_node_pointer);
  653. last_node_pointer = node_pointer;
  654. if (!next_node) {
  655. nodes_in_reversed_order = nodes_to_consume;
  656. break;
  657. }
  658. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  659. nodes_to_consume = next;
  660. }
  661. for(;;) {
  662. node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
  663. f(node_pointer->v);
  664. element_count += 1;
  665. node * next_node = pool.get_pointer(node_pointer->next);
  666. if (!next_node) {
  667. pool.template destruct<true>(nodes_in_reversed_order);
  668. break;
  669. }
  670. tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
  671. pool.template destruct<true>(nodes_in_reversed_order);
  672. nodes_in_reversed_order = next;
  673. }
  674. return element_count;
  675. }
  676. /**
  677. * \return true, if stack is empty.
  678. *
  679. * \note It only guarantees that at some point during the execution of the function the stack has been empty.
  680. * It is rarely practical to use this value in program logic, because the stack can be modified by other threads.
  681. * */
  682. bool empty(void) const
  683. {
  684. return pool.get_pointer(tos.load()) == NULL;
  685. }
  686. private:
  687. #ifndef BOOST_DOXYGEN_INVOKED
  688. detail::atomic<tagged_node_handle> tos;
  689. static const int padding_size = BOOST_LOCKFREE_CACHELINE_BYTES - sizeof(tagged_node_handle);
  690. char padding[padding_size];
  691. pool_t pool;
  692. #endif
  693. };
  694. } /* namespace lockfree */
  695. } /* namespace boost */
  696. #endif /* BOOST_LOCKFREE_STACK_HPP_INCLUDED */