From 43f85a15884a344238e25c118768d36976ae783a Mon Sep 17 00:00:00 2001 From: ame Date: Tue, 7 May 2024 14:09:48 -0500 Subject: more work on threads --- src/config.c | 1 + src/config.h | 2 ++ src/io.c | 8 ++++++++ src/lua.c | 13 +++++++++++++ src/lua.h | 3 ++- src/net.c | 3 ++- src/thread.c | 30 ++++++++++++++---------------- 7 files changed, 42 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index 4f6838f..bbbfa19 100644 --- a/src/config.c +++ b/src/config.c @@ -7,6 +7,7 @@ int _max_depth = 2; int _start_nl_at = 3; int _collapse_all = 0; int _collapse_to_memory = 1; +int _print_meta = 0; int _file_malloc_chunk = 512; diff --git a/src/config.h b/src/config.h index 8de4399..74efc15 100644 --- a/src/config.h +++ b/src/config.h @@ -6,6 +6,7 @@ extern int _max_depth; extern int _start_nl_at; extern int _collapse_all; extern int _collapse_to_memory; +extern int _print_meta; extern int _file_malloc_chunk; @@ -21,6 +22,7 @@ static struct str_to_int config_map[] = { {"collapse_all", &_collapse_all}, {"start_nl_at", &_start_nl_at}, {"collapse_to_memory", &_collapse_to_memory}, + {"print_meta", &_print_meta}, {NULL,NULL} }; diff --git a/src/io.c b/src/io.c index 36561d1..06fa535 100644 --- a/src/io.c +++ b/src/io.c @@ -157,6 +157,14 @@ void i_pprint(lua_State* L, int indent, int skip_indent){ else printf(color_gray" : <%s>"color_reset,type); } + if(_print_meta && lua_getmetatable(L, -1) != 0){ + printf(color_lblue" (metatable "color_reset); + int c = lua_gettop(L) - 1; + i_pprint(L,indent+1,1); + printf(color_lblue")"color_reset); + lua_settop(L, c); + } + } int l_pprint(lua_State* L){ diff --git a/src/lua.c b/src/lua.c index 80f0a3d..868d253 100644 --- a/src/lua.c +++ b/src/lua.c @@ -158,3 +158,16 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){ //lua_settop(src, old_top); _seen = seen; } + +/** + * @brief extracts a table to be global + * + * @param {lua_State*} source +*/ +void lua_set_global_table(lua_State* L){ + lua_pushnil(L); + for(;lua_next(L, -2) != 0;){ + lua_setglobal(L, lua_tostring(L, -2)); + } +} + diff --git a/src/lua.h b/src/lua.h index 3686285..bd0da87 100644 --- a/src/lua.h +++ b/src/lua.h @@ -7,6 +7,7 @@ void* __malloc_(size_t); void __free_(void*); void i_dcopy(lua_State* src, lua_State* dest, void*); +void lua_set_global_table(lua_State*); //generic macro that takes other macros (see below) #define _tset_b(L, Tidx, K, V, F)\ @@ -33,7 +34,7 @@ void i_dcopy(lua_State* src, lua_State* dest, void*); _tset_b(L, Tidx, K, V, lua_pushcfunction) #define luaI_tsetlud(L, Tidx, K, V)\ _tset_b(L, Tidx, K, V, lua_pushlightuserdata) - + int writer(lua_State*, const void*, size_t, void*); #if LUA_VERSION_NUM == 504 diff --git a/src/net.c b/src/net.c index 14882f8..2898f0e 100644 --- a/src/net.c +++ b/src/net.c @@ -759,7 +759,8 @@ void* handle_client(void *_arg){ //time_end("copy", copy) lua_settop(args->L, old_top); //l_pprint(L); - lua_setglobal(L, "_G"); + //lua_setglobal(L, "_G"); + lua_set_global_table(L); pthread_mutex_unlock(&mutex); //printf("start: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); //read full request 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 #include #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; } -- cgit v1.2.3