mirror of https://github.com/bjornbytes/lovr.git
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:
parent
d017ca0fac
commit
a4c1d9fce6
|
@ -41,6 +41,9 @@ local conf = {
|
|||
math = {
|
||||
globals = true
|
||||
},
|
||||
thread = {
|
||||
workers = -1
|
||||
},
|
||||
window = {
|
||||
width = 720,
|
||||
height = 800,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue