aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Win32/Mutex.c
blob: b5af90e61d5a66477cb68fe5dc3ef266760352f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "Thread.h"


BH_Mutex *BH_MutexNew(void)
{
    BH_Mutex *mutex;

    /* Allocate space for mutex and initialize it */
    mutex = malloc(sizeof(BH_Mutex));
    if (mutex && !InitializeCriticalSectionAndSpinCount(&mutex->handle, 0x400))
    {
        free(mutex);
        return NULL;
    }

    return mutex;
}


void BH_MutexFree(BH_Mutex *mutex)
{
    DeleteCriticalSection(&mutex->handle);
    free(mutex);
}


int BH_MutexLock(BH_Mutex *mutex)
{
    EnterCriticalSection(&mutex->handle);

    return BH_OK;
}


int BH_MutexUnlock(BH_Mutex *mutex)
{
    LeaveCriticalSection(&mutex->handle);

    return BH_OK;
}


int BH_MutexLockTry(BH_Mutex *mutex)
{
    if (!TryEnterCriticalSection(&mutex->handle))
        return BH_ERROR;

    return BH_OK;
}