spinlock

Implementing a spinlock in Boost. Example Needed

自闭症网瘾萝莉.ら 提交于 2019-12-11 03:27:17
问题 I wanted to know if boost has any libraries that assist in implementing spin locks. I know boost supports mutexes but I could not find any examples that show or describe spinlocks in boost.Any examples showing how to implement a spin lock using boost(preferably) would be appreciated.(C++98) 回答1: Example using Boost.Atomic: #include <boost/atomic.hpp> class SpinLock { boost::atomic_flag flag; // it differs from std::atomic_flag a bit - // does not require ATOMIC_FLAG_INIT public: void lock() {

onSpinWait​() method of Thread class

送分小仙女□ 提交于 2019-12-09 14:01:51
问题 While learning Java 9 I came across a new method of Thread class, called onSpinWait​. According to javadocs, this method is used for this: Indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities. Can someone help me understand this method giving a real-life example? 回答1: It's the same (and probably compiles to) as the x86 opcode PAUSE and equivalent the Win32 macro YieldProcessor , GCC's __mm_pause() and the C#

Linux Kernel Threads with spinlock freeze

烂漫一生 提交于 2019-12-08 13:11:12
问题 I have two kernel threads and I am trying to print from these two threads in alternate fashion. I am using spinlock to sync these two threads. int kthread_1_function(void *data) { while(1) { spin_lock(&lock1); pr_alert("In Kernel Thread 1!!!\n"); spin_unlock(&lock2); if(kthread_should_stop()) { spin_unlock(&lock2); return 1; } } return 0; } int kthread_2_function(void *data) { while(1) { spin_lock(&lock2); pr_alert("In Kernel Thread 2!!!\n"); spin_unlock(&lock1); if(kthread_should_stop()) {

How can Microsoft's OpenMP spinlock time be controlled?

ぐ巨炮叔叔 提交于 2019-12-07 02:28:50
问题 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 OpenMP used by the Gnu compiler supports an environment variable GOMP_SPINCOUNT (docs) which I believe also controls that library's equivalent implementation detail (although apparently expressed as an iteration count rather than a time). My

APC User-Cache suitable for high load environments?

試著忘記壹切 提交于 2019-12-06 12:12:41
We try to deploy APC user-cache in a high load environment as local 2nd-tier cache on each server for our central caching service (redis), for caching database queries with rarely changing results, and configuration. We basically looked at what Facebook did (years ago): http://www.slideshare.net/guoqing75/4069180-caching-performance-lessons-from-facebook http://www.slideshare.net/shire/php-tek-2007-apc-facebook It works pretty well for some time, but after some hours under high load, APC runs into problems, so the whole mod_php does not execute any PHP anymore. Even a simple PHP script with

Is there any simple way to improve performance of this spinlock function?

本小妞迷上赌 提交于 2019-12-06 10:13:07
问题 I'm trying to implement a spinlock in my code but the spinlock that I implemented based on Wikipedia results in extremely slow performance. int lockValue = 0; void lock() { __asm__("loop: \n\t" "movl $1, %eax \n\t" "xchg %eax, lockValue \n\t" "test %eax, %eax \n\t" "jnz loop"); } Is there any way of improving this to make it faster? Thanks. 回答1: How about something like this (I understand this is the KeAcquireSpinLock implementation). My at&t assembly is weak unfortunately. spin_lock: rep;

Why does this code deadlock?

╄→гoц情女王★ 提交于 2019-12-06 06:24:22
问题 I created 2 Linux kernel threads in my loadable module and I bind them to separate CPU cores running on a dual core Android device. After I run this few times, I noticed that the device reboots with a HW watchdog timer reset. I hit the issue consistently. What could be causing the deadlock? Basically, what i need to do is, make sure both the threads run do_something() at the same time on different cores without anybody stealing the cpu cycles(i.e. interrupts are disabled). I am using a

What is the minimum X86 assembly needed for a spinlock

帅比萌擦擦* 提交于 2019-12-06 04:12:37
To implement a spinlock in assembly. Here I post a solution I came up with. Is it correct? Do you know a shorter one? lock: mov ecx, 0 .loop: xchg [eax], ecx cmp ecx, 0 je .loop release: lock dec dword [eax] eax is initialized to -1 (which means lock is free). This should work for many threads (not necessarily 2). Shortest would probably be: acquire: lock bts [eax],0 jc acquire release: mov [eax],0 For performance, it's best to use a "test, test and set" approach, and use pause , like this: acquire: lock bts [eax],0 ;Optimistic first attempt jnc l2 ;Success if acquired l1: pause test [eax],1

When should & shouldn't I use this C# utility class to control threads via Interlocked

你离开我真会死。 提交于 2019-12-05 18:28:49
I'm trying to understand the logic behind how this class was written, and when I should and shouldn't use it. Any insight would be appreciated internal struct SpinLock { private volatile int lockHeld; private readonly static int processorCount; public bool IsHeld { get { return this.lockHeld != 0; } } static SpinLock() { SpinLock.processorCount = Environment.ProcessorCount; } public void Enter() { if (Interlocked.CompareExchange(ref this.lockHeld, 1, 0) != 0) { this.EnterSpin(); } } private void EnterSpin() { int num = 0; while (this.lockHeld != null || Interlocked.CompareExchange(ref this

How can Microsoft's OpenMP spinlock time be controlled?

空扰寡人 提交于 2019-12-05 07:46:29
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 OpenMP used by the Gnu compiler supports an environment variable GOMP_SPINCOUNT ( docs ) which I believe also controls that library's equivalent implementation detail (although apparently expressed as an iteration count rather than a time). My question is: what control(s) (if any) do Microsoft provide to control this parameter in the OpenMP used