-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCcpSemaphore.cpp
More file actions
208 lines (181 loc) · 5.21 KB
/
Copy pathCcpSemaphore.cpp
File metadata and controls
208 lines (181 loc) · 5.21 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright © 2013 CCP ehf.
#include "include/CcpSemaphore.h"
#include "tracy/TracyC.h"
// OS specific includes:
#ifdef _WIN32
// Nothing specific
using NativeHandle = HANDLE;
#elif defined(__APPLE__)
#include <mach/semaphore.h>
#include <mach/mach.h>
using NativeHandle = semaphore_t;
#else
using NativeHandle = sem_t;
#endif
struct CcpSemaphore::Private
{
#if CCP_TELEMETRY_ENABLED
// Lazily announce the semaphore to Tracy. Subsequent calls are no-ops once a context exists.
void EnsureTelemetryLockAnnounced();
// Opaque pointer to TracyCLockCtx, kept as void* so this header does not
// need to pull in Tracy headers.
TracyCLockCtx telemetryLockContext;
#endif
NativeHandle semaphore;
const char* semaphoreName{ nullptr };
};
// Fully qualified, preferred constructor.
CcpSemaphore::CcpSemaphore( const char* semaphoreName, uint32_t initialCount, uint32_t maximumCount ) : m_impl( std::make_unique<Private>() )
{
// Make sure to keep our own copy of the semaphoreName
if (semaphoreName != nullptr)
{
const size_t strLen = std::strlen( semaphoreName ) + 1;
char* copy = new char[strLen];
strcpy_s( copy, strLen, semaphoreName );
m_impl->semaphoreName = copy;
}
else
{
m_impl->semaphoreName = nullptr;
}
// OS specific implementation:
#ifdef _WIN32
m_impl->semaphore = ::CreateSemaphore( 0, initialCount, maximumCount, 0 );
#elif defined(__APPLE__)
semaphore_create( current_task(), &m_impl->semaphore, SYNC_POLICY_FIFO, initialCount );
#else
sem_init( &m_impl->semaphore, 0, initialCount );
#endif
}
namespace
{
void AnnounceSemaphoreToTelemetry( TracyCLockCtx& ctx, const char* name )
{
// Lazy initialization pattern because there are many instances which are created statically,
// and therefore would never be announced to tracy.
if ( !ctx )
{
TracyCLockAnnounce( ctx );
if ( ctx ) {
TracyCLockCustomName( ctx, name, strlen( name ) );
}
}
}
}
// Preferred constructor, with default value overloads (see header file for details)
CcpSemaphore::CcpSemaphore( const char* semaphoreName )
: CcpSemaphore( semaphoreName, 0, 1 )
{
}
CcpSemaphore::CcpSemaphore()
: CcpSemaphore( "CcpSemaphore", 0, 1 )
{
}
CcpSemaphore::CcpSemaphore( uint32_t initialCount, uint32_t maximumCount )
: CcpSemaphore( "CcpSemaphore", initialCount, maximumCount )
{
}
CcpSemaphore::~CcpSemaphore()
{
// OS specific implementation:
#ifdef _WIN32
::CloseHandle( m_impl->semaphore );
#elif defined(__APPLE__)
semaphore_destroy( current_task(), m_impl->semaphore );
#else
sem_destroy( &m_impl->semaphore );
#endif
delete[] m_impl->semaphoreName;
#if CCP_TELEMETRY_ENABLED
if ( CcpTelemetryLockTrackingIsEnabled() && m_impl->telemetryLockContext && CcpTelemetryIsConnected() )
{
TracyCLockTerminate( m_impl->telemetryLockContext );
}
#endif
}
bool CcpSemaphore::Wait()
{
#if CCP_TELEMETRY_ENABLED
bool notifyTracy{ false };
if ( CcpTelemetryLockTrackingIsEnabled() && CcpTelemetryIsConnected() )
{
AnnounceSemaphoreToTelemetry( m_impl->telemetryLockContext, m_impl->semaphoreName );
if ( m_impl->telemetryLockContext )
{
notifyTracy = TracyCLockBeforeLock( m_impl->telemetryLockContext );
}
}
#endif
// OS specific implementation:
#ifdef _WIN32
const bool result = ::WaitForSingleObject( m_impl->semaphore, INFINITE ) == 0;
#elif defined(__APPLE__)
const bool result = semaphore_wait( m_impl->semaphore ) == KERN_SUCCESS;
#else
const bool result = sem_wait( &m_impl->semaphore ) == 0;
#endif
#if CCP_TELEMETRY_ENABLED
if ( notifyTracy && CcpTelemetryLockTrackingIsEnabled() && CcpTelemetryIsConnected() )
{
TracyCLockAfterLock( m_impl->telemetryLockContext );
}
#endif
return result;
}
bool CcpSemaphore::TimedWait( uint32_t timeoutInMs )
{
#if CCP_TELEMETRY_ENABLED
bool notifyTracy{ false };
if ( CcpTelemetryLockTrackingIsEnabled() && CcpTelemetryIsConnected() )
{
AnnounceSemaphoreToTelemetry( m_impl->telemetryLockContext, m_impl->semaphoreName );
if ( m_impl->telemetryLockContext )
{
notifyTracy = TracyCLockBeforeLock( m_impl->telemetryLockContext );
}
}
#endif
// OS specific implementation:
#ifdef _WIN32
const bool result = ::WaitForSingleObject( m_impl->semaphore, timeoutInMs ) == 0;
#elif defined(__APPLE__)
mach_timespec_t mts;
mts.tv_sec = timeoutInMs / 1000;
mts.tv_nsec = ( timeoutInMs % 1000 ) * 1000000;
const bool result = semaphore_timedwait( m_impl->semaphore, mts ) == KERN_SUCCESS;
#else
timespec ts;
ts.tv_sec = timeoutInMs / 1000;
ts.tv_nsec = (timeoutInMs % 1000) * 1000000;
const bool result = sem_timedwait( &m_impl->semaphore, &ts ) == 0;
#endif
#if CCP_TELEMETRY_ENABLED
if ( notifyTracy && CcpTelemetryLockTrackingIsEnabled() && CcpTelemetryIsConnected() )
{
TracyCLockAfterLock( m_impl->telemetryLockContext );
}
#endif
return result;
}
void CcpSemaphore::Signal()
{
// OS specific implementation:
#ifdef _WIN32
::ReleaseSemaphore( m_impl->semaphore, 1, 0 );
#elif defined(__APPLE__)
semaphore_signal( m_impl->semaphore );
#else
sem_post( &m_impl->semaphore );
#endif
#if CCP_TELEMETRY_ENABLED
if ( CcpTelemetryLockTrackingIsEnabled() && CcpTelemetryIsConnected() )
{
AnnounceSemaphoreToTelemetry( m_impl->telemetryLockContext, m_impl->semaphoreName );
if ( m_impl->telemetryLockContext )
{
TracyCLockAfterUnlock( m_impl->telemetryLockContext );
}
}
#endif
}