stack.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. detail::consume_noop consume_functor;
  213. (void)consume_all(consume_functor);
  214. }
  215. private:
  216. #ifndef BOOST_DOXYGEN_INVOKED
  217. void initialize(void)
  218. {
  219. tos.store(tagged_node_handle(pool.null_handle(), 0));
  220. }
  221. void link_nodes_atomic(node * new_top_node, node * end_node)
  222. {
  223. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  224. for (;;) {
  225. tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
  226. end_node->next = pool.get_handle(old_tos);
  227. if (tos.compare_exchange_weak(old_tos, new_tos))
  228. break;
  229. }
  230. }
  231. void link_nodes_unsafe(node * new_top_node, node * end_node)
  232. {
  233. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  234. tagged_node_handle new_tos (pool.get_handle(new_top_node), old_tos.get_tag());
  235. end_node->next = pool.get_handle(old_tos);
  236. tos.store(new_tos, memory_order_relaxed);
  237. }
  238. template <bool Threadsafe, bool Bounded, typename ConstIterator>
  239. tuple<node*, node*> prepare_node_list(ConstIterator begin, ConstIterator end, ConstIterator & ret)
  240. {
  241. ConstIterator it = begin;
  242. node * end_node = pool.template construct<Threadsafe, Bounded>(*it++);
  243. if (end_node == NULL) {
  244. ret = begin;
  245. return make_tuple<node*, node*>(NULL, NULL);
  246. }
  247. node * new_top_node = end_node;
  248. end_node->next = NULL;
  249. BOOST_TRY {
  250. /* link nodes */
  251. for (; it != end; ++it) {
  252. node * newnode = pool.template construct<Threadsafe, Bounded>(*it);
  253. if (newnode == NULL)
  254. break;
  255. newnode->next = new_top_node;
  256. new_top_node = newnode;
  257. }
  258. } BOOST_CATCH (...) {
  259. for (node * current_node = new_top_node; current_node != NULL;) {
  260. node * next = current_node->next;
  261. pool.template destruct<Threadsafe>(current_node);
  262. current_node = next;
  263. }
  264. BOOST_RETHROW;
  265. }
  266. BOOST_CATCH_END
  267. ret = it;
  268. return make_tuple(new_top_node, end_node);
  269. }
  270. #endif
  271. public:
  272. /** Pushes object t to the stack.
  273. *
  274. * \post object will be pushed to the stack, if internal node can be allocated
  275. * \returns true, if the push operation is successful.
  276. *
  277. * \note Thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  278. * from the OS. This may not be lock-free.
  279. * \throws if memory allocator throws
  280. * */
  281. bool push(T const & v)
  282. {
  283. return do_push<false>(v);
  284. }
  285. /** Pushes object t to the stack.
  286. *
  287. * \post object will be pushed to the stack, if internal node can be allocated
  288. * \returns true, if the push operation is successful.
  289. *
  290. * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
  291. * */
  292. bool bounded_push(T const & v)
  293. {
  294. return do_push<true>(v);
  295. }
  296. #ifndef BOOST_DOXYGEN_INVOKED
  297. private:
  298. template <bool Bounded>
  299. bool do_push(T const & v)
  300. {
  301. node * newnode = pool.template construct<true, Bounded>(v);
  302. if (newnode == 0)
  303. return false;
  304. link_nodes_atomic(newnode, newnode);
  305. return true;
  306. }
  307. template <bool Bounded, typename ConstIterator>
  308. ConstIterator do_push(ConstIterator begin, ConstIterator end)
  309. {
  310. node * new_top_node;
  311. node * end_node;
  312. ConstIterator ret;
  313. tie(new_top_node, end_node) = prepare_node_list<true, Bounded>(begin, end, ret);
  314. if (new_top_node)
  315. link_nodes_atomic(new_top_node, end_node);
  316. return ret;
  317. }
  318. public:
  319. #endif
  320. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  321. *
  322. * \return iterator to the first element, which has not been pushed
  323. *
  324. * \note Operation is applied atomically
  325. * \note 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. template <typename ConstIterator>
  330. ConstIterator push(ConstIterator begin, ConstIterator end)
  331. {
  332. return do_push<false, ConstIterator>(begin, end);
  333. }
  334. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  335. *
  336. * \return iterator to the first element, which has not been pushed
  337. *
  338. * \note Operation is applied atomically
  339. * \note Thread-safe and non-blocking. If internal memory pool is exhausted, the push operation will fail
  340. * \throws if memory allocator throws
  341. */
  342. template <typename ConstIterator>
  343. ConstIterator bounded_push(ConstIterator begin, ConstIterator end)
  344. {
  345. return do_push<true, ConstIterator>(begin, end);
  346. }
  347. /** Pushes object t to the stack.
  348. *
  349. * \post object will be pushed to the stack, if internal node can be allocated
  350. * \returns true, if the push operation is successful.
  351. *
  352. * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  353. * from the OS. This may not be lock-free.
  354. * \throws if memory allocator throws
  355. * */
  356. bool unsynchronized_push(T const & v)
  357. {
  358. node * newnode = pool.template construct<false, false>(v);
  359. if (newnode == 0)
  360. return false;
  361. link_nodes_unsafe(newnode, newnode);
  362. return true;
  363. }
  364. /** Pushes as many objects from the range [begin, end) as freelist node can be allocated.
  365. *
  366. * \return iterator to the first element, which has not been pushed
  367. *
  368. * \note Not thread-safe. If internal memory pool is exhausted and the memory pool is not fixed-sized, a new node will be allocated
  369. * from the OS. This may not be lock-free.
  370. * \throws if memory allocator throws
  371. */
  372. template <typename ConstIterator>
  373. ConstIterator unsynchronized_push(ConstIterator begin, ConstIterator end)
  374. {
  375. node * new_top_node;
  376. node * end_node;
  377. ConstIterator ret;
  378. tie(new_top_node, end_node) = prepare_node_list<false, false>(begin, end, ret);
  379. if (new_top_node)
  380. link_nodes_unsafe(new_top_node, end_node);
  381. return ret;
  382. }
  383. /** Pops object from stack.
  384. *
  385. * \post if pop operation is successful, object will be copied to ret.
  386. * \returns true, if the pop operation is successful, false if stack was empty.
  387. *
  388. * \note Thread-safe and non-blocking
  389. *
  390. * */
  391. bool pop(T & ret)
  392. {
  393. return pop<T>(ret);
  394. }
  395. /** Pops object from stack.
  396. *
  397. * \pre type T must be convertible to U
  398. * \post if pop operation is successful, object will be copied to ret.
  399. * \returns true, if the pop operation is successful, false if stack was empty.
  400. *
  401. * \note Thread-safe and non-blocking
  402. *
  403. * */
  404. template <typename U>
  405. bool pop(U & ret)
  406. {
  407. BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
  408. detail::consume_via_copy<U> consumer(ret);
  409. return consume_one(consumer);
  410. }
  411. /** Pops object from stack.
  412. *
  413. * \post if pop operation is successful, object will be copied to ret.
  414. * \returns true, if the pop operation is successful, false if stack was empty.
  415. *
  416. * \note Not thread-safe, but non-blocking
  417. *
  418. * */
  419. bool unsynchronized_pop(T & ret)
  420. {
  421. return unsynchronized_pop<T>(ret);
  422. }
  423. /** Pops object from stack.
  424. *
  425. * \pre type T must be convertible to U
  426. * \post if pop operation is successful, object will be copied to ret.
  427. * \returns true, if the pop operation is successful, false if stack was empty.
  428. *
  429. * \note Not thread-safe, but non-blocking
  430. *
  431. * */
  432. template <typename U>
  433. bool unsynchronized_pop(U & ret)
  434. {
  435. BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
  436. tagged_node_handle old_tos = tos.load(detail::memory_order_relaxed);
  437. node * old_tos_pointer = pool.get_pointer(old_tos);
  438. if (!pool.get_pointer(old_tos))
  439. return false;
  440. node * new_tos_ptr = pool.get_pointer(old_tos_pointer->next);
  441. tagged_node_handle new_tos(pool.get_handle(new_tos_ptr), old_tos.get_next_tag());
  442. tos.store(new_tos, memory_order_relaxed);
  443. detail::copy_payload(old_tos_pointer->v, ret);
  444. pool.template destruct<false>(old_tos);
  445. return true;
  446. }
  447. /** consumes one element via a functor
  448. *
  449. * pops one element from the stack and applies the functor on this object
  450. *
  451. * \returns true, if one element was consumed
  452. *
  453. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  454. * */
  455. template <typename Functor>
  456. bool consume_one(Functor & f)
  457. {
  458. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  459. for (;;) {
  460. node * old_tos_pointer = pool.get_pointer(old_tos);
  461. if (!old_tos_pointer)
  462. return false;
  463. tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
  464. if (tos.compare_exchange_weak(old_tos, new_tos)) {
  465. f(old_tos_pointer->v);
  466. pool.template destruct<true>(old_tos);
  467. return true;
  468. }
  469. }
  470. }
  471. /// \copydoc boost::lockfree::stack::consume_one(Functor & rhs)
  472. template <typename Functor>
  473. bool consume_one(Functor const & f)
  474. {
  475. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  476. for (;;) {
  477. node * old_tos_pointer = pool.get_pointer(old_tos);
  478. if (!old_tos_pointer)
  479. return false;
  480. tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
  481. if (tos.compare_exchange_weak(old_tos, new_tos)) {
  482. f(old_tos_pointer->v);
  483. pool.template destruct<true>(old_tos);
  484. return true;
  485. }
  486. }
  487. }
  488. /** consumes all elements via a functor
  489. *
  490. * sequentially pops all elements from the stack and applies the functor on each object
  491. *
  492. * \returns number of elements that are consumed
  493. *
  494. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  495. * */
  496. template <typename Functor>
  497. size_t consume_all(Functor & f)
  498. {
  499. size_t element_count = 0;
  500. while (consume_one(f))
  501. element_count += 1;
  502. return element_count;
  503. }
  504. /// \copydoc boost::lockfree::stack::consume_all(Functor & rhs)
  505. template <typename Functor>
  506. size_t consume_all(Functor const & f)
  507. {
  508. size_t element_count = 0;
  509. while (consume_one(f))
  510. element_count += 1;
  511. return element_count;
  512. }
  513. /** consumes all elements via a functor
  514. *
  515. * atomically pops all elements from the stack and applies the functor on each object
  516. *
  517. * \returns number of elements that are consumed
  518. *
  519. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  520. * */
  521. template <typename Functor>
  522. size_t consume_all_atomic(Functor & f)
  523. {
  524. size_t element_count = 0;
  525. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  526. for (;;) {
  527. node * old_tos_pointer = pool.get_pointer(old_tos);
  528. if (!old_tos_pointer)
  529. return 0;
  530. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  531. if (tos.compare_exchange_weak(old_tos, new_tos))
  532. break;
  533. }
  534. tagged_node_handle nodes_to_consume = old_tos;
  535. for(;;) {
  536. node * node_pointer = pool.get_pointer(nodes_to_consume);
  537. f(node_pointer->v);
  538. element_count += 1;
  539. node * next_node = pool.get_pointer(node_pointer->next);
  540. if (!next_node) {
  541. pool.template destruct<true>(nodes_to_consume);
  542. break;
  543. }
  544. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  545. pool.template destruct<true>(nodes_to_consume);
  546. nodes_to_consume = next;
  547. }
  548. return element_count;
  549. }
  550. /// \copydoc boost::lockfree::stack::consume_all_atomic(Functor & rhs)
  551. template <typename Functor>
  552. size_t consume_all_atomic(Functor const & f)
  553. {
  554. size_t element_count = 0;
  555. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  556. for (;;) {
  557. node * old_tos_pointer = pool.get_pointer(old_tos);
  558. if (!old_tos_pointer)
  559. return 0;
  560. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  561. if (tos.compare_exchange_weak(old_tos, new_tos))
  562. break;
  563. }
  564. tagged_node_handle nodes_to_consume = old_tos;
  565. for(;;) {
  566. node * node_pointer = pool.get_pointer(nodes_to_consume);
  567. f(node_pointer->v);
  568. element_count += 1;
  569. node * next_node = pool.get_pointer(node_pointer->next);
  570. if (!next_node) {
  571. pool.template destruct<true>(nodes_to_consume);
  572. break;
  573. }
  574. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  575. pool.template destruct<true>(nodes_to_consume);
  576. nodes_to_consume = next;
  577. }
  578. return element_count;
  579. }
  580. /** consumes all elements via a functor
  581. *
  582. * atomically pops all elements from the stack and applies the functor on each object in reversed order
  583. *
  584. * \returns number of elements that are consumed
  585. *
  586. * \note Thread-safe and non-blocking, if functor is thread-safe and non-blocking
  587. * */
  588. template <typename Functor>
  589. size_t consume_all_atomic_reversed(Functor & f)
  590. {
  591. size_t element_count = 0;
  592. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  593. for (;;) {
  594. node * old_tos_pointer = pool.get_pointer(old_tos);
  595. if (!old_tos_pointer)
  596. return 0;
  597. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  598. if (tos.compare_exchange_weak(old_tos, new_tos))
  599. break;
  600. }
  601. tagged_node_handle nodes_to_consume = old_tos;
  602. node * last_node_pointer = NULL;
  603. tagged_node_handle nodes_in_reversed_order;
  604. for(;;) {
  605. node * node_pointer = pool.get_pointer(nodes_to_consume);
  606. node * next_node = pool.get_pointer(node_pointer->next);
  607. node_pointer->next = pool.get_handle(last_node_pointer);
  608. last_node_pointer = node_pointer;
  609. if (!next_node) {
  610. nodes_in_reversed_order = nodes_to_consume;
  611. break;
  612. }
  613. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  614. nodes_to_consume = next;
  615. }
  616. for(;;) {
  617. node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
  618. f(node_pointer->v);
  619. element_count += 1;
  620. node * next_node = pool.get_pointer(node_pointer->next);
  621. if (!next_node) {
  622. pool.template destruct<true>(nodes_in_reversed_order);
  623. break;
  624. }
  625. tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
  626. pool.template destruct<true>(nodes_in_reversed_order);
  627. nodes_in_reversed_order = next;
  628. }
  629. return element_count;
  630. }
  631. /// \copydoc boost::lockfree::stack::consume_all_atomic_reversed(Functor & rhs)
  632. template <typename Functor>
  633. size_t consume_all_atomic_reversed(Functor const & f)
  634. {
  635. size_t element_count = 0;
  636. tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
  637. for (;;) {
  638. node * old_tos_pointer = pool.get_pointer(old_tos);
  639. if (!old_tos_pointer)
  640. return 0;
  641. tagged_node_handle new_tos(pool.null_handle(), old_tos.get_next_tag());
  642. if (tos.compare_exchange_weak(old_tos, new_tos))
  643. break;
  644. }
  645. tagged_node_handle nodes_to_consume = old_tos;
  646. node * last_node_pointer = NULL;
  647. tagged_node_handle nodes_in_reversed_order;
  648. for(;;) {
  649. node * node_pointer = pool.get_pointer(nodes_to_consume);
  650. node * next_node = pool.get_pointer(node_pointer->next);
  651. node_pointer->next = pool.get_handle(last_node_pointer);
  652. last_node_pointer = node_pointer;
  653. if (!next_node) {
  654. nodes_in_reversed_order = nodes_to_consume;
  655. break;
  656. }
  657. tagged_node_handle next(pool.get_handle(next_node), nodes_to_consume.get_next_tag());
  658. nodes_to_consume = next;
  659. }
  660. for(;;) {
  661. node * node_pointer = pool.get_pointer(nodes_in_reversed_order);
  662. f(node_pointer->v);
  663. element_count += 1;
  664. node * next_node = pool.get_pointer(node_pointer->next);
  665. if (!next_node) {
  666. pool.template destruct<true>(nodes_in_reversed_order);
  667. break;
  668. }
  669. tagged_node_handle next(pool.get_handle(next_node), nodes_in_reversed_order.get_next_tag());
  670. pool.template destruct<true>(nodes_in_reversed_order);
  671. nodes_in_reversed_order = next;
  672. }
  673. return element_count;
  674. }
  675. /**
  676. * \return true, if stack is empty.
  677. *
  678. * \note It only guarantees that at some point during the execution of the function the stack has been empty.
  679. * It is rarely practical to use this value in program logic, because the stack can be modified by other threads.
  680. * */
  681. bool empty(void) const
  682. {
  683. return pool.get_pointer(tos.load()) == NULL;
  684. }
  685. private:
  686. #ifndef BOOST_DOXYGEN_INVOKED
  687. detail::atomic<tagged_node_handle> tos;
  688. static const int padding_size = BOOST_LOCKFREE_CACHELINE_BYTES - sizeof(tagged_node_handle);
  689. char padding[padding_size];
  690. pool_t pool;
  691. #endif
  692. };
  693. } /* namespace lockfree */
  694. } /* namespace boost */
  695. #endif /* BOOST_LOCKFREE_STACK_HPP_INCLUDED */