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 #undef PLAT_tilegx_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__) && !defined(__aarch64__)
34 # define PLAT_arm_linux 1
35 #elif defined(__linux__) && defined(__aarch64__) && !defined(__arm__)
36 # define PLAT_arm64_linux 1
37 #elif defined(__linux__) && defined(__s390x__)
38 # define PLAT_s390x_linux 1
39 #elif defined(__linux__) && defined(__mips__)
40 # define PLAT_mips32_linux 1
41 #elif defined(__linux__) && defined(__tilegx__)
42 # define PLAT_tilegx_linux 1
43 #endif
44
45 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \
46 || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin)
47 # define INC(_lval,_lqual) \
48 __asm__ __volatile__ ( \
49 "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" )
50 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux)
51 # define INC(_lval,_lqual) \
52 __asm__ __volatile__( \
53 "1:\n" \
54 " lwarx 15,0,%0\n" \
55 " addi 15,15,1\n" \
56 " stwcx. 15,0,%0\n" \
57 " bne- 1b\n" \
58 : /*out*/ : /*in*/ "b"(&(_lval)) \
59 : /*trash*/ "r15", "cr0", "memory" \
60 )
61 #elif defined(PLAT_arm_linux)
62 # define INC(_lval,_lqual) \
63 __asm__ __volatile__( \
64 "1:\n" \
65 " ldrex r8, [%0, #0]\n" \
66 " add r8, r8, #1\n" \
67 " strex r9, r8, [%0, #0]\n" \
68 " cmp r9, #0\n" \
69 " bne 1b\n" \
70 : /*out*/ : /*in*/ "r"(&(_lval)) \
71 : /*trash*/ "r8", "r9", "cc", "memory" \
72 );
73 #elif defined(PLAT_arm64_linux)
74 # define INC(_lval,_lqual) \
75 __asm__ __volatile__( \
76 "1:\n" \
77 " ldxr w8, [%0, #0]\n" \
78 " add w8, w8, #1\n" \
79 " stxr w9, w8, [%0, #0]\n" \
80 " cmp w9, #0\n" \
81 " bne 1b\n" \
82 : /*out*/ : /*in*/ "r"(&(_lval)) \
83 : /*trash*/ "x8", "x9", "cc", "memory" \
84 );
85 #elif defined(PLAT_s390x_linux)
86 # define INC(_lval,_lqual) \
87 __asm__ __volatile__( \
88 "1: l 0,%0\n" \
89 " lr 1,0\n" \
90 " ahi 1,1\n" \
91 " cs 0,1,%0\n" \
92 " jl 1b\n" \
93 : "+m" (_lval) :: "cc", "1","2" \
94 )
95 #elif defined(PLAT_mips32_linux)
96 # define INC(_lval,_lqual) \
97 __asm__ __volatile__ ( \
98 "1:\n" \
99 " move $8, %0\n" \
100 " ll $9, 0($8)\n" \
101 " addiu $9, $9, 1\n" \
102 " sc $9, 0($8)\n" \
103 " li $10, 1\n" \
104 " bne $9, $10, 1b\n" \
105 " nop\n" \
106 : /*out*/ : /*in*/ "r"(&(_lval)) \
107 : /*trash*/ "$8", "$9", "$10", "cc", "memory" \
108 )
109 #elif defined(PLAT_tilegx_linux)
110 # define INC(_lval,_lqual) \
111 if (sizeof(_lval) == 4) \
112 __insn_fetchadd(&(_lval), 1); \
113 else if(sizeof(_lval) == 8) \
114 __insn_fetchadd(&(_lval), 1)
115 #else
116 # error "Fix Me for this platform"
117 #endif
118
119
120 int x = 0;
121
child_fn(void * arg)122 void* child_fn ( void* arg )
123 {
124 INC(x, "childfn");
125 return NULL;
126 }
127
main(void)128 int main ( void )
129 {
130 pthread_t child;
131
132 if (pthread_create(&child, NULL, child_fn, NULL)) {
133 perror("pthread_create");
134 exit(1);
135 }
136
137 INC(x, "main");
138
139 if (pthread_join(child, NULL)) {
140 perror("pthread join");
141 exit(1);
142 }
143
144 printf("x = %d\n", x);
145 return 0;
146 }
147