Loading...
Searching...
No Matches
coroutine.hpp
1//
2// Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
3// Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
4//
5// Distributed under the Boost Software License, Version 1.0. (See accompanying
6// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7//
8
9#ifndef BOOST_REDIS_DETAIL_COROUTINE_HPP
10#define BOOST_REDIS_DETAIL_COROUTINE_HPP
11
12// asio::coroutine uses __COUNTER__ internally, which can trigger
13// ODR violations if we use them in header-only code. These manifest as
14// extremely hard-to-debug bugs only present in release builds.
15// Use this instead when doing coroutines in non-template code.
16// Adapted from Boost.MySQL.
17
18// Coroutine state is represented as an integer (resume_point_var).
19// Every yield gets assigned a unique value (resume_point_id).
20// Yielding sets the next resume point, returns, and sets a case label for re-entering.
21// Coroutines need to switch on resume_point_var to re-enter.
22
23// Enclosing this in a scope allows placing the macro inside a brace-less for/while loop
24// The empty scope after the case label is required because labels can't be at the end of a compound statement
25#define BOOST_REDIS_YIELD(resume_point_var, resume_point_id, ...) \
26 { \
27 resume_point_var = resume_point_id; \
28 return {__VA_ARGS__}; \
29 case resume_point_id: \
30 { \
31 } \
32 }
33
34#define BOOST_REDIS_CORO_INITIAL case 0:
35
36#endif