1 2 #include "config.h" 3 #include <pthread.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <assert.h> 7 8 /* Simple test program, no race. Parent and child both modify x and 9 use the hardware bus lock (implicitly, since XCHG r,m on x86/amd64 10 does not require an explicit LOCK prefix.). */ 11 12 #undef PLAT_ppc64_aix5 13 #undef PLAT_ppc32_aix5 14 #undef PLAT_x86_darwin 15 #undef PLAT_amd64_darwin 16 #undef PLAT_x86_linux 17 #undef PLAT_amd64_linux 18 #undef PLAT_ppc32_linux 19 #undef PLAT_ppc64_linux 20 #undef PLAT_arm_linux 21 22 #if defined(_AIX) && defined(__64BIT__) 23 # define PLAT_ppc64_aix5 1 24 #elif defined(_AIX) && !defined(__64BIT__) 25 # define PLAT_ppc32_aix5 1 26 #elif defined(__APPLE__) && defined(__i386__) 27 # define PLAT_x86_darwin 1 28 #elif defined(__APPLE__) && defined(__x86_64__) 29 # define PLAT_amd64_darwin 1 30 #elif defined(__linux__) && defined(__i386__) 31 # define PLAT_x86_linux 1 32 #elif defined(__linux__) && defined(__x86_64__) 33 # define PLAT_amd64_linux 1 34 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__) 35 # define PLAT_ppc32_linux 1 36 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__) 37 # define PLAT_ppc64_linux 1 38 #elif defined(__linux__) && defined(__arm__) 39 # define PLAT_arm_linux 1 40 #endif 41 42 43 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \ 44 || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin) 45 # define XCHG_M_R(_addr,_lval) \ 46 __asm__ __volatile__( \ 47 "xchgl %0, %1" \ 48 : /*out*/ "+r"(_lval) \ 49 : /*in*/ "m"(_addr) \ 50 : "memory", "cc" \ 51 ) 52 # define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \ 53 __asm__ __volatile__( \ 54 "lock xchgl %0, %1" \ 55 : /*out*/ "+r"(_lval) \ 56 : /*in*/ "m"(_addr) \ 57 : "memory", "cc" \ 58 ) 59 60 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) \ 61 || defined(PLAT_ppc32_aix5) || defined(PLAT_ppc64_aix5) \ 62 || defined(PLAT_arm_linux) 63 # if defined(HAVE_BUILTIN_ATOMIC) 64 # define XCHG_M_R(_addr,_lval) \ 65 do { \ 66 int tmp; \ 67 while ((tmp = *(int*)(& _addr)), \ 68 ! __sync_bool_compare_and_swap((int*)&_addr, tmp, _lval)) \ 69 ; \ 70 _lval = tmp; \ 71 } while (0) 72 # else 73 # warning "XCHG_M_R() implementation is missing. Either" \ 74 "provide one or use a newer gcc version." 75 # define XCHG_M_R(_addr,_lval) \ 76 do { int tmp = *(int*)(& _addr); \ 77 *(int*)(& _addr) = (_lval); \ 78 _lval = tmp; \ 79 } while (0) 80 # endif 81 # define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \ 82 XCHG_M_R(_addr,_lval) 83 84 #else 85 # error "Unsupported architecture" 86 87 #endif 88 89 int x = 0; 90 91 void* child_fn ( void* arg ) 92 { 93 int v = 12345; 94 XCHG_M_R_with_redundant_LOCK(x, v); 95 assert(v == 0 || v == 6789); 96 return NULL; 97 } 98 99 int main ( void ) 100 { 101 int v = 6789; 102 pthread_t child; 103 104 if (pthread_create(&child, NULL, child_fn, NULL)) { 105 perror("pthread_create"); 106 exit(1); 107 } 108 109 XCHG_M_R(x, v); 110 assert(v == 0 || v == 12345); 111 112 if (pthread_join(child, NULL)) { 113 perror("pthread join"); 114 exit(1); 115 } 116 117 if (v == 0 || v == 12345) 118 printf("success\n"); 119 else 120 printf("failure\n"); 121 122 return v; 123 } 124