문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top