spinlock

Spinlocks, How Useful Are They?

半腔热情 提交于 2019-11-29 20:26:33
How often do you find yourself actually using spinlocks in your code? How common is it to come across a situation where using a busy loop actually outperforms the usage of locks? Personally, when I write some sort of code that requires thread safety, I tend to benchmark it with different synchronization primitives, and as far as it goes, it seems like using locks gives better performance than using spinlocks. No matter for how little time I actually hold the lock, the amount of contention I receive when using spinlocks is far greater than the amount I get from using locks (of course, I run my

spin_lock_irqsave vs spin_lock_irq

有些话、适合烂在心里 提交于 2019-11-29 19:45:38
On an SMP machine we must use spin_lock_irqsave and not spin_lock_irq from interrupt context. Why would we want to save the flags (which contain the IF)? Is there another interrupt routine that could interrupt us? Hagai I am new to the kernel but from what I gather from Robert Love's book "Linux Kernel Development", if interrupts are already disabled on the processor before your code starts locking, when you call spin_unlock_irq you will release the lock in an erroneous manner. If you save the flags and release it with the flags, the function spin_lock_irqsave will just return the interrupt to

Why everyone states that SpinLock is faster? [closed]

非 Y 不嫁゛ 提交于 2019-11-29 14:19:45
问题 I have read a lot of docs and articles and posts all over the internet. Almost everyone and everywhere commits that SpinLock is faster for a short running pieces of code, but I made a test, and it appears to me that simple Monitor.Enter works faster than SpinLock.Enter (Test is compiled against .NET 4.5) using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; using System.Linq; using System.Globalization;

Intel Inspector reports a data race in my spinlock implementation

强颜欢笑 提交于 2019-11-29 14:08:06
问题 I made a very simple spinlock using the Interlocked functions in Windows and tested it on a dual-core CPU (two threads that increment a variable); The program seems to work OK (it gives the same result every time, which is not the case when no synchronization is used), but Intel Parallel Inspector says that there is a race condition at value += j (see the code below). The warning disappears when using Critical Sections instead of my SpinLock. Is my implementation of SpinLock correct or not ?

Do spin locks always require a memory barrier? Is spinning on a memory barrier expensive?

帅比萌擦擦* 提交于 2019-11-28 23:46:31
I wrote some lock-free code that works fine with local reads, under most conditions. Does local spinning on a memory read necessarily imply I have to ALWAYS insert a memory barrier before the spinning read? (To validate this, I managed to produce a reader/writer combination which results in a reader never seeing the written value, under certain very specific conditions--dedicated CPU, process attached to CPU, optimizer turned all the way up, no other work done in the loop--so the arrows do point in that direction, but I'm not entirely sure about the cost of spinning through a memory barrier.)

Why spinlocks don't work in uniprocessor (unicore) systems?

妖精的绣舞 提交于 2019-11-28 19:23:21
问题 I know that spinlocks work with spining, different kernel paths exist and Kernels are preemptive, so why spinlocks don't work in uniprocessor systems? (for example, in Linux) 回答1: If I understand your question, you're asking why spin locks are a bad idea on single core machines. They should still work , but can be much more expensive than true thread-sleeping concurrency: When you use a spinlock, you're essentially asserting that you don't think you will have to wait long. You are saying that

Spinlocks, How Useful Are They?

依然范特西╮ 提交于 2019-11-28 16:48:21
问题 How often do you find yourself actually using spinlocks in your code? How common is it to come across a situation where using a busy loop actually outperforms the usage of locks? Personally, when I write some sort of code that requires thread safety, I tend to benchmark it with different synchronization primitives, and as far as it goes, it seems like using locks gives better performance than using spinlocks. No matter for how little time I actually hold the lock, the amount of contention I

Is my spin lock implementation correct and optimal?

安稳与你 提交于 2019-11-28 16:05:43
I'm using a spin lock to protect a very small critical section. Contention happens very rarely so a spin lock is more appropriate than a regular mutex. My current code is as follows, and assumes x86 and GCC: volatile int exclusion = 0; void lock() { while (__sync_lock_test_and_set(&exclusion, 1)) { // Do nothing. This GCC builtin instruction // ensures memory barrier. } } void unlock() { __sync_synchronize(); // Memory barrier. exclusion = 0; } So I'm wondering: Is this code correct? Does it correctly ensure mutual exclusion? Does it work on all x86 operating systems? Does it work on x86_64

spin_lock_irqsave vs spin_lock_irq

无人久伴 提交于 2019-11-28 15:24:27
问题 On an SMP machine we must use spin_lock_irqsave and not spin_lock_irq from interrupt context. Why would we want to save the flags (which contain the IF)? Is there another interrupt routine that could interrupt us? 回答1: I am new to the kernel but from what I gather from Robert Love's book "Linux Kernel Development", if interrupts are already disabled on the processor before your code starts locking, when you call spin_unlock_irq you will release the lock in an erroneous manner. If you save the

Difference between Mutex, Semaphore & Spin Locks

家住魔仙堡 提交于 2019-11-28 15:21:30
I am doing experiments with IPC, especially with Mutex, Semaphore and Spin Lock. What I learnt is Mutex is used for Asynchronous Locking (with sleeping (as per theories I read on NET)) Mechanism, Semaphore are Synchronous Locking (with Signaling and Sleeping) Mechanism, and Spin Locks are Synchronous but Non-sleeping Mechanism. Can anyone help me to clarify these stuff deeply? And another doubt is about Mutex, when I wrote program with thread & mutex, while one thread is running another thread is not in Sleep state but it continuously tries to acquire the Lock. So Mutex is sleeping or Non