-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCcpSemaphore.cpp
More file actions
127 lines (97 loc) · 2.24 KB
/
CcpSemaphore.cpp
File metadata and controls
127 lines (97 loc) · 2.24 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright © 2013 CCP ehf.
#include "include/CcpSemaphore.h"
#include "include/CCPAssert.h"
#ifdef _WIN32
CcpSemaphore::CcpSemaphore()
{
m_semaphore = ::CreateSemaphore( 0, 0, 1, 0 );
}
CcpSemaphore::CcpSemaphore( uint32_t initialCount, uint32_t maximumCount )
{
m_semaphore = ::CreateSemaphore( 0, initialCount, maximumCount, 0 );
}
CcpSemaphore::~CcpSemaphore()
{
::CloseHandle( m_semaphore );
}
bool CcpSemaphore::Wait()
{
return ::WaitForSingleObject( m_semaphore, INFINITE ) == 0;
}
bool CcpSemaphore::TimedWait( uint32_t timeout )
{
return ::WaitForSingleObject( m_semaphore, timeout ) == 0;
}
void CcpSemaphore::Signal()
{
::ReleaseSemaphore( m_semaphore, 1, 0 );
}
#elif defined(__APPLE__)
#include <mach/semaphore.h>
#include <mach/mach.h>
CcpSemaphore::CcpSemaphore()
{
semaphore_create( current_task(), &m_semaphore, SYNC_POLICY_FIFO, 0 );
}
CcpSemaphore::CcpSemaphore( uint32_t initialCount, uint32_t maximumCount )
{
semaphore_create( current_task(), &m_semaphore, SYNC_POLICY_FIFO, initialCount );
}
CcpSemaphore::~CcpSemaphore()
{
semaphore_destroy( current_task(), m_semaphore );
}
#include <errno.h>
bool CcpSemaphore::Wait()
{
return semaphore_wait( m_semaphore ) == KERN_SUCCESS;
}
bool CcpSemaphore::TimedWait( uint32_t timeoutInMs )
{
mach_timespec_t mts;
mts.tv_sec = timeoutInMs / 1000;
mts.tv_nsec = ( timeoutInMs % 1000 ) * 1000000;
return semaphore_timedwait( m_semaphore, mts ) == KERN_SUCCESS;
}
void CcpSemaphore::Signal()
{
semaphore_signal( m_semaphore );
}
#else
CcpSemaphore::CcpSemaphore()
{
sem_init( &m_semaphore, 0, 0 );
}
CcpSemaphore::CcpSemaphore( uint32_t initialCount, uint32_t maximumCount )
{
sem_init( &m_semaphore, 0, initialCount );
}
CcpSemaphore::~CcpSemaphore()
{
sem_destroy( &m_semaphore );
}
#include <errno.h>
bool CcpSemaphore::Wait()
{
if( sem_wait( &m_semaphore ) == 0 )
{
return true;
}
return false;
}
bool CcpSemaphore::TimedWait( uint32_t timeoutInMs )
{
timespec ts;
ts.tv_sec = timeoutInMs / 1000;
ts.tv_nsec = (timeoutInMs % 1000) * 1000000;
if( sem_timedwait( &m_semaphore, &ts ) == 0 )
{
return true;
}
return false;
}
void CcpSemaphore::Signal()
{
sem_post( &m_semaphore );
}
#endif