Possibly the fault is elsewhere, due to the fact that you have forgotten to tell the compiler you modified eax
and edx
. The fix is to list them as clobbers (the part after the 3rd colon). Unless you are forced to use inline asm, use the atomic builtins instead.
segmentation fault with cmpxchg in inline asm
-
05-07-2023 - |
Pregunta
I'm writing my_simple_mutex using inline asm. The part of the code below that is commented out works fine, however, the version with cmpxchg terminates with a segfault. I'm using g++ 4.8.2 in cygwin.
void simple_mutex::spin_lock(){
/*asm ("spin_lock:\n\t"
"rep; nop;\n\t"
"lock; bts $0x00, %0;\n\t"
"jc spin_lock;\n\t"
:"=m"(lock)
:"m"(lock)
:
);
*/
asm ("spin_lock:\n\t"
"rep; nop;\n\t"
"movl $0x00, %%eax\n\t"
"movl $0x01, %%edx\n\t"
"lock; cmpxchg %%edx, %0\n\t"
"jnz spin_lock;\n\t"
:"=m"(lock)
:"m"(lock)
:
);
}
The variable lock
is of type int
. Any ideas what I'm doing wrong?
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow