50 lines
1009 B
C
50 lines
1009 B
C
#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;
|
|
}
|