1 2 #include <pthread.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* Simple test program, no race. Parent and child both modify x and 7 use the hardware bus lock. */ 8 9 #undef PLAT_x86_darwin 10 #undef PLAT_amd64_darwin 11 #undef PLAT_x86_linux 12 #undef PLAT_amd64_linux 13 #undef PLAT_ppc32_linux 14 #undef PLAT_ppc64_linux 15 #undef PLAT_arm_linux 16 #undef PLAT_arm64_linux 17 #undef PLAT_s390x_linux 18 #undef PLAT_mips32_linux 19 20 #if defined(__APPLE__) && defined(__i386__) 21 # define PLAT_x86_darwin 1 22 #elif defined(__APPLE__) && defined(__x86_64__) 23 # define PLAT_amd64_darwin 1 24 #elif defined(__linux__) && defined(__i386__) 25 # define PLAT_x86_linux 1 26 #elif defined(__linux__) && defined(__x86_64__) 27 # define PLAT_amd64_linux 1 28 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__) 29 # define PLAT_ppc32_linux 1 30 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__) 31 # define PLAT_ppc64_linux 1 32 #elif defined(__linux__) && defined(__arm__) && !defined(__aarch64__) 33 # define PLAT_arm_linux 1 34 #elif defined(__linux__) && defined(__aarch64__) && !defined(__arm__) 35 # define PLAT_arm64_linux 1 36 #elif defined(__linux__) && defined(__s390x__) 37 # define PLAT_s390x_linux 1 38 #elif defined(__linux__) && defined(__mips__) 39 # define PLAT_mips32_linux 1 40 #endif 41 42 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \ 43 || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin) 44 # define INC(_lval,_lqual) \ 45 __asm__ __volatile__ ( \ 46 "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" ) 47 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) 48 # define INC(_lval,_lqual) \ 49 __asm__ __volatile__( \ 50 "1:\n" \ 51 " lwarx 15,0,%0\n" \ 52 " addi 15,15,1\n" \ 53 " stwcx. 15,0,%0\n" \ 54 " bne- 1b\n" \ 55 : /*out*/ : /*in*/ "b"(&(_lval)) \ 56 : /*trash*/ "r15", "cr0", "memory" \ 57 ) 58 #elif defined(PLAT_arm_linux) 59 # define INC(_lval,_lqual) \ 60 __asm__ __volatile__( \ 61 "1:\n" \ 62 " ldrex r8, [%0, #0]\n" \ 63 " add r8, r8, #1\n" \ 64 " strex r9, r8, [%0, #0]\n" \ 65 " cmp r9, #0\n" \ 66 " bne 1b\n" \ 67 : /*out*/ : /*in*/ "r"(&(_lval)) \ 68 : /*trash*/ "r8", "r9", "cc", "memory" \ 69 ); 70 #elif defined(PLAT_arm64_linux) 71 # define INC(_lval,_lqual) \ 72 __asm__ __volatile__( \ 73 "1:\n" \ 74 " ldxr w8, [%0, #0]\n" \ 75 " add w8, w8, #1\n" \ 76 " stxr w9, w8, [%0, #0]\n" \ 77 " cmp w9, #0\n" \ 78 " bne 1b\n" \ 79 : /*out*/ : /*in*/ "r"(&(_lval)) \ 80 : /*trash*/ "x8", "x9", "cc", "memory" \ 81 ); 82 #elif defined(PLAT_s390x_linux) 83 # define INC(_lval,_lqual) \ 84 __asm__ __volatile__( \ 85 "1: l 0,%0\n" \ 86 " lr 1,0\n" \ 87 " ahi 1,1\n" \ 88 " cs 0,1,%0\n" \ 89 " jl 1b\n" \ 90 : "+m" (_lval) :: "cc", "1","2" \ 91 ) 92 #elif defined(PLAT_mips32_linux) 93 # define INC(_lval,_lqual) \ 94 __asm__ __volatile__ ( \ 95 "1:\n" \ 96 " move $8, %0\n" \ 97 " ll $9, 0($8)\n" \ 98 " addiu $9, $9, 1\n" \ 99 " sc $9, 0($8)\n" \ 100 " li $10, 1\n" \ 101 " bne $9, $10, 1b\n" \ 102 " nop\n" \ 103 : /*out*/ : /*in*/ "r"(&(_lval)) \ 104 : /*trash*/ "$8", "$9", "$10", "cc", "memory" \ 105 ) 106 #else 107 # error "Fix Me for this platform" 108 #endif 109 110 111 int x = 0; 112 113 void* child_fn ( void* arg ) 114 { 115 INC(x, "childfn"); 116 return NULL; 117 } 118 119 int main ( void ) 120 { 121 pthread_t child; 122 123 if (pthread_create(&child, NULL, child_fn, NULL)) { 124 perror("pthread_create"); 125 exit(1); 126 } 127 128 INC(x, "main"); 129 130 if (pthread_join(child, NULL)) { 131 perror("pthread join"); 132 exit(1); 133 } 134 135 printf("x = %d\n", x); 136 return 0; 137 } 138