HyperDbg Debugger
Loading...
Searching...
No Matches
Spinlock.h File Reference

Headers of spinlock routines. More...

Go to the source code of this file.

Macros

#define ScopedSpinlock(LockObject, CodeToRun)

Functions

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, UINT32 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.

Detailed Description

Headers of spinlock routines.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-10

Macro Definition Documentation

◆ ScopedSpinlock

#define ScopedSpinlock ( LockObject,
CodeToRun )
Value:
SpinlockUnlock(&LockObject), \
CodeToRun)
#define MetaScopedExpr(Pre_, Post_, ScopedExpr_)
Definition MetaMacros.h:14
VOID SpinlockLock(volatile LONG *Lock)
Tries to get the lock and won't return until successfully get the lock.
Definition Spinlock.c:53
VOID SpinlockUnlock(volatile LONG *Lock)
Release the lock.
Definition Spinlock.c:162
39#define ScopedSpinlock(LockObject, CodeToRun) \
40 MetaScopedExpr(SpinlockLock(&LockObject), \
41 SpinlockUnlock(&LockObject), \
42 CodeToRun)

Function Documentation

◆ SpinlockCheckLock()

BOOLEAN SpinlockCheckLock ( volatile LONG * Lock)

Check the lock without changing the state.

Parameters
LockLock variable
Returns
BOOLEAN Whether the lock is acquired or not
175{
176 if (*Lock)
177 {
178 return TRUE;
179 }
180 else
181 {
182 return FALSE;
183 }
184}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113

◆ 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
DestinationA pointer to the destination value
ExchangeThe exchange value
ComperandThe value to compare to Destination
Returns
VOID
94{
95 UINT32 Wait = 1;
96
97 while (InterlockedCompareExchange(Destination, Exchange, Comperand) != Comperand)
98 {
99 for (UINT32 i = 0; i < Wait; ++i)
100 {
101 _mm_pause();
102 }
103
104 //
105 // Don't call "pause" too many times. If the Wait becomes too big,
106 // clamp it to the MaxWait.
107 //
108
109 if (Wait * 2 > g_MaxWait)
110 {
111 Wait = g_MaxWait;
112 }
113 else
114 {
115 Wait = Wait * 2;
116 }
117 }
118}
unsigned int UINT32
Definition BasicTypes.h:54

◆ SpinlockLock()

VOID SpinlockLock ( volatile LONG * Lock)

Tries to get the lock and won't return until successfully get the lock.

Parameters
LockLock variable
Returns
VOID
54{
55 UINT32 Wait = 1;
56
57 while (!SpinlockTryLock(Lock))
58 {
59 for (UINT32 i = 0; i < Wait; ++i)
60 {
61 _mm_pause();
62 }
63
64 //
65 // Don't call "pause" too many times. If the Wait becomes too big,
66 // clamp it to the MaxWait.
67 //
68
69 if (Wait * 2 > g_MaxWait)
70 {
71 Wait = g_MaxWait;
72 }
73 else
74 {
75 Wait = Wait * 2;
76 }
77 }
78}
BOOLEAN SpinlockTryLock(volatile LONG *Lock)
Tries to get the lock otherwise returns.
Definition Spinlock.c:41

◆ SpinlockLockWithCustomWait()

VOID SpinlockLockWithCustomWait ( volatile LONG * Lock,
UINT32 MaximumWait )

Tries to get the lock and won't return until successfully get the lock.

Parameters
LockLock variable
MaximumWaitMaximum wait (pause) count
Returns
VOID
129{
130 UINT32 Wait = 1;
131
132 while (!SpinlockTryLock(Lock))
133 {
134 for (UINT32 i = 0; i < Wait; ++i)
135 {
136 _mm_pause();
137 }
138
139 //
140 // Don't call "pause" too many times. If the Wait becomes too big,
141 // clamp it to the MaxWait.
142 //
143
144 if (Wait * 2 > MaximumWait)
145 {
146 Wait = MaximumWait;
147 }
148 else
149 {
150 Wait = Wait * 2;
151 }
152 }
153}

◆ SpinlockTryLock()

BOOLEAN SpinlockTryLock ( volatile LONG * Lock)

Tries to get the lock otherwise returns.

Parameters
LockLock variable
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
LockLock variable
Returns
VOID
163{
164 *Lock = 0;
165}