2 /* FIXME: this is basically a bad test as it is scheduling-
3 sensitive. Sometimes the output is:
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
31 #undef PLAT_amd64_linux
32 #undef PLAT_ppc32_linux
33 #undef PLAT_ppc64_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__)
68 # define PLAT_mips64_linux 1
70 # define PLAT_mips32_linux 1
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
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__( \
96 : /*out*/ : /*in*/ "b"(&(_lval)) \
97 : /*trash*/ "r15", "cr0", "memory" \
99 #elif defined(PLAT_arm_linux)
100 # define INC(_lval,_lqual) \
101 __asm__ __volatile__( \
103 " ldrex r8, [%0, #0]\n" \
104 " add r8, r8, #1\n" \
105 " strex r9, r8, [%0, #0]\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__( \
115 " ldxr w8, [%0, #0]\n" \
116 " add w8, w8, #1\n" \
117 " stxr w9, w8, [%0, #0]\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__( \
131 : "+m" (_lval) :: "cc", "0","1" \
133 #elif defined(PLAT_mips32_linux) || defined(PLAT_mips64_linux)
134 # define INC(_lval,_lqual) \
135 __asm__ __volatile__ ( \
138 " ll $t1, 0($t0)\n" \
139 " addiu $t1, $t1, 1\n" \
140 " sc $t1, 0($t0)\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__ ( \
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" \
158 # error "Fix Me for this platform"
167 void* child_fn ( void* arg
)
171 struct timespec ts
= { 0, 1000 * 1000 };
177 printf("child: new value %d\n", oldx
);
191 if (pthread_create(&child
, NULL
, child_fn
, NULL
)) {
192 perror("pthread_create");
196 for (i
= 0; i
< LIMIT
; i
++) {
198 if (i
== 5) sleep(1); /* make sure child doesn't starve */
201 if (pthread_join(child
, NULL
)) {
202 perror("pthread join");
206 printf("done, x = %d\n", x
);