24#ifndef PXR_BASE_TF_BIG_RW_MUTEX_H
25#define PXR_BASE_TF_BIG_RW_MUTEX_H
28#include "pxr/base/tf/api.h"
34#include "pxr/base/tf/spinRWMutex.h"
40PXR_NAMESPACE_OPEN_SCOPE
73 static constexpr unsigned NumStates = 16;
78 static constexpr int NotLocked = 0;
79 static constexpr int WriteLocked = -1;
91 static constexpr int NotAcquired = -1;
92 static constexpr int WriteAcquired = -2;
98 , _acqState(NotAcquired) {
153 _acqState = _mutex->_AcquireRead(_GetSeed());
160 _mutex->_AcquireWrite();
161 _acqState = WriteAcquired;
179 void _ReleaseRead() {
181 _mutex->_ReleaseRead(_acqState);
182 _acqState = NotAcquired;
185 void _ReleaseWrite() {
186 TF_AXIOM(_acqState == WriteAcquired);
187 _mutex->_ReleaseWrite();
188 _acqState = NotAcquired;
193 inline int _GetSeed()
const {
194 return static_cast<int>(
195 static_cast<unsigned>(
TfHash()(
this)) >> 8);
207 inline int _AcquireRead(
int seed) {
209 int stateIndex = seed % NumStates;
210 if (ARCH_UNLIKELY(_writerActive) ||
211 !_states[stateIndex].mutex.TryAcquireRead()) {
212 _AcquireReadContended(stateIndex);
218 TF_API
void _AcquireReadContended(
int stateIndex);
220 void _ReleaseRead(
int stateIndex) {
221 _states[stateIndex].mutex.ReleaseRead();
224 TF_API
void _AcquireWrite();
225 TF_API
void _ReleaseWrite();
231 char _unused_padding[
235 std::unique_ptr<_LockState []> _states;
236 std::atomic<bool> _writerActive;
240PXR_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.