From 0a04e02d3c16c04260c7e78f848776321c2ef1f0 Mon Sep 17 00:00:00 2001 From: SinisterRectus Date: Mon, 12 Oct 2020 15:30:03 -0400 Subject: [PATCH 1/2] Adds uv.loop_mode function --- docs.md | 7 +++++++ src/loop.c | 15 ++++++++++++++- src/luv.c | 3 +++ src/luv.h | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs.md b/docs.md index edc069c1..4f01f56e 100644 --- a/docs.md +++ b/docs.md @@ -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 diff --git a/src/loop.c b/src/loop.c index e30cf41c..1cf393ce 100644 --- a/src/loop.c +++ b/src/loop.c @@ -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); diff --git a/src/luv.c b/src/luv.c index b83ec6b9..3c9cda6c 100644 --- a/src/luv.c +++ b/src/luv.c @@ -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}, @@ -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 @@ -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) { diff --git a/src/luv.h b/src/luv.h index 87f2a06b..25d0b20f 100644 --- a/src/luv.h +++ b/src/luv.h @@ -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; From 3628d05c7ff0ffe514d6279009fd7a32a85f1ac4 Mon Sep 17 00:00:00 2001 From: SinisterRectus Date: Mon, 12 Oct 2020 15:53:38 -0400 Subject: [PATCH 2/2] Adds loop test --- tests/test-loop.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/test-loop.lua diff --git a/tests/test-loop.lua b/tests/test-loop.lua new file mode 100644 index 00000000..f0a4504c --- /dev/null +++ b/tests/test-loop.lua @@ -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)