aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/thread.c59
-rw-r--r--src/thread.h2
-rw-r--r--src/util.h2
3 files changed, 62 insertions, 1 deletions
diff --git a/src/thread.c b/src/thread.c
index a5eb892..6d4540d 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -85,6 +85,65 @@ int l_tunlock(lua_State* L){
return 0;
}
+int _mutex_lock(lua_State* L){
+ lua_pushstring(L, "_");
+ lua_gettable(L, 1);
+ pthread_mutex_t *lock = lua_touserdata(L, -1);
+
+ pthread_mutex_lock(lock);
+
+ return 0;
+}
+
+int _mutex_unlock(lua_State* L){
+ lua_pushstring(L, "_");
+ lua_gettable(L, 1);
+ pthread_mutex_t *lock = lua_touserdata(L, -1);
+
+ pthread_mutex_unlock(lock);
+
+ return 0;
+}
+
+int _mutex_free(lua_State* L){
+ lua_pushstring(L, "_");
+ lua_gettable(L, 1);
+ pthread_mutex_t *lock = lua_touserdata(L, -1);
+
+ if(lock != NULL){
+ pthread_mutex_destroy(&*lock);
+ free(lock);
+
+ luaI_tsetlud(L, 1, "_", NULL);
+ }
+
+ return 0;
+}
+
+int l_mutex(lua_State* L){
+ pthread_mutex_t *lock = malloc(sizeof * lock);
+
+ if(pthread_mutex_init(&*lock, NULL) != 0)
+ luaI_error(L, -1, "mutex init failed");
+
+ lua_newtable(L);
+ int idx = lua_gettop(L);
+ luaI_tsetlud(L, idx, "_", lock);
+ luaI_tsetcf(L, idx, "lock", _mutex_lock);
+ luaI_tsetcf(L, idx, "unlock", _mutex_unlock);
+ luaI_tsetcf(L, idx, "free", _mutex_free);
+
+ lua_newtable(L);
+ int midx = lua_gettop(L);
+ luaI_tsetcf(L, midx, "__gc", _mutex_free);
+
+ lua_pushvalue(L, midx);
+ lua_setmetatable(L, idx);
+ lua_pushvalue(L, idx);
+
+ return 1;
+}
+
int l_res(lua_State* L){
int return_count = lua_gettop(L) - 1;
lua_pushstring(L, "_");
diff --git a/src/thread.h b/src/thread.h
index be9cadd..0b3a27d 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -6,6 +6,7 @@ int l_tlock(lua_State*);
int l_tunlock(lua_State*);
int l_buffer(lua_State*);
int l_testcopy(lua_State*);
+int l_mutex(lua_State*);
void lib_thread_clean();
@@ -17,6 +18,7 @@ static const luaL_Reg thread_function_list [] = {
{"unlock",l_tunlock},
{"buffer",l_buffer},
{"testcopy",l_testcopy},
+ {"mutex", l_mutex},
{NULL,NULL}
};
diff --git a/src/util.h b/src/util.h
index 62cf80d..99f127c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -38,4 +38,4 @@ void _p_fatal(const char*, int, const char*, const char*);
void p_error(const char*);
char* strnstr(const char*, const char*, size_t);
-#endif //__UTIL_H \ No newline at end of file
+#endif //__UTIL_H