Files
CgeThread/win32/Mutex.c
Mikhail Romanko 04e02490b7
Some checks failed
CI / build-and-analyze (push) Failing after 1m18s
Initial commit
2026-08-09 12:55:16 +03:00

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;
}