Is there an easy way to improve the performance of this spinlock feature?

I am trying to implement spin lock in my code, but the spin lock that I implemented on Wikipedia leads to extremely poor 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 a way to improve this to make it faster?

Thank.

+2
c inline-assembly spinlock
Aug 12 2018-12-12T00:
source share
2 answers

How about something like this (I understand this is a KeAcquireSpinLock implementation). Unfortunately, my build is weak.

 spin_lock: rep; nop test lockValue, 1 jnz spin_lock lock bts lockValue jc spin_lock 
+5
Aug 12 '12 at 15:05
source share
— -
  "movl $1,%%edx \n\t" // edx = 1; ".set lockloop,. \n\t" // symbol lockloop set to pc "xorl %%eax,%%eax \n\t" // eax = 0; "lock cmpxchgl %%edx,(%%ebx)\n\t" // if (*mu_ptr == eax) *mu_ptr = edx; // else { eax = *mu_ptr; "jnz lockloop \n\t" // goto lockloop; } 
+4
Nov 01
source share



All Articles