This commit is contained in:
49
win32/Mutex.c
Normal file
49
win32/Mutex.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "Internal.h"
|
||||
#include "Codemap.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
CgeMutex *CgeMutexNew(int *result) {
|
||||
CgeMutex *mutex;
|
||||
int code = CGE_THREAD_EOOM;
|
||||
|
||||
mutex = malloc(sizeof(*mutex));
|
||||
if (mutex && !InitializeCriticalSectionAndSpinCount(&mutex->handle, 0x400)) {
|
||||
code = errorToErrorCode(GetLastError());
|
||||
free(mutex);
|
||||
mutex = NULL;
|
||||
}
|
||||
|
||||
if (result)
|
||||
*result = code;
|
||||
|
||||
return mutex;
|
||||
}
|
||||
|
||||
void CgeMutexFree(CgeMutex *mutex) {
|
||||
if (!mutex)
|
||||
return;
|
||||
DeleteCriticalSection(&mutex->handle);
|
||||
free(mutex);
|
||||
}
|
||||
|
||||
int CgeMutexLock(CgeMutex *mutex, int *result) {
|
||||
(void)result;
|
||||
EnterCriticalSection(&mutex->handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CgeMutexUnlock(CgeMutex *mutex, int *result) {
|
||||
(void)result;
|
||||
LeaveCriticalSection(&mutex->handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CgeMutexTryLock(CgeMutex *mutex, int *result) {
|
||||
if (TryEnterCriticalSection(&mutex->handle))
|
||||
return 1;
|
||||
|
||||
if (result)
|
||||
*result = CGE_THREAD_EBUSY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user