简体   繁体   中英

segmentation fault with cmpxchg in inline asm

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM