|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Niklas Hauser |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#ifndef MODM_RF_MACROS_FIBER_HPP |
| 13 | +#define MODM_RF_MACROS_FIBER_HPP |
| 14 | + |
| 15 | +#include <modm/processing/fiber.hpp> |
| 16 | + |
| 17 | +/// @ingroup modm_processing_resumable |
| 18 | +/// @{ |
| 19 | + |
| 20 | +/// Declare start of resumable function with index. |
| 21 | +/// @warning Use at start of the `resumable()` implementation! |
| 22 | +#define RF_BEGIN(...) |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * End the resumable function and return a result. |
| 27 | + * |
| 28 | + * @warning Use at end of the `resumable()` implementation only! |
| 29 | + * @hideinitializer |
| 30 | + */ |
| 31 | +#define RF_END_RETURN(...) \ |
| 32 | + return __VA_ARGS__ |
| 33 | + |
| 34 | +/** |
| 35 | + * End the resumable function. You can use this to return `void`, or if the result does not matter. |
| 36 | + * |
| 37 | + * @warning Use at end of the `resumable()` implementation only! |
| 38 | + * @hideinitializer |
| 39 | + */ |
| 40 | +#define RF_END() \ |
| 41 | + return |
| 42 | + |
| 43 | +/** |
| 44 | + * End the resumable function by calling another resumable function and returning its result. |
| 45 | + * |
| 46 | + * @warning Use at end of the `resumable()` implementation only! |
| 47 | + * @hideinitializer |
| 48 | + */ |
| 49 | +#define RF_END_RETURN_CALL(...) \ |
| 50 | + return __VA_ARGS__ |
| 51 | + |
| 52 | +/// Yield resumable function until next invocation. |
| 53 | +/// @hideinitializer |
| 54 | +#define RF_YIELD() \ |
| 55 | + modm::fiber::yield() |
| 56 | + |
| 57 | +/// Cause resumable function to wait until given child protothread completes. |
| 58 | +/// @hideinitializer |
| 59 | +#define RF_WAIT_THREAD(...) \ |
| 60 | + RF_WAIT_WHILE((__VA_ARGS__).run()) |
| 61 | + |
| 62 | +/// Cause resumable function to wait **while** given `condition` is true. |
| 63 | +/// @hideinitializer |
| 64 | +#define RF_WAIT_WHILE(...) \ |
| 65 | + while(__VA_ARGS__) { RF_YIELD(); } |
| 66 | + |
| 67 | +/// Cause resumable function to wait **until** given `condition` is true. |
| 68 | +/// @hideinitializer |
| 69 | +#define RF_WAIT_UNTIL(...) \ |
| 70 | + RF_WAIT_WHILE(!(__VA_ARGS__)) |
| 71 | + |
| 72 | +/// Calls a resumable function and returns its result. |
| 73 | +/// @hideinitializer |
| 74 | +#define RF_CALL(...) \ |
| 75 | + __VA_ARGS__ |
| 76 | + |
| 77 | +/** |
| 78 | + * Calls a resumable function, busy-waits and returns its result. |
| 79 | + * |
| 80 | + * @hideinitializer |
| 81 | + */ |
| 82 | +#define RF_CALL_BLOCKING(...) \ |
| 83 | + __VA_ARGS__ |
| 84 | + |
| 85 | +/// Exits a resumable function and returns another resumable function's result. |
| 86 | +/// @hideinitializer |
| 87 | +#define RF_RETURN_CALL(...) \ |
| 88 | + return __VA_ARGS__ |
| 89 | + |
| 90 | +/// Stop and exit from resumable function with an optional result. |
| 91 | +/// @hideinitializer |
| 92 | +#define RF_RETURN(...) \ |
| 93 | + return __VA_ARGS__ |
| 94 | + |
| 95 | +/// @} |
| 96 | + |
| 97 | +#endif // MODM_RF_MACROS_FIBER_HPP |
0 commit comments