5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_COROUTINE_HPP
12 #define BOOST_ASIO_COROUTINE_HPP
22 /// Provides support for implementing stackless coroutines.
24 * The @c coroutine class may be used to implement stackless coroutines. The
25 * class itself is used to store the current state of the coroutine.
27 * Coroutines are copy-constructible and assignable, and the space overhead is
28 * a single int. They can be used as a base class:
30 * @code class session : coroutine
35 * or as a data member:
43 * or even bound in as a function argument using lambdas or @c bind(). The
44 * important thing is that as the application maintains a copy of the object
45 * for as long as the coroutine must be kept alive.
47 * @par Pseudo-keywords
49 * A coroutine is used in conjunction with certain "pseudo-keywords", which
50 * are implemented as macros. These macros are defined by a header file:
52 * @code #include <boost/asio/yield.hpp>@endcode
54 * and may conversely be undefined as follows:
56 * @code #include <boost/asio/unyield.hpp>@endcode
60 * The @c reenter macro is used to define the body of a coroutine. It takes a
61 * single argument: a pointer or reference to a coroutine object. For example,
62 * if the base class is a coroutine object you may write:
64 * @code reenter (this)
66 * ... coroutine body ...
69 * and if a data member or other variable you can write:
71 * @code reenter (coro_)
73 * ... coroutine body ...
76 * When @c reenter is executed at runtime, control jumps to the location of the
77 * last @c yield or @c fork.
79 * The coroutine body may also be a single statement, such as:
81 * @code reenter (this) for (;;)
86 * @b Limitation: The @c reenter macro is implemented using a switch. This
87 * means that you must take care when using local variables within the
88 * coroutine body. The local variable is not allowed in a position where
89 * reentering the coroutine could bypass the variable definition.
91 * <b>yield <em>statement</em></b>
93 * This form of the @c yield keyword is often used with asynchronous operations:
95 * @code yield socket_->async_read_some(buffer(*buffer_), *this); @endcode
97 * This divides into four logical steps:
99 * @li @c yield saves the current state of the coroutine.
100 * @li The statement initiates the asynchronous operation.
101 * @li The resume point is defined immediately following the statement.
102 * @li Control is transferred to the end of the coroutine body.
104 * When the asynchronous operation completes, the function object is invoked
105 * and @c reenter causes control to transfer to the resume point. It is
106 * important to remember to carry the coroutine state forward with the
107 * asynchronous operation. In the above snippet, the current class is a
108 * function object object with a coroutine object as base class or data member.
110 * The statement may also be a compound statement, and this permits us to
111 * define local variables with limited scope:
115 * mutable_buffers_1 b = buffer(*buffer_);
116 * socket_->async_read_some(b, *this);
119 * <b>yield return <em>expression</em> ;</b>
121 * This form of @c yield is often used in generators or coroutine-based parsers.
122 * For example, the function object:
124 * @code struct interleave : coroutine
128 * char operator()(char c)
130 * reenter (this) for (;;)
132 * yield return is1.get();
133 * yield return is2.get();
138 * defines a trivial coroutine that interleaves the characters from two input
141 * This type of @c yield divides into three logical steps:
143 * @li @c yield saves the current state of the coroutine.
144 * @li The resume point is defined immediately following the semicolon.
145 * @li The value of the expression is returned from the function.
149 * This form of @c yield is equivalent to the following steps:
151 * @li @c yield saves the current state of the coroutine.
152 * @li The resume point is defined immediately following the semicolon.
153 * @li Control is transferred to the end of the coroutine body.
155 * This form might be applied when coroutines are used for cooperative
156 * threading and scheduling is explicitly managed. For example:
158 * @code struct task : coroutine
165 * while (... not finished ...)
167 * ... do something ...
169 * ... do some more ...
184 * <b>yield break ;</b>
186 * The final form of @c yield is used to explicitly terminate the coroutine.
187 * This form is comprised of two steps:
189 * @li @c yield sets the coroutine state to indicate termination.
190 * @li Control is transferred to the end of the coroutine body.
192 * Once terminated, calls to is_complete() return true and the coroutine cannot
195 * Note that a coroutine may also be implicitly terminated if the coroutine
196 * body is exited without a yield, e.g. by return, throw or by running to the
199 * <b>fork <em>statement</em></b>
201 * The @c fork pseudo-keyword is used when "forking" a coroutine, i.e. splitting
202 * it into two (or more) copies. One use of @c fork is in a server, where a new
203 * coroutine is created to handle each client connection:
205 * @code reenter (this)
209 * socket_.reset(new tcp::socket(my_context_));
210 * yield acceptor->async_accept(*socket_, *this);
211 * fork server(*this)();
212 * } while (is_parent());
213 * ... client-specific handling follows ...
216 * The logical steps involved in a @c fork are:
218 * @li @c fork saves the current state of the coroutine.
219 * @li The statement creates a copy of the coroutine and either executes it
220 * immediately or schedules it for later execution.
221 * @li The resume point is defined immediately following the semicolon.
222 * @li For the "parent", control immediately continues from the next line.
224 * The functions is_parent() and is_child() can be used to differentiate
225 * between parent and child. You would use these functions to alter subsequent
228 * Note that @c fork doesn't do the actual forking by itself. It is the
229 * application's responsibility to create a clone of the coroutine and call it.
230 * The clone can be called immediately, as above, or scheduled for delayed
231 * execution using something like boost::asio::post().
233 * @par Alternate macro names
235 * If preferred, an application can use macro names that follow a more typical
236 * naming convention, rather than the pseudo-keywords. These are:
238 * @li @c BOOST_ASIO_CORO_REENTER instead of @c reenter
239 * @li @c BOOST_ASIO_CORO_YIELD instead of @c yield
240 * @li @c BOOST_ASIO_CORO_FORK instead of @c fork
245 /// Constructs a coroutine in its initial state.
246 coroutine() : value_(0) {}
248 /// Returns true if the coroutine is the child of a fork.
249 bool is_child() const { return value_ < 0; }
251 /// Returns true if the coroutine is the parent of a fork.
252 bool is_parent() const { return !is_child(); }
254 /// Returns true if the coroutine has reached its terminal state.
255 bool is_complete() const { return value_ == -1; }
258 friend class detail::coroutine_ref;
268 coroutine_ref(coroutine& c) : value_(c.value_), modified_(false) {}
269 coroutine_ref(coroutine* c) : value_(c->value_), modified_(false) {}
270 ~coroutine_ref() { if (!modified_) value_ = -1; }
271 operator int() const { return value_; }
272 int& operator=(int v) { modified_ = true; return value_ = v; }
274 void operator=(const coroutine_ref&);
279 } // namespace detail
283 #define BOOST_ASIO_CORO_REENTER(c) \
284 switch (::boost::asio::detail::coroutine_ref _coro_value = c) \
285 case -1: if (_coro_value) \
287 goto terminate_coroutine; \
288 terminate_coroutine: \
290 goto bail_out_of_coroutine; \
291 bail_out_of_coroutine: \
294 else /* fall-through */ case 0:
296 #define BOOST_ASIO_CORO_YIELD_IMPL(n) \
297 for (_coro_value = (n);;) \
298 if (_coro_value == 0) \
304 switch (_coro_value ? 0 : 1) \
306 /* fall-through */ case -1: if (_coro_value) \
307 goto terminate_coroutine; \
309 /* fall-through */ case 1: if (_coro_value) \
310 goto bail_out_of_coroutine; \
311 else /* fall-through */ case 0:
313 #define BOOST_ASIO_CORO_FORK_IMPL(n) \
314 for (_coro_value = -(n);; _coro_value = (n)) \
315 if (_coro_value == (n)) \
322 #if defined(_MSC_VER)
323 # define BOOST_ASIO_CORO_YIELD BOOST_ASIO_CORO_YIELD_IMPL(__COUNTER__ + 1)
324 # define BOOST_ASIO_CORO_FORK BOOST_ASIO_CORO_FORK_IMPL(__COUNTER__ + 1)
325 #else // defined(_MSC_VER)
326 # define BOOST_ASIO_CORO_YIELD BOOST_ASIO_CORO_YIELD_IMPL(__LINE__)
327 # define BOOST_ASIO_CORO_FORK BOOST_ASIO_CORO_FORK_IMPL(__LINE__)
328 #endif // defined(_MSC_VER)
330 #endif // BOOST_ASIO_COROUTINE_HPP