FreeBSD regtest: add missing helgrind expecteds
[valgrind.git] / helgrind / tests / tc08_hbl2.c
blob6a8543fa1d6fa6b0396ba91b287c37ec01bdd810
2 /* FIXME: this is basically a bad test as it is scheduling-
3 sensitive. Sometimes the output is:
5 child: new value 6
6 child: new value 10
7 done, x = 10
9 and sometimes
11 child: new value 10
12 done, x = 10
15 #include <pthread.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
20 /* Simple test program, no race. Parent writes atomically to a counter
21 whilst child reads it. When counter reaches a prearranged value,
22 child joins back to parent. Parent (writer) uses hardware bus lock;
23 child is only reading and so does not need to use a bus lock. */
25 #undef PLAT_x86_darwin
26 #undef PLAT_amd64_darwin
27 #undef PLAT_x86_freebsd
28 #undef PLAT_amd64_freebsd
29 #undef PLAT_arm64_freebsd
30 #undef PLAT_x86_linux
31 #undef PLAT_amd64_linux
32 #undef PLAT_ppc32_linux
33 #undef PLAT_ppc64_linux
34 #undef PLAT_arm_linux
35 #undef PLAT_arm64_linux
36 #undef PLAT_s390x_linux
37 #undef PLAT_mips32_linux
38 #undef PLAT_mips64_linux
39 #undef PLAT_x86_solaris
40 #undef PLAT_amd64_solaris
42 #if defined(__APPLE__) && defined(__i386__)
43 # define PLAT_x86_darwin 1
44 #elif defined(__APPLE__) && defined(__x86_64__)
45 # define PLAT_amd64_darwin 1
46 #elif defined(__FreeBSD__) && defined(__i386__)
47 # define PLAT_x86_freebsd 1
48 #elif defined(__FreeBSD__) && defined(__amd64__)
49 # define PLAT_amd64_freebsd 1
50 #elif defined(__FreeBSD__) && defined(__aarch64__)
51 # define PLAT_arm64_freebsd 1
52 #elif defined(__linux__) && defined(__i386__)
53 # define PLAT_x86_linux 1
54 #elif defined(__linux__) && defined(__x86_64__)
55 # define PLAT_amd64_linux 1
56 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__)
57 # define PLAT_ppc32_linux 1
58 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__)
59 # define PLAT_ppc64_linux 1
60 #elif defined(__linux__) && defined(__arm__) && !defined(__aarch64__)
61 # define PLAT_arm_linux 1
62 #elif defined(__linux__) && defined(__aarch64__) && !defined(__arm__)
63 # define PLAT_arm64_linux 1
64 #elif defined(__linux__) && defined(__s390x__)
65 # define PLAT_s390x_linux 1
66 #elif defined(__linux__) && defined(__mips__)
67 #if (__mips==64)
68 # define PLAT_mips64_linux 1
69 #else
70 # define PLAT_mips32_linux 1
71 #endif
72 #elif defined(__linux__) && defined(__nanomips__)
73 # define PLAT_nanomips_linux 1
74 #elif defined(__sun__) && defined(__i386__)
75 # define PLAT_x86_solaris 1
76 #elif defined(__sun__) && defined(__x86_64__)
77 # define PLAT_amd64_solaris 1
78 #endif
81 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \
82 || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin) \
83 || defined(PLAT_amd64_solaris) || defined(PLAT_x86_solaris) \
84 || defined(PLAT_amd64_freebsd) || defined(PLAT_x86_freebsd)
85 # define INC(_lval,_lqual) \
86 __asm__ __volatile__ ( \
87 "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" )
88 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux)
89 # define INC(_lval,_lqual) \
90 __asm__ __volatile__( \
91 "1:\n" \
92 " lwarx 15,0,%0\n" \
93 " addi 15,15,1\n" \
94 " stwcx. 15,0,%0\n" \
95 " bne- 1b\n" \
96 : /*out*/ : /*in*/ "b"(&(_lval)) \
97 : /*trash*/ "r15", "cr0", "memory" \
99 #elif defined(PLAT_arm_linux)
100 # define INC(_lval,_lqual) \
101 __asm__ __volatile__( \
102 "1:\n" \
103 " ldrex r8, [%0, #0]\n" \
104 " add r8, r8, #1\n" \
105 " strex r9, r8, [%0, #0]\n" \
106 " cmp r9, #0\n" \
107 " bne 1b\n" \
108 : /*out*/ : /*in*/ "r"(&(_lval)) \
109 : /*trash*/ "r8", "r9", "cc", "memory" \
111 #elif defined(PLAT_arm64_linux) || defined(PLAT_arm64_freebsd)
112 # define INC(_lval,_lqual) \
113 __asm__ __volatile__( \
114 "1:\n" \
115 " ldxr w8, [%0, #0]\n" \
116 " add w8, w8, #1\n" \
117 " stxr w9, w8, [%0, #0]\n" \
118 " cmp w9, #0\n" \
119 " bne 1b\n" \
120 : /*out*/ : /*in*/ "r"(&(_lval)) \
121 : /*trash*/ "x8", "x9", "cc", "memory" \
123 #elif defined(PLAT_s390x_linux)
124 # define INC(_lval,_lqual) \
125 __asm__ __volatile__( \
126 "1: l 0,%0\n" \
127 " lr 1,0\n" \
128 " ahi 1,1\n" \
129 " cs 0,1,%0\n" \
130 " jl 1b\n" \
131 : "+m" (_lval) :: "cc", "0","1" \
133 #elif defined(PLAT_mips32_linux) || defined(PLAT_mips64_linux)
134 # define INC(_lval,_lqual) \
135 __asm__ __volatile__ ( \
136 "1:\n" \
137 " move $t0, %0\n" \
138 " ll $t1, 0($t0)\n" \
139 " addiu $t1, $t1, 1\n" \
140 " sc $t1, 0($t0)\n" \
141 " beqz $t1, 1b\n" \
142 : /*out*/ : /*in*/ "r"(&(_lval)) \
143 : /*trash*/ "t0", "t1", "memory" \
145 #elif defined(PLAT_nanomips_linux)
146 # define INC(_lval,_lqual) \
147 __asm__ __volatile__ ( \
148 "1:\n" \
149 " move $t0, %0\n" \
150 " ll $t1, 0($t0)\n" \
151 " addiu $t1, $t1, 1\n" \
152 " sc $t1, 0($t0)\n" \
153 " beqc $t1, $zero, 1b\n" \
154 : /*out*/ : /*in*/ "r"(&(_lval)) \
155 : /*trash*/ "$t0", "$t1", "memory" \
157 #else
158 # error "Fix Me for this platform"
159 #endif
163 #define LIMIT 10
165 volatile int x = 0;
167 void* child_fn ( void* arg )
169 int q = 0;
170 int oldx = 0;
171 struct timespec ts = { 0, 1000 * 1000 };
173 while (1) {
174 q = (x >= LIMIT);
175 if (x != oldx) {
176 oldx = x;
177 printf("child: new value %d\n", oldx);
178 fflush(stdout);
180 if (q) break;
181 nanosleep(&ts, 0);
183 return NULL;
186 int main ( void )
188 pthread_t child;
189 int i;
191 if (pthread_create(&child, NULL, child_fn, NULL)) {
192 perror("pthread_create");
193 exit(1);
196 for (i = 0; i < LIMIT; i++) {
197 INC(x, "main");
198 if (i == 5) sleep(1); /* make sure child doesn't starve */
201 if (pthread_join(child, NULL)) {
202 perror("pthread join");
203 exit(1);
206 printf("done, x = %d\n", x);
208 return 0;