From a0e9b343216b2062134b86521a74286e10703015 Mon Sep 17 00:00:00 2001 From: ame Date: Fri, 14 Nov 2025 02:06:22 -0600 Subject: sleep and thread fixes --- src/thread.c | 22 ++++++++++++++++++---- src/thread.h | 2 ++ 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/thread.c b/src/thread.c index 8dcfe2a..9b9ddb2 100644 --- a/src/thread.c +++ b/src/thread.c @@ -262,7 +262,12 @@ int _thread_kill(lua_State* L){ lua_gettable(L, 1); struct thread_info* info = lua_touserdata(L, -1); - if(info->tid != 0) pthread_kill(info->tid, SIGUSR1); + if(info->tid != 0){ + pthread_kill(info->tid, SIGUSR1); + pthread_mutex_lock(&*info->close_lock); + pthread_cond_signal(&*info->cond); + pthread_mutex_unlock(&*info->close_lock); + } info->tid = 0; return 0; @@ -359,9 +364,11 @@ int _buffer_set(lua_State* L){ luaI_deepcopy(L, buffer->L, SKIP_LOCALS); pthread_mutex_unlock(&*buffer->lock); - lua_getmetatable(L, 2); - int idx = lua_gettop(L); - luaI_tsetnil(L, idx, "__gc"); + if(lua_type(L, 2) == LUA_TTABLE || lua_type(L, 2) == LUA_TUSERDATA){ + lua_getmetatable(L, 2); + int idx = lua_gettop(L); + luaI_tsetnil(L, idx, "__gc"); + } return 1; } @@ -552,6 +559,13 @@ int l_usleep(lua_State* L){ return 0; } +int l_sleep(lua_State* L){ + double n = lua_tonumber(L, 1); + usleep(n * 1000 * 1000); + + return 0; +} + int l_testcopy(lua_State* L){ lua_State* temp = luaL_newstate(); luaI_deepcopy(L, temp, SKIP_GC); diff --git a/src/thread.h b/src/thread.h index 8061971..5b637d8 100644 --- a/src/thread.h +++ b/src/thread.h @@ -6,6 +6,7 @@ int l_buffer(lua_State*); int l_testcopy(lua_State*); int l_mutex(lua_State*); int l_usleep(lua_State*); +int l_sleep(lua_State*); void lib_thread_clean(); @@ -17,6 +18,7 @@ static const luaL_Reg thread_function_list [] = { {"testcopy",l_testcopy}, {"mutex", l_mutex}, {"usleep", l_usleep}, + {"sleep", l_sleep}, {NULL,NULL} }; -- cgit v1.2.3