aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorame <[email protected]>2024-05-07 14:09:48 -0500
committerame <[email protected]>2024-05-07 14:09:48 -0500
commit3200f04de946049e950c6f7fd1e9d4d2f599c9ea (patch)
tree68a9e2f5e2f496fc630d036fd27f2aae9fb2ad23 /src
parenta571dd1ac9fc4d46e0bbc95a3ab88f52ee37b1a1 (diff)
more work on threads
Diffstat (limited to 'src')
-rw-r--r--src/config.c1
-rw-r--r--src/config.h2
-rw-r--r--src/io.c8
-rw-r--r--src/lua.c13
-rw-r--r--src/lua.h3
-rw-r--r--src/net.c3
-rw-r--r--src/thread.c30
7 files changed, 42 insertions, 18 deletions
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 <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;
}