aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/reg.c22
-rw-r--r--src/thread.c46
-rw-r--r--src/thread.h2
-rw-r--r--src/types/larray.c3
-rw-r--r--src/types/larray.h3
-rw-r--r--tests/h.lua14
-rw-r--r--tests/s.lua15
7 files changed, 54 insertions, 51 deletions
diff --git a/src/reg.c b/src/reg.c
index 966537f..417062a 100644
--- a/src/reg.c
+++ b/src/reg.c
@@ -18,22 +18,20 @@ void sigHandle(int s){
static int lua_exit(lua_State* L){
- sigHandle(0);
+ lib_thread_clean();
+ //sigHandle(0);
return 0;
}
int luaopen_llib(lua_State* L) {
- lua_newuserdata(L, sizeof(void*));
- luaL_newmetatable(L, "gc");
- lua_pushstring(L, "__gc");
- lua_pushcfunction(L, &lua_exit);
- lua_settable(L, -3);
-
- lua_setmetatable(L, -2);
- lua_setfield(L, LUA_REGISTRYINDEX, "grr");
- signal(SIGTERM, sigHandle);
- signal(SIGINT, sigHandle);
-
+ lua_newuserdata(L, 1);
+ int ud = lua_gettop(L);
+ lua_newtable(L);
+ int meta = lua_gettop(L);
+ luaI_tsetcf(L, meta, "__gc", lua_exit);
+ lua_pushvalue(L, meta);
+ lua_setmetatable(L, ud);
+
//create <lib>.array functions
lua_newtable(L);
diff --git a/src/thread.c b/src/thread.c
index 393d3fb..dd4052a 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -25,25 +25,39 @@ pthread_mutex_t thread_priority_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZE
pthread_mutex_t thread_lock_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
larray_t* thread_locks = NULL;
+void lib_thread_clean(){
+ if(thread_locks == NULL) return;
+
+ for(int i = 0; i != thread_locks->size; i++){
+ if(thread_locks->arr[i].used){
+ //pthread_mutex_destroy(thread_locks->arr[i].value);
+ free(thread_locks->arr[i].value);
+ }
+ }
+
+ larray_clear(thread_locks);
+}
int l_tlock(lua_State* L){
int idx = luaL_checkinteger(L, 1);
pthread_mutex_lock(&thread_lock_lock);
- pthread_mutex_lock(&thread_priority_lock);
- pthread_mutex_unlock(&thread_priority_lock);
+ //pthread_mutex_lock(&thread_priority_lock);
+ //pthread_mutex_unlock(&thread_priority_lock);
pthread_mutex_t mutex;
if(thread_locks == NULL) thread_locks = larray_init();
int i = 0;
if((i = larray_geti(thread_locks, idx)) == -1){
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
- larray_set(&thread_locks, idx, (void*)mutex);
+ pthread_mutex_t* mp = malloc(sizeof * mp);
+ *mp = mutex;
+ int id = larray_set(&thread_locks, idx, (void*)mp);
} else {
- pthread_mutex_t m = (pthread_mutex_t)thread_locks->arr[i].value;
+ pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value;
pthread_mutex_lock(&thread_priority_lock);
pthread_mutex_unlock(&thread_lock_lock);
- pthread_mutex_lock(&m);
+ pthread_mutex_lock(m);
pthread_mutex_lock(&thread_lock_lock);
pthread_mutex_unlock(&thread_priority_lock);
@@ -61,8 +75,9 @@ int l_tunlock(lua_State* L){
pthread_mutex_lock(&thread_lock_lock);
int i = 0;
if(thread_locks != NULL && (i = larray_geti(thread_locks, idx)) != -1){
- pthread_mutex_t m = (pthread_mutex_t)thread_locks->arr[i].value;
- pthread_mutex_unlock(&m);
+ pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value;
+
+ pthread_mutex_unlock(m);
thread_locks->arr[i].value = (void*)m;
}
@@ -145,13 +160,16 @@ int l_clean(lua_State* L){
lua_pushstring(L, "_");
lua_gettable(L, 1);
struct thread_info* info = lua_touserdata(L, -1);
- if(info->L != NULL){
- lua_gc(info->L, LUA_GCRESTART);
- lua_gc(info->L, LUA_GCCOLLECT);
- lua_close(info->L);
- info->L = NULL;
- pthread_mutex_destroy(&info->lock);
- free(info);
+ if(info != NULL && info->L != NULL){
+
+ lua_gc(info->L, LUA_GCRESTART);
+ lua_gc(info->L, LUA_GCCOLLECT);
+ lua_close(info->L);
+ info->L = NULL;
+ pthread_mutex_destroy(&info->lock);
+ free(info);
+
+ luaI_tsetlud(L, 1, "_", NULL);
}
return 0;
}
diff --git a/src/thread.h b/src/thread.h
index e9ead6d..9dab9b1 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -4,6 +4,8 @@ int l_async(lua_State*);
int l_tlock(lua_State*);
int l_tunlock(lua_State*);
+void lib_thread_clean();
+
static const luaL_Reg thread_function_list [] = {
{"async",l_async},
{"lock",l_tlock},
diff --git a/src/types/larray.c b/src/types/larray.c
index 7a5afbc..2e9a5b4 100644
--- a/src/types/larray.c
+++ b/src/types/larray.c
@@ -29,7 +29,7 @@ void larray_expand(larray_t** _l){
*_l = remade;
}
-void larray_set(larray_t** _l, uint64_t idx, void* value){
+int larray_set(larray_t** _l, uint64_t idx, void* value){
larray_t* l = *_l;
if(l->len + 1 >= l->size){
@@ -51,6 +51,7 @@ void larray_set(larray_t** _l, uint64_t idx, void* value){
l->len++;
*_l = l;
+ return ind;
}
int larray_geti(larray_t* l, uint64_t idx){
diff --git a/src/types/larray.h b/src/types/larray.h
index ff67857..1213758 100644
--- a/src/types/larray.h
+++ b/src/types/larray.h
@@ -1,4 +1,5 @@
#include <stdint.h>
+#include <stdlib.h>
struct larray_item {
uint64_t idx;
@@ -14,7 +15,7 @@ typedef struct {
larray_t* larray_initl(int len);
larray_t* larray_init();
void larray_expand(larray_t** _l);
-void larray_set(larray_t** _l, uint64_t idx, void* value);
+int larray_set(larray_t** _l, uint64_t idx, void* value);
int larray_geti(larray_t* l, uint64_t idx);
void* larray_get(larray_t* l, uint64_t idx);
void larray_clear(larray_t* l);
diff --git a/tests/h.lua b/tests/h.lua
index b8ccd13..c96e98e 100644
--- a/tests/h.lua
+++ b/tests/h.lua
@@ -1,8 +1,8 @@
require "llib"
llib.thread.lock(1)
-llib.thread.lock(2)
-llib.thread.unlock(2)
+--llib.thread.lock(2)
+--llib.thread.unlock(2)
local thread_a = llib.thread.async(function (res)
--os.execute("sleep 1")
@@ -10,21 +10,19 @@ local thread_a = llib.thread.async(function (res)
print("waiting..")
llib.thread.lock(1)
print("signal!")
- res(_G.llib.crypto.md5())
+ res()
print("after")
end)
os.execute("sleep 1")
+print("unlock")
llib.thread.unlock(1)
+
awa = thread_a:await()
+--print((awa + "hi"):final())
os.execute("sleep 1")
-for i=1,999 do
-print((awa + "hi"):final())
-end
-
thread_a:clean()
print("clean exit")
-
diff --git a/tests/s.lua b/tests/s.lua
index a543ff0..bac5072 100644
--- a/tests/s.lua
+++ b/tests/s.lua
@@ -1,17 +1,2 @@
require "llib"
-function _G.sleep (a)
- local sec = tonumber(os.clock() + a);
- while (os.clock() < sec) do
- end
-end
-for i=1,50 do
- llib.net.spawn(function()
- local sec = tonumber(os.clock() + 1);
- while (os.clock() < sec) do
- end
- print("hi")
- end)
-end
-
-while true do end \ No newline at end of file