diff options
| author | ruki <[email protected]> | 2024-03-31 22:17:29 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-31 22:17:29 +0800 |
| commit | 1f2ca3502f9b01da53d76d517f0e2706a8b32364 (patch) | |
| tree | 5937f2b57746ef2e1aa59474193b34d7ce27c2c7 | |
| parent | dee0d869c5630bbbf464be28ca62331be36aff3d (diff) | |
| parent | 454048ab2a488f706801c4cc15a06272a8b005a8 (diff) | |
Merge pull request #4908 from xmake-io/signal
Add signal module
| -rw-r--r-- | core/src/xmake/os/signal.c | 69 | ||||
| -rw-r--r-- | core/src/xmake/os/sleep.c | 7 | ||||
| -rw-r--r-- | tests/modules/signal/sigint.lua | 8 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 9 | ||||
| -rw-r--r-- | xmake/core/base/signal.lua | 46 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/signal.lua | 24 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 4 |
7 files changed, 131 insertions, 36 deletions
diff --git a/core/src/xmake/os/signal.c b/core/src/xmake/os/signal.c index 5d3fa5e3c..066cc7042 100644 --- a/core/src/xmake/os/signal.c +++ b/core/src/xmake/os/signal.c @@ -46,9 +46,15 @@ * types */ typedef enum __xm_os_signal_e { - XM_OS_SIGINT = 1 + XM_OS_SIGINT = 2 }xm_os_signal_e; +typedef enum __xm_os_signal_handler_e { + XM_OS_SIGFUN = 0, + XM_OS_SIGDFL = 1, + XM_OS_SIGIGN = 2, +}xm_os_signal_handler_e; + /* ////////////////////////////////////////////////////////////////////////////////////// * globals */ @@ -103,35 +109,66 @@ tb_int_t xm_os_signal(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); g_lua = lua; + tb_int_t handler = XM_OS_SIGFUN; // check signal handler - if (!lua_isfunction(lua, 2)) + if (lua_isnumber(lua, 2)) + handler = (tb_int_t) luaL_checkinteger(lua, 2); + else if (!lua_isfunction(lua, 2)) return 0; // save signal handler tb_int_t signo = (tb_int_t)luaL_checkinteger(lua, 1); - tb_char_t name[64] = {0}; - tb_snprintf(name, sizeof(name), "_SIGNAL_HANDLER_%d", signo); - lua_pushvalue(lua, 2); - lua_setglobal(lua, name); + if (handler == XM_OS_SIGFUN) + { + tb_char_t name[64] = {0}; + tb_snprintf(name, sizeof(name), "_SIGNAL_HANDLER_%d", signo); + lua_pushvalue(lua, 2); + lua_setglobal(lua, name); + } #if defined(TB_CONFIG_OS_WINDOWS) - if (signo == XM_OS_SIGINT) - SetConsoleCtrlHandler(xm_os_signal_handler, TRUE); -#elif defined(SIGINT) // for checking signal - tb_int_t signo_native = -1; + if (signo != XM_OS_SIGINT) + return 0; + + switch (handler) + { + case XM_OS_SIGFUN: + SetConsoleCtrlHandler(xm_os_signal_handler, TRUE); + break; + case XM_OS_SIGDFL: + SetConsoleCtrlHandler(NULL, FALSE); + break; + case XM_OS_SIGIGN: + SetConsoleCtrlHandler(NULL, TRUE); + break; + default: + break; + } +#elif defined(SIGINT) switch (signo) { case XM_OS_SIGINT: -#ifdef SIGINT - signo_native = SIGINT; -#endif + signo = SIGINT; break; default: - break; + return 0; + } + + switch (handler) + { + case XM_OS_SIGFUN: + signal(signo, xm_os_signal_handler); + break; + case XM_OS_SIGDFL: + signal(signo, SIG_DFL); + break; + case XM_OS_SIGIGN: + signal(signo, SIG_IGN); + break; + default: + break; } - if (signo_native >= 0) - signal(signo_native, xm_os_signal_handler); #endif return 0; } diff --git a/core/src/xmake/os/sleep.c b/core/src/xmake/os/sleep.c index 87d6340a7..e89ebff25 100644 --- a/core/src/xmake/os/sleep.c +++ b/core/src/xmake/os/sleep.c @@ -35,15 +35,8 @@ */ tb_int_t xm_os_sleep(lua_State* lua) { - // check tb_assert_and_check_return_val(lua, 0); - - // get the interval (ms) tb_long_t interval = (tb_long_t)luaL_checklong(lua, 1); - - // sleep it if (interval >= 0) tb_msleep(interval); - - // ok return 0; } diff --git a/tests/modules/signal/sigint.lua b/tests/modules/signal/sigint.lua new file mode 100644 index 000000000..5b4cb03a3 --- /dev/null +++ b/tests/modules/signal/sigint.lua @@ -0,0 +1,8 @@ +import("core.base.signal") + +function main() + signal.register(signal.SIGINT, function (signo) + print("signal.SIGINT(%d)", signo) + end) + io.read() +end diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 836a8991f..2d7023ac4 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -45,7 +45,6 @@ os._getenvs = os._getenvs or os.getenvs os._cpuinfo = os._cpuinfo or os.cpuinfo os._meminfo = os._meminfo or os.meminfo os._readlink = os._readlink or os.readlink -os._signal = os._signal or os.signal -- syserror code os.SYSERR_UNKNOWN = -1 @@ -54,9 +53,6 @@ os.SYSERR_NOT_PERM = 1 os.SYSERR_NOT_FILEDIR = 2 os.SYSERR_NOT_ACCESS = 3 --- signal code -os.SIGINT = 1 - -- copy single file or directory function os._cp(src, dst, rootdir, opt) opt = opt or {} @@ -1419,11 +1415,6 @@ function os.readlink(symlink) return os._readlink(path.absolute(symlink)) end --- register signal handler -function os.signal(signo, handler) - os._signal(signo, handler) -end - -- get the program directory function os.programdir() return xmake._PROGRAM_DIR diff --git a/xmake/core/base/signal.lua b/xmake/core/base/signal.lua new file mode 100644 index 000000000..e281bb1f4 --- /dev/null +++ b/xmake/core/base/signal.lua @@ -0,0 +1,46 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file signal.lua +-- + +-- define module: signal +local signal = signal or {} + +-- load modules +local os = require("base/os") + +-- signal code +signal.SIGINT = 2 + +-- register signal handler +function signal.register(signo, handler) + os.signal(signo, handler) +end + +-- reset signal, SIGDFL +function signal.reset(signo) + os.signal(signo, 1) +end + +-- ignore signal, SIGIGN +function signal.ignore(signo) + os.signal(signo, 2) +end + +-- return module: signal +return signal diff --git a/xmake/core/sandbox/modules/import/core/base/signal.lua b/xmake/core/sandbox/modules/import/core/base/signal.lua new file mode 100644 index 000000000..2cf3fc0bb --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/signal.lua @@ -0,0 +1,24 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file signal.lua +-- + +-- load modules +return require("base/signal") + + diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 5e2a94e92..9004f66ee 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -82,7 +82,6 @@ sandbox_os.projectdir = os.projectdir sandbox_os.projectfile = os.projectfile sandbox_os.getwinsize = os.getwinsize sandbox_os.getpid = os.getpid -sandbox_os.signal = os.signal -- syserror code sandbox_os.SYSERR_UNKNOWN = os.SYSERR_UNKNOWN @@ -91,9 +90,6 @@ sandbox_os.SYSERR_NOT_PERM = os.SYSERR_NOT_PERM sandbox_os.SYSERR_NOT_FILEDIR = os.SYSERR_NOT_FILEDIR sandbox_os.SYSERR_NOT_ACCESS = os.SYSERR_NOT_ACCESS --- signal code -sandbox_os.SIGINT = os.SIGINT - -- copy file or directory function sandbox_os.cp(srcpath, dstpath, opt) assert(srcpath and dstpath) |
