AGX Dynamics
2.38.0.2
Loading...
Searching...
No Matches
SpinMutex.h
Go to the documentation of this file.
1
/*
2
Copyright 2007-2024. Algoryx Simulation AB.
3
4
All AGX source code, intellectual property, documentation, sample code,
5
tutorials, scene files and technical white papers, are copyrighted, proprietary
6
and confidential material of Algoryx Simulation AB. You may not download, read,
7
store, distribute, publish, copy or otherwise disseminate, use or expose this
8
material unless having a written signed agreement with Algoryx Simulation AB, or having been
9
advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10
valid commercial license from Algoryx Simulation AB.
11
12
Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13
from using this software, unless otherwise stated in written agreements with
14
Algoryx Simulation AB.
15
*/
16
17
#ifndef AGX_SPINMUTEX_H
18
#define AGX_SPINMUTEX_H
19
20
#include <agx/config/AGX_USE_SSE.h>
21
#include <agx/config/AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING.h>
22
#include <
agx/macros.h
>
23
#include <
agx/debug.h
>
24
25
#include <
agx/Integer.h
>
26
#include <atomic>
27
28
#if AGX_USE_SSE()
29
#include <xmmintrin.h>
30
#include <emmintrin.h>
31
#endif
32
33
34
#ifdef _MSC_VER
35
# pragma warning(push)
36
# pragma warning( disable : 4275 )
// warning C4275: non dll-interface class
37
# pragma warning(disable: 4251)
// warning C4251: class X needs to have dll-interface to be used by clients of class Y
38
#endif
39
40
41
namespace
agx
42
{
46
class
AGXCORE_EXPORT
SpinMutex
47
{
48
public
:
49
SpinMutex
();
50
~SpinMutex
();
51
52
// Odd to have copy constructor on a mutex, but existing code requries it.
53
// Try to prevent FormatT<T>::initializeElements on mutexes.
54
SpinMutex
(
const
SpinMutex
& other);
55
56
// Odd to have assignment operator on a mutex, but existing code requires it.
57
// Try to remove ReferencedEntity::setObserverMutex.
58
SpinMutex
&
operator=
(
const
SpinMutex
& other);
59
63
void
lock();
64
68
void
unlock();
69
75
bool
trylock();
76
77
bool
try_lock();
78
82
bool
isLocked()
const
;
83
84
private
:
85
std::atomic<int> m_lock;
86
};
87
88
89
90
/* Implementation */
91
AGXCORE_EXPORT
extern
bool
forcedShutdown
();
92
93
AGX_FORCE_INLINE
SpinMutex::SpinMutex
()
94
: m_lock{0}
95
{
96
}
97
98
AGX_FORCE_INLINE
SpinMutex::~SpinMutex
()
99
{
100
agxAssert
(
forcedShutdown
() || !this->
isLocked
());
101
}
102
103
AGX_FORCE_INLINE
void
SpinMutex::lock
()
104
{
105
#if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
106
return
;
107
#endif
108
109
agx::Int32
prev = m_lock.exchange(1, std::memory_order_acquire);
110
111
while
(prev != 0)
112
{
113
#if AGX_USE_SSE()
114
_mm_pause();
115
#endif
116
prev = m_lock.exchange(1, std::memory_order_acquire);
117
}
118
}
119
120
AGX_FORCE_INLINE
void
SpinMutex::unlock
()
121
{
122
#if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
123
return
;
124
#endif
125
126
m_lock.store(0, std::memory_order_release);
127
}
128
129
AGX_FORCE_INLINE
bool
SpinMutex::trylock
()
130
{
131
#if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
132
return
true
;
133
#endif
134
135
int32_t prev = m_lock.exchange(1, std::memory_order_acquire);
136
return
(prev == 0);
137
}
138
139
AGX_FORCE_INLINE
bool
SpinMutex::try_lock
()
140
{
141
#if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
142
return
true
;
143
#endif
144
145
int32_t prev = m_lock.exchange(1, std::memory_order_acquire);
146
return
(prev == 0);
147
}
148
149
AGX_FORCE_INLINE
bool
SpinMutex::isLocked
()
const
150
{
151
return
m_lock.load( std::memory_order_relaxed ) != 0;
152
}
153
}
154
155
#ifdef _MSC_VER
156
# pragma warning(pop)
157
#endif
158
159
#endif
/* AGX_SPINMUTEX_H */
Integer.h
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agx::SpinMutex
Spin-lock mutex.
Definition
SpinMutex.h:47
agx::SpinMutex::unlock
void unlock()
Unlock the mutex.
Definition
SpinMutex.h:120
agx::SpinMutex::lock
void lock()
Lock the mutex.
Definition
SpinMutex.h:103
agx::SpinMutex::SpinMutex
SpinMutex()
Definition
SpinMutex.h:93
agx::SpinMutex::trylock
bool trylock()
Deprecated.
Definition
SpinMutex.h:129
agx::SpinMutex::operator=
SpinMutex & operator=(const SpinMutex &other)
agx::SpinMutex::try_lock
bool try_lock()
Definition
SpinMutex.h:139
agx::SpinMutex::isLocked
bool isLocked() const
Debug.
Definition
SpinMutex.h:149
agx::SpinMutex::~SpinMutex
~SpinMutex()
Definition
SpinMutex.h:98
agx::SpinMutex::SpinMutex
SpinMutex(const SpinMutex &other)
debug.h
agxAssert
#define agxAssert(expr)
Definition
debug.h:143
macros.h
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition
macros.h:58
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition
AddedMassInteraction.h:23
agx::Int32
int32_t Int32
Definition
Integer.h:37
agx::forcedShutdown
AGXCORE_EXPORT bool forcedShutdown()
include
agx
SpinMutex.h
Generated on Sun Sep 1 2024 11:24:40 for AGX Dynamics by
1.9.8