Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71738 - in trunk/boost/asio/detail: . impl
From: chris_at_[hidden]
Date: 2011-05-05 09:22:31


Author: chris_kohlhoff
Date: 2011-05-05 09:22:29 EDT (Thu, 05 May 2011)
New Revision: 71738
URL: http://svn.boost.org/trac/boost/changeset/71738

Log:
Fix move-related bug in strand implementation. Refs #5515.

Text files modified:
   trunk/boost/asio/detail/impl/strand_service.hpp | 44 ++++++++-------------------------------
   trunk/boost/asio/detail/impl/strand_service.ipp | 39 +++++++++++++++++++++++++++++++++++
   trunk/boost/asio/detail/strand_service.hpp | 7 ++++++
   3 files changed, 55 insertions(+), 35 deletions(-)

Modified: trunk/boost/asio/detail/impl/strand_service.hpp
==============================================================================
--- trunk/boost/asio/detail/impl/strand_service.hpp (original)
+++ trunk/boost/asio/detail/impl/strand_service.hpp 2011-05-05 09:22:29 EDT (Thu, 05 May 2011)
@@ -73,19 +73,14 @@
       sizeof(op), handler), 0 };
   p.p = new (p.v) op(handler);
 
- // If we are running inside the io_service, and no other handler is queued
- // or running, then the handler can run immediately.
- bool can_dispatch = call_stack<io_service_impl>::contains(&io_service_);
- impl->mutex_.lock();
- bool first = (++impl->count_ == 1);
- if (can_dispatch && first)
- {
- // Immediate invocation is allowed.
- impl->mutex_.unlock();
+ BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "dispatch"));
 
- // Memory must be releaesed before any upcall is made.
- p.reset();
+ bool dispatch_immediately = do_dispatch(impl, p.p);
+ operation* o = p.p;
+ p.v = p.p = 0;
 
+ if (dispatch_immediately)
+ {
     // Indicate that this strand is executing on the current thread.
     call_stack<strand_impl>::context ctx(impl);
 
@@ -93,22 +88,9 @@
     on_dispatch_exit on_exit = { &io_service_, impl };
     (void)on_exit;
 
- boost::asio::detail::fenced_block b;
- boost_asio_handler_invoke_helpers::invoke(handler, handler);
- return;
+ completion_handler<Handler>::do_complete(
+ &io_service_, o, boost::system::error_code(), 0);
   }
-
- BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "dispatch"));
-
- // Immediate invocation is not allowed, so enqueue for later.
- impl->queue_.push(p.p);
- impl->mutex_.unlock();
- p.v = p.p = 0;
-
- // The first handler to be enqueued is responsible for scheduling the
- // strand.
- if (first)
- io_service_.post_immediate_completion(impl);
 }
 
 // Request the io_service to invoke the given handler and return immediately.
@@ -125,16 +107,8 @@
 
   BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "post"));
 
- // Add the handler to the queue.
- impl->mutex_.lock();
- bool first = (++impl->count_ == 1);
- impl->queue_.push(p.p);
- impl->mutex_.unlock();
+ do_post(impl, p.p);
   p.v = p.p = 0;
-
- // The first handler to be enqueue is responsible for scheduling the strand.
- if (first)
- io_service_.post_immediate_completion(impl);
 }
 
 } // namespace detail

Modified: trunk/boost/asio/detail/impl/strand_service.ipp
==============================================================================
--- trunk/boost/asio/detail/impl/strand_service.ipp (original)
+++ trunk/boost/asio/detail/impl/strand_service.ipp 2011-05-05 09:22:29 EDT (Thu, 05 May 2011)
@@ -75,6 +75,45 @@
   impl = implementations_[index].get();
 }
 
+bool strand_service::do_dispatch(implementation_type& impl, operation* op)
+{
+ // If we are running inside the io_service, and no other handler is queued
+ // or running, then the handler can run immediately.
+ bool can_dispatch = call_stack<io_service_impl>::contains(&io_service_);
+ impl->mutex_.lock();
+ bool first = (++impl->count_ == 1);
+ if (can_dispatch && first)
+ {
+ // Immediate invocation is allowed.
+ impl->mutex_.unlock();
+ return true;
+ }
+
+ // Immediate invocation is not allowed, so enqueue for later.
+ impl->queue_.push(op);
+ impl->mutex_.unlock();
+
+ // The first handler to be enqueued is responsible for scheduling the
+ // strand.
+ if (first)
+ io_service_.post_immediate_completion(impl);
+
+ return false;
+}
+
+void strand_service::do_post(implementation_type& impl, operation* op)
+{
+ // Add the handler to the queue.
+ impl->mutex_.lock();
+ bool first = (++impl->count_ == 1);
+ impl->queue_.push(op);
+ impl->mutex_.unlock();
+
+ // The first handler to be enqueue is responsible for scheduling the strand.
+ if (first)
+ io_service_.post_immediate_completion(impl);
+}
+
 void strand_service::do_complete(io_service_impl* owner, operation* base,
     boost::system::error_code /*ec*/, std::size_t /*bytes_transferred*/)
 {

Modified: trunk/boost/asio/detail/strand_service.hpp
==============================================================================
--- trunk/boost/asio/detail/strand_service.hpp (original)
+++ trunk/boost/asio/detail/strand_service.hpp 2011-05-05 09:22:29 EDT (Thu, 05 May 2011)
@@ -87,6 +87,13 @@
   void post(implementation_type& impl, Handler handler);
 
 private:
+ // Helper function to dispatch a handler. Returns true if the handler should
+ // be dispatched immediately.
+ BOOST_ASIO_DECL bool do_dispatch(implementation_type& impl, operation* op);
+
+ // Helper fiunction to post a handler.
+ BOOST_ASIO_DECL void do_post(implementation_type& impl, operation* op);
+
   BOOST_ASIO_DECL static void do_complete(io_service_impl* owner,
       operation* base, boost::system::error_code ec,
       std::size_t bytes_transferred);


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