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