Headers of spinlock routines.
More...
Go to the source code of this file.
|
BOOLEAN | SpinlockTryLock (volatile LONG *Lock) |
| Tries to get the lock otherwise returns.
|
|
BOOLEAN | SpinlockCheckLock (volatile LONG *Lock) |
| Check the lock without changing the state.
|
|
void | SpinlockLock (volatile LONG *Lock) |
| Tries to get the lock and won't return until successfully get the lock.
|
|
void | SpinlockLockWithCustomWait (volatile LONG *Lock, unsigned MaxWait) |
| Tries to get the lock and won't return until successfully get the lock.
|
|
void | SpinlockUnlock (volatile LONG *Lock) |
| Release the lock.
|
|
void | SpinlockInterlockedCompareExchange (LONG volatile *Destination, LONG Exchange, LONG Comperand) |
| Interlocked spinlock that tries to change the value and makes sure that it changed the target value.
|
|
Headers of spinlock routines.
- Author
- Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
- Version
- 0.1
- Date
- 2020-04-10
- Copyright
- This project is released under the GNU Public License v3.
◆ ScopedSpinlock
#define ScopedSpinlock |
( |
| LockObject, |
|
|
| CodeToRun ) |
Value:
CodeToRun)
void SpinlockLock(volatile LONG *Lock)
Tries to get the lock and won't return until successfully get the lock.
Definition Spinlock.c:52
void SpinlockUnlock(volatile LONG *Lock)
Release the lock.
Definition Spinlock.c:158
39#define ScopedSpinlock(LockObject, CodeToRun) \
40 MetaScopedExpr(SpinlockLock(&LockObject), \
41 SpinlockUnlock(&LockObject), \
42 CodeToRun)
◆ SpinlockCheckLock()
BOOLEAN SpinlockCheckLock |
( |
volatile LONG * | Lock | ) |
|
Check the lock without changing the state.
- Parameters
-
170{
171 if (*Lock)
172 {
174 }
175 else
176 {
178 }
179}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
◆ SpinlockInterlockedCompareExchange()
void SpinlockInterlockedCompareExchange |
( |
LONG volatile * | Destination, |
|
|
LONG | Exchange, |
|
|
LONG | Comperand ) |
Interlocked spinlock that tries to change the value and makes sure that it changed the target value.
- Parameters
-
Destination | A pointer to the destination value |
Exchange | The exchange value |
Comperand | The value to compare to Destination |
92{
93 unsigned wait = 1;
94
95 while (InterlockedCompareExchange(Destination, Exchange, Comperand) != Comperand)
96 {
97 for (unsigned i = 0; i < wait; ++i)
98 {
99 _mm_pause();
100 }
101
102
103
104
105
106
107 if (wait * 2 > MaxWait)
108 {
109 wait = MaxWait;
110 }
111 else
112 {
113 wait = wait * 2;
114 }
115 }
116}
◆ SpinlockLock()
void SpinlockLock |
( |
volatile LONG * | Lock | ) |
|
Tries to get the lock and won't return until successfully get the lock.
- Parameters
-
53{
54 unsigned wait = 1;
55
57 {
58 for (unsigned i = 0; i < wait; ++i)
59 {
60 _mm_pause();
61 }
62
63
64
65
66
67
68 if (wait * 2 > MaxWait)
69 {
70 wait = MaxWait;
71 }
72 else
73 {
74 wait = wait * 2;
75 }
76 }
77}
BOOLEAN SpinlockTryLock(volatile LONG *Lock)
Tries to get the lock otherwise returns.
Definition Spinlock.c:41
◆ SpinlockLockWithCustomWait()
void SpinlockLockWithCustomWait |
( |
volatile LONG * | Lock, |
|
|
unsigned | MaximumWait ) |
Tries to get the lock and won't return until successfully get the lock.
- Parameters
-
LONG | Lock variable |
LONG | MaxWait Maximum wait (pause) count |
126{
127 unsigned wait = 1;
128
130 {
131 for (unsigned i = 0; i < wait; ++i)
132 {
133 _mm_pause();
134 }
135
136
137
138
139
140
141 if (wait * 2 > MaximumWait)
142 {
143 wait = MaximumWait;
144 }
145 else
146 {
147 wait = wait * 2;
148 }
149 }
150}
◆ SpinlockTryLock()
BOOLEAN SpinlockTryLock |
( |
volatile LONG * | Lock | ) |
|
Tries to get the lock otherwise returns.
- Parameters
-
- Returns
- BOOLEAN If it was successful on getting the lock
42{
43 return (!(*Lock) && !_interlockedbittestandset(Lock, 0));
44}
◆ SpinlockUnlock()
void SpinlockUnlock |
( |
volatile LONG * | Lock | ) |
|
Release the lock.
- Parameters
-