diff options
| -rw-r--r-- | library/lullaby/thread.lua | 4 | ||||
| -rw-r--r-- | src/thread.c | 22 | ||||
| -rw-r--r-- | src/thread.h | 2 | ||||
| -rw-r--r-- | tests/units/thread-infinite.lua | 23 |
4 files changed, 47 insertions, 4 deletions
diff --git a/library/lullaby/thread.lua b/library/lullaby/thread.lua index 41444ff..43e897d 100644 --- a/library/lullaby/thread.lua +++ b/library/lullaby/thread.lua @@ -109,4 +109,8 @@ function thread.mutex() end ---@param N integer function thread.usleep(N) end +---puts the thread to sleep for N seconds +---@param N number +function thread.sleep(N) end + return thread 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}
};
diff --git a/tests/units/thread-infinite.lua b/tests/units/thread-infinite.lua new file mode 100644 index 0000000..b3183f9 --- /dev/null +++ b/tests/units/thread-infinite.lua @@ -0,0 +1,23 @@ +local pass = llby.thread.buffer(true) + +local th = llby.thread.async(function(res) + while true do + res:testclose() + end +end) + +local readyb = llby.thread.buffer(false) + +local th2 = llby.thread.async(function(res) + llby.thread.usleep(1000 * 1000) + if not readyb:get() then + pass:set(false) + th:kill() + end +end) + +th:close() +readyb:set(true) +th2:await() + +return pass:get() |
