AGX Dynamics 2.40.0.0
Loading...
Searching...
No Matches
SpinMutex.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_SPINMUTEX_H
18#define AGX_SPINMUTEX_H
19
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
41namespace agx
42{
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.
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 */
92
94 : m_lock{0}
95 {
96 }
97
99 {
100 agxAssert(forcedShutdown() || !this->isLocked());
101 }
102
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
121 {
122 #if AGX_DEBUG_SYNCHRONIZATION_OVERHEAD_TESTING()
123 return;
124 #endif
125
126 m_lock.store(0, std::memory_order_release);
127 }
128
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
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
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 */
#define AGXCORE_EXPORT
Spin-lock mutex.
Definition: SpinMutex.h:47
void unlock()
Unlock the mutex.
Definition: SpinMutex.h:120
void lock()
Lock the mutex.
Definition: SpinMutex.h:103
bool trylock()
Deprecated.
Definition: SpinMutex.h:129
SpinMutex & operator=(const SpinMutex &other)
bool try_lock()
Definition: SpinMutex.h:139
bool isLocked() const
Debug.
Definition: SpinMutex.h:149
SpinMutex(const SpinMutex &other)
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
int32_t Int32
Definition: Integer.h:37
AGXCORE_EXPORT bool forcedShutdown()