Loading...
Searching...
No Matches
spinMutex.h
1//
2// Copyright 2023 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_SPIN_MUTEX_H
25#define PXR_BASE_TF_SPIN_MUTEX_H
26
27#include "pxr/pxr.h"
28#include "pxr/base/tf/api.h"
29
30#include "pxr/base/arch/hints.h"
32
33#include <atomic>
34
35PXR_NAMESPACE_OPEN_SCOPE
36
59{
60public:
61
63 TfSpinMutex() : _lockState(false) {}
64
67 struct ScopedLock {
68
71 : _mutex(&m)
72 , _acquired(false) {
73 Acquire();
74 }
75
77 ScopedLock() : _mutex(nullptr), _acquired(false) {}
78
81 Release();
82 }
83
87 Release();
88 _mutex = &m;
89 Acquire();
90 }
91
94 void Release() {
95 if (_acquired) {
96 _Release();
97 }
98 }
99
102 void Acquire() {
103 TF_DEV_AXIOM(!_acquired);
104 _mutex->Acquire();
105 _acquired = true;
106 }
107
108 private:
109
110 void _Release() {
111 TF_DEV_AXIOM(_acquired);
112 _mutex->Release();
113 _acquired = false;
114 }
115
116 TfSpinMutex *_mutex;
117 bool _acquired;
118 };
119
124 inline bool TryAcquire() {
125 return _lockState.exchange(true, std::memory_order_acquire) == false;
126 }
127
131 void Acquire() {
132 if (ARCH_LIKELY(TryAcquire())) {
133 return;
134 }
135 _AcquireContended();
136 }
137
139 inline void Release() {
140 _lockState.store(false, std::memory_order_release);
141 }
142
143private:
144
145 TF_API void _AcquireContended();
146
147 std::atomic<bool> _lockState;
148};
149
150PXR_NAMESPACE_CLOSE_SCOPE
151
152#endif // PXR_BASE_TF_SPIN_MUTEX_H
This class implements a simple spin lock that emphasizes throughput when there is little to no conten...
Definition: spinMutex.h:59
TfSpinMutex()
Construct a mutex, initially unlocked.
Definition: spinMutex.h:63
void Release()
Release this thread's lock on this mutex.
Definition: spinMutex.h:139
void Acquire()
Acquire a lock on this mutex.
Definition: spinMutex.h:131
bool TryAcquire()
Acquire a lock on this mutex if it is not currently held by another thread.
Definition: spinMutex.h:124
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.
Definition: diagnostic.h:222
Compiler hints.
Scoped lock utility class.
Definition: spinMutex.h:67
ScopedLock()
Construct a scoped lock not associated with a mutex.
Definition: spinMutex.h:77
void Acquire(TfSpinMutex &m)
If the current scoped lock is acquired, Release() it, then associate this lock with m and acquire a l...
Definition: spinMutex.h:86
void Release()
Release the currently required lock on the associated mutex.
Definition: spinMutex.h:94
ScopedLock(TfSpinMutex &m)
Construct a scoped lock for mutex m and acquire a lock.
Definition: spinMutex.h:70
void Acquire()
Acquire a lock on this lock's associated mutex.
Definition: spinMutex.h:102
~ScopedLock()
If this scoped lock is acquired, Release() it.
Definition: spinMutex.h:80