// // Created by nicol on 2018-03-18. // #ifdef _WIN32 #include #endif #include #include "mutex.h" #ifdef _WIN32 #include struct st_mutex { CRITICAL_SECTION handle; }; mutex_t mutex_create() { mutex_t mutex = malloc(sizeof(struct st_mutex)); InitializeCriticalSection(&mutex->handle); return mutex; } void mutex_lock(mutex_t mutex) { EnterCriticalSection(&mutex->handle); } void mutex_unlock(mutex_t mutex) { LeaveCriticalSection(&mutex->handle); } void mutex_destroy(mutex_t mutex) { DeleteCriticalSection(&mutex->handle); } #endif #ifdef __linux__ #include struct st_mutex { pthread_mutex_t handle; }; mutex_t mutex_create() { mutex_t mutex = malloc(sizeof(struct st_mutex)); pthread_mutex_init(&mutex->handle, 0); return mutex; } void mutex_lock(mutex_t mutex) { pthread_mutex_lock(&mutex->handle); } void mutex_unlock(mutex_t mutex) { pthread_mutex_unlock(&mutex->handle); } void mutex_destroy(mutex_t mutex) { pthread_mutex_destroy(&mutex->handle); free(mutex); } #endif