What are spin locks?

I always wondered what it was: every time I hear about them, images of futuristic flywheels seem to dance (roll?) In my mind ...

What are they?

+88
spinlock
Dec 24 '09 at 8:34
source share
11 answers

When you use regular locks (mutexes, critical sections, etc.), the operating system puts your thread in the WAIT state and interrupts it, scheduling other threads on the same kernel. This results in poor performance if the latency is really short, because your thread must now wait for a lead in order to get the CPU time again.

In addition, kernel objects are not available in all kernel states, for example, in the interrupt handler, when swapping is not available, etc.

Spinlocks do not cause an interrupt, but wait in a loop ("spin") until the other kernel releases the lock. This prevents the thread from losing its quantum and continue as soon as the lock is released. A simple spin-lock mechanism allows the kernel to use it in almost any state.

Therefore, on a single-core computer, spinlocking is simply “disabling interrupts” or “boosting IRQL,” which completely prevents thread scheduling.

Spin locks ultimately allow kernels to avoid "large kernel locks" (the lock obtained when the kernel enters the kernel and is released upon exit) and has granular locking of the kernel primitives, causing better multiprocessing on multi-core machines and therefore higher performance .

EDIT: The question arose: "Does this mean that I should use spinlocks wherever possible?" and I will try to answer him:

As I already mentioned, spin locks are only useful in places where the expected latency is shorter than a quantum (read: milliseconds), and unloading does not make much sense (for example, kernel objects are not available).

If the wait time is unknown or you are in user mode, spinlocks are not effective. You consume 100% of the processor time on the standby kernel by checking if spin lock is available. You prevent other threads from running on this kernel before your quantum expires. This scenario is only possible for short packets at the kernel level and is unlikely for a user mode application.

Here is a SO question regarding this: Spinlocks, how useful are they?

+106
Dec 24 '09 at 8:55
source share
— -

Say that the resource is protected by a lock, the thread that wants to access the resource must first get the lock. If the lock is not available, the thread can repeatedly check whether the lock is released. During this time, waiting for a thread waits, checks for a lock, uses a CPU, but does no useful work. Such a lock is called a spin lock.

+19
Dec 24 '09 at 8:40
source share

There are a lot of cycles that continue until a certain condition is met:

while(cantGoOn) {}; 
+17
Dec 24 '09 at 8:38
source share
  while(something != TRUE ){}; // it happend move_on(); 
+7
Dec 24 '09 at 8:37
source share

This is the type of lock that makes waiting busy

It was considered an anti-pattern, with the exception of programming a driver of a very low level (when it may happen that calling the “right” wait function has more costs than just busy locking for several cycles).

See, for example, Spinlocks in the Linux kernel .

+5
Dec 24 '09 at 9:25
source share

SpinLocks are those in which a thread waits until a lock is available. This is usually used to avoid the overhead of obtaining kernel objects when, for a short period of time, there is a scope for obtaining a kernel object.

Example:

 While(SpinCount-- && Kernel Object is not free) {} try acquiring Kernel object 
+3
Dec 24 '09 at 8:42
source share

You want to use spin lock if you think it’s cheaper to enter a busy waiting cycle and combine the resource instead of locking when the resource is locked.

Spinning can be useful when locks are fine-grained and large (for example, lock on a node in a linked list), and also when lock times are always very short. In the general case, while holding the spin lock, you need to avoid locking, call everything that by itself can block, simultaneously hold more than one spin lock, dynamically send calls (interface and virtual machines), making statically sent calls to any code, t or memory allocation.

It is also important to note that SpinLock is a value type for performance reasons. Thus, one must be very careful not to accidentally copy the SpinLock instance, since the two instances (the original and the copy) will then be completely independent of each other, which is likely to lead to erroneous application behavior. If a SpinLock instance is to be passed, it must be passed by reference, not by value.

+3
Nov 12 '15 at 12:26
source share

In a nutshell, spin-lock uses atomic comparison and swap (CAS) or test and set as instructions for implementing lock-free, lifeless iteration. Such structures scale well in multi-core machines.

+1
Dec 24 '09 at 8:43
source share

This is a loop that revolves around until a condition is met.

0
Dec 24 '09 at 8:37
source share

Well, yes - the spin lock point (compared to traditional critical sections, etc.) is that they offer better performance in some circumstances (multi-core systems ..), since they do not immediately give the rest of the stream quantum,

0
Dec 24 '09 at 8:43
source share

Spinlock is a type of lock that is non-blocking and cannot sleep. Any thread that wants to obtain a spin lock for any shared or critical resource will continuously rotate, losing the CPU processing cycle, until it receives a lock for the specified resource. After receiving a spin lock, he tries to complete the work in his quantum and then release the resource accordingly. Spinlock is the type of lock with the highest priority, just to say that this is not a preventive type of lock.

0
May 29 '17 at 8:11 a.m.
source share



All Articles