Skip to content

Commit

Permalink
feat: introduce thread {set,get}priority api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozg committed Feb 8, 2024
1 parent b18da1c commit adb2fee
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
25 changes: 25 additions & 0 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}
};
Expand Down
23 changes: 23 additions & 0 deletions tests/test-thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit adb2fee

Please sign in to comment.