Chris@16
|
1 //
|
Chris@16
|
2 // detail/strand_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <boost/asio/io_service.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/mutex.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/op_queue.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/operation.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/scoped_ptr.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28 namespace asio {
|
Chris@16
|
29 namespace detail {
|
Chris@16
|
30
|
Chris@16
|
31 // Default service implementation for a strand.
|
Chris@16
|
32 class strand_service
|
Chris@16
|
33 : public boost::asio::detail::service_base<strand_service>
|
Chris@16
|
34 {
|
Chris@16
|
35 private:
|
Chris@16
|
36 // Helper class to re-post the strand on exit.
|
Chris@16
|
37 struct on_do_complete_exit;
|
Chris@16
|
38
|
Chris@16
|
39 // Helper class to re-post the strand on exit.
|
Chris@16
|
40 struct on_dispatch_exit;
|
Chris@16
|
41
|
Chris@16
|
42 public:
|
Chris@16
|
43
|
Chris@16
|
44 // The underlying implementation of a strand.
|
Chris@16
|
45 class strand_impl
|
Chris@16
|
46 : public operation
|
Chris@16
|
47 {
|
Chris@16
|
48 public:
|
Chris@16
|
49 strand_impl();
|
Chris@16
|
50
|
Chris@16
|
51 private:
|
Chris@16
|
52 // Only this service will have access to the internal values.
|
Chris@16
|
53 friend class strand_service;
|
Chris@16
|
54 friend struct on_do_complete_exit;
|
Chris@16
|
55 friend struct on_dispatch_exit;
|
Chris@16
|
56
|
Chris@16
|
57 // Mutex to protect access to internal data.
|
Chris@16
|
58 boost::asio::detail::mutex mutex_;
|
Chris@16
|
59
|
Chris@16
|
60 // Indicates whether the strand is currently "locked" by a handler. This
|
Chris@16
|
61 // means that there is a handler upcall in progress, or that the strand
|
Chris@16
|
62 // itself has been scheduled in order to invoke some pending handlers.
|
Chris@16
|
63 bool locked_;
|
Chris@16
|
64
|
Chris@16
|
65 // The handlers that are waiting on the strand but should not be run until
|
Chris@16
|
66 // after the next time the strand is scheduled. This queue must only be
|
Chris@16
|
67 // modified while the mutex is locked.
|
Chris@16
|
68 op_queue<operation> waiting_queue_;
|
Chris@16
|
69
|
Chris@16
|
70 // The handlers that are ready to be run. Logically speaking, these are the
|
Chris@16
|
71 // handlers that hold the strand's lock. The ready queue is only modified
|
Chris@16
|
72 // from within the strand and so may be accessed without locking the mutex.
|
Chris@16
|
73 op_queue<operation> ready_queue_;
|
Chris@16
|
74 };
|
Chris@16
|
75
|
Chris@16
|
76 typedef strand_impl* implementation_type;
|
Chris@16
|
77
|
Chris@16
|
78 // Construct a new strand service for the specified io_service.
|
Chris@16
|
79 BOOST_ASIO_DECL explicit strand_service(boost::asio::io_service& io_service);
|
Chris@16
|
80
|
Chris@16
|
81 // Destroy all user-defined handler objects owned by the service.
|
Chris@16
|
82 BOOST_ASIO_DECL void shutdown_service();
|
Chris@16
|
83
|
Chris@16
|
84 // Construct a new strand implementation.
|
Chris@16
|
85 BOOST_ASIO_DECL void construct(implementation_type& impl);
|
Chris@16
|
86
|
Chris@16
|
87 // Request the io_service to invoke the given handler.
|
Chris@16
|
88 template <typename Handler>
|
Chris@16
|
89 void dispatch(implementation_type& impl, Handler& handler);
|
Chris@16
|
90
|
Chris@16
|
91 // Request the io_service to invoke the given handler and return immediately.
|
Chris@16
|
92 template <typename Handler>
|
Chris@16
|
93 void post(implementation_type& impl, Handler& handler);
|
Chris@16
|
94
|
Chris@16
|
95 // Determine whether the strand is running in the current thread.
|
Chris@16
|
96 BOOST_ASIO_DECL bool running_in_this_thread(
|
Chris@16
|
97 const implementation_type& impl) const;
|
Chris@16
|
98
|
Chris@16
|
99 private:
|
Chris@16
|
100 // Helper function to dispatch a handler. Returns true if the handler should
|
Chris@16
|
101 // be dispatched immediately.
|
Chris@16
|
102 BOOST_ASIO_DECL bool do_dispatch(implementation_type& impl, operation* op);
|
Chris@16
|
103
|
Chris@16
|
104 // Helper fiunction to post a handler.
|
Chris@16
|
105 BOOST_ASIO_DECL void do_post(implementation_type& impl,
|
Chris@16
|
106 operation* op, bool is_continuation);
|
Chris@16
|
107
|
Chris@16
|
108 BOOST_ASIO_DECL static void do_complete(io_service_impl* owner,
|
Chris@16
|
109 operation* base, const boost::system::error_code& ec,
|
Chris@16
|
110 std::size_t bytes_transferred);
|
Chris@16
|
111
|
Chris@16
|
112 // The io_service implementation used to post completions.
|
Chris@16
|
113 io_service_impl& io_service_;
|
Chris@16
|
114
|
Chris@16
|
115 // Mutex to protect access to the array of implementations.
|
Chris@16
|
116 boost::asio::detail::mutex mutex_;
|
Chris@16
|
117
|
Chris@16
|
118 // Number of implementations shared between all strand objects.
|
Chris@16
|
119 #if defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
|
Chris@16
|
120 enum { num_implementations = BOOST_ASIO_STRAND_IMPLEMENTATIONS };
|
Chris@16
|
121 #else // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
|
Chris@16
|
122 enum { num_implementations = 193 };
|
Chris@16
|
123 #endif // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
|
Chris@16
|
124
|
Chris@16
|
125 // Pool of implementations.
|
Chris@16
|
126 scoped_ptr<strand_impl> implementations_[num_implementations];
|
Chris@16
|
127
|
Chris@16
|
128 // Extra value used when hashing to prevent recycled memory locations from
|
Chris@16
|
129 // getting the same strand implementation.
|
Chris@16
|
130 std::size_t salt_;
|
Chris@16
|
131 };
|
Chris@16
|
132
|
Chris@16
|
133 } // namespace detail
|
Chris@16
|
134 } // namespace asio
|
Chris@16
|
135 } // namespace boost
|
Chris@16
|
136
|
Chris@16
|
137 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
138
|
Chris@16
|
139 #include <boost/asio/detail/impl/strand_service.hpp>
|
Chris@16
|
140 #if defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
141 # include <boost/asio/detail/impl/strand_service.ipp>
|
Chris@16
|
142 #endif // defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
143
|
Chris@16
|
144 #endif // BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP
|