From 463325d637250e13dbc3153ce59048cf9be57bdb Mon Sep 17 00:00:00 2001 From: ame Date: Sat, 10 Aug 2024 23:48:17 -0500 Subject: malloc mutex_t --- src/lua.c | 10 ++++++---- src/net.c | 44 +++++++++++++++++++++++++++++++++++++------- src/thread.c | 52 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 77 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/lua.c b/src/lua.c index 5e083cc..238e1b8 100644 --- a/src/lua.c +++ b/src/lua.c @@ -53,8 +53,9 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ double n; int old_top = lua_gettop(src); int modi = 0; - - switch(lua_type(src, -1)){ + + int type; + switch(type = lua_type(src, -1)){ case LUA_TNUMBER: n = lua_tonumber(src, -1); if(n == (int)n) lua_pushinteger(dest, (int)n); @@ -123,7 +124,7 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ str* awa = str_init(""); lua_dump(src, writer, (void*)awa, 0); - luaL_loadbuffer(dest, awa->c, awa->len, awa->c); + luaL_loadbuffer(dest, awa->c, awa->len, "fun"); //lua_remove(dest, -2); str_free(awa); break; @@ -138,7 +139,8 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ lua_pushlightuserdata(dest, lua_touserdata(src, -1)); break; default: - printf("unknown type %i\n",lua_type(src, -1)); + printf("unknown type %i vs (old)%i\n",lua_type(src, -1), type); + abort(); lua_pushnumber(dest, 5); break; } diff --git a/src/net.c b/src/net.c index d87f5f8..5e4e906 100644 --- a/src/net.c +++ b/src/net.c @@ -38,6 +38,7 @@ struct sarray_t { }; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t con_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER; /** @@ -744,8 +745,11 @@ void* handle_client(void *_arg){ char* buffer; char dummy[2] = {0, 0}; int header_eof; + lua_State* L = args->L; //sleep(1); //create state for this thread + + /* lua_State* L = luaL_newstate(); luaL_openlibs(L); @@ -755,14 +759,17 @@ void* handle_client(void *_arg){ lua_getglobal(args->L, "_G"); //time_start(copy) - luaI_deepcopy(args->L, L, 0); + luaI_deepcopy(args->L, L, SKIP_GC); //time_end("copy", copy) lua_settop(args->L, old_top); + pthread_mutex_unlock(&con_mutex); + //l_pprint(L); //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 //time_start(recv) @@ -892,8 +899,6 @@ void* handle_client(void *_arg){ luaI_tsetv(L, res_idx, "header", header_idx); - //printf("wrote table: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); - //the function(s) //get all function that kinda match parray_t* owo = (parray_t*)v; for(int i = 0; i != owo->len; i++){ @@ -903,6 +908,7 @@ void* handle_client(void *_arg){ for(int z = 0; z != awa->len; z++){ char* path; struct lchar* wowa = awa->cs[z]; + //if request is HEAD, it is valid for GET and HEAD listeners if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, sR->c) == 0 || (strcmp(sR->c, "HEAD") && strcmp(wowa->req, "GET"))){ @@ -920,7 +926,7 @@ void* handle_client(void *_arg){ } } parray_lclear(owo); //dont free the rest - //printf("out: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); client_fd = luaL_checkinteger(L, -1); @@ -942,7 +948,6 @@ void* handle_client(void *_arg){ free(file_cont); } - //printf("end anyways\n"); parray_clear(table, STR); } @@ -950,7 +955,7 @@ void* handle_client(void *_arg){ close(client_fd); free(args); free(buffer); - //lua_close(L); + lua_close(L); //printf("closed: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); pthread_mutex_lock(&mutex); threads--; @@ -992,6 +997,9 @@ int start_serv(lua_State* L, int port){ if (pthread_mutex_init(&mutex, NULL) != 0) p_fatal("mutex init failed\n"); + if (pthread_mutex_init(&con_mutex, NULL) != 0) + p_fatal("con_mutex init failed\n"); + for(;;){ struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); @@ -1006,17 +1014,39 @@ int start_serv(lua_State* L, int port){ args->fd = *client_fd; args->port = port; args->cli = client_addr; - args->L = L; + args->L = luaL_newstate(); + + luaL_openlibs(args->L); + + pthread_mutex_lock(&mutex); + int old_top = lua_gettop(L); + lua_getglobal(L, "_G"); + + //time_start(copy) + luaI_deepcopy(L, args->L, SKIP_GC); + + //time_end("copy", copy) + lua_settop(L, old_top); + + //l_pprint(L); + //lua_setglobal(L, "_G"); + lua_set_global_table(args->L); + pthread_mutex_unlock(&mutex); pthread_mutex_lock(&mutex); threads++; pthread_mutex_unlock(&mutex); + //pthread_mutex_lock(&con_mutex); + //send request to handle_client() pthread_t thread_id; pthread_create(&thread_id, NULL, handle_client, (void*)args); pthread_detach(thread_id); + //double lock, wait for thread to unlock it + //pthread_mutex_lock(&con_mutex); + //handle_client((void*)args); free(client_fd); } diff --git a/src/thread.c b/src/thread.c index 4757d30..0295da9 100644 --- a/src/thread.c +++ b/src/thread.c @@ -113,7 +113,6 @@ int l_res(lua_State* L){ void* handle_thread(void* _args){ struct thread_info* args = (struct thread_info*)_args; lua_State* L = args->L; - lua_gc(L, LUA_GCSTOP); lua_newtable(L); int res_idx = lua_gettop(L); @@ -163,11 +162,13 @@ int l_clean(lua_State* L){ struct thread_info* info = lua_touserdata(L, -1); if(info != NULL && info->L != NULL){ - lua_gc(info->L, LUA_GCRESTART); + //lua_gc(info->L, LUA_GCRESTART); lua_gc(info->L, LUA_GCCOLLECT); lua_close(info->L); info->L = NULL; + pthread_mutex_destroy(&info->lock); + pthread_cancel(info->tid); free(info); luaI_tsetlud(L, 1, "_", NULL); @@ -177,7 +178,9 @@ int l_clean(lua_State* L){ int l_async(lua_State* oL){ lua_State* L = luaL_newstate(); + lua_gc(L, LUA_GCSTOP); + luaL_openlibs(L); lua_getglobal(oL, "_G"); luaI_deepcopy(oL, L, SKIP_GC); lua_set_global_table(L); @@ -188,7 +191,7 @@ int l_async(lua_State* oL){ pthread_mutex_init(&args->lock, NULL); pthread_mutex_lock(&args->lock); args->return_count = 0; - + args->function = str_init(""); lua_pushvalue(oL, 1); lua_dump(oL, writer, (void*)args->function, 0); @@ -214,30 +217,37 @@ int l_async(lua_State* oL){ struct thread_buffer { lua_State* L; - pthread_mutex_t lock; + pthread_mutex_t* lock; }; int _buffer_get(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); - pthread_mutex_lock(&buffer->lock); + pthread_mutex_lock(&*buffer->lock); luaI_deepcopy(buffer->L, L, 0); - pthread_mutex_unlock(&buffer->lock); + pthread_mutex_unlock(&*buffer->lock); return 1; } int _buffer_set(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); - pthread_mutex_lock(&buffer->lock); + pthread_mutex_lock(&*buffer->lock); lua_settop(buffer->L, 0); - luaI_deepcopy(L, buffer->L, 0); - pthread_mutex_unlock(&buffer->lock); + luaI_deepcopy(L, buffer->L, SKIP_GC); + pthread_mutex_unlock(&*buffer->lock); return 1; } +#include +_Atomic int used = 0; + int _buffer_mod(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); - pthread_mutex_lock(&buffer->lock); - luaI_deepcopy(buffer->L, L, 0); + pthread_mutex_lock(&*buffer->lock); + //printf("%p\n", &*buffer->lock); + assert(used == 0); + used = 1; + + luaI_deepcopy(buffer->L, L, SKIP_GC); int item = lua_gettop(L); lua_pushvalue(L, 2); lua_pushvalue(L, item); @@ -247,7 +257,10 @@ int _buffer_mod(lua_State* L){ lua_settop(buffer->L, 0); luaI_deepcopy(L, buffer->L, 0); } - pthread_mutex_unlock(&buffer->lock); + + used = 0; + + pthread_mutex_unlock(&*buffer->lock); return 1; } @@ -318,12 +331,12 @@ void meta_proxy_gen(lua_State* L, struct thread_buffer *buffer, int meta_idx, in int k, v = lua_gettop(L); k = lua_gettop(L) - 1; - char* fn = calloc(128, sizeof * fn); //todo: find optimal value for these + char* fn = calloc(128, sizeof * fn); const char* key = lua_tostring(L, k); sprintf(fn, "return function(_a,_b,_c)\ return __proxy_call(__this_obj,'%s',table.unpack({_a,_b,_c}));end", key); luaL_dostring(L, fn); - //printf("%s\n",fn); + free(fn); int nf = lua_gettop(L); @@ -336,7 +349,10 @@ return __proxy_call(__this_obj,'%s',table.unpack({_a,_b,_c}));end", key); int l_buffer_gc(lua_State* L){ printf("gc\n"); struct thread_buffer *buffer = lua_touserdata(L, 1); - pthread_mutex_lock(&buffer->lock); + pthread_mutex_lock(&*buffer->lock); + pthread_mutex_destroy(&*buffer->lock); + free(buffer->lock); + lua_close(buffer->L); return 0; } @@ -349,7 +365,8 @@ int l_buffer(lua_State* L){ int buffer_idx = lua_gettop(L); buffer->L = luaL_newstate(); - pthread_mutex_init(&buffer->lock, NULL); + buffer->lock = malloc(sizeof * buffer->lock); + if(pthread_mutex_init(&*buffer->lock, NULL) != 0) p_fatal("pthread_mutex_init failed"); lua_pushvalue(L, 1); luaI_deepcopy(L, buffer->L, SKIP_GC); @@ -369,11 +386,10 @@ void _lua_getfenv(lua_State* L){ } int l_testcopy(lua_State* L){ - lua_settop(L, 0); lua_State* temp = luaL_newstate(); - //luaI_deepcopy(temp, L, NULL, SKIP_GC); + luaI_deepcopy(L, temp, SKIP_GC); lua_close(temp); return 1; } -- cgit v1.2.3