Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85013 - in trunk/boost/lockfree: . detail
From: tim_at_[hidden]
Date: 2013-07-12 13:48:35


Author: timblechmann
Date: 2013-07-12 13:48:35 EDT (Fri, 12 Jul 2013)
New Revision: 85013
URL: http://svn.boost.org/trac/boost/changeset/85013

Log:
lockfree: stack - avoid temporary copy when consuming via functor

Signed-off-by: Tim Blechmann <tim_at_[hidden]>

Text files modified:
   trunk/boost/lockfree/detail/copy_payload.hpp | 17 ++++++++++++
   trunk/boost/lockfree/stack.hpp | 54 ++++++++++++++++++++++-----------------
   2 files changed, 47 insertions(+), 24 deletions(-)

Modified: trunk/boost/lockfree/detail/copy_payload.hpp
==============================================================================
--- trunk/boost/lockfree/detail/copy_payload.hpp Fri Jul 12 13:48:06 2013 (r85012)
+++ trunk/boost/lockfree/detail/copy_payload.hpp 2013-07-12 13:48:35 EDT (Fri, 12 Jul 2013) (r85013)
@@ -44,6 +44,23 @@
     copy_type::copy(t, u);
 }
 
+template <typename T>
+struct consume_via_copy
+{
+ consume_via_copy(T & out):
+ out(out)
+ {}
+
+ template <typename U>
+ void operator()(U & element)
+ {
+ copy_payload(element, out);
+ }
+
+ T & out;
+};
+
+
 }}}
 
 #endif /* BOOST_LOCKFREE_DETAIL_COPY_PAYLOAD_HPP_INCLUDED */

Modified: trunk/boost/lockfree/stack.hpp
==============================================================================
--- trunk/boost/lockfree/stack.hpp Fri Jul 12 13:48:06 2013 (r85012)
+++ trunk/boost/lockfree/stack.hpp 2013-07-12 13:48:35 EDT (Fri, 12 Jul 2013) (r85013)
@@ -435,21 +435,9 @@
     bool pop(U & ret)
     {
         BOOST_STATIC_ASSERT((boost::is_convertible<T, U>::value));
- tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
-
- for (;;) {
- node * old_tos_pointer = pool.get_pointer(old_tos);
- if (!old_tos_pointer)
- return false;
-
- tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
+ detail::consume_via_copy<U> consumer(ret);
 
- if (tos.compare_exchange_weak(old_tos, new_tos)) {
- detail::copy_payload(old_tos_pointer->v, ret);
- pool.template destruct<true>(old_tos);
- return true;
- }
- }
+ return consume_one(consumer);
     }
 
 
@@ -505,24 +493,42 @@
     template <typename Functor>
     bool consume_one(Functor & f)
     {
- T element;
- bool success = pop(element);
- if (success)
- f(element);
+ tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
+
+ for (;;) {
+ node * old_tos_pointer = pool.get_pointer(old_tos);
+ if (!old_tos_pointer)
+ return false;
 
- return success;
+ tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
+
+ if (tos.compare_exchange_weak(old_tos, new_tos)) {
+ f(old_tos_pointer->v);
+ pool.template destruct<true>(old_tos);
+ return true;
+ }
+ }
     }
 
     /// \copydoc boost::lockfree::stack::consume_one(Functor & rhs)
     template <typename Functor>
     bool consume_one(Functor const & f)
     {
- T element;
- bool success = pop(element);
- if (success)
- f(element);
+ tagged_node_handle old_tos = tos.load(detail::memory_order_consume);
 
- return success;
+ for (;;) {
+ node * old_tos_pointer = pool.get_pointer(old_tos);
+ if (!old_tos_pointer)
+ return false;
+
+ tagged_node_handle new_tos(old_tos_pointer->next, old_tos.get_next_tag());
+
+ if (tos.compare_exchange_weak(old_tos, new_tos)) {
+ f(old_tos_pointer->v);
+ pool.template destruct<true>(old_tos);
+ return true;
+ }
+ }
     }
 
     /** consumes all elements via a functor


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk