From 99b8ba83770da7250fa735b46c3cff40d142c94e Mon Sep 17 00:00:00 2001 From: Konstantin Osipov <kostja.osipov@gmail.com> Date: Wed, 15 Feb 2012 02:03:56 +0400 Subject: [PATCH] Blueprint 'lua-fiber-status': review comments. Update style, add comments. --- core/fiber.m | 6 +++++- core/tarantool_lua.m | 42 +++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/core/fiber.m b/core/fiber.m index c727f9fb29..6dba8ff44b 100644 --- a/core/fiber.m +++ b/core/fiber.m @@ -251,10 +251,14 @@ fiber_yield(void) coro_transfer(&caller->coro.ctx, &callee->coro.ctx); } +/** + * Return true if the current fiber is a callee of fiber f, + * false otherwise. + */ bool 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) { if (f == *sp_ptr) return true; diff --git a/core/tarantool_lua.m b/core/tarantool_lua.m index ca9ae097dd..f6e26c256a 100644 --- a/core/tarantool_lua.m +++ b/core/tarantool_lua.m @@ -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 lbox_fiber_status(struct lua_State *L) { struct fiber *f = lbox_checkfiber(L, 1); + const char *status; if (f->fid == 0) { - /* this fiber is dead */ - lua_pushstring(L, "dead"); - return 1; - } - - if (f == fiber) { - /* the fiber is current running fiber */ - lua_pushstring(L, "running"); - return 1; + /* This fiber is dead. */ + status = "dead"; + } else if (f == fiber) { + /* The fiber is the current running fiber. */ + status = "running"; + } else if (fiber_is_caller(f)) { + /* The fiber is current fiber's caller. */ + status = "normal"; + } else { + /* None of the above: must be suspended. */ + status = "suspended"; } - - 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"); + lua_pushstring(L, status); return 1; } -- GitLab