From a4c1d9fce6fd4305cfc6e4177bbefa9c788aacbc Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 4 Jan 2024 12:39:32 -0800 Subject: [PATCH] Thread module initializes job system; Worker count can be set from conf.lua. A negative worker count is relative to the number of cores. -1 is the default. --- etc/boot.lua | 3 +++ etc/nogame/conf.lua | 1 - src/api/l_thread.c | 17 ++++++++++++++++- src/modules/thread/thread.c | 10 +++++++++- src/modules/thread/thread.h | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/etc/boot.lua b/etc/boot.lua index ead585fe..c3218a3f 100644 --- a/etc/boot.lua +++ b/etc/boot.lua @@ -41,6 +41,9 @@ local conf = { math = { globals = true }, + thread = { + workers = -1 + }, window = { width = 720, height = 800, diff --git a/etc/nogame/conf.lua b/etc/nogame/conf.lua index c9e81f29..cea1cc1c 100644 --- a/etc/nogame/conf.lua +++ b/etc/nogame/conf.lua @@ -6,5 +6,4 @@ function lovr.conf(t) t.headset.supersample = true t.modules.audio = false t.modules.physics = false - t.modules.thread = false end diff --git a/src/api/l_thread.c b/src/api/l_thread.c index 7b6c4078..5107e29c 100644 --- a/src/api/l_thread.c +++ b/src/api/l_thread.c @@ -2,6 +2,7 @@ #include "data/blob.h" #include "event/event.h" #include "thread/thread.h" +#include "core/os.h" #include "util.h" #include #include @@ -88,7 +89,21 @@ int luaopen_lovr_thread(lua_State* L) { luax_register(L, lovrThreadModule); luax_registertype(L, Thread); luax_registertype(L, Channel); - lovrThreadModuleInit(); + + int32_t workers = -1; + + luax_pushconf(L); + lua_getfield(L, -1, "thread"); + if (lua_istable(L, -1)) { + lua_getfield(L, -1, "workers"); + if (lua_type(L, -1) == LUA_TNUMBER) { + workers = lua_tointeger(L, -1); + } + lua_pop(L, 1); + } + lua_pop(L, 2); + + lovrThreadModuleInit(workers); luax_atexit(L, lovrThreadModuleDestroy); return 1; } diff --git a/src/modules/thread/thread.c b/src/modules/thread/thread.c index ebfa7d9d..407be602 100644 --- a/src/modules/thread/thread.c +++ b/src/modules/thread/thread.c @@ -1,6 +1,7 @@ #include "thread/thread.h" #include "data/blob.h" #include "event/event.h" +#include "core/job.h" #include "core/os.h" #include "util.h" #include @@ -38,10 +39,16 @@ static struct { map_t channels; } state; -bool lovrThreadModuleInit(void) { +bool lovrThreadModuleInit(int32_t workers) { if (atomic_fetch_add(&state.ref, 1)) return false; mtx_init(&state.channelLock, mtx_plain); map_init(&state.channels, 0); + + uint32_t cores = os_get_core_count(); + if (workers < 0) workers = cores + workers; + workers = MAX(workers, 0); + job_init(workers); + return true; } @@ -54,6 +61,7 @@ void lovrThreadModuleDestroy(void) { } mtx_destroy(&state.channelLock); map_free(&state.channels); + job_destroy(); memset(&state, 0, sizeof(state)); } diff --git a/src/modules/thread/thread.h b/src/modules/thread/thread.h index b39f4d19..302d3204 100644 --- a/src/modules/thread/thread.h +++ b/src/modules/thread/thread.h @@ -14,7 +14,7 @@ struct Variant; typedef struct Thread Thread; typedef struct Channel Channel; -bool lovrThreadModuleInit(void); +bool lovrThreadModuleInit(int32_t workers); void lovrThreadModuleDestroy(void); struct Channel* lovrThreadGetChannel(const char* name);