diff options
| author | ame <[email protected]> | 2024-05-03 13:40:55 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-05-03 13:40:55 -0500 |
| commit | a571dd1ac9fc4d46e0bbc95a3ab88f52ee37b1a1 (patch) | |
| tree | dbaffb5e488e0d0cf7f2fcb598ee2db1515aab05 /src | |
| parent | 81b85b557c308ff743b7b83f2b823e508eef54eb (diff) | |
starting threads and deep copy metatables
Diffstat (limited to 'src')
| -rw-r--r-- | src/lua.c | 28 | ||||
| -rw-r--r-- | src/lua.h | 4 | ||||
| -rw-r--r-- | src/reg.c | 2 | ||||
| -rw-r--r-- | src/thread.c | 116 | ||||
| -rw-r--r-- | src/thread.h | 9 |
5 files changed, 157 insertions, 2 deletions
@@ -3,6 +3,7 @@ #include "io.h"
#include <stdlib.h>
#include <string.h>
+#include "lua5.4/lua.h"
#include "types/str.h"
#include "types/parray.h"
@@ -32,6 +33,14 @@ int writer(lua_State *L, const void* p, size_t sz, void* ud){ return 0;
}
+
+/**
+ * @brief copy top element from src to dest
+ *
+ * @param {lua_State*} source
+ * @param {lua_State*} dest
+ * @param {void*} items already visited, use NULL
+*/
void i_dcopy(lua_State* src, lua_State* dest, void* _seen){
parray_t* seen = (parray_t*)_seen;
int wnull = seen == NULL;
@@ -100,6 +109,7 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){ lua_settable(dest, at);
lua_pop(src, 3);
}
+ //printf("done\n");
//lua_settop(dest, at);
break;
case LUA_TFUNCTION:
@@ -119,9 +129,25 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){ //lua_pushvalue(dest, -1);
break;
case LUA_TUSERDATA:
+ case LUA_TLIGHTUSERDATA:
+ lua_pushlightuserdata(dest, lua_touserdata(src, -1));
+ int tidx = lua_gettop(dest);
+
+ if(lua_getmetatable(src, -1)){
+ //int a = lua_gettop(src);
+ //l_pprint(src);
+ //lua_settop(src, a);
+ //printf("**--**\n");
+ i_dcopy(src, dest, seen);
+ //int a = lua_gettop(dest);
+ //l_pprint(dest);
+ //lua_settop(dest, a);
+ lua_setmetatable(dest, tidx);
+ lua_pop(src, 1);
+ }
+ lua_settop(dest, tidx);
//printf("aww\n");
//lua_pushnumber(dest, 8);
- lua_pushlightuserdata(dest, lua_touserdata(src, -1));
break;
default:
printf("%i\n",lua_type(src, -1));
@@ -31,7 +31,9 @@ void i_dcopy(lua_State* src, lua_State* dest, void*); _tset_b(L, Tidx, K, V, lua_pushvalue)
#define luaI_tsetcf(L, Tidx, K, V)\
_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
@@ -5,6 +5,7 @@ #include "io.h"
#include "math.h"
#include "net.h"
+#include "thread.h"
#include <signal.h>
#include <stdlib.h>
@@ -44,6 +45,7 @@ int luaopen_llib(lua_State* L) { lreg("math", math_function_list);
lreg("config", config_function_list);
lreg("net", net_function_list);
+ lreg("thread", thread_function_list);
//make llib global
lua_setglobal(L, "llib");
diff --git a/src/thread.c b/src/thread.c new file mode 100644 index 0000000..5c06770 --- /dev/null +++ b/src/thread.c @@ -0,0 +1,116 @@ +#include "thread.h" +#include "lua5.4/lauxlib.h" +#include "lua5.4/lua.h" +#include "stdint.h" +#include <stdlib.h> +#include <unistd.h> +#include <pthread.h> +#include "types/str.h" + +struct thread_info { + str* function; + lua_State* L; + int return_count; + pthread_t tid; + pthread_cond_t cond; + pthread_mutex_t lock; +}; + +#include "io.h" + +int l_res(lua_State* L){ + int return_count = lua_gettop(L) - 1; + lua_pushstring(L, "t"); + lua_gettable(L, 1); + struct thread_info* info = lua_touserdata(L, -1); + info->return_count = return_count; + + for(int i = info->return_count - 1; i != -1; i--){ + int ot = lua_gettop(L); + + lua_pushvalue(L, 2 + i); + l_pprint(L); + i_dcopy(L, info->L, NULL); + + lua_settop(L, ot); + } + + 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); + + return 1; +} + +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); + + luaL_loadbuffer(L, args->function->c, args->function->len, "thread"); + str_free(args->function); + + lua_pushvalue(L, res_idx); + lua_call(L, 1, 0); + + return NULL; +} + +int l_await(lua_State* L){ + lua_pushstring(L, "t"); + lua_gettable(L, 1); + struct thread_info* info = lua_touserdata(L, -1); + + pthread_mutex_lock(&info->lock); + + for(int i = 0; i != info->return_count; i++){ + int ot = lua_gettop(info->L); + + lua_pushvalue(info->L, ot - info->return_count + i); + i_dcopy(info->L, L, NULL); + + lua_settop(info->L, ot); + } + + return info->return_count; +} + +int l_async(lua_State* oL){ + lua_State* L = luaL_newstate(); + + luaL_openlibs(L); + + lua_getglobal(oL, "_G"); + i_dcopy(oL, L, NULL); + lua_setglobal(L, "_G"); + + struct thread_info* args = calloc(1, sizeof * args); + args->L = L; + args->cond = PTHREAD_COND_INITIALIZER; + args->lock = PTHREAD_MUTEX_INITIALIZER; + args->return_count = 0; + + args->function = str_init(""); + lua_pushvalue(oL, 1); + lua_dump(oL, writer, (void*)args->function, 0); + + pthread_create(&args->tid, NULL, handle_thread, (void*)args); + pthread_detach(args->tid); + + lua_newtable(oL); + int res_idx = lua_gettop(oL); + luaI_tsetcf(oL, res_idx, "res", l_await); + luaI_tsetlud(oL, res_idx, "t", args); + lua_pushvalue(oL, res_idx); + return 1; +} diff --git a/src/thread.h b/src/thread.h new file mode 100644 index 0000000..d25b448 --- /dev/null +++ b/src/thread.h @@ -0,0 +1,9 @@ +#include "lua.h" + +int l_async(lua_State*); + +static const luaL_Reg thread_function_list [] = { + {"async",l_async}, + + {NULL,NULL} +}; |
