Is there any simple way to improve performance of this spinlock function?
-
25-06-2021 - |
سؤال
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.
المحلول
How about something like this (I understand this is the KeAcquireSpinLock implementation). My at&t assembly is weak unfortunately.
spin_lock:
rep; nop
test lockValue, 1
jnz spin_lock
lock bts lockValue
jc spin_lock
نصائح أخرى
"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; }
لا تنتمي إلى StackOverflow