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) {}
149 _acqState = ReadAcquired;
159 _acqState = ReadAcquired;
171 _acqState = WriteAcquired;
181 _acqState = WriteAcquired;
194 _acqState = WriteAcquired;
206 _acqState = ReadAcquired;
212 void _ReleaseRead() {
215 _acqState = NotAcquired;
218 void _ReleaseWrite() {
221 _acqState = NotAcquired;
233 if (ARCH_LIKELY(!(_lockState.fetch_add(OneReader) & WriterFlag))) {
241 _lockState -= OneReader;
262 _lockState -= OneReader;
269 int state = _lockState.fetch_or(WriterFlag);
270 if (!(state & WriterFlag)) {
297 _lockState &= ~WriterFlag;
312 int state = _lockState.fetch_or(WriterFlag);
313 if (!(state & WriterFlag)) {
316 if (_lockState.fetch_sub(
317 OneReader) != (OneReader | WriterFlag)) {
337 _lockState += (OneReader-1);
345 enum _StagedAcquireWriteState {
358 _StagedAcquireWriteState
359 _StagedAcquireWriteStep(_StagedAcquireWriteState curState) {
362 case _StageNotAcquired:
363 state = _lockState.fetch_or(WriterFlag);
364 if (!(state & WriterFlag)) {
367 return state == 0 ? _StageAcquired : _StageAcquiring;
370 return _StageNotAcquired;
371 case _StageAcquiring:
374 return _StageAcquired;
377 return _StageAcquired;
381 TF_API
void _WaitForReaders()
const;
382 TF_API
void _WaitForWriter()
const;
384 std::atomic<int> _lockState;
387PXR_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.
bool TryAcquireRead()
Try to acquire a read lock on this lock's associated mutex.
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...
bool TryAcquireWrite()
Try to acquire a write lock on this lock's associated mutex.
bool TryAcquire(TfSpinRWMutex &m, bool write=true)
If the current scoped lock is acquired, Release() it, then associate this lock with m and try to acqu...
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.
bool TryAcquire(bool write=true)
Try to acquire either a read or a write lock on this lock's associated mutex.
void AcquireWrite()
Acquire a write lock on this lock's associated mutex.
void AcquireRead()
Acquire a read lock on this lock's associated mutex.