spinlock

replace spin lock with signal

Hi, i have alot of spin locks in my multithread code and most of the time they are waiting for other threads to do work and thus chew alot of cpu usage. In linux i normally use pthread_cond_wait and pthread_cond_signal to pause a thread and wake up when signaled. Is there something like this in the boost libraries? Having a quick look i...

Is my spin lock implementation correct and optimal?

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

Spinlocks, How Much Useful Are They?

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

Does running an SMP kernel on a single-cpu machine hurt performance?

On my Ubuntu machine, default kernel image which is running is built for smp (CONFIG_SMP=y). But this machine has only 1 cpu. On uni-processor kernel, unlike smp kernel, spin_lock/unlock are null functions. So how does spin_lock() and spin_unlock behave in this setup? Is there any performance impact due to such smp specific code? ...

What exactly are "spin-locks"?

I always wondered what they are: every time I hear about them, images of futuristic flywheel-like devices go dancing (rolling?) through my mind... What are they? ...

Alternative to spinlock

Hi, I am using the following spinlock approach: while(!hasPerformedAction()){ //wait for the user to perform the action //can add timer here too } setHasPerformedAction(false); return getActionPerfomed(); this basically waits for a user to perform an action and then returns it. Currently something requests an answer from th...

Why does one loop take longer to detect a shared memory update than another loop?

I've written a 'server' program that writes to shared memory, and a client program that reads from the memory. The server has different 'channels' that it can be writing to, which are just different linked lists that it's appending items too. The client is interested in some of the linked lists, and wants to read every node that's added ...

spin_lock_irqsave vs spin_lock_irq

On a 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 contains the IF)? Is there another interrupt routine that could interrupt us? ...

Is it safe to spin on a volatile variable in user-mode threads?

I'm not quite sure if it's safe to spin on a volatile variable in user-mode threads, to implement a light-weight spin_lock, I looked at the tbb source code, tbb_machine.h:170, //! Spin WHILE the value of the variable is equal to a given value /** T and U should be comparable types. */ template<typename T, typename U> void spin_wait_whi...

Cross-platform and cross-process atomic int writes on file

Hello! I'm writing an application that will have to be able to handle many concurrent accesses to it, either by threads as by processes. So no mutex'es or locks should be applied to this. To make the use of locks go down to a minimum, I'm designing for the file to be "append-only", so all data is first appended to disk, and then the ad...

How is spin lock implemented under the hood?

This is a lock that can be held by only one thread of execution at a time. An attempt to acquire the lock by another thread of execution makes the latter loop until the lock is released. How does it handle the case when two threads try to acquire the lock exactly the same time? I think this question also applies to variou...

Do C# 4.0 BCL SpinLock's spin/block when they can't get a lock ?

Hi, Given the following code: ... private static SpinLock logLock = new SpinLock(false); ... private static void ThreadFunc() { bool lockTaken = false; logLock.Enter(ref lockTaken) { try { // do stuff with during an acquired SpinLock } finally { logLock.Exit()...

Usage of spinlock and cli together

I recently downloaded linux source from http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.1.tar.bz2 . I came across the below paragraph in the file called spinlocks.txt in linux-2.6.34.1\Documentation folder. " it does mean that if you have some code that does cli(); .. critical section .. sti(); and another sequence that does...

spin_lock on non-preemtive linux kernels

I read that on a system with 1 CPU and non preemtive linux kernel (2.6.x) a spin_lock call is equivalent to an empty call, and thus implemented that way. I can't understand that: shouldn't it be equivalent to a sleep on a mutex? Even on non-preemtive kernels interrupt handlers may still be executed for example or I might call a function...

Why spinlock in linux kernel is in the ".subsection 1" (or ".text.lock.smth")?

Hello In linux kernel in the implementation of spinlocks, e.g. http://lxr.linux.no/#linux+v2.6.18/include/asm-i386/semaphore.h#L97 97static inline void down(struct semaphore * sem) 98{ 99 might_sleep(); 100 __asm__ __volatile__( 101 "# atomic down operation\n\t" 102 LOCK_PREFIX "decl...

How can I use spinlocks on list entries inside the linux kernel?

Hi, I'm developing a patch for the linux kernel. I have to use several lists and I have to protect'em against concurrent modification on a multicore machine. I'm trying to use spinlocks for this goal, but there's something I can't understand. I have to lock the entries of a list (I'm using linux default implementation of linked lists) an...

CMU: Semaphores!

Check My Understanding of semaphores, please! I understand the idea behind counting semaphores and binary semaphores. However the difference between a spinlock and semaphore implemented with signal() and wait() kind of blend together to me. For example a spinlock has basically two values (a binary true/false for locked or unlocked). ...

Avoiding sleep while holding a spinlock

Hello! 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 space is an obvious example: the required user-space page may need to be swapp...

Is a process preemptable when it holds a spin lock?

It seems that you can disable interrupt when acquire a spin lock. But is it preemptable? I suspect not. But what if the critical section takes too long to finish(in this case, a semaphore would be more appropriate tho. But what happens if it's a spin lock)? Thanks, ...

How best to synchronize memory access shared between kernel and user space, in Windows.

I can't find any function to acquire spinlock in Win32 Apis. Is there a reason? When I need to use spinlock, what do I do? I know there is an CriticalSectionAndSpinCount funtion. But that's not what I want. Edit: I want to synchronize a memory which will be shared between kernel space and user space. -The memory will be mapped. I shou...