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 | 3200f04de946049e950c6f7fd1e9d4d2f599c9ea (patch) | |
| tree | 68a9e2f5e2f496fc630d036fd27f2aae9fb2ad23 | |
| parent | a571dd1ac9fc4d46e0bbc95a3ab88f52ee37b1a1 (diff) | |
more work on threads
| -rw-r--r-- | docs/thread.md | 36 | ||||
| -rw-r--r-- | src/config.c | 1 | ||||
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/io.c | 8 | ||||
| -rw-r--r-- | src/lua.c | 13 | ||||
| -rw-r--r-- | src/lua.h | 3 | ||||
| -rw-r--r-- | src/net.c | 3 | ||||
| -rw-r--r-- | src/thread.c | 30 | ||||
| -rw-r--r-- | tests/h.lua | 22 |
9 files changed, 76 insertions, 42 deletions
diff --git a/docs/thread.md b/docs/thread.md index aa34f5c..32df9c9 100644 --- a/docs/thread.md +++ b/docs/thread.md @@ -34,18 +34,23 @@ t:await() these have the same backend (and limitations) of network threads ```lua -local thread = llib.thread.async(function(info) +local thread = llib.thread.async(function(res, rej) local N = 0 ... - return N + res(N) end) ``` ### thread function parameters ** -as used with "info" above +as used with "res" above + +#### res:res() ** -#### info:send() ** +'takes any amount of "any" values + +send a value(s) to thread:await() call then stalls the thread until cleaned +#### res:send() ** 'takes "any" value @@ -56,22 +61,29 @@ info:send(5) info:send("hello") ``` -#### info:pause() ** - -pauses the thread (must be resumed from outside) - ### thread return object ** #### thread:await() ** -'optional timeout +'optional timeout in ms and boolean whether to keep or not -waits for the thread to return, and returns whatever it returned, if timeout is exceeded nil +waits for the thread to return, and returns whatever it returned then closes it, or nil if timeout was exceeded +if the input is the boolean value true, it will keep the thread alive (otherwise await() can not be called again) ```lua thread:await() -- value of N (above) ``` +```lua +thread:await(20) -- value of N (above) or nil +``` + +```lua +thread:await(true) -- value of N (above) +thread:await() -- same +thread:await() -- error, function above performed cleanup +``` + #### thread:next() ** gets the most oldest value sent using info:send() and pops it @@ -85,7 +97,3 @@ thread:next() -- "hello" #### thread:kill() ** kills the thread - -#### thread:pause() thread:resume() ** - -stops or continues the thread 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} }; @@ -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){ @@ -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));
+ }
+}
+
@@ -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
@@ -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 <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; } diff --git a/tests/h.lua b/tests/h.lua index 0a0cfdd..1472c89 100644 --- a/tests/h.lua +++ b/tests/h.lua @@ -1,16 +1,18 @@ require "llib" -local th = llib.thread.async(function (res) - _G.llib.io.pprint(res) - _G.llib.io.pprint({aa=45}) - res:res(_G.llib.crypto.md5()) +local thread_a = llib.thread.async(function (res) + --os.execute("sleep 1") + --print((_G.ll + "hi"):final()) + res:res(_G.llib.crypto.md5("meow")) print("after") end) -os.execute("sleep 1") - -print("out:") -a = th:res(); -print((a + "hi"):final()) +local thread_b = llib.thread.async(function (res) + --os.execute("sleep 1") + --print((_G.ll + "hi"):final()) + res:res(_G.llib.crypto.sha512("meow")) + print("after") +end) -while true do end
\ No newline at end of file +print(thread_a:await()) +print(thread_b:await())
\ No newline at end of file |
