From 897f5843d61cc2d827b719e27d83d7f9cff3a20e Mon Sep 17 00:00:00 2001 From: ame Date: Sun, 12 May 2024 21:00:33 -0500 Subject: memopry stuff --- src/reg.c | 22 ++++++++++------------ src/thread.c | 46 ++++++++++++++++++++++++++++++++-------------- src/thread.h | 2 ++ src/types/larray.c | 3 ++- src/types/larray.h | 3 ++- 5 files changed, 48 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/reg.c b/src/reg.c index 966537f..417062a 100644 --- a/src/reg.c +++ b/src/reg.c @@ -18,22 +18,20 @@ void sigHandle(int s){ static int lua_exit(lua_State* L){ - sigHandle(0); + lib_thread_clean(); + //sigHandle(0); return 0; } int luaopen_llib(lua_State* L) { - lua_newuserdata(L, sizeof(void*)); - luaL_newmetatable(L, "gc"); - lua_pushstring(L, "__gc"); - lua_pushcfunction(L, &lua_exit); - lua_settable(L, -3); - - lua_setmetatable(L, -2); - lua_setfield(L, LUA_REGISTRYINDEX, "grr"); - signal(SIGTERM, sigHandle); - signal(SIGINT, sigHandle); - + lua_newuserdata(L, 1); + int ud = lua_gettop(L); + lua_newtable(L); + int meta = lua_gettop(L); + luaI_tsetcf(L, meta, "__gc", lua_exit); + lua_pushvalue(L, meta); + lua_setmetatable(L, ud); + //create .array functions lua_newtable(L); diff --git a/src/thread.c b/src/thread.c index 393d3fb..dd4052a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -25,25 +25,39 @@ pthread_mutex_t thread_priority_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZE pthread_mutex_t thread_lock_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; larray_t* thread_locks = NULL; +void lib_thread_clean(){ + if(thread_locks == NULL) return; + + for(int i = 0; i != thread_locks->size; i++){ + if(thread_locks->arr[i].used){ + //pthread_mutex_destroy(thread_locks->arr[i].value); + free(thread_locks->arr[i].value); + } + } + + larray_clear(thread_locks); +} int l_tlock(lua_State* L){ int idx = luaL_checkinteger(L, 1); pthread_mutex_lock(&thread_lock_lock); - pthread_mutex_lock(&thread_priority_lock); - pthread_mutex_unlock(&thread_priority_lock); + //pthread_mutex_lock(&thread_priority_lock); + //pthread_mutex_unlock(&thread_priority_lock); pthread_mutex_t mutex; if(thread_locks == NULL) thread_locks = larray_init(); int i = 0; if((i = larray_geti(thread_locks, idx)) == -1){ pthread_mutex_init(&mutex, NULL); pthread_mutex_lock(&mutex); - larray_set(&thread_locks, idx, (void*)mutex); + pthread_mutex_t* mp = malloc(sizeof * mp); + *mp = mutex; + int id = larray_set(&thread_locks, idx, (void*)mp); } else { - pthread_mutex_t m = (pthread_mutex_t)thread_locks->arr[i].value; + pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value; pthread_mutex_lock(&thread_priority_lock); pthread_mutex_unlock(&thread_lock_lock); - pthread_mutex_lock(&m); + pthread_mutex_lock(m); pthread_mutex_lock(&thread_lock_lock); pthread_mutex_unlock(&thread_priority_lock); @@ -61,8 +75,9 @@ int l_tunlock(lua_State* L){ pthread_mutex_lock(&thread_lock_lock); int i = 0; if(thread_locks != NULL && (i = larray_geti(thread_locks, idx)) != -1){ - pthread_mutex_t m = (pthread_mutex_t)thread_locks->arr[i].value; - pthread_mutex_unlock(&m); + pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value; + + pthread_mutex_unlock(m); thread_locks->arr[i].value = (void*)m; } @@ -145,13 +160,16 @@ int l_clean(lua_State* L){ lua_pushstring(L, "_"); lua_gettable(L, 1); struct thread_info* info = lua_touserdata(L, -1); - if(info->L != NULL){ - lua_gc(info->L, LUA_GCRESTART); - lua_gc(info->L, LUA_GCCOLLECT); - lua_close(info->L); - info->L = NULL; - pthread_mutex_destroy(&info->lock); - free(info); + if(info != NULL && info->L != NULL){ + + lua_gc(info->L, LUA_GCRESTART); + lua_gc(info->L, LUA_GCCOLLECT); + lua_close(info->L); + info->L = NULL; + pthread_mutex_destroy(&info->lock); + free(info); + + luaI_tsetlud(L, 1, "_", NULL); } return 0; } diff --git a/src/thread.h b/src/thread.h index e9ead6d..9dab9b1 100644 --- a/src/thread.h +++ b/src/thread.h @@ -4,6 +4,8 @@ int l_async(lua_State*); int l_tlock(lua_State*); int l_tunlock(lua_State*); +void lib_thread_clean(); + static const luaL_Reg thread_function_list [] = { {"async",l_async}, {"lock",l_tlock}, diff --git a/src/types/larray.c b/src/types/larray.c index 7a5afbc..2e9a5b4 100644 --- a/src/types/larray.c +++ b/src/types/larray.c @@ -29,7 +29,7 @@ void larray_expand(larray_t** _l){ *_l = remade; } -void larray_set(larray_t** _l, uint64_t idx, void* value){ +int larray_set(larray_t** _l, uint64_t idx, void* value){ larray_t* l = *_l; if(l->len + 1 >= l->size){ @@ -51,6 +51,7 @@ void larray_set(larray_t** _l, uint64_t idx, void* value){ l->len++; *_l = l; + return ind; } int larray_geti(larray_t* l, uint64_t idx){ diff --git a/src/types/larray.h b/src/types/larray.h index ff67857..1213758 100644 --- a/src/types/larray.h +++ b/src/types/larray.h @@ -1,4 +1,5 @@ #include +#include struct larray_item { uint64_t idx; @@ -14,7 +15,7 @@ typedef struct { larray_t* larray_initl(int len); larray_t* larray_init(); void larray_expand(larray_t** _l); -void larray_set(larray_t** _l, uint64_t idx, void* value); +int larray_set(larray_t** _l, uint64_t idx, void* value); int larray_geti(larray_t* l, uint64_t idx); void* larray_get(larray_t* l, uint64_t idx); void larray_clear(larray_t* l); -- cgit v1.2.3