Chris@16
|
1 //
|
Chris@16
|
2 // detail/task_io_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_TASK_IO_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_TASK_IO_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
|
Chris@16
|
20 #if !defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/system/error_code.hpp>
|
Chris@16
|
23 #include <boost/asio/io_service.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/atomic_count.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/call_stack.hpp>
|
Chris@101
|
26 #include <boost/asio/detail/event.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/mutex.hpp>
|
Chris@16
|
28 #include <boost/asio/detail/op_queue.hpp>
|
Chris@16
|
29 #include <boost/asio/detail/reactor_fwd.hpp>
|
Chris@16
|
30 #include <boost/asio/detail/task_io_service_operation.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 namespace boost {
|
Chris@16
|
35 namespace asio {
|
Chris@16
|
36 namespace detail {
|
Chris@16
|
37
|
Chris@16
|
38 struct task_io_service_thread_info;
|
Chris@16
|
39
|
Chris@16
|
40 class task_io_service
|
Chris@16
|
41 : public boost::asio::detail::service_base<task_io_service>
|
Chris@16
|
42 {
|
Chris@16
|
43 public:
|
Chris@16
|
44 typedef task_io_service_operation operation;
|
Chris@16
|
45
|
Chris@16
|
46 // Constructor. Specifies the number of concurrent threads that are likely to
|
Chris@16
|
47 // run the io_service. If set to 1 certain optimisation are performed.
|
Chris@16
|
48 BOOST_ASIO_DECL task_io_service(boost::asio::io_service& io_service,
|
Chris@16
|
49 std::size_t concurrency_hint = 0);
|
Chris@16
|
50
|
Chris@16
|
51 // Destroy all user-defined handler objects owned by the service.
|
Chris@16
|
52 BOOST_ASIO_DECL void shutdown_service();
|
Chris@16
|
53
|
Chris@16
|
54 // Initialise the task, if required.
|
Chris@16
|
55 BOOST_ASIO_DECL void init_task();
|
Chris@16
|
56
|
Chris@16
|
57 // Run the event loop until interrupted or no more work.
|
Chris@16
|
58 BOOST_ASIO_DECL std::size_t run(boost::system::error_code& ec);
|
Chris@16
|
59
|
Chris@16
|
60 // Run until interrupted or one operation is performed.
|
Chris@16
|
61 BOOST_ASIO_DECL std::size_t run_one(boost::system::error_code& ec);
|
Chris@16
|
62
|
Chris@16
|
63 // Poll for operations without blocking.
|
Chris@16
|
64 BOOST_ASIO_DECL std::size_t poll(boost::system::error_code& ec);
|
Chris@16
|
65
|
Chris@16
|
66 // Poll for one operation without blocking.
|
Chris@16
|
67 BOOST_ASIO_DECL std::size_t poll_one(boost::system::error_code& ec);
|
Chris@16
|
68
|
Chris@16
|
69 // Interrupt the event processing loop.
|
Chris@16
|
70 BOOST_ASIO_DECL void stop();
|
Chris@16
|
71
|
Chris@16
|
72 // Determine whether the io_service is stopped.
|
Chris@16
|
73 BOOST_ASIO_DECL bool stopped() const;
|
Chris@16
|
74
|
Chris@16
|
75 // Reset in preparation for a subsequent run invocation.
|
Chris@16
|
76 BOOST_ASIO_DECL void reset();
|
Chris@16
|
77
|
Chris@16
|
78 // Notify that some work has started.
|
Chris@16
|
79 void work_started()
|
Chris@16
|
80 {
|
Chris@16
|
81 ++outstanding_work_;
|
Chris@16
|
82 }
|
Chris@16
|
83
|
Chris@16
|
84 // Notify that some work has finished.
|
Chris@16
|
85 void work_finished()
|
Chris@16
|
86 {
|
Chris@16
|
87 if (--outstanding_work_ == 0)
|
Chris@16
|
88 stop();
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 // Return whether a handler can be dispatched immediately.
|
Chris@16
|
92 bool can_dispatch()
|
Chris@16
|
93 {
|
Chris@16
|
94 return thread_call_stack::contains(this) != 0;
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 // Request invocation of the given handler.
|
Chris@16
|
98 template <typename Handler>
|
Chris@16
|
99 void dispatch(Handler& handler);
|
Chris@16
|
100
|
Chris@16
|
101 // Request invocation of the given handler and return immediately.
|
Chris@16
|
102 template <typename Handler>
|
Chris@16
|
103 void post(Handler& handler);
|
Chris@16
|
104
|
Chris@16
|
105 // Request invocation of the given operation and return immediately. Assumes
|
Chris@16
|
106 // that work_started() has not yet been called for the operation.
|
Chris@16
|
107 BOOST_ASIO_DECL void post_immediate_completion(
|
Chris@16
|
108 operation* op, bool is_continuation);
|
Chris@16
|
109
|
Chris@16
|
110 // Request invocation of the given operation and return immediately. Assumes
|
Chris@16
|
111 // that work_started() was previously called for the operation.
|
Chris@16
|
112 BOOST_ASIO_DECL void post_deferred_completion(operation* op);
|
Chris@16
|
113
|
Chris@16
|
114 // Request invocation of the given operations and return immediately. Assumes
|
Chris@16
|
115 // that work_started() was previously called for each operation.
|
Chris@16
|
116 BOOST_ASIO_DECL void post_deferred_completions(op_queue<operation>& ops);
|
Chris@16
|
117
|
Chris@16
|
118 // Process unfinished operations as part of a shutdown_service operation.
|
Chris@16
|
119 // Assumes that work_started() was previously called for the operations.
|
Chris@16
|
120 BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops);
|
Chris@16
|
121
|
Chris@16
|
122 private:
|
Chris@101
|
123 // Structure containing thread-specific data.
|
Chris@16
|
124 typedef task_io_service_thread_info thread_info;
|
Chris@16
|
125
|
Chris@16
|
126 // Enqueue the given operation following a failed attempt to dispatch the
|
Chris@16
|
127 // operation for immediate invocation.
|
Chris@16
|
128 BOOST_ASIO_DECL void do_dispatch(operation* op);
|
Chris@16
|
129
|
Chris@16
|
130 // Run at most one operation. May block.
|
Chris@16
|
131 BOOST_ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock,
|
Chris@16
|
132 thread_info& this_thread, const boost::system::error_code& ec);
|
Chris@16
|
133
|
Chris@16
|
134 // Poll for at most one operation.
|
Chris@16
|
135 BOOST_ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock,
|
Chris@16
|
136 thread_info& this_thread, const boost::system::error_code& ec);
|
Chris@16
|
137
|
Chris@16
|
138 // Stop the task and all idle threads.
|
Chris@16
|
139 BOOST_ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock);
|
Chris@16
|
140
|
Chris@16
|
141 // Wake a single idle thread, or the task, and always unlock the mutex.
|
Chris@16
|
142 BOOST_ASIO_DECL void wake_one_thread_and_unlock(
|
Chris@16
|
143 mutex::scoped_lock& lock);
|
Chris@16
|
144
|
Chris@16
|
145 // Helper class to perform task-related operations on block exit.
|
Chris@16
|
146 struct task_cleanup;
|
Chris@16
|
147 friend struct task_cleanup;
|
Chris@16
|
148
|
Chris@16
|
149 // Helper class to call work-related operations on block exit.
|
Chris@16
|
150 struct work_cleanup;
|
Chris@16
|
151 friend struct work_cleanup;
|
Chris@16
|
152
|
Chris@16
|
153 // Whether to optimise for single-threaded use cases.
|
Chris@16
|
154 const bool one_thread_;
|
Chris@16
|
155
|
Chris@16
|
156 // Mutex to protect access to internal data.
|
Chris@16
|
157 mutable mutex mutex_;
|
Chris@16
|
158
|
Chris@101
|
159 // Event to wake up blocked threads.
|
Chris@101
|
160 event wakeup_event_;
|
Chris@101
|
161
|
Chris@16
|
162 // The task to be run by this service.
|
Chris@16
|
163 reactor* task_;
|
Chris@16
|
164
|
Chris@16
|
165 // Operation object to represent the position of the task in the queue.
|
Chris@16
|
166 struct task_operation : operation
|
Chris@16
|
167 {
|
Chris@16
|
168 task_operation() : operation(0) {}
|
Chris@16
|
169 } task_operation_;
|
Chris@16
|
170
|
Chris@16
|
171 // Whether the task has been interrupted.
|
Chris@16
|
172 bool task_interrupted_;
|
Chris@16
|
173
|
Chris@16
|
174 // The count of unfinished work.
|
Chris@16
|
175 atomic_count outstanding_work_;
|
Chris@16
|
176
|
Chris@16
|
177 // The queue of handlers that are ready to be delivered.
|
Chris@16
|
178 op_queue<operation> op_queue_;
|
Chris@16
|
179
|
Chris@16
|
180 // Flag to indicate that the dispatcher has been stopped.
|
Chris@16
|
181 bool stopped_;
|
Chris@16
|
182
|
Chris@16
|
183 // Flag to indicate that the dispatcher has been shut down.
|
Chris@16
|
184 bool shutdown_;
|
Chris@16
|
185
|
Chris@16
|
186 // Per-thread call stack to track the state of each thread in the io_service.
|
Chris@16
|
187 typedef call_stack<task_io_service, thread_info> thread_call_stack;
|
Chris@16
|
188 };
|
Chris@16
|
189
|
Chris@16
|
190 } // namespace detail
|
Chris@16
|
191 } // namespace asio
|
Chris@16
|
192 } // namespace boost
|
Chris@16
|
193
|
Chris@16
|
194 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
195
|
Chris@16
|
196 #include <boost/asio/detail/impl/task_io_service.hpp>
|
Chris@16
|
197 #if defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
198 # include <boost/asio/detail/impl/task_io_service.ipp>
|
Chris@16
|
199 #endif // defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
200
|
Chris@16
|
201 #endif // !defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
202
|
Chris@16
|
203 #endif // BOOST_ASIO_DETAIL_TASK_IO_SERVICE_HPP
|