61 lines
1.1 KiB
C
61 lines
1.1 KiB
C
//
|
|
// Created by nicol on 2018-03-18.
|
|
//
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include "mutex.h"
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
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 <pthread.h>
|
|
|
|
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 |