Motr  M0
Coroutine

Data Structures

struct  m0_co_la_item
 
struct  m0_co_locals_allocator
 
struct  m0_co_context
 
struct  m0_co_op
 

Macros

#define M0_TRACE_SUBSYSTEM   M0_TRACE_SUBSYS_LIB
 
#define M0_CO_START(context)
 
#define M0_CO_END(context)
 
#define M0_CO_FUN(context, function)
 
#define M0_CO_FRAME_DATA(field)   (__frame_data__->field)
 
#define M0_CO_REENTER(context, ...)
 
#define M0_CO__REENTER(context, frame_data)
 
#define M0_CO_YIELD_RC(context, rc)
 
#define M0_CO_YIELD(context)   M0_CO_YIELD_RC(context, -EAGAIN)
 

Enumerations

enum  m0_co_op_state { COR_INVALID, COR_INIT, COR_ACTIVE, COR_DONE }
 
enum  {
  M0_MCC_STACK_NR = 0x20, M0_MCC_LOCALS_ALLOC_SZ = 4096, M0_MCC_LOCALS_ALLOC_SHIFT = 3, M0_MCC_LOCALS_ALLOC_ALIGN = 1ULL << M0_MCC_LOCALS_ALLOC_SHIFT,
  M0_MCC_LOCALS_ALLOC_PAD_SZ = 2 * M0_MCC_LOCALS_ALLOC_ALIGN
}
 

Functions

static int locals_alloc_init (struct m0_co_locals_allocator *alloc)
 
static void locals_alloc_fini (struct m0_co_locals_allocator *alloc)
 
static void * locals_alloc (struct m0_co_locals_allocator *alloc, uint64_t frame, uint64_t size)
 
static void locals_free (struct m0_co_locals_allocator *alloc, uint64_t frame)
 
M0_INTERNAL void m0_co_context_locals_alloc (struct m0_co_context *context, uint64_t size)
 
M0_INTERNAL void m0_co_context_locals_free (struct m0_co_context *context)
 
M0_INTERNAL void * m0_co_context_locals (struct m0_co_context *context)
 
M0_INTERNAL int m0_co_context_init (struct m0_co_context *context)
 
M0_INTERNAL void m0_co_context_fini (struct m0_co_context *context)
 
M0_INTERNAL void m0_co_op_init (struct m0_co_op *op)
 
M0_INTERNAL void m0_co_op_fini (struct m0_co_op *op)
 
M0_INTERNAL void m0_co_op_reset (struct m0_co_op *op)
 
M0_INTERNAL void m0_co_op_active (struct m0_co_op *op)
 
M0_INTERNAL void m0_co_op_done (struct m0_co_op *op)
 
M0_INTERNAL int m0_co_op_tick_ret (struct m0_co_op *op, struct m0_fom *fom, int next_state)
 

Variables

enum m0_co_op_state M0_XCA_DOMAIN
 
static struct m0_sm_state_descr co_states []
 
static struct m0_sm_trans_descr co_trans []
 
M0_INTERNAL struct m0_sm_conf co_states_conf
 

Detailed Description

Macro Definition Documentation

◆ M0_CO__REENTER

#define M0_CO__REENTER (   context,
  frame_data 
)
Value:
({ \
uint64_t size = sizeof(*frame_data); \
M0_LOG(M0_DEBUG, "M0_CO_REENTER: context=%p yeild=%d", \
context, !!context->mc_yield); \
if (!context->mc_yield) { \
m0_co_context_locals_alloc(context, (size)); \
frame_data = m0_co_context_locals(context); \
} else { \
M0_ASSERT(context->mc_yield_frame < M0_MCC_STACK_NR); \
frame_data = m0_co_context_locals(context); \
goto *context->mc_stack[context->mc_yield_frame++]; \
} \
})
M0_INTERNAL void * m0_co_context_locals(struct m0_co_context *context)
Definition: coroutine.c:124
m0_bcount_t size
Definition: di.c:39

Mostly for internal usage.

See also
M0_CO_REENTER() Typical usecase: struct foo_context { int a; ... int rc; }; struct foo_context *data; M0_CO__REENTER(context, data);

Definition at line 223 of file coroutine.h.

◆ M0_CO_END

#define M0_CO_END (   context)
Value:
({ \
int rc = ((context)->mc_yield ? (context)->mc_co_end_ret : 0); \
if (rc == 0) { \
M0_ASSERT((context)->mc_frame == 0); \
M0_ASSERT((context)->mc_yield_frame == 0); \
m0_co_context_locals_free((context)); \
} \
rc; \
})
int32_t rc
Definition: trigger_fop.h:47
Parameters
context
See also
m0_co_context
Returns
-EAGAIN if coroutine is in progress.
0 if coroutine is succeeded.

Definition at line 167 of file coroutine.h.

◆ M0_CO_FRAME_DATA

#define M0_CO_FRAME_DATA (   field)    (__frame_data__->field)

Definition at line 199 of file coroutine.h.

◆ M0_CO_FUN

#define M0_CO_FUN (   context,
  function 
)
Value:
({ \
__label__ save; \
M0_LOG(M0_DEBUG, "M0_CO_FUN: context=%p yeild=%d", \
context, !!context->mc_yield); \
M0_ASSERT(context->mc_frame < M0_MCC_STACK_NR); \
context->mc_stack[context->mc_frame++] = &&save; \
save: (function); \
if (context->mc_yield) { \
return; \
} else { \
m0_co_context_locals_free(context); \
context->mc_frame--; \
} \
})
Parameters
_context
See also
m0_co_context
Parameters
function– function call with or without assignments F(rc) = fooX(context, ...)

Definition at line 183 of file coroutine.h.

◆ M0_CO_REENTER

#define M0_CO_REENTER (   context,
  ... 
)
Value:
struct foo_context { \
__VA_ARGS__ \
}; \
struct foo_context *__frame_data__; \
M0_CO__REENTER((context), __frame_data__);
Parameters
_context
See also
m0_co_context
Parameters
__VA_ARGS__– ‘Restorable variables’, local context of this function

Definition at line 205 of file coroutine.h.

◆ M0_CO_START

#define M0_CO_START (   context)
Value:
({ \
M0_ASSERT((context)->mc_yield_frame == 0); \
})

M0_CO_START()/M0_CO_END() wrap coroutine call and provide means to control the control flow of it.

Definition at line 157 of file coroutine.h.

◆ M0_CO_YIELD

#define M0_CO_YIELD (   context)    M0_CO_YIELD_RC(context, -EAGAIN)

Definition at line 266 of file coroutine.h.

◆ M0_CO_YIELD_RC

#define M0_CO_YIELD_RC (   context,
  rc 
)
Value:
({ \
__label__ save; \
M0_LOG(M0_DEBUG, "M0_CO_YIELD: context=%p yeild=%d", \
context, !!context->mc_yield); \
context->mc_yield = true; \
context->mc_co_end_ret = (rc); \
M0_ASSERT(context->mc_frame < M0_MCC_STACK_NR); \
context->mc_stack[context->mc_frame++] = &&save; \
return; \
save: \
M0_ASSERT(context->mc_yield); \
M0_ASSERT(context->mc_frame == context->mc_yield_frame); \
context->mc_yield = false; \
context->mc_yield_frame = 0; \
context->mc_frame--; \
})
int32_t rc
Definition: trigger_fop.h:47

M0_CO_YIELD() is used like return statement in cases, function needs to wait for some external event, like IO. Typically, user has to arm something, which will generete such an event and call M0_CO_YIELD(). On behalf of FOM, event has to wake up this FOM when it's ready or in any other suitable case. After this point M0_CO_* machinery will return control flow back into the point right after M0_CO_YIELD().

Parameters
_context
See also
m0_co_context

Definition at line 248 of file coroutine.h.

◆ M0_TRACE_SUBSYSTEM

#define M0_TRACE_SUBSYSTEM   M0_TRACE_SUBSYS_LIB

Definition at line 29 of file coroutine.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
M0_MCC_STACK_NR 
M0_MCC_LOCALS_ALLOC_SZ 
M0_MCC_LOCALS_ALLOC_SHIFT 
M0_MCC_LOCALS_ALLOC_ALIGN 
M0_MCC_LOCALS_ALLOC_PAD_SZ 

Definition at line 37 of file coroutine.h.

◆ m0_co_op_state

Enumerator
COR_INVALID 
COR_INIT 
COR_ACTIVE 
COR_DONE 

Definition at line 141 of file coroutine.c.

Function Documentation

◆ locals_alloc()

static void* locals_alloc ( struct m0_co_locals_allocator alloc,
uint64_t  frame,
uint64_t  size 
)
static

Definition at line 51 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ locals_alloc_fini()

static void locals_alloc_fini ( struct m0_co_locals_allocator alloc)
static

Definition at line 44 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ locals_alloc_init()

static int locals_alloc_init ( struct m0_co_locals_allocator alloc)
static

Definition at line 36 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ locals_free()

static void locals_free ( struct m0_co_locals_allocator alloc,
uint64_t  frame 
)
static

Definition at line 89 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_context_fini()

M0_INTERNAL void m0_co_context_fini ( struct m0_co_context context)

Definition at line 136 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_context_init()

M0_INTERNAL int m0_co_context_init ( struct m0_co_context context)

Definition at line 130 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_context_locals()

M0_INTERNAL void * m0_co_context_locals ( struct m0_co_context context)

Definition at line 124 of file coroutine.c.

◆ m0_co_context_locals_alloc()

M0_INTERNAL void m0_co_context_locals_alloc ( struct m0_co_context context,
uint64_t  size 
)

Definition at line 111 of file coroutine.c.

Here is the call graph for this function:

◆ m0_co_context_locals_free()

M0_INTERNAL void m0_co_context_locals_free ( struct m0_co_context context)

Definition at line 118 of file coroutine.c.

Here is the call graph for this function:

◆ m0_co_op_active()

M0_INTERNAL void m0_co_op_active ( struct m0_co_op op)

Definition at line 210 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_op_done()

M0_INTERNAL void m0_co_op_done ( struct m0_co_op op)

Definition at line 218 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_op_fini()

M0_INTERNAL void m0_co_op_fini ( struct m0_co_op op)

Definition at line 188 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_op_init()

M0_INTERNAL void m0_co_op_init ( struct m0_co_op op)

Definition at line 179 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_op_reset()

M0_INTERNAL void m0_co_op_reset ( struct m0_co_op op)

Definition at line 203 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ m0_co_op_tick_ret()

M0_INTERNAL int m0_co_op_tick_ret ( struct m0_co_op op,
struct m0_fom fom,
int  next_state 
)

Definition at line 226 of file coroutine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ co_states

struct m0_sm_state_descr co_states[]
static
Initial value:
= {
[COR_INIT] = {
.sd_flags = M0_SDF_INITIAL,
.sd_name = "COR_INIT",
.sd_allowed = M0_BITS(COR_ACTIVE),
},
[COR_ACTIVE] = {
.sd_flags = 0,
.sd_name = "COR_ACTIVE",
.sd_allowed = M0_BITS(COR_DONE),
},
[COR_DONE] = {
.sd_flags = M0_SDF_TERMINAL,
.sd_name = "COR_DONE",
.sd_allowed = 0,
},
}
#define M0_BITS(...)
Definition: misc.h:236

Definition at line 148 of file coroutine.c.

◆ co_states_conf

M0_INTERNAL struct m0_sm_conf co_states_conf
Initial value:
= {
.scf_name = "m0_co_op::co_sm",
.scf_nr_states = ARRAY_SIZE(co_states),
.scf_state = co_states,
.scf_trans_nr = ARRAY_SIZE(co_trans),
.scf_trans = co_trans
}
static struct m0_sm_trans_descr co_trans[]
Definition: coroutine.c:166
static struct m0_sm_state_descr co_states[]
Definition: coroutine.c:148
#define ARRAY_SIZE(a)
Definition: misc.h:45

Definition at line 171 of file coroutine.c.

◆ co_trans

struct m0_sm_trans_descr co_trans[]
static
Initial value:
= {
{ "started", COR_INIT, COR_ACTIVE },
{ "completed", COR_ACTIVE, COR_DONE },
}

Definition at line 166 of file coroutine.c.

◆ M0_XCA_DOMAIN

enum m0_co_op_state M0_XCA_DOMAIN