Skip to content
Snippets Groups Projects
Commit 99b8ba83 authored by Konstantin Osipov's avatar Konstantin Osipov
Browse files

Blueprint 'lua-fiber-status': review comments.

Update style, add comments.
parent f8bdad7b
No related branches found
No related tags found
No related merge requests found
...@@ -251,10 +251,14 @@ fiber_yield(void) ...@@ -251,10 +251,14 @@ fiber_yield(void)
coro_transfer(&caller->coro.ctx, &callee->coro.ctx); coro_transfer(&caller->coro.ctx, &callee->coro.ctx);
} }
/**
* Return true if the current fiber is a callee of fiber f,
* false otherwise.
*/
bool bool
fiber_is_caller(struct fiber *f) fiber_is_caller(struct fiber *f)
{ {
/* 'unwinding' fiber's stack */ /* 'Unwinding' the fiber stack. */
for (struct fiber **sp_ptr = sp; sp_ptr > call_stack; --sp_ptr) { for (struct fiber **sp_ptr = sp; sp_ptr > call_stack; --sp_ptr) {
if (f == *sp_ptr) if (f == *sp_ptr)
return true; return true;
......
...@@ -518,32 +518,36 @@ lbox_fiber_yield(struct lua_State *L) ...@@ -518,32 +518,36 @@ lbox_fiber_yield(struct lua_State *L)
} }
/** /**
* Get fiber status * Get fiber status.
* This follows the rules of Lua coroutine.status() function:
* Returns the status of fibier, as a string:
* - "running", if the fiber is running (that is, it called status);
* - "suspended", if the fiber is suspended in a call to yield(),
* or if it has not started running yet;
* - "normal" if the fiber is active but not running (that is,
* it has resumed another fiber);
* - "dead" if the fiber has finished its body function, or if it
* has stopped with an error.
*/ */
static int static int
lbox_fiber_status(struct lua_State *L) lbox_fiber_status(struct lua_State *L)
{ {
struct fiber *f = lbox_checkfiber(L, 1); struct fiber *f = lbox_checkfiber(L, 1);
const char *status;
if (f->fid == 0) { if (f->fid == 0) {
/* this fiber is dead */ /* This fiber is dead. */
lua_pushstring(L, "dead"); status = "dead";
return 1; } else if (f == fiber) {
} /* The fiber is the current running fiber. */
status = "running";
if (f == fiber) { } else if (fiber_is_caller(f)) {
/* the fiber is current running fiber */ /* The fiber is current fiber's caller. */
lua_pushstring(L, "running"); status = "normal";
return 1; } else {
/* None of the above: must be suspended. */
status = "suspended";
} }
lua_pushstring(L, status);
if (fiber_is_caller(f)) {
/* the fiber is current fiber caller */
lua_pushstring(L, "normal");
return 1;
}
/* the fiber is sleeping */
lua_pushstring(L, "suspended");
return 1; return 1;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment