7#ifndef PXR_BASE_TF_BIG_RW_MUTEX_H
8#define PXR_BASE_TF_BIG_RW_MUTEX_H
11#include "pxr/base/tf/api.h"
17#include "pxr/base/tf/spinRWMutex.h"
23PXR_NAMESPACE_OPEN_SCOPE
56 static constexpr unsigned NumStates = 16;
61 static constexpr int NotLocked = 0;
62 static constexpr int WriteLocked = -1;
74 static constexpr int NotAcquired = -1;
75 static constexpr int WriteAcquired = -2;
81 , _acqState(NotAcquired) {
86 ScopedLock() : _mutex(nullptr), _acqState(NotAcquired) {}
136 _acqState = _mutex->_AcquireRead(_GetSeed());
143 _mutex->_AcquireWrite();
144 _acqState = WriteAcquired;
162 void _ReleaseRead() {
164 _mutex->_ReleaseRead(_acqState);
165 _acqState = NotAcquired;
168 void _ReleaseWrite() {
169 TF_AXIOM(_acqState == WriteAcquired);
170 _mutex->_ReleaseWrite();
171 _acqState = NotAcquired;
176 inline int _GetSeed()
const {
177 return static_cast<int>(
178 static_cast<unsigned>(
TfHash()(
this)) >> 8);
190 inline int _AcquireRead(
int seed) {
192 int stateIndex = seed % NumStates;
193 if (ARCH_UNLIKELY(_writerActive) ||
194 !_states[stateIndex].mutex.TryAcquireRead()) {
195 _AcquireReadContended(stateIndex);
201 TF_API
void _AcquireReadContended(
int stateIndex);
203 void _ReleaseRead(
int stateIndex) {
204 _states[stateIndex].mutex.ReleaseRead();
207 TF_API
void _AcquireWrite();
208 TF_API
void _ReleaseWrite();
214 char _unused_padding[
218 std::unique_ptr<_LockState []> _states;
219 std::atomic<bool> _writerActive;
223PXR_NAMESPACE_CLOSE_SCOPE
Provide architecture-specific memory-alignment information.
This class implements a readers-writer mutex and provides a scoped lock utility.
TF_API TfBigRWMutex()
Construct a mutex, initially unlocked.
A user-extensible hashing mechanism for use with runtime hash tables.
This class implements a readers-writer spin lock that emphasizes throughput when there is light conte...
Stripped down version of diagnostic.h that doesn't define std::string.
#define ARCH_CACHE_LINE_SIZE
The size of a CPU cache line on the current processor architecture in bytes.
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
Scoped lock utility class.
bool UpgradeToWriter()
Change this lock's acquisition state from a read lock to a write lock.
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.
ScopedLock(TfBigRWMutex &m, bool write=true)
Construct a scoped lock for mutex m and acquire either a read or a write lock depending on write.
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 Acquire(TfBigRWMutex &m, bool write=true)
If the current scoped lock is acquired, Release() it, then associate this lock with m and acquire eit...
void AcquireWrite()
Acquire a write lock on this lock's associated mutex.
void AcquireRead()
Acquire a read lock on this lock's associated mutex.