Chris@16: // Chris@16: // detail/strand_service.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP Chris@16: #define BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: // Default service implementation for a strand. Chris@16: class strand_service Chris@16: : public boost::asio::detail::service_base Chris@16: { Chris@16: private: Chris@16: // Helper class to re-post the strand on exit. Chris@16: struct on_do_complete_exit; Chris@16: Chris@16: // Helper class to re-post the strand on exit. Chris@16: struct on_dispatch_exit; Chris@16: Chris@16: public: Chris@16: Chris@16: // The underlying implementation of a strand. Chris@16: class strand_impl Chris@16: : public operation Chris@16: { Chris@16: public: Chris@16: strand_impl(); Chris@16: Chris@16: private: Chris@16: // Only this service will have access to the internal values. Chris@16: friend class strand_service; Chris@16: friend struct on_do_complete_exit; Chris@16: friend struct on_dispatch_exit; Chris@16: Chris@16: // Mutex to protect access to internal data. Chris@16: boost::asio::detail::mutex mutex_; Chris@16: Chris@16: // Indicates whether the strand is currently "locked" by a handler. This Chris@16: // means that there is a handler upcall in progress, or that the strand Chris@16: // itself has been scheduled in order to invoke some pending handlers. Chris@16: bool locked_; Chris@16: Chris@16: // The handlers that are waiting on the strand but should not be run until Chris@16: // after the next time the strand is scheduled. This queue must only be Chris@16: // modified while the mutex is locked. Chris@16: op_queue waiting_queue_; Chris@16: Chris@16: // The handlers that are ready to be run. Logically speaking, these are the Chris@16: // handlers that hold the strand's lock. The ready queue is only modified Chris@16: // from within the strand and so may be accessed without locking the mutex. Chris@16: op_queue ready_queue_; Chris@16: }; Chris@16: Chris@16: typedef strand_impl* implementation_type; Chris@16: Chris@16: // Construct a new strand service for the specified io_service. Chris@16: BOOST_ASIO_DECL explicit strand_service(boost::asio::io_service& io_service); Chris@16: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: BOOST_ASIO_DECL void shutdown_service(); Chris@16: Chris@16: // Construct a new strand implementation. Chris@16: BOOST_ASIO_DECL void construct(implementation_type& impl); Chris@16: Chris@16: // Request the io_service to invoke the given handler. Chris@16: template Chris@16: void dispatch(implementation_type& impl, Handler& handler); Chris@16: Chris@16: // Request the io_service to invoke the given handler and return immediately. Chris@16: template Chris@16: void post(implementation_type& impl, Handler& handler); Chris@16: Chris@16: // Determine whether the strand is running in the current thread. Chris@16: BOOST_ASIO_DECL bool running_in_this_thread( Chris@16: const implementation_type& impl) const; Chris@16: Chris@16: private: Chris@16: // Helper function to dispatch a handler. Returns true if the handler should Chris@16: // be dispatched immediately. Chris@16: BOOST_ASIO_DECL bool do_dispatch(implementation_type& impl, operation* op); Chris@16: Chris@16: // Helper fiunction to post a handler. Chris@16: BOOST_ASIO_DECL void do_post(implementation_type& impl, Chris@16: operation* op, bool is_continuation); Chris@16: Chris@16: BOOST_ASIO_DECL static void do_complete(io_service_impl* owner, Chris@16: operation* base, const boost::system::error_code& ec, Chris@16: std::size_t bytes_transferred); Chris@16: Chris@16: // The io_service implementation used to post completions. Chris@16: io_service_impl& io_service_; Chris@16: Chris@16: // Mutex to protect access to the array of implementations. Chris@16: boost::asio::detail::mutex mutex_; Chris@16: Chris@16: // Number of implementations shared between all strand objects. Chris@16: #if defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS) Chris@16: enum { num_implementations = BOOST_ASIO_STRAND_IMPLEMENTATIONS }; Chris@16: #else // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS) Chris@16: enum { num_implementations = 193 }; Chris@16: #endif // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS) Chris@16: Chris@16: // Pool of implementations. Chris@16: scoped_ptr implementations_[num_implementations]; Chris@16: Chris@16: // Extra value used when hashing to prevent recycled memory locations from Chris@16: // getting the same strand implementation. Chris@16: std::size_t salt_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP