stack.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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/no_exceptions_support.hpp>
  11. #include <boost/integer_traits.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <boost/tuple/tuple.hpp>
  14. #include <boost/type_traits/is_copy_constructible.hpp>
  15. #include <boost/lockfree/detail/allocator_rebind_helper.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 stack
  118. // @{
  119. stack(void):
  120. pool(node_allocator(), capacity)
  121. {
  122. BOOST_ASSERT(has_capacity);
  123. initialize();
  124. }
  125. template <typename U>
  126. explicit stack(typename detail::allocator_rebind_helper<node_allocator, U>::type const & alloc):
  127. pool(alloc, capacity)
  128. {
  129. BOOST_STATIC_ASSERT(has_capacity);
  130. initialize();
  131. }
  132. explicit stack(allocator const & alloc):
  133. pool(alloc, capacity)
  134. {
  135. BOOST_ASSERT(has_capacity);
  136. initialize();
  137. }
  138. // @}
  139. //! Construct stack, allocate n nodes for the freelist.
  140. // @{
  141. explicit stack(size_type n):
  142. pool(node_allocator(), n)
  143. {
  144. BOOST_ASSERT(!has_capacity);
  145. initialize();
  146. }
  147. template <typename U>
  148. stack(size_type n, typename detail::allocator_rebind_helper<node_allocator, U>::type const & alloc):
  149. pool(alloc, n)
  150. {
  151. BOOST_STATIC_ASSERT(!has_capacity);
  152. initialize();
  153. }
  154. // @}
  155. /** Allocate n nodes for freelist
  156. *
  157. * \pre only valid if no capacity<> argument given
  158. * \note thread-safe, may block if memory allocator blocks
  159. *
  160. * */
  161. void reserve(size_type n)
  162. {
  163. BOOST_STATIC_ASSERT(!has_capacity);
  164. pool.template reserve<true>(n);
  165. }
  166. /** Allocate n nodes for freelist
  167. *
  168. * \pre only valid if no capacity<> argument given
  169. * \note not thread-safe, may block if memory allocator blocks
  170. *
  171. * */
  172. void reserve_unsafe(size_type n)
  173. {
  174. BOOST_STATIC_ASSERT(!has_capacity);
  175. pool.template reserve<false>(n);
  176. }
  177. /** Destroys stack, free all nodes from freelist.
  178. *
  179. * \note not thread-safe
  180. *
  181. * */
  182. ~stack(void)
  183. {
  184. T dummy;
  185. while(unsynchronized_pop(dummy))
  186. {}
  187. }
  188. private:
  189. #ifndef BOOST_DOXYGEN_INVOKED
  190. void initialize(void)
  191. {
  192. tos.store(tagged_node_handle(pool.null_handle(), 0));
  193. }
  194. void link_nodes_atomic(node * new_top_node, node * end_node)
  195. {
  196. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  197. for (;;) {
  198. tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
  199. end_node->next = pool.get_handle(old_tos);
  200. if (tos.compare_exchange_weak(old_tos, new_tos))
  201. break;
  202. }
  203. }
  204. void link_nodes_unsafe(node * new_top_node, node * end_node)
  205. {
  206. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  207. tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
  208. end_node->next = pool.get_pointer(old_tos);
  209. tos.store(new_tos, memory_order_relaxed);
  210. }
  211. template <bool Threadsafe, bool Bounded, typename ConstIterator>
  212. tuple<node*, node*> prepare_node_list(ConstIterator begin, ConstIterator end, ConstIterator & ret)
  213. {
  214. ConstIterator it = begin;
  215. node * end_node = pool.template construct<Threadsafe, Bounded>(*it++);
  216. if (end_node == NULL) {
  217. ret = begin;
  218. return make_tuple<node*, node*>(NULL, NULL);
  219. }
  220. node * new_top_node = end_node;
  221. end_node->next = NULL;
  222. BOOST_TRY {
  223. /* link nodes */
  224. for (; it != end; ++it) {
  225. node * newnode = pool.template construct<Threadsafe, Bounded>(*it);
  226. if (newnode == NULL)
  227. break;
  228. newnode->next = new_top_node;
  229. new_top_node = newnode;
  230. }
  231. } BOOST_CATCH (...) {
  232. for (node * current_node = new_top_node; current_node != NULL;) {
  233. node * next = current_node->next;
  234. pool.template destruct<Threadsafe>(current_node);
  235. current_node = next;
  236. }
  237. BOOST_RETHROW;
  238. }
  239. BOOST_CATCH_END
  240. ret = it;
  241. return make_tuple(new_top_node, end_node);
  242. }
  243. #endif
  244. public:
  245. /** Pushes object t to the stack.
  246. *
  247. * \post object will be pushed to the stack, if internal node can be allocated
  248. * \returns true, if the push operation is successful.
  249. *
  250. * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  251. * from the OS. This may not be lock-free.
  252. * \throws if memory allocator throws
  253. * */
  254. bool push(T const & v)
  255. {
  256. return do_push<false>(v);
  257. }
  258. /** Pushes object t to the stack.
  259. *
  260. * \post object will be pushed to the stack, if internal node can be allocated
  261. * \returns true, if the push operation is successful.
  262. *
  263. * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
  264. * */
  265. bool bounded_push(T const & v)
  266. {
  267. return do_push<true>(v);
  268. }
  269. #ifndef BOOST_DOXYGEN_INVOKED
  270. private:
  271. template <bool Bounded>
  272. bool do_push(T const & v)
  273. {
  274. node * newnode = pool.template construct<true, Bounded>(v);
  275. if (newnode == 0)
  276. return false;
  277. link_nodes_atomic(newnode, newnode);
  278. return true;
  279. }
  280. template <bool Bounded, typename ConstIterator>
  281. ConstIterator do_push(ConstIterator begin, ConstIterator end)
  282. {
  283. node * new_top_node;
  284. node * end_node;
  285. ConstIterator ret;
  286. tie(new_top_node, end_node) = prepare_node_list<true, Bounded>(begin, end, ret);
  287. if (new_top_node)
  288. link_nodes_atomic(new_top_node, end_node);
  289. return ret;
  290. }
  291. public:
  292. #endif
  293. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  294. *
  295. * \return iterator to the first element, which has not been pushed
  296. *
  297. * \note Operation is applied atomically
  298. * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  299. * from the OS. This may not be lock-free.
  300. * \throws if memory allocator throws
  301. */
  302. template <typename ConstIterator>
  303. ConstIterator push(ConstIterator begin, ConstIterator end)
  304. {
  305. return do_push<false, ConstIterator>(begin, end);
  306. }
  307. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  308. *
  309. * \return iterator to the first element, which has not been pushed
  310. *
  311. * \note Operation is applied atomically
  312. * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
  313. * \throws if memory allocator throws
  314. */
  315. template <typename ConstIterator>
  316. ConstIterator bounded_push(ConstIterator begin, ConstIterator end)
  317. {
  318. return do_push<true, ConstIterator>(begin, end);
  319. }
  320. /** Pushes object t to the stack.
  321. *
  322. * \post object will be pushed to the stack, if internal node can be allocated
  323. * \returns true, if the push operation is successful.
  324. *
  325. * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  326. * from the OS. This may not be lock-free.
  327. * \throws if memory allocator throws
  328. * */
  329. bool unsynchronized_push(T const & v)
  330. {
  331. node * newnode = pool.template construct<false, false>(v);
  332. if (newnode == 0)
  333. return false;
  334. link_nodes_unsafe(newnode, newnode);
  335. return true;
  336. }
  337. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  338. *
  339. * \return iterator to the first element, which has not been pushed
  340. *
  341. * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  342. * from the OS. This may not be lock-free.
  343. * \throws if memory allocator throws
  344. */
  345. template <typename ConstIterator>
  346. ConstIterator unsynchronized_push(ConstIterator begin, ConstIterator end)
  347. {
  348. node * new_top_node;
  349. node * end_node;
  350. ConstIterator ret;
  351. tie(new_top_node, end_node) = prepare_node_list<false, false>(begin, end, ret);
  352. if (new_top_node)
  353. link_nodes_unsafe(new_top_node, end_node);
  354. return ret;
  355. }
  356. /** Pops object from stack.
  357. *
  358. * \post if pop operation is successful, object will be copied to ret.
  359. * \returns true, if the pop operation is successful, false if stack was empty.
  360. *
  361. * \note Thread-safe and non-blocking
  362. *
  363. * */
  364. bool pop(T & ret)
  365. {
  366. return pop<T>(ret);
  367. }
  368. /** Pops object from stack.
  369. *
  370. * \pre type T must be convertible to U
  371. * \post if pop operation is successful, object will be copied to ret.
  372. * \returns true, if the pop operation is successful, false if stack was empty.
  373. *
  374. * \note Thread-safe and non-blocking
  375. *
  376. * */
  377. template <typename U>
  378. bool pop(U & ret)
  379. {
  380. BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
  381. detail::consume_via_copy<U> consumer(ret);
  382. return consume_one(consumer);
  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 Not thread-safe, but non-blocking
  390. *
  391. * */
  392. bool unsynchronized_pop(T & ret)
  393. {
  394. return unsynchronized_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 Not thread-safe, but non-blocking
  403. *
  404. * */
  405. template <typename U>
  406. bool unsynchronized_pop(U & ret)
  407. {
  408. BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
  409. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  410. node * old_tos_pointer = pool.get_pointer(old_tos);
  411. if (!pool.get_pointer(old_tos))
  412. return false;
  413. node * new_tos_ptr = pool.get_pointer(old_tos_pointer->next);
  414. tagged_node_handle new_tos(pool.get_handle(new_tos_ptr), old_tos.get_next_tag());
  415. tos.store(new_tos, memory_order_relaxed);
  416. detail::copy_payload(old_tos_pointer->v, ret);
  417. pool.template destruct<false>(old_tos);
  418. return true;
  419. }
  420. /** consumes one element via a functor
  421. *
  422. * pops one element from the stack and applies the functor on this object
  423. *
  424. * \returns true, if one element was consumed
  425. *
  426. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  427. * */
  428. template <typename Functor>
  429. bool consume_one(Functor & f)
  430. {
  431. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  432. for (;;) {
  433. node * old_tos_pointer = pool.get_pointer(old_tos);
  434. if (!old_tos_pointer)
  435. return false;
  436. tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
  437. if (tos.compare_exchange_weak(old_tos, new_tos)) {
  438. f(old_tos_pointer->v);
  439. pool.template destruct<true>(old_tos);
  440. return true;
  441. }
  442. }
  443. }
  444. /// \copydoc boost::lockfree::stack::consume_one(Functor & rhs)
  445. template <typename Functor>
  446. bool consume_one(Functor const & f)
  447. {
  448. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  449. for (;;) {
  450. node * old_tos_pointer = pool.get_pointer(old_tos);
  451. if (!old_tos_pointer)
  452. return false;
  453. tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
  454. if (tos.compare_exchange_weak(old_tos, new_tos)) {
  455. f(old_tos_pointer->v);
  456. pool.template destruct<true>(old_tos);
  457. return true;
  458. }
  459. }
  460. }
  461. /** consumes all elements via a functor
  462. *
  463. * sequentially pops all elements from the stack and applies the functor on each object
  464. *
  465. * \returns number of elements that are consumed
  466. *
  467. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  468. * */
  469. template <typename Functor>
  470. size_t consume_all(Functor & f)
  471. {
  472. size_t element_count = 0;
  473. while (consume_one(f))
  474. element_count += 1;
  475. return element_count;
  476. }
  477. /// \copydoc boost::lockfree::stack::consume_all(Functor & rhs)
  478. template <typename Functor>
  479. size_t consume_all(Functor const & f)
  480. {
  481. size_t element_count = 0;
  482. while (consume_one(f))
  483. element_count += 1;
  484. return element_count;
  485. }
  486. /** consumes all elements via a functor
  487. *
  488. * atomically pops all elements from the stack and applies the functor on each object
  489. *
  490. * \returns number of elements that are consumed
  491. *
  492. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  493. * */
  494. template <typename Functor>
  495. size_t consume_all_atomic(Functor & f)
  496. {
  497. size_t element_count = 0;
  498. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  499. for (;;) {
  500. node * old_tos_pointer = pool.get_pointer(old_tos);
  501. if (!old_tos_pointer)
  502. return 0;
  503. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  504. if (tos.compare_exchange_weak(old_tos, new_tos))
  505. break;
  506. }
  507. tagged_node_handle nodes_to_consume = old_tos;
  508. for(;;) {
  509. node * node_pointer = pool.get_pointer(nodes_to_consume);
  510. f(node_pointer->v);
  511. element_count += 1;
  512. node * next_node = pool.get_pointer(node_pointer->next);
  513. if (!next_node) {
  514. pool.template destruct<true>(nodes_to_consume);
  515. break;
  516. }
  517. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  518. pool.template destruct<true>(nodes_to_consume);
  519. nodes_to_consume = next;
  520. }
  521. return element_count;
  522. }
  523. /// \copydoc boost::lockfree::stack::consume_all_atomic(Functor & rhs)
  524. template <typename Functor>
  525. size_t consume_all_atomic(Functor const & f)
  526. {
  527. size_t element_count = 0;
  528. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  529. for (;;) {
  530. node * old_tos_pointer = pool.get_pointer(old_tos);
  531. if (!old_tos_pointer)
  532. return 0;
  533. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  534. if (tos.compare_exchange_weak(old_tos, new_tos))
  535. break;
  536. }
  537. tagged_node_handle nodes_to_consume = old_tos;
  538. for(;;) {
  539. node * node_pointer = pool.get_pointer(nodes_to_consume);
  540. f(node_pointer->v);
  541. element_count += 1;
  542. node * next_node = pool.get_pointer(node_pointer->next);
  543. if (!next_node) {
  544. pool.template destruct<true>(nodes_to_consume);
  545. break;
  546. }
  547. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  548. pool.template destruct<true>(nodes_to_consume);
  549. nodes_to_consume = next;
  550. }
  551. return element_count;
  552. }
  553. /** consumes all elements via a functor
  554. *
  555. * atomically pops all elements from the stack and applies the functor on each object in reversed order
  556. *
  557. * \returns number of elements that are consumed
  558. *
  559. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  560. * */
  561. template <typename Functor>
  562. size_t consume_all_atomic_reversed(Functor & f)
  563. {
  564. size_t element_count = 0;
  565. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  566. for (;;) {
  567. node * old_tos_pointer = pool.get_pointer(old_tos);
  568. if (!old_tos_pointer)
  569. return 0;
  570. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  571. if (tos.compare_exchange_weak(old_tos, new_tos))
  572. break;
  573. }
  574. tagged_node_handle nodes_to_consume = old_tos;
  575. node * last_node_pointer = NULL;
  576. tagged_node_handle nodes_in_reversed_order;
  577. for(;;) {
  578. node * node_pointer = pool.get_pointer(nodes_to_consume);
  579. node * next_node = pool.get_pointer(node_pointer->next);
  580. node_pointer->next = pool.get_handle(last_node_pointer);
  581. last_node_pointer = node_pointer;
  582. if (!next_node) {
  583. nodes_in_reversed_order = nodes_to_consume;
  584. break;
  585. }
  586. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  587. nodes_to_consume = next;
  588. }
  589. for(;;) {
  590. node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
  591. f(node_pointer->v);
  592. element_count += 1;
  593. node * next_node = pool.get_pointer(node_pointer->next);
  594. if (!next_node) {
  595. pool.template destruct<true>(nodes_in_reversed_order);
  596. break;
  597. }
  598. tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
  599. pool.template destruct<true>(nodes_in_reversed_order);
  600. nodes_in_reversed_order = next;
  601. }
  602. return element_count;
  603. }
  604. /// \copydoc boost::lockfree::stack::consume_all_atomic_reversed(Functor & rhs)
  605. template <typename Functor>
  606. size_t consume_all_atomic_reversed(Functor const & f)
  607. {
  608. size_t element_count = 0;
  609. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  610. for (;;) {
  611. node * old_tos_pointer = pool.get_pointer(old_tos);
  612. if (!old_tos_pointer)
  613. return 0;
  614. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  615. if (tos.compare_exchange_weak(old_tos, new_tos))
  616. break;
  617. }
  618. tagged_node_handle nodes_to_consume = old_tos;
  619. node * last_node_pointer = NULL;
  620. tagged_node_handle nodes_in_reversed_order;
  621. for(;;) {
  622. node * node_pointer = pool.get_pointer(nodes_to_consume);
  623. node * next_node = pool.get_pointer(node_pointer->next);
  624. node_pointer->next = pool.get_handle(last_node_pointer);
  625. last_node_pointer = node_pointer;
  626. if (!next_node) {
  627. nodes_in_reversed_order = nodes_to_consume;
  628. break;
  629. }
  630. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  631. nodes_to_consume = next;
  632. }
  633. for(;;) {
  634. node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
  635. f(node_pointer->v);
  636. element_count += 1;
  637. node * next_node = pool.get_pointer(node_pointer->next);
  638. if (!next_node) {
  639. pool.template destruct<true>(nodes_in_reversed_order);
  640. break;
  641. }
  642. tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
  643. pool.template destruct<true>(nodes_in_reversed_order);
  644. nodes_in_reversed_order = next;
  645. }
  646. return element_count;
  647. }
  648. /**
  649. * \return true, if stack is empty.
  650. *
  651. * \note It only guarantees that at some point during the execution of the function the stack has been empty.
  652. * It is rarely practical to use this value in program logic, because the stack can be modified by other threads.
  653. * */
  654. bool empty(void) const
  655. {
  656. return pool.get_pointer(tos.load()) == NULL;
  657. }
  658. private:
  659. #ifndef BOOST_DOXYGEN_INVOKED
  660. detail::atomic<tagged_node_handle> tos;
  661. static const int padding_size = BOOST_LOCKFREE_CACHELINE_BYTES - sizeof(tagged_node_handle);
  662. char padding[padding_size];
  663. pool_t pool;
  664. #endif
  665. };
  666. } /* namespace lockfree */
  667. } /* namespace boost */
  668. #endif /* BOOST_LOCKFREE_STACK_HPP_INCLUDED */