diff options
| author | ame <[email protected]> | 2024-05-07 14:09:48 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-05-07 14:09:48 -0500 |
| commit | 43f85a15884a344238e25c118768d36976ae783a (patch) | |
| tree | 68a9e2f5e2f496fc630d036fd27f2aae9fb2ad23 /src/thread.c | |
| parent | 618263f6f85d08bfd85d4f0376663aa1e2f58eac (diff) | |
more work on threads
Diffstat (limited to 'src/thread.c')
| -rw-r--r-- | src/thread.c | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/src/thread.c b/src/thread.c index 5c06770..25ad500 100644 --- a/src/thread.c +++ b/src/thread.c @@ -6,6 +6,7 @@ #include <unistd.h> #include <pthread.h> #include "types/str.h" +#include "util.h" struct thread_info { str* function; @@ -20,7 +21,7 @@ struct thread_info { int l_res(lua_State* L){ int return_count = lua_gettop(L) - 1; - lua_pushstring(L, "t"); + lua_pushstring(L, "_"); lua_gettable(L, 1); struct thread_info* info = lua_touserdata(L, -1); info->return_count = return_count; @@ -29,7 +30,6 @@ int l_res(lua_State* L){ int ot = lua_gettop(L); lua_pushvalue(L, 2 + i); - l_pprint(L); i_dcopy(L, info->L, NULL); lua_settop(L, ot); @@ -37,11 +37,8 @@ int l_res(lua_State* L){ pthread_mutex_unlock(&info->lock); - pthread_cond_t c = PTHREAD_COND_INITIALIZER; - pthread_mutex_t l = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_lock(&l); - for (;;) pthread_cond_wait(&c, &l); - pthread_mutex_unlock(&l); + pthread_exit(NULL); + p_error("thread did not exit"); return 1; } @@ -50,12 +47,10 @@ void* handle_thread(void* _args){ struct thread_info* args = (struct thread_info*)_args; lua_State* L = args->L; - pthread_mutex_lock(&args->lock); - lua_newtable(L); int res_idx = lua_gettop(L); luaI_tsetcf(L, res_idx, "res", l_res); - luaI_tsetlud(L, res_idx, "t", args); + luaI_tsetlud(L, res_idx, "_", args); luaL_loadbuffer(L, args->function->c, args->function->len, "thread"); str_free(args->function); @@ -63,11 +58,13 @@ void* handle_thread(void* _args){ lua_pushvalue(L, res_idx); lua_call(L, 1, 0); + pthread_mutex_unlock(&args->lock); + return NULL; } int l_await(lua_State* L){ - lua_pushstring(L, "t"); + lua_pushstring(L, "_"); lua_gettable(L, 1); struct thread_info* info = lua_touserdata(L, -1); @@ -92,12 +89,13 @@ int l_async(lua_State* oL){ lua_getglobal(oL, "_G"); i_dcopy(oL, L, NULL); - lua_setglobal(L, "_G"); + lua_set_global_table(L); struct thread_info* args = calloc(1, sizeof * args); args->L = L; - args->cond = PTHREAD_COND_INITIALIZER; - args->lock = PTHREAD_MUTEX_INITIALIZER; + args->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; + args->lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&args->lock); args->return_count = 0; args->function = str_init(""); @@ -109,8 +107,8 @@ int l_async(lua_State* oL){ lua_newtable(oL); int res_idx = lua_gettop(oL); - luaI_tsetcf(oL, res_idx, "res", l_await); - luaI_tsetlud(oL, res_idx, "t", args); + luaI_tsetcf(oL, res_idx, "await", l_await); + luaI_tsetlud(oL, res_idx, "_", args); lua_pushvalue(oL, res_idx); return 1; } |
