From ed72a09f0857f31de041edb1e778753151034a49 Mon Sep 17 00:00:00 2001 From: malloc Date: Thu, 10 Jan 2019 17:01:27 -0600 Subject: [PATCH] NEED A GOOD LAWYER --- src/server/sock/tcp_bsd.c | 1 - src/server/util/thread.h | 15 +++++++++++++- src/server/util/thread_posix.c | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/server/sock/tcp_bsd.c b/src/server/sock/tcp_bsd.c index 9f793fa..1d75fcd 100644 --- a/src/server/sock/tcp_bsd.c +++ b/src/server/sock/tcp_bsd.c @@ -1,5 +1,4 @@ #ifndef _WIN32 #include "tcp.h" - #endif \ No newline at end of file diff --git a/src/server/util/thread.h b/src/server/util/thread.h index 43be41b..d292474 100644 --- a/src/server/util/thread.h +++ b/src/server/util/thread.h @@ -18,7 +18,20 @@ typedef pthread_mutex_t glv_mutex_t; #endif -glv_thread_t* glv_thread_create(); +#include +typedef void(*glv_func_t)(void*); + +glv_thread_t* glv_thread_create(glv_func_t func, void* args); +void glv_thread_join(glv_thread_t* thread); void glv_thread_destroy(glv_thread_t* thread); +/** END THREAD DECLS **/ +/**********************/ +/** BEGIN MUTX DECLS **/ + +glv_mutex_t* glv_mutex_create(); +void glv_mutex_lock(glv_mutex_t* mutex); +void glv_mutex_unlock(glv_mutex_t* mutex); +void glv_mutex_destroy(glv_mutex_t* mutex); + #endif diff --git a/src/server/util/thread_posix.c b/src/server/util/thread_posix.c index 0f26c01..d8cdbd0 100644 --- a/src/server/util/thread_posix.c +++ b/src/server/util/thread_posix.c @@ -1,6 +1,44 @@ #ifndef _WIN32 #include "thread.h" +typedef struct { + glv_func_t func; + void* args; +} wrapper_t; +static void* _wrapper_func(void* proxy) { + wrapper_t wrapper = *((wrapper_t*)proxy); + free(proxy); + + wrapper.func(wrapper.args); +} + +glv_thread_t* glv_thread_create(glv_func_t func, void* args) { + wrapper_t* proxy = malloc(sizeof(wrapper_t)); + proxy->func = func; + proxy->args = args; + + glv_thread_t* thread = malloc(sizeof(thread)); + if(pthread_create(thread, NULL, _wrapper_func, proxy) == 0) + return thread; + else + return NULL; +} + +void glv_thread_join(glv_thread_t* thread) { + pthread_join(*thread, NULL); +} + +void glv_thread_destroy(glv_thread_t* thread) { + free(thread); +} + +/** END THREAD IMPL **/ +/*********************/ +/** BEGIN MUTX IMPL **/ + +glv_mutex_t* glv_mutex_create() { + +} #endif