diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 47462f5..585b83e 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -4,3 +4,10 @@ target_link_libraries( PRIVATE Mw ) +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") + target_link_libraries( + MDECore + PRIVATE + pthread + ) +endif() diff --git a/core/mutex.c b/core/mutex.c index cd90210..6160030 100644 --- a/core/mutex.c +++ b/core/mutex.c @@ -19,4 +19,24 @@ void MDEMutexUnlock(MDEMutex mutex) { SetEvent(mutex); } #else +#include + +MDEMutex MDEMutexCreate(void) { + pthread_mutex_t* m = malloc(sizeof(*m)); + pthread_mutex_init(m, NULL); + return m; +} + +void MDEMutexDestroy(MDEMutex mutex) { + pthread_mutex_destroy(mutex); + free(mutex); +} + +void MDEMutexLock(MDEMutex mutex) { + pthread_mutex_lock(mutex); +} + +void MDEMutexUnlock(MDEMutex mutex) { + pthread_mutex_unlock(mutex); +} #endif diff --git a/core/thread.c b/core/thread.c index 1d9ac66..2022934 100644 --- a/core/thread.c +++ b/core/thread.c @@ -31,4 +31,36 @@ void MDEThreadJoin(MDEThread thread) { WaitForSingleObject(thread, INFINITE); } #else +#include + +static void* call_wrapper(void* param) { + void** p = param; + void (*call)(void* user_data) = p[0]; + void* ud = p[1]; + free(p); + + call(ud); + + return NULL; +} + +MDEThread MDEThreadCreate(void (*call)(void* user_data), void* user_data) { + void** p = malloc(sizeof(*p) * 2); + pthread_t* t = malloc(sizeof(*t)); + + p[0] = call; + p[1] = user_data; + + pthread_create(t, NULL, call_wrapper, p); + return t; +} + +void MDEThreadDestroy(MDEThread thread) { + free(thread); +} + +void MDEThreadJoin(MDEThread thread) { + void* p; + pthread_join(thread, &p); +} #endif