-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monitor.c
51 lines (39 loc) · 861 Bytes
/
Monitor.c
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
#include "Monitor.h"
#include <stdio.h>
bool InitMutex(Mutex* _Mutex)
{
return sem_init(&_Mutex->Semaphore, 0, 1) == 0;
}
void DestroyMutex(Mutex* _Mutex)
{
sem_destroy(&_Mutex->Semaphore);
}
void LockMutex(Mutex* _Mutex)
{
sem_wait(&_Mutex->Semaphore);
}
void UnlockMutex(Mutex* _Mutex)
{
sem_post(&_Mutex->Semaphore);
}
bool InitCondVar(CondVar* _CondVar)
{
_CondVar->Count = 0;
return sem_init(&_CondVar->Semaphore, 0, 0) == 0;
}
void DestroyCondVar(CondVar* _CondVar)
{
sem_destroy(&_CondVar->Semaphore);
}
void WaitCondVar(CondVar* _CondVar, Mutex* _Mutex)
{
_CondVar->Count++;
UnlockMutex(_Mutex);
sem_wait(&_CondVar->Semaphore);
LockMutex(_Mutex);
_CondVar->Count--;
}
void PostCondVar(CondVar* _CondVar)
{
sem_post(&_CondVar->Semaphore);
}