Motr
M0
|
#include <coroutine.h>
Data Fields | |
void * | mc_stack [M0_MCC_STACK_NR] |
void * | mc_locals [M0_MCC_STACK_NR] |
uint64_t | mc_frame |
bool | mc_yield |
int | mc_co_end_ret |
uint64_t | mc_yield_frame |
struct m0_co_locals_allocator | mc_alloc |
Coroutine context.
Saves current state, including coroutine local variables, control flow data, other state variables w.r.t. restore this state after reentering the stack of functions coroutines are used in.
While working with coroutines, user has to use local on-stack variables with understanding, that after yield their values have to be resorted manually. Instead he has to put M0_CO_REENTER() inside the beginning of every function, and inside every function in every function-stack which is being yielded.
‘Restorable variables’ are used like locals and can be declared inside M0_CO_REENTER(). The following code can be inside one of yielded functions:
By design, ‘restorable variables’ can be accessed with M0_CO_FRAME_DATA(), which can be redefined and used like in the following example:
It can be seen, that user has to maintain ‘struct m0_co_context context’ inside his own structures. For motr code, it's is implied to be related to FOM or FOM objects. m0_co_context_{init,fini}() used for these purposes.
Every function, which potentially can be unwinded during yield has to be wrapped with M0_CO_FUN().
Every place inside underlying functions wrapped by M0_CO_FUN() can be yielded with M0_CO_YIELD(). After this call, function stack is being unwinded up to the place in code where top-level coroutine call is being performed. Our code assumes this place to be near by FOM tick()-function:
User controls execution of coroutine by checking ‘M0_CO_END(&context)’-value. Coroutine is in progress if -EAGAIN is returned, if 0 – it is succeeded.
Definition at line 136 of file coroutine.h.
struct m0_co_locals_allocator mc_alloc |
simple pool allocator for locals
Definition at line 150 of file coroutine.h.
int mc_co_end_ret |
code returned from M0_CO_END() macro, set in M0_CO_YIELD()
Definition at line 146 of file coroutine.h.
uint64_t mc_frame |
current frame pointer
Definition at line 142 of file coroutine.h.
void* mc_locals[M0_MCC_STACK_NR] |
frame locals stack
Definition at line 140 of file coroutine.h.
void* mc_stack[M0_MCC_STACK_NR] |
frame address stack
Definition at line 138 of file coroutine.h.
bool mc_yield |
true if stack is unwinding
Definition at line 144 of file coroutine.h.
uint64_t mc_yield_frame |
current frame pointer during reentering
Definition at line 148 of file coroutine.h.