blob: fe0a67bb65f5b3819e758178b0482d9f5438ac36 (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include "Thread.h"
BH_Semaphore *BH_SemaphoreNew(int value)
{
BH_Semaphore *semaphore;
/* Allocate space for mutex and initialize it */
semaphore = malloc(sizeof(BH_Semaphore));
if (semaphore)
{
semaphore->handle = CreateSemaphore(NULL, value, 0x7FFF, NULL);
if (!semaphore->handle)
{
free(semaphore);
return NULL;
}
}
return semaphore;
}
void BH_SemaphoreFree(BH_Semaphore *semaphore)
{
CloseHandle(semaphore->handle);
free(semaphore);
}
int BH_SemaphorePost(BH_Semaphore *semaphore)
{
if (!ReleaseSemaphore(semaphore->handle, 1, NULL))
return BH_ERROR;
return BH_OK;
}
int BH_SemaphoreWait(BH_Semaphore *semaphore)
{
if (WaitForSingleObject(semaphore->handle, INFINITE))
return BH_ERROR;
return BH_OK;
}
int BH_SemaphoreTryWait(BH_Semaphore *semaphore)
{
if (WaitForSingleObject(semaphore->handle, 0))
return BH_ERROR;
return BH_OK;
}
int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
uint32_t timeout)
{
switch (WaitForSingleObject(semaphore->handle, timeout))
{
case 0: return BH_OK;
case WAIT_TIMEOUT: return BH_TIMEOUT;
default: return BH_ERROR;
}
}
|