A spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available.
Questions tagged [spinlock]
318 questions
7
votes
3 answers
Why is "sleeping" not allowed while holding a spinlock?
Possible Duplicate:
Why can't you sleep while holding spinlock?
As far as I know, spinlocks should be used in short duration, and are only choices in code such as interrupt handler where sleeping (preemption) is not allowed.
However, I do not…

SHH
- 3,226
- 1
- 28
- 41
7
votes
0 answers
What is the purpose of these padding instructions in a spin-lock in a critical section on Windows?
I've been reverse engineering the EnterCriticalSection function on Windows 10 and found this interesting spin-loop:
It goes:
lbl_loop:
mov ecx, [rsp+60h]
mov ecx, [rsp+60h]
mov ecx, [rsp+60h]
pause
mov ecx, [rsp+60h]
inc …

c00000fd
- 20,994
- 29
- 177
- 400
7
votes
1 answer
Why does this spinlock require memory_order_acquire_release instead of just acquire?
// spinlockAcquireRelease.cpp
#include
#include
class Spinlock{
std::atomic_flag flag;
public:
Spinlock(): flag(ATOMIC_FLAG_INIT) {}
void lock(){
while(flag.test_and_set(std::memory_order_acquire) ); // line 12
}
…

Huy Le
- 1,439
- 4
- 19
7
votes
2 answers
How C++ Standard prevents deadlock in spinlock mutex with memory_order_acquire and memory_order_release?
TL:DR: if a mutex implementation uses acquire and release operations, could an implementation do compile-time reordering like would normally be allowed and overlap two critical sections that should be independent, from different locks? This would…

Alex Guteniev
- 12,039
- 2
- 34
- 79
7
votes
1 answer
How to migrate from OSSpinLock to os_unfair_lock()?
As of macOS 10.12, OSSpinLock has been deprecated. The XCode error messages urge me to use os_unfair_lock_unlock() instead.
As a legacy of some open source stuff I'm relying on, I'm using RegexKitLite from 2010.
How can I convert the spin lock…

Tritonal
- 607
- 4
- 16
7
votes
1 answer
Interrupt on a processor while another process is spinning for lock
There is one spin lock in my code which is shared between two threads. When one thread is holding the lock and the other thread is trying to get the lock, the second thread will keep spinning on a processor. So what will happen if an interrupt…

Saturn
- 966
- 7
- 14
7
votes
1 answer
How to use a spin lock if copy_to_user needs to be called?
I have written a small driver to read some data and give it to the user. My driver can be used by more than one application, i.e. it's a reentrant driver, hence the use of a spin lock. But I discovered that copy_to_user should not be called with a…

Suri
- 3,287
- 9
- 45
- 75
7
votes
1 answer
difference between pthread_spinlock and boost::smart_ptr::spinlock?
I found the following spinlock code in boost::smart_ptr:
bool try_lock()
{
return (__sync_lock_test_and_set(&v_, 1) == 0);
}
void lock()
{
for (unsigned k=0; !try_lock(); ++k)
{
if (k<4)
; // spin
else if…

Steve Lorimer
- 27,059
- 17
- 118
- 213
6
votes
1 answer
Why does hint::spin_loop use ISB on aarch64?
In std::hint there's a spin_loop function with the following definition in its documentation:
Emits a machine instruction to signal the processor that it is running in a busy-wait spin-loop (“spin lock”).
Upon receiving the spin-loop signal the…

Błażej Michalik
- 4,474
- 40
- 55
6
votes
1 answer
Are the Linux/SMP spinlocks unnecessarily slow?
Having been reading through Understanding the Linux kernel (Bovet & Cesati), the chapter on Kernel Synchronisation states that the spin lock acquisition code boils down to:
1: lock:
btsl $0, slp
jnc 3
2: testb $1, slp
jne 2
…

paxdiablo
- 854,327
- 234
- 1,573
- 1,953
6
votes
2 answers
Avoiding sleep while holding a spinlock
I've recently read section 5.5.2 (Spinlocks and Atomic Context) of LDDv3 book:
Avoiding sleep while holding a lock can be more difficult; many kernel functions can sleep, and this behavior is not always well documented. Copying data to or from user…

Dmitry Krivenok
- 326
- 3
- 10
6
votes
2 answers
How is a spin lock woken up in Linux/ARM64?
In the Linux kernel, arch_spin_lock() is implemented as follows:
static inline void arch_spin_lock(arch_spinlock_t *lock)
{
unsigned int tmp;
arch_spinlock_t lockval, newval;
asm volatile(
/* Atomically increment the next ticket.…

pranith
- 869
- 9
- 24
6
votes
1 answer
How can Microsoft's OpenMP spinlock time be controlled?
The OpenMP used by the Intel compiler supports an environment variable KMP_BLOCKTIME (docs) which I believe controls the busy-waiting (spinlocked) time the threads will spend waiting for new work (linked document claims this defaults to 200ms).
The…

timday
- 24,582
- 12
- 83
- 135
6
votes
1 answer
In the linux kernel, will a call to kfree ever sleep?
The title is pretty much the question...
I am writing some code that uses a spinlock to help in list management. The documentation on spinlocks are pretty clear about not making a call to anything that would sleep. I know there are ways to…

Gabe
- 847
- 1
- 9
- 16
6
votes
4 answers
Why linux disables kernel preemption after the kernel code holds a spinlock?
I am new to Linux and am reading Linux device drivers book by Rubini & Corbet. I am confused at one statement related to spinlocks; the book states
If a nonpreemptive uniprocessor system ever went into a
spin on a lock, it would spin forever; no…

Vishal Gandhi
- 85
- 1
- 4