File tree Expand file tree Collapse file tree 5 files changed +44
-11
lines changed
Expand file tree Collapse file tree 5 files changed +44
-11
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,20 @@ typedef struct RunContext {
9292static thread_local RunContext * contextPool ;
9393static thread_local lua_State * workerState ;
9494
95+ static void onWorkerQuit (void ) {
96+ if (workerState ) {
97+ lua_close (workerState );
98+ workerState = NULL ;
99+ }
100+
101+ while (contextPool ) {
102+ RunContext * context = contextPool ;
103+ contextPool = context -> next ;
104+ arr_free (& context -> code );
105+ lovrFree (context );
106+ }
107+ }
108+
95109static bool luax_runlua (void * * arg ) {
96110 RunContext * context = * arg ;
97111 lua_State * L = workerState ;
@@ -282,7 +296,7 @@ int luaopen_lovr_thread(lua_State* L) {
282296 lua_newtable (L );
283297 lua_setfield (L , LUA_REGISTRYINDEX , "_lovrbytecode" );
284298
285- lovrThreadModuleInit (workers );
299+ lovrThreadModuleInit (workers , onWorkerQuit );
286300 luax_atexit (L , lovrThreadModuleDestroy );
287301 return 1 ;
288302}
Original file line number Diff line number Diff line change @@ -16,9 +16,10 @@ static struct {
1616 atomic_uint head ;
1717 atomic_uint tail ;
1818 job jobs [MAX_JOBS ];
19- void (* setupWorker )(uint32_t id );
2019 thrd_t workers [MAX_WORKERS ];
2120 uint32_t workerCount ;
21+ fn_hook * workerInit ;
22+ fn_hook * workerQuit ;
2223 cnd_t hasJob ;
2324 mtx_t lock ;
2425 bool quit ;
@@ -32,8 +33,10 @@ static void runJob(void) {
3233}
3334
3435static int workerLoop (void * arg ) {
35- if (state .setupWorker ) {
36- state .setupWorker ((uint32_t ) (uintptr_t ) arg );
36+ uint32_t id = (uint32_t ) (uintptr_t ) arg ;
37+
38+ if (state .workerInit ) {
39+ state .workerInit (id );
3740 }
3841
3942 for (;;) {
@@ -51,14 +54,20 @@ static int workerLoop(void* arg) {
5154 }
5255
5356 mtx_unlock (& state .lock );
57+
58+ if (state .workerQuit ) {
59+ state .workerQuit (id );
60+ }
61+
5462 return 0 ;
5563}
5664
57- bool job_init (uint32_t count , void ( * setupWorker )( uint32_t id ) ) {
65+ bool job_init (uint32_t count , fn_hook * init , fn_hook * quit ) {
5866 mtx_init (& state .lock , mtx_plain );
5967 cnd_init (& state .hasJob );
6068
61- state .setupWorker = setupWorker ;
69+ state .workerInit = init ;
70+ state .workerQuit = quit ;
6271 if (count > MAX_WORKERS ) count = MAX_WORKERS ;
6372 for (uint32_t i = 0 ; i < count ; i ++ , state .workerCount ++ ) {
6473 if (thrd_create (& state .workers [i ], workerLoop , (void * ) (uintptr_t ) i ) != thrd_success ) {
Original file line number Diff line number Diff line change 44#pragma once
55
66typedef void fn_job (void * arg );
7+ typedef void fn_hook (uint32_t worker );
78
8- bool job_init (uint32_t workerCount , void ( * setupWorker )( uint32_t index ) );
9+ bool job_init (uint32_t workerCount , fn_hook * init , fn_hook * quit );
910void job_destroy (void );
1011bool job_start (fn_job * fn , void * arg );
1112void job_spin (void );
Original file line number Diff line number Diff line change @@ -39,24 +39,33 @@ static atomic_uint ref;
3939
4040static struct {
4141 uint32_t workers ;
42+ void (* onWorkerQuit )(void );
4243 mtx_t channelLock ;
4344 map_t channels ;
4445} state ;
4546
46- static void setupWorker (uint32_t id ) {
47+ static void workerInit (uint32_t id ) {
4748 lovrProfileSetThreadName ("Worker" );
4849 os_thread_set_name ("Worker" );
4950}
5051
51- bool lovrThreadModuleInit (int32_t workers ) {
52+ static void workerQuit (uint32_t id ) {
53+ if (state .onWorkerQuit ) {
54+ state .onWorkerQuit ();
55+ }
56+ }
57+
58+ bool lovrThreadModuleInit (int32_t workers , void (* onWorkerQuit )(void )) {
5259 if (!lovrModuleAcquire (& ref )) return true;
60+
5361 mtx_init (& state .channelLock , mtx_plain );
5462 map_init (& state .channels , 0 );
5563
5664 uint32_t cores = os_get_core_count ();
5765 if (workers < 0 ) workers += cores ;
5866 state .workers = MAX (workers , 0 );
59- job_init (state .workers , setupWorker );
67+ state .onWorkerQuit = onWorkerQuit ;
68+ job_init (state .workers , workerInit , workerQuit );
6069
6170 lovrModuleReady (& ref );
6271 return true;
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ union Variant;
1414typedef struct Thread Thread ;
1515typedef struct Channel Channel ;
1616
17- bool lovrThreadModuleInit (int32_t workers );
17+ bool lovrThreadModuleInit (int32_t workers , void ( * onWorkerQuit )( void ) );
1818void lovrThreadModuleDestroy (void );
1919uint32_t lovrThreadGetWorkerCount (void );
2020struct Channel * lovrThreadGetChannel (const char * name );
You can’t perform that action at this time.
0 commit comments