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.
This commit is contained in:
bjorn 2024-01-04 12:39:32 -08:00
parent d017ca0fac
commit a4c1d9fce6
5 changed files with 29 additions and 4 deletions

View File

@ -41,6 +41,9 @@ local conf = {
math = {
globals = true
},
thread = {
workers = -1
},
window = {
width = 720,
height = 800,

View File

@ -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

View File

@ -2,6 +2,7 @@
#include "data/blob.h"
#include "event/event.h"
#include "thread/thread.h"
#include "core/os.h"
#include "util.h"
#include <lualib.h>
#include <stdlib.h>
@ -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;
}

View File

@ -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 <math.h>
@ -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));
}

View File

@ -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);