github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/test/cmpxchtest.c (about) 1 #include "stdio.h" 2 3 4 int xch(int* pmutex) 5 { 6 int ret= 0; 7 8 asm volatile ( 9 "\tmovq $1, %%rax\n" 10 "\tmovq %[pmutex], %%rcx\n" 11 "\txchgb %%al, (%%rcx)\n" 12 "\tmovl %%eax, %[ret]\n" 13 : [ret] "=g" (ret) 14 : [pmutex] "p" (pmutex) 15 : "%rax", "%rcx"); 16 17 return ret; 18 } 19 20 21 int main(int an, char** av) 22 { 23 int mutex= 0; 24 int ret= 0; 25 26 ret= 0; 27 printf("old mutex: %d, ", mutex); 28 ret= xch(&mutex); 29 printf("return: %d\n", ret); 30 mutex= 1; 31 printf("old mutex: %d, ", mutex); 32 ret= xch(&mutex); 33 printf("return: %d\n", ret); 34 return 0; 35 } 36