]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // |
b32b8144 FG |
2 | // detail/scheduler.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~ | |
7c673cae | 4 | // |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
b32b8144 FG |
11 | #ifndef BOOST_ASIO_DETAIL_SCHEDULER_HPP |
12 | #define BOOST_ASIO_DETAIL_SCHEDULER_HPP | |
7c673cae FG |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
7c673cae | 20 | #include <boost/system/error_code.hpp> |
b32b8144 | 21 | #include <boost/asio/execution_context.hpp> |
7c673cae | 22 | #include <boost/asio/detail/atomic_count.hpp> |
b32b8144 FG |
23 | #include <boost/asio/detail/conditionally_enabled_event.hpp> |
24 | #include <boost/asio/detail/conditionally_enabled_mutex.hpp> | |
7c673cae FG |
25 | #include <boost/asio/detail/op_queue.hpp> |
26 | #include <boost/asio/detail/reactor_fwd.hpp> | |
b32b8144 | 27 | #include <boost/asio/detail/scheduler_operation.hpp> |
92f5a8d4 | 28 | #include <boost/asio/detail/thread.hpp> |
b32b8144 | 29 | #include <boost/asio/detail/thread_context.hpp> |
7c673cae FG |
30 | |
31 | #include <boost/asio/detail/push_options.hpp> | |
32 | ||
33 | namespace boost { | |
34 | namespace asio { | |
35 | namespace detail { | |
36 | ||
b32b8144 | 37 | struct scheduler_thread_info; |
7c673cae | 38 | |
b32b8144 FG |
39 | class scheduler |
40 | : public execution_context_service_base<scheduler>, | |
41 | public thread_context | |
7c673cae FG |
42 | { |
43 | public: | |
b32b8144 | 44 | typedef scheduler_operation operation; |
7c673cae FG |
45 | |
46 | // Constructor. Specifies the number of concurrent threads that are likely to | |
b32b8144 FG |
47 | // run the scheduler. If set to 1 certain optimisation are performed. |
48 | BOOST_ASIO_DECL scheduler(boost::asio::execution_context& ctx, | |
92f5a8d4 TL |
49 | int concurrency_hint = 0, bool own_thread = true); |
50 | ||
51 | // Destructor. | |
52 | BOOST_ASIO_DECL ~scheduler(); | |
7c673cae FG |
53 | |
54 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 55 | BOOST_ASIO_DECL void shutdown(); |
7c673cae FG |
56 | |
57 | // Initialise the task, if required. | |
58 | BOOST_ASIO_DECL void init_task(); | |
59 | ||
60 | // Run the event loop until interrupted or no more work. | |
61 | BOOST_ASIO_DECL std::size_t run(boost::system::error_code& ec); | |
62 | ||
63 | // Run until interrupted or one operation is performed. | |
64 | BOOST_ASIO_DECL std::size_t run_one(boost::system::error_code& ec); | |
65 | ||
b32b8144 FG |
66 | // Run until timeout, interrupted, or one operation is performed. |
67 | BOOST_ASIO_DECL std::size_t wait_one( | |
68 | long usec, boost::system::error_code& ec); | |
69 | ||
7c673cae FG |
70 | // Poll for operations without blocking. |
71 | BOOST_ASIO_DECL std::size_t poll(boost::system::error_code& ec); | |
72 | ||
73 | // Poll for one operation without blocking. | |
74 | BOOST_ASIO_DECL std::size_t poll_one(boost::system::error_code& ec); | |
75 | ||
76 | // Interrupt the event processing loop. | |
77 | BOOST_ASIO_DECL void stop(); | |
78 | ||
b32b8144 | 79 | // Determine whether the scheduler is stopped. |
7c673cae FG |
80 | BOOST_ASIO_DECL bool stopped() const; |
81 | ||
b32b8144 FG |
82 | // Restart in preparation for a subsequent run invocation. |
83 | BOOST_ASIO_DECL void restart(); | |
7c673cae FG |
84 | |
85 | // Notify that some work has started. | |
86 | void work_started() | |
87 | { | |
88 | ++outstanding_work_; | |
89 | } | |
90 | ||
b32b8144 FG |
91 | // Used to compensate for a forthcoming work_finished call. Must be called |
92 | // from within a scheduler-owned thread. | |
93 | BOOST_ASIO_DECL void compensating_work_started(); | |
94 | ||
7c673cae FG |
95 | // Notify that some work has finished. |
96 | void work_finished() | |
97 | { | |
98 | if (--outstanding_work_ == 0) | |
99 | stop(); | |
100 | } | |
101 | ||
102 | // Return whether a handler can be dispatched immediately. | |
103 | bool can_dispatch() | |
104 | { | |
105 | return thread_call_stack::contains(this) != 0; | |
106 | } | |
107 | ||
20effc67 TL |
108 | /// Capture the current exception so it can be rethrown from a run function. |
109 | BOOST_ASIO_DECL void capture_current_exception(); | |
110 | ||
7c673cae FG |
111 | // Request invocation of the given operation and return immediately. Assumes |
112 | // that work_started() has not yet been called for the operation. | |
113 | BOOST_ASIO_DECL void post_immediate_completion( | |
114 | operation* op, bool is_continuation); | |
115 | ||
20effc67 TL |
116 | // Request invocation of the given operations and return immediately. Assumes |
117 | // that work_started() has not yet been called for the operations. | |
118 | BOOST_ASIO_DECL void post_immediate_completions(std::size_t n, | |
119 | op_queue<operation>& ops, bool is_continuation); | |
120 | ||
7c673cae FG |
121 | // Request invocation of the given operation and return immediately. Assumes |
122 | // that work_started() was previously called for the operation. | |
123 | BOOST_ASIO_DECL void post_deferred_completion(operation* op); | |
124 | ||
125 | // Request invocation of the given operations and return immediately. Assumes | |
126 | // that work_started() was previously called for each operation. | |
127 | BOOST_ASIO_DECL void post_deferred_completions(op_queue<operation>& ops); | |
128 | ||
b32b8144 FG |
129 | // Enqueue the given operation following a failed attempt to dispatch the |
130 | // operation for immediate invocation. | |
131 | BOOST_ASIO_DECL void do_dispatch(operation* op); | |
132 | ||
133 | // Process unfinished operations as part of a shutdownoperation. Assumes that | |
134 | // work_started() was previously called for the operations. | |
7c673cae FG |
135 | BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops); |
136 | ||
b32b8144 FG |
137 | // Get the concurrency hint that was used to initialise the scheduler. |
138 | int concurrency_hint() const | |
139 | { | |
140 | return concurrency_hint_; | |
141 | } | |
142 | ||
7c673cae | 143 | private: |
b32b8144 FG |
144 | // The mutex type used by this scheduler. |
145 | typedef conditionally_enabled_mutex mutex; | |
7c673cae | 146 | |
b32b8144 FG |
147 | // The event type used by this scheduler. |
148 | typedef conditionally_enabled_event event; | |
149 | ||
150 | // Structure containing thread-specific data. | |
151 | typedef scheduler_thread_info thread_info; | |
7c673cae FG |
152 | |
153 | // Run at most one operation. May block. | |
154 | BOOST_ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock, | |
155 | thread_info& this_thread, const boost::system::error_code& ec); | |
156 | ||
b32b8144 FG |
157 | // Run at most one operation with a timeout. May block. |
158 | BOOST_ASIO_DECL std::size_t do_wait_one(mutex::scoped_lock& lock, | |
159 | thread_info& this_thread, long usec, const boost::system::error_code& ec); | |
160 | ||
7c673cae FG |
161 | // Poll for at most one operation. |
162 | BOOST_ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock, | |
163 | thread_info& this_thread, const boost::system::error_code& ec); | |
164 | ||
165 | // Stop the task and all idle threads. | |
166 | BOOST_ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock); | |
167 | ||
168 | // Wake a single idle thread, or the task, and always unlock the mutex. | |
169 | BOOST_ASIO_DECL void wake_one_thread_and_unlock( | |
170 | mutex::scoped_lock& lock); | |
171 | ||
92f5a8d4 TL |
172 | // Helper class to run the scheduler in its own thread. |
173 | class thread_function; | |
174 | friend class thread_function; | |
175 | ||
7c673cae FG |
176 | // Helper class to perform task-related operations on block exit. |
177 | struct task_cleanup; | |
178 | friend struct task_cleanup; | |
179 | ||
180 | // Helper class to call work-related operations on block exit. | |
181 | struct work_cleanup; | |
182 | friend struct work_cleanup; | |
183 | ||
184 | // Whether to optimise for single-threaded use cases. | |
185 | const bool one_thread_; | |
186 | ||
187 | // Mutex to protect access to internal data. | |
188 | mutable mutex mutex_; | |
189 | ||
190 | // Event to wake up blocked threads. | |
191 | event wakeup_event_; | |
192 | ||
193 | // The task to be run by this service. | |
194 | reactor* task_; | |
195 | ||
196 | // Operation object to represent the position of the task in the queue. | |
197 | struct task_operation : operation | |
198 | { | |
199 | task_operation() : operation(0) {} | |
200 | } task_operation_; | |
201 | ||
202 | // Whether the task has been interrupted. | |
203 | bool task_interrupted_; | |
204 | ||
205 | // The count of unfinished work. | |
206 | atomic_count outstanding_work_; | |
207 | ||
208 | // The queue of handlers that are ready to be delivered. | |
209 | op_queue<operation> op_queue_; | |
210 | ||
211 | // Flag to indicate that the dispatcher has been stopped. | |
212 | bool stopped_; | |
213 | ||
214 | // Flag to indicate that the dispatcher has been shut down. | |
215 | bool shutdown_; | |
216 | ||
b32b8144 FG |
217 | // The concurrency hint used to initialise the scheduler. |
218 | const int concurrency_hint_; | |
92f5a8d4 TL |
219 | |
220 | // The thread that is running the scheduler. | |
221 | boost::asio::detail::thread* thread_; | |
7c673cae FG |
222 | }; |
223 | ||
224 | } // namespace detail | |
225 | } // namespace asio | |
226 | } // namespace boost | |
227 | ||
228 | #include <boost/asio/detail/pop_options.hpp> | |
229 | ||
7c673cae | 230 | #if defined(BOOST_ASIO_HEADER_ONLY) |
b32b8144 | 231 | # include <boost/asio/detail/impl/scheduler.ipp> |
7c673cae FG |
232 | #endif // defined(BOOST_ASIO_HEADER_ONLY) |
233 | ||
b32b8144 | 234 | #endif // BOOST_ASIO_DETAIL_SCHEDULER_HPP |