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_x86_darwin 13 #undef PLAT_amd64_darwin 14 #undef PLAT_x86_linux 15 #undef PLAT_amd64_linux 16 #undef PLAT_ppc32_linux 17 #undef PLAT_ppc64_linux 18 #undef PLAT_arm_linux 19 #undef PLAT_s390x_linux 20 21 #if defined(__APPLE__) && defined(__i386__) 22 # define PLAT_x86_darwin 1 23 #elif defined(__APPLE__) && defined(__x86_64__) 24 # define PLAT_amd64_darwin 1 25 #elif defined(__linux__) && defined(__i386__) 26 # define PLAT_x86_linux 1 27 #elif defined(__linux__) && defined(__x86_64__) 28 # define PLAT_amd64_linux 1 29 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__) 30 # define PLAT_ppc32_linux 1 31 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__) 32 # define PLAT_ppc64_linux 1 33 #elif defined(__linux__) && defined(__arm__) 34 # define PLAT_arm_linux 1 35 #elif defined(__linux__) && defined(__s390x__) 36 # define PLAT_s390x_linux 1 37 #endif 38 39 40 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \ 41 || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin) 42 # define XCHG_M_R(_addr,_lval) \ 43 __asm__ __volatile__( \ 44 "xchgl %0, %1" \ 45 : /*out*/ "+r"(_lval) \ 46 : /*in*/ "m"(_addr) \ 47 : "memory", "cc" \ 48 ) 49 # define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \ 50 __asm__ __volatile__( \ 51 "lock xchgl %0, %1" \ 52 : /*out*/ "+r"(_lval) \ 53 : /*in*/ "m"(_addr) \ 54 : "memory", "cc" \ 55 ) 56 57 #elif defined(PLAT_s390x_linux) 58 # define XCHG_M_R(_addr,_lval) \ 59 do { \ 60 __asm__ __volatile__( \ 61 "0: l 0,%[global]\n\t" \ 62 " cs 0,%[local],%[global]\n\t" \ 63 " bne 0b\n\t" \ 64 " lr %[local],0\n\t" \ 65 : /*out*/ [global]"+m"(_addr), [local]"+d"(_lval) \ 66 : /*in*/ \ 67 : "0", "memory", "cc" \ 68 ); \ 69 } while (0) 70 71 # define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \ 72 XCHG_M_R(_addr,_lval) 73 74 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) \ 75 || defined(PLAT_arm_linux) 76 # if defined(HAVE_BUILTIN_ATOMIC) 77 # define XCHG_M_R(_addr,_lval) \ 78 do { \ 79 int tmp; \ 80 while ((tmp = *(int*)(& _addr)), \ 81 ! __sync_bool_compare_and_swap((int*)&_addr, tmp, _lval)) \ 82 ; \ 83 _lval = tmp; \ 84 } while (0) 85 # else 86 # warning "XCHG_M_R() implementation is missing. Either" \ 87 "provide one or use a newer gcc version." 88 # define XCHG_M_R(_addr,_lval) \ 89 do { int tmp = *(int*)(& _addr); \ 90 *(int*)(& _addr) = (_lval); \ 91 _lval = tmp; \ 92 } while (0) 93 # endif 94 # define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \ 95 XCHG_M_R(_addr,_lval) 96 97 #else 98 # error "Unsupported architecture" 99 100 #endif 101 102 int x = 0; 103 104 void* child_fn ( void* arg ) 105 { 106 int v = 12345; 107 XCHG_M_R_with_redundant_LOCK(x, v); 108 assert(v == 0 || v == 6789); 109 return NULL; 110 } 111 112 int main ( void ) 113 { 114 int v = 6789; 115 pthread_t child; 116 117 if (pthread_create(&child, NULL, child_fn, NULL)) { 118 perror("pthread_create"); 119 exit(1); 120 } 121 122 XCHG_M_R(x, v); 123 assert(v == 0 || v == 12345); 124 125 if (pthread_join(child, NULL)) { 126 perror("pthread join"); 127 exit(1); 128 } 129 130 if (v == 0 || v == 12345) 131 printf("success\n"); 132 else 133 printf("failure\n"); 134 135 return v; 136 } 137