7#ifndef PXR_BASE_TF_SPIN_RW_MUTEX_H
8#define PXR_BASE_TF_SPIN_RW_MUTEX_H
11#include "pxr/base/tf/api.h"
18PXR_NAMESPACE_OPEN_SCOPE
52 static constexpr int OneReader = 2;
53 static constexpr int WriterFlag = 1;
65 static constexpr int NotAcquired = 0;
66 static constexpr int ReadAcquired = 1;
67 static constexpr int WriteAcquired = 2;
73 , _acqState(NotAcquired) {
78 ScopedLock() : _mutex(nullptr), _acqState(NotAcquired) {}
130 _acqState = ReadAcquired;
138 _acqState = WriteAcquired;
147 _acqState = WriteAcquired;
158 _acqState = ReadAcquired;
164 void _ReleaseRead() {
167 _acqState = NotAcquired;
170 void _ReleaseWrite() {
173 _acqState = NotAcquired;
185 if (ARCH_LIKELY(!(_lockState.fetch_add(OneReader) & WriterFlag))) {
193 _lockState -= OneReader;
214 _lockState -= OneReader;
221 int state = _lockState.fetch_or(WriterFlag);
222 if (!(state & WriterFlag)) {
249 _lockState &= ~WriterFlag;
264 int state = _lockState.fetch_or(WriterFlag);
265 if (!(state & WriterFlag)) {
268 if (_lockState.fetch_sub(
269 OneReader) != (OneReader | WriterFlag)) {
289 _lockState += (OneReader-1);
297 enum _StagedAcquireWriteState {
310 _StagedAcquireWriteState
311 _StagedAcquireWriteStep(_StagedAcquireWriteState curState) {
314 case _StageNotAcquired:
315 state = _lockState.fetch_or(WriterFlag);
316 if (!(state & WriterFlag)) {
319 return state == 0 ? _StageAcquired : _StageAcquiring;
322 return _StageNotAcquired;
323 case _StageAcquiring:
326 return _StageAcquired;
329 return _StageAcquired;
333 TF_API
void _WaitForReaders()
const;
334 TF_API
void _WaitForWriter()
const;
336 std::atomic<int> _lockState;
339PXR_NAMESPACE_CLOSE_SCOPE
This class implements a readers-writer mutex and provides a scoped lock utility.
This class implements a readers-writer spin lock that emphasizes throughput when there is light conte...
bool UpgradeToWriter()
Upgrade this thread's lock on this mutex (which must be a read lock) to a write lock.
bool DowngradeToReader()
Downgrade this mutex, which must be locked for write by this thread, to being locked for read by this...
bool TryAcquireRead()
Attempt to acquire a read lock on this mutex without waiting for writers.
TfSpinRWMutex()
Construct a mutex, initially unlocked.
void ReleaseRead()
Release this thread's read lock on this mutex.
bool TryAcquireWrite()
Attempt to acquire a write lock on this mutex without waiting for other writers.
void ReleaseWrite()
Release this thread's write lock on this mutex.
void AcquireWrite()
Acquire a write lock on this mutex.
void AcquireRead()
Acquire a read lock on this mutex.
Stripped down version of diagnostic.h that doesn't define std::string.
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Scoped lock utility class.
bool UpgradeToWriter()
Change this lock's acquisition state from a read lock to a write lock.
ScopedLock(TfSpinRWMutex &m, bool write=true)
Construct a scoped lock for mutex m and acquire either a read or a write lock depending on write.
void Acquire(bool write=true)
Acquire either a read or write lock on this lock's associated mutex depending on write.
ScopedLock()
Construct a scoped lock not associated with a mutex.
bool DowngradeToReader()
Change this lock's acquisition state from a write lock to a read lock.
void Acquire(TfSpinRWMutex &m, bool write=true)
If the current scoped lock is acquired, Release() it, then associate this lock with m and acquire eit...
void Release()
Release the currently required lock on the associated mutex.
~ScopedLock()
If this scoped lock is acquired for either read or write, Release() it.
void AcquireWrite()
Acquire a write lock on this lock's associated mutex.
void AcquireRead()
Acquire a read lock on this lock's associated mutex.