Questions tagged [spinlock]

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.

318 questions
2
votes
1 answer

Is it safe to use the Interlocked.CompareExchange as a lock, only for enter, not for exit?

I found some multithreaded code in the quite popular LazyCache library, that uses an int[] field as a granular locking mechanism, with the intention to prevent concurrent invocation of a method with the same key as argument. I am highly skeptical…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
2
votes
1 answer

Synchronization primitives in DriverKit

In a DriverKit extension, I would like to block a call from a user client until a specific hardware interrupt fires. Since there are no semaphores available (Does the DriverKit SDK support semaphores?), I've reached for a very basic spinlock using…
2
votes
0 answers

Working example of UMONITOR/UMWAIT-based assembly (asm) spin-wait loops as a replacement for PAUSE-based test-test-and-set loops

In the Intel 64 and IA-32 Architectures Optimization Reference Manual, Intel gives an example of a PAUSE-based spin-wait loop ("Example 11-4. Spin-wait Loop and PAUSE Instruction"). However, as for the UMONITOR/UMWAIT instruction, there are only…
Maxim Masiutin
  • 3,991
  • 4
  • 55
  • 72
2
votes
1 answer

Memory order for a ticket-taking spin-lock mutex

Suppose I have the following ticket-taking spinlock mutex implementation (in C using GCC atomic builtins). As I understand it, the use of the "release" memory order in the unlock function is correct. I'm unsure, though, about the lock function.…
Harris M Snyder
  • 274
  • 1
  • 8
2
votes
0 answers

What could cause a deadlock in ImageProviderReleaseInfoCallback / __spin_lock

I have a Cocoa application that uses a number of NSOperationQueue instances to process images in the background. Each queue processes invocation operations that essentially turn NSImage objects into CGImageRef instances using: CGImageRef ref =…
Mark
  • 6,647
  • 1
  • 45
  • 88
2
votes
1 answer

C++ proper atomic memory ordering on a "thread barrier" synchronization pattern

I need to properly synchronize access to some shared resource between a predefined number of worker threads (statically known via application config) and a predefined number of control-plane threads. The control-plane threads receive requests from…
pa5h1nh0
  • 262
  • 3
  • 13
2
votes
1 answer

SpinLock doesn't really do busy-loop waiting?

I have the following code class Program { static SpinLock sl = new SpinLock(); public static void Foo(object arg) { bool r = false; sl.Enter(ref r); } public static void…
arrowd
  • 33,231
  • 8
  • 79
  • 110
2
votes
0 answers

Spinlock in x86-64 assembly

Hello, Currently I am learning how to write multithreaded programs and I tried to implement spinlock in x86-64 assembly. It works by checking L0C_A1 local variable, if it is equal to 1 then thread can enter critical section, if it is equal to 0 then…
pajacol
  • 41
  • 3
2
votes
1 answer

Implementing spin-lock without XCHG?

C++ spin-lock can be easily implemented using std::atomic_flag, it can be coded roughly (without special features) like following: std::atomic_flag f = ATOMIC_FLAG_INIT; while (f.test_and_set(std::memory_order_acquire)); // Acquire lock // Here do…
Arty
  • 14,883
  • 6
  • 36
  • 69
2
votes
1 answer

Does NDIS spinlock serves as a memory barrier for DMA?

In an NDIS driver I need to write some data to a shared memory and then notify the HW to fetch this data. Writing to the shared memory is protected by an NDIS spinlock. There is a possible race between writing to the shared memory and notifying the…
Rony Ross
  • 21
  • 1
2
votes
1 answer

Is there something like Rust's core::hint::spin_loop for C++?

I'm implementing a spin lock in C++ (since for whatever reason there's no spin lock implementation in the standard library) and I'm looking for a cross-platform and cross-architecture way to emit a PAUSE/YIELD instruction. Rust has…
eyelash
  • 3,197
  • 25
  • 33
2
votes
1 answer

How to disable the spinlock for synchronization in OpenJDK

Background I am analyzing a performance issue on a Java servlet running on AdoptOpenJDK 11. The response time of the servlet slows down as the number of concurrent requests increases. To isolate the cause of the issue, I had collected performance…
SATO Yusuke
  • 1,600
  • 15
  • 39
2
votes
1 answer

How to invert spin_lock_init

I'm trying to understand how to use the Linux kernel spinlocks. From reading the header, I think I have to first declare one and initialize it like this with spin_lock_init: spinlock_t xxx_lock; spin_lock_init(&xxx_lock); and then I can lock and…
memememe
  • 663
  • 6
  • 21
2
votes
1 answer

Why spinlocks can become performance issue in multithreaded programs?

I know what spinlocks are and that they use busy waiting. But why can it become a performance problem in multithreaded programs on a multicore processor? And what can be done about it?
2
votes
0 answers

How do I implement a simple spin lock using FetchAndAdd

Tried something like this: typedef struct __lock_t { int flag; } lock_t; void init(lock_t *lock) { lock->flag = 0; } void lock(lock_t *lock) { while (FetchAndAdd(&lock->flag) != 0) ; // spin-wait (do nothing) } void unlock(lock_t…
user9043303