Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds uv.loop_mode function #517

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ uv.loop_configure("block_signal", "sigprof")
**Note:** Be prepared to handle the `ENOSYS` error; it means the loop option is
not supported by the platform.

### `uv.loop_mode()`

If the loop is running, returns a string indicating the mode in use. If the loop
is not running, `nil` is returned instead.

**Returns:** `string` or `nil`

### `uv.loop_alive()`

Returns `true` if there are referenced active handles, active requests, or
Expand Down
15 changes: 14 additions & 1 deletion src/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,25 @@ static const char *const luv_runmodes[] = {

static int luv_run(lua_State* L) {
int mode = luaL_checkoption(L, 1, "default", luv_runmodes);
int ret = uv_run(luv_loop(L), (uv_run_mode)mode);
luv_ctx_t* ctx = luv_context(L);
ctx->mode = mode;
int ret = uv_run(ctx->loop, (uv_run_mode)mode);
ctx->mode = -1;
if (ret < 0) return luv_error(L, ret);
lua_pushboolean(L, ret);
return 1;
}

static int luv_loop_mode(lua_State* L) {
luv_ctx_t* ctx = luv_context(L);
if (ctx->mode == -1) {
lua_pushnil(L);
} else {
lua_pushstring(L, luv_runmodes[ctx->mode]);
}
return 1;
}

static int luv_loop_alive(lua_State* L) {
int ret = uv_loop_alive(luv_loop(L));
if (ret < 0) return luv_error(L, ret);
Expand Down
3 changes: 3 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static const luaL_Reg luv_functions[] = {
// loop.c
{"loop_close", luv_loop_close},
{"run", luv_run},
{"loop_mode", luv_loop_mode},
{"loop_alive", luv_loop_alive},
{"stop", luv_stop},
{"backend_fd", luv_backend_fd},
Expand Down Expand Up @@ -716,6 +717,7 @@ LUALIB_API void luv_set_loop(lua_State* L, uv_loop_t* loop) {

ctx->loop = loop;
ctx->L = L;
ctx->mode = -1;
}

// Set an external event callback routine, before luaopen_luv
Expand Down Expand Up @@ -775,6 +777,7 @@ LUALIB_API int luaopen_luv (lua_State* L) {

ctx->loop = loop;
ctx->L = L;
ctx->mode = -1;

ret = uv_loop_init(loop);
if (ret < 0) {
Expand Down
1 change: 1 addition & 0 deletions src/luv.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef struct {
uv_loop_t* loop; /* main loop */
lua_State* L; /* main thread,ensure coroutines works */
luv_CFpcall pcall; /* luv event callback function in protected mode */
int mode; /* the mode used to run the loop (-1 if not running) */

void* extra; /* extra data */
} luv_ctx_t;
Expand Down
13 changes: 13 additions & 0 deletions tests/test-loop.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return require('lib/tap')(function (test)

test("uv.loop_mode", function (print, p, expect, uv)
assert(uv.loop_mode() == nil)
local timer = uv.new_timer()
uv.timer_start(timer, 100, 0, expect(function ()
assert(uv.loop_mode() == "default")
uv.timer_stop(timer)
uv.close(timer)
end))
end)

end)