diff --git a/CMakeLists.txt b/CMakeLists.txt index 199c9e94..022bc9ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5) project (luv C ASM) set(LUV_VERSION_MAJOR 1) -set(LUV_VERSION_MINOR 47) +set(LUV_VERSION_MINOR 48) set(LUV_VERSION_PATCH 0) set(LUV_VERSION ${LUV_VERSION_MAJOR}.${LUV_VERSION_MINOR}.${LUV_VERSION_PATCH}) diff --git a/deps/libuv b/deps/libuv index be6b81a3..e9f29cb9 160000 --- a/deps/libuv +++ b/deps/libuv @@ -1 +1 @@ -Subproject commit be6b81a352d17513c95be153afcb3148f1a451cd +Subproject commit e9f29cb984231524e3931aa0ae2c5dae1a32884e diff --git a/deps/luajit b/deps/luajit index 29b0b282..0d313b24 160000 --- a/deps/luajit +++ b/deps/luajit @@ -1 +1 @@ -Subproject commit 29b0b282f59ac533313199f4f7be79490b7eee51 +Subproject commit 0d313b243194a0b8d2399d8b549ca5a0ff234db5 diff --git a/docs.md b/docs.md index 6514d648..d82e5011 100644 --- a/docs.md +++ b/docs.md @@ -3407,6 +3407,43 @@ the number to correspond with the table keys used in `uv.thread_getaffinity` and **Returns:** `integer` or `fail` +### `uv.thread_setpriority(thread, priority)` + +> method form `thread:setpriority(priority)` + +**Parameters:** +- `thread`: `luv_thread_t userdata` +- `priority`: ``number` + +Sets the specified thread's scheduling priority setting. It requires elevated +privilege to set specific priorities on some platforms. + +The priority can be set to the following constants. + +- uv.constants.THREAD_PRIORITY_HIGHEST +- uv.constants.THREAD_PRIORITY_ABOVE_NORMAL +- uv.constants.THREAD_PRIORITY_NORMAL +- uv.constants.THREAD_PRIORITY_BELOW_NORMAL +- uv.constants.THREAD_PRIORITY_LOWEST + +**Returns:** `boolean` or `fail` + +### `uv.thread_getpriority(thread) + +> method form `thread:getpriority()` + +**Parameters:** +- `thread`: `luv_thread_t userdata` + +Gets the thread's priority setting. + +Retrieves the scheduling priority of the specified thread. The returned priority +value is platform dependent. + +For Linux, when schedule policy is SCHED_OTHER (default), priority is 0. + +**Returns:** `number` or `fail` + ### `uv.thread_self()` Returns the handle for the thread in which this is called. diff --git a/src/constants.c b/src/constants.c index 0a662cc4..7549aace 100644 --- a/src/constants.c +++ b/src/constants.c @@ -355,6 +355,19 @@ static int luv_constants(lua_State* L) { lua_setfield(L, -2, "TTY_MODE_IO"); #endif +#if LUV_UV_VERSION_GEQ(1, 48, 0) + lua_pushinteger(L, UV_THREAD_PRIORITY_HIGHEST); + lua_setfield(L, -2, "THREAD_PRIORITY_HIGHEST"); + lua_pushinteger(L, UV_THREAD_PRIORITY_ABOVE_NORMAL); + lua_setfield(L, -2, "THREAD_PRIORITY_ABOVE_NORMAL"); + lua_pushinteger(L, UV_THREAD_PRIORITY_NORMAL); + lua_setfield(L, -2, "THREAD_PRIORITY_NORMAL"); + lua_pushinteger(L, UV_THREAD_PRIORITY_BELOW_NORMAL); + lua_setfield(L, -2, "THREAD_PRIORITY_BELOW_NORMAL"); + lua_pushinteger(L, UV_THREAD_PRIORITY_LOWEST); + lua_setfield(L, -2, "THREAD_PRIORITY_LOWEST"); +#endif + return 1; } diff --git a/src/luv.c b/src/luv.c index a55f917c..12bed6b0 100644 --- a/src/luv.c +++ b/src/luv.c @@ -392,6 +392,10 @@ static const luaL_Reg luv_functions[] = { {"thread_setaffinity", luv_thread_setaffinity}, {"thread_getcpu", luv_thread_getcpu}, #endif +#if LUV_UV_VERSION_GEQ(1, 48, 0) + {"thread_getpriority", luv_thread_getpriority}, + {"thread_setpriority", luv_thread_setpriority}, +#endif // work.c {"new_work", luv_new_work}, diff --git a/src/thread.c b/src/thread.c index 7defe35c..7b3f1bc7 100644 --- a/src/thread.c +++ b/src/thread.c @@ -463,6 +463,26 @@ static int luv_thread_getcpu(lua_State* L) { } #endif +#if LUV_UV_VERSION_GEQ(1, 48, 0) +static int luv_thread_getpriority(lua_State* L) { + int priority; + luv_thread_t* tid = luv_check_thread(L, 1); + int ret = uv_thread_getpriority(tid->handle, &priority); + if (ret < 0) return luv_error(L, ret); + lua_pushinteger(L, priority); + return 1; +} + +static int luv_thread_setpriority(lua_State* L) { + luv_thread_t* tid = luv_check_thread(L, 1); + int priority = luaL_checkinteger(L, 2); + int ret = uv_thread_setpriority(tid->handle, priority); + if (ret < 0) return luv_error(L, ret); + lua_pushboolean(L, 1); + return 1; +} +#endif + static int luv_thread_join(lua_State* L) { luv_thread_t* tid = luv_check_thread(L, 1); int ret = uv_thread_join(&tid->handle); @@ -498,6 +518,11 @@ static const luaL_Reg luv_thread_methods[] = { #if LUV_UV_VERSION_GEQ(1, 45, 0) {"getaffinity", luv_thread_getaffinity}, {"setaffinity", luv_thread_setaffinity}, + {"getcpu", luv_thread_getcpu}, +#endif +#if LUV_UV_VERSION_GEQ(1, 48, 0) + {"getpriority", luv_thread_getpriority}, + {"setpriority", luv_thread_setpriority}, #endif {NULL, NULL} }; diff --git a/tests/test-misc.lua b/tests/test-misc.lua index c1811d5b..5ceb2502 100644 --- a/tests/test-misc.lua +++ b/tests/test-misc.lua @@ -184,7 +184,13 @@ return require('lib/tap')(function (test) test("uv constants", function(print, p, expect, uv) assert(type(uv.constants)=='table') for k, v in pairs(uv.constants) do - assert(v >= 0, k) + if k=='THREAD_PRIORITY_LOWEST' then + assert(v == -2) + elseif k=='THREAD_PRIORITY_BELOW_NORMAL' then + assert(v == -1) + else + assert(v >= 0, k) + end end end) diff --git a/tests/test-pipe.lua b/tests/test-pipe.lua index bc553661..45787fec 100644 --- a/tests/test-pipe.lua +++ b/tests/test-pipe.lua @@ -115,7 +115,7 @@ return require('lib/tap')(function (test) if isLinux then assert(server:bind2('\0' .. pipe_name)) local name = server:getsockname() - assert(#name > #pipe_name + 1 ) + assert(#name == #pipe_name + 1 ) assert(name:sub(1, #pipe_name + 1) == '\0' .. pipe_name) pipe_name = name else diff --git a/tests/test-thread.lua b/tests/test-thread.lua index 89a4c317..e7d0ad19 100644 --- a/tests/test-thread.lua +++ b/tests/test-thread.lua @@ -154,4 +154,27 @@ return require('lib/tap')(function (test) end end, mask_size):join() end, "1.45.0") + + test("getpriority, setpriority", function(_, p, _, uv) + assert(type(uv.constants.THREAD_PRIORITY_HIGHEST)=='number') + assert(type(uv.constants.THREAD_PRIORITY_ABOVE_NORMAL)=='number') + assert(type(uv.constants.THREAD_PRIORITY_NORMAL)=='number') + assert(type(uv.constants.THREAD_PRIORITY_BELOW_NORMAL)=='number') + assert(type(uv.constants.THREAD_PRIORITY_LOWEST)=='number') + + local thread = uv.new_thread(function() + local _uv = require('luv') + local self = _uv.thread_self() + local priority = assert(self:getpriority()) + print('priority in thread', priority) + end) + + local priority = assert(thread:getpriority()) + print('default priority', priority) + + assert(thread:setpriority(uv.constants.THREAD_PRIORITY_LOWEST)) + priority = assert(thread:getpriority()) + print('priority after change', priority) + thread:join() + end, "1.48.0") end)