-
Notifications
You must be signed in to change notification settings - Fork 5
/
CriticalSection.cpp
60 lines (54 loc) · 1.32 KB
/
CriticalSection.cpp
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
#include "CriticalSection.hpp"
#include "Exception.hpp"
BEGIN_INANITY
CriticalSection::CriticalSection()
{
try
{
#if defined(___INANITY_PLATFORM_WINDOWS)
InitializeCriticalSection(&criticalSection);
#elif defined(___INANITY_PLATFORM_POSIX)
if(pthread_mutex_init(&mutex, 0))
THROW_SECONDARY("Can't initialize mutex", Exception::SystemError());
#else
#error Unknown platform
#endif
}
catch(Exception* exception)
{
THROW_SECONDARY("Can't create critical section", exception);
}
}
CriticalSection::~CriticalSection()
{
#if defined(___INANITY_PLATFORM_WINDOWS)
DeleteCriticalSection(&criticalSection);
#elif defined(___INANITY_PLATFORM_POSIX)
pthread_mutex_destroy(&mutex);
#else
#error Unknown platform
#endif
}
void CriticalSection::Enter()
{
#if defined(___INANITY_PLATFORM_WINDOWS)
EnterCriticalSection(&criticalSection);
#elif defined(___INANITY_PLATFORM_POSIX)
if(pthread_mutex_lock(&mutex))
THROW_SECONDARY("Can't enter critical section", Exception::SystemError());
#else
#error Unknown platform
#endif
}
void CriticalSection::Leave()
{
#if defined(___INANITY_PLATFORM_WINDOWS)
LeaveCriticalSection(&criticalSection);
#elif defined(___INANITY_PLATFORM_POSIX)
if(pthread_mutex_unlock(&mutex))
THROW_SECONDARY("Can't leave critical section", Exception::SystemError());
#else
#error Unknown platform
#endif
}
END_INANITY