-
Notifications
You must be signed in to change notification settings - Fork 89
/
palMutex.h
375 lines (325 loc) · 14.2 KB
/
palMutex.h
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
***********************************************************************************************************************
*
* Copyright (c) 2014-2024 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
/**
***********************************************************************************************************************
* @file palMutex.h
* @brief PAL utility collection Mutex and MutexAuto class declarations.
***********************************************************************************************************************
*/
#pragma once
#include "palAssert.h"
#include <pthread.h>
#include <string.h>
namespace Util
{
/**
***********************************************************************************************************************
* @brief Platform-agnostic mutex primitive.
***********************************************************************************************************************
*/
class Mutex
{
public:
/// Defines MutexData as a unix pthread_mutex_t
typedef pthread_mutex_t MutexData;
Mutex() noexcept : m_osMutex {} { pthread_mutex_init(&m_osMutex, nullptr); }
~Mutex() { pthread_mutex_destroy(&m_osMutex); };
/// Enters the critical section if it is not contended. If it is contended, wait for the critical section to become
/// available, then enter it.
void Lock();
/// Enters the critical section if it is not contended. Does not wait for the critical section to become available
/// if it is contended.
///
/// @returns True if the critical section was entered, false otherwise.
bool TryLock();
/// Leaves the critical section.
void Unlock();
/// Returns the OS specific mutex data.
MutexData* GetMutexData() { return &m_osMutex; }
private:
MutexData m_osMutex; ///< Opaque structure to the OS-specific Mutex data
PAL_DISALLOW_COPY_AND_ASSIGN(Mutex);
};
/**
***********************************************************************************************************************
* @brief A "resource acquisition is initialization" (RAII) wrapper for the Mutex class.
*
* The RAII paradigm allows critical sections to be automatically acquired during this class' constructor, and
* automatically released when a stack-allocated wrapper object goes out-of-scope. As such, it only makes sense to use
* this class for stack-allocated objects.
*
* This object will ensure that anything between when the object is allocated on the stack and when it goes out of scope
* will be protected from access by multiple threads. See the below example.
*
* [Code not protected]
* {
* [Code not protected]
* MutexAuto lock(pPtrToMutex);
* [Code is protected]
* }
* [Code not protected]
***********************************************************************************************************************
*/
class MutexAuto
{
public:
/// Locks the given Mutex.
explicit MutexAuto(Mutex* pMutex) : m_pMutex(pMutex)
{
PAL_ASSERT(m_pMutex != nullptr);
m_pMutex->Lock();
}
/// Unlocks the Mutex we locked in the constructor.
~MutexAuto()
{
m_pMutex->Unlock();
}
private:
Mutex* const m_pMutex; ///< The Mutex which this object wraps.
PAL_DISALLOW_DEFAULT_CTOR(MutexAuto);
PAL_DISALLOW_COPY_AND_ASSIGN(MutexAuto);
};
/**
***********************************************************************************************************************
* @brief Platform-agnostic rw lock primitive.
***********************************************************************************************************************
*/
class RWLock
{
public:
/// Defines RWLockData as a unix pthread_rwlock_t
typedef pthread_rwlock_t RWLockData;
/// @note pthread_rwlock_init will not fail as called
RWLock() noexcept : m_osRWLock {} { pthread_rwlock_init(&m_osRWLock, nullptr); }
~RWLock() noexcept { pthread_rwlock_destroy(&m_osRWLock); };
/// Enumerates the lock type of RWLockAuto
enum LockType
{
ReadOnly = 0, ///< Lock in readonly mode, in other words shared mode.
ReadWrite ///< Lock in readwrite mode, in other words exclusive mode.
};
/// Acquires a rw lock in shared mode if it is not contended in exclusive mode.
/// If it is contended, wait for rw lock to become available, then enter it.
void LockForRead();
/// Acquires a rw lock in exclusive mode if it is not contended.
/// If it is contended, wait for rw lock to become available, then enter it.
void LockForWrite();
/// Try to acquires a rw lock in shared mode if it is not contended in exclusive mode.
/// Does not wait for the rw lock to become available.
/// @returns True if the rw lock was acquired, false otherwise.
bool TryLockForRead();
/// Try to acquires a rw lock in exclusive mode if it is not contended.
/// Does not wait for the rw lock to become available.
/// @returns True if the rw lock was acquired, false otherwise.
bool TryLockForWrite();
/// Release the rw lock which is previously contended in shared mode.
void UnlockForRead();
/// Release the rw lock which is previously contended in exclusive mode.
void UnlockForWrite();
/// Returns the OS specific RWLOCK data.
RWLockData* GetRWLockData() { return &m_osRWLock; }
private:
RWLockData m_osRWLock; ///< Opaque structure to the OS-specific RWLock data
PAL_DISALLOW_COPY_AND_ASSIGN(RWLock);
};
/**
***********************************************************************************************************************
* @brief A "resource acquisition is initialization" (RAII) wrapper for the RWLock class.
*
* The RAII paradigm allows rw lcok to be automatically acquired during this class' constructor, and
* automatically released when a stack-allocated wrapper object goes out-of-scope. As such, it only makes sense to use
* this class for stack-allocated objects.
*
* This object will ensure that anything between when the object is allocated on the stack and when it goes out of scope
* will be protected from access by multiple threads. See the below example.
*
* [Code not protected]
* {
* [Code not protected]
* RWLockAuto lock(pPtrToMutex, type);
* [Code is protected]
* }
* [Code not protected]
***********************************************************************************************************************
*/
template <RWLock::LockType type>
class RWLockAuto
{
public:
/// Locks the given RWLock.
explicit RWLockAuto(RWLock* pRWLock) : m_pRWLock(pRWLock)
{
PAL_ASSERT(m_pRWLock != nullptr);
if (type == RWLock::ReadOnly)
{
m_pRWLock->LockForRead();
}
else
{
m_pRWLock->LockForWrite();
}
}
/// Unlocks the RWLock we locked in the constructor.
~RWLockAuto()
{
if (type == RWLock::ReadOnly)
{
m_pRWLock->UnlockForRead();
}
else
{
m_pRWLock->UnlockForWrite();
}
}
private:
RWLock* const m_pRWLock; ///< The RWLock which this object wraps.
PAL_DISALLOW_DEFAULT_CTOR(RWLockAuto);
PAL_DISALLOW_COPY_AND_ASSIGN(RWLockAuto);
};
/// Yields the current thread to another thread in the ready state (if available).
extern void YieldThread();
/// Atomic write of 64-bit unsigned integer, using a relaxed memory ordering policy.
/// If you need to synchronize more than just pTarget, you may need a new function.
///
/// @param [in] pTarget Pointer to the value to be read.
///
/// @returns The original value of *pTarget.
extern void AtomicWriteRelaxed64(volatile uint64* pTarget, uint64 newValue);
/// Atomic read of 64-bit unsigned integer, using a relaxed memory ordering policy.
/// If you need to synchronize more than just pTarget, you may need a new function.
///
/// @param [in] pTarget Pointer to the value to be read.
///
/// @returns The original value of *pTarget.
extern uint64 AtomicReadRelaxed64(const volatile uint64* pTarget);
/// Atomically increments the specified 32-bit unsigned integer.
///
/// @param [in,out] pValue Pointer to the value to be incremented.
///
/// @returns Result of the increment operation.
extern uint32 AtomicIncrement(volatile uint32* pValue);
/// Atomically increment a 64-bit-unsigned integer
///
/// @param [in,out] pAddend Pointer to the value to be incremented
///
/// @returns Result of the increment operation.
extern uint64 AtomicIncrement64(volatile uint64* pAddend);
/// Atomically decrements the specified 32-bit unsigned integer.
///
/// @param [in,out] pValue Pointer to the value to be decremented.
///
/// @returns Result of the decrement operation.
extern uint32 AtomicDecrement(volatile uint32* pValue);
/// Atomically decrements the specified 64-bit unsigned integer.
///
/// @param [in,out] pValue Pointer to the value to be decremented.
///
/// @returns Result of the decrement operation.
extern uint32 AtomicDecrement64(volatile uint64* pValue);
/// Performs an atomic compare and swap operation on two 32-bit unsigned integers. This operation compares *pTarget
/// with oldValue and replaces it with newValue if they match. If the values don't match, no action is taken.
/// The original value of *pTarget is returned as a result.
///
/// @param [in,out] pTarget Pointer to the destination value of the operation.
/// @param [in] oldValue Value to compare *pTarget to.
/// @param [in] newValue Value to replace *pTarget with if *pTarget matches oldValue.
///
/// @returns Previous value at *pTarget.
extern uint32 AtomicCompareAndSwap(volatile uint32* pTarget, uint32 oldValue, uint32 newValue);
/// Atomically exchanges a pair of 32-bit unsigned integers.
///
/// @param [in,out] pTarget Pointer to the destination value of the operation.
/// @param [in] value New value to be stored in *pTarget.
///
/// @returns Previous value at *pTarget.
extern uint32 AtomicExchange(volatile uint32* pTarget, uint32 value);
/// Atomically exchanges a pair of 64-bit unsigned integers.
///
/// @param [in,out] pTarget Pointer to the destination value of the operation.
/// @param [in] value New value to be stored in *pTarget.
///
/// @returns Previous value at *pTarget.
extern uint64 AtomicExchange64(volatile uint64* pTarget, uint64 value);
/// Atomically exchanges a pair of pointers.
///
/// @param [in,out] ppTarget Pointer to the address to exchange. The function sets the address pointed to by *ppTarget
/// to pValue.
/// @param [in] pValue New pointer to be stored in *ppTarget.
///
/// @returns Previous value at *ppTarget.
extern void* AtomicExchangePointer(void*volatile* ppTarget, void* pValue);
/// Performs an atomic compare and swap operation on a pair of pointers. This operation compares *ppTarget
/// with pOldValue and replaces it with pNewValue if they match. If the values don't match, no action is taken.
/// The original value of *ppTarget is returned as a result.
///
/// @param [in,out] ppTarget Pointer to the destination value of the operation.
/// @param [in] pOldValue Old pointer to compare *ppTarget to.
/// @param [in] pNewValue New pointer to replace *ppTarget with if *ppTarget matches pOldValue.
///
/// @returns Previous value at *ppTarget.
extern void* AtomicCompareExchangePointer(void*volatile* ppTarget, void* pOldValue, void* pNewValue);
/// Atomically add a value to the specific 32-bit unsigned integer.
///
/// @param [in,out] pAddend Pointer to the value to be modified.
/// @param [in] value Value to add to *pAddend.
///
/// @returns Result of the add operation.
extern uint32 AtomicAdd(volatile uint32* pAddend, uint32 value);
/// Atomically add a value to the specified 64-bit unsigned integer.
///
/// @param [in,out] pAddend Pointer to the value to be modified.
/// @param [in] value Value to add to *pAddend.
///
/// @returns Result of the add operation.
extern uint64 AtomicAdd64(volatile uint64* pAddend, uint64 value);
/// Atomically OR a value to the specific 32-bit unsigned integer.
///
/// @param [in,out] pTarget Pointer to the value to be modified.
/// @param [in] value Value to OR to *pTarget.
///
/// @returns The original value of *pTarget.
extern uint32 AtomicOr(volatile uint32* pTarget, uint32 value);
/// Atomically OR a value to the specified 64-bit unsigned integer.
///
/// @param [in,out] pTarget Pointer to the value to be modified.
/// @param [in] value Value to OR to *pTarget.
///
/// @returns The original value of *pTarget.
extern uint64 AtomicOr64(volatile uint64* pTarget, uint64 value);
/// Atomically AND a value to the specific 32-bit unsigned integer.
///
/// @param [in,out] pTarget Pointer to the value to be modified.
/// @param [in] value Value to AND to *pTarget.
///
/// @returns The original value of *pTarget.
extern uint32 AtomicAnd(volatile uint32* pTarget, uint32 value);
/// Atomically AND a value to the specified 64-bit unsigned integer.
///
/// @param [in,out] pTarget Pointer to the value to be modified.
/// @param [in] value Value to AND to *pTarget.
///
/// @returns The original value of *pTarget.
extern uint64 AtomicAnd64(volatile uint64* pTarget, uint64 value);
} // Util