• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* FIXME: this is basically a bad test as it is scheduling-
3    sensitive.  Sometimes the output is:
4 
5    child: new value 6
6    child: new value 10
7    done, x = 10
8 
9    and sometimes
10 
11    child: new value 10
12    done, x = 10
13 */
14 
15 #include <pthread.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 
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. */
24 
25 #undef PLAT_x86_darwin
26 #undef PLAT_amd64_darwin
27 #undef PLAT_x86_linux
28 #undef PLAT_amd64_linux
29 #undef PLAT_ppc32_linux
30 #undef PLAT_ppc64_linux
31 #undef PLAT_arm_linux
32 #undef PLAT_arm64_linux
33 #undef PLAT_s390x_linux
34 #undef PLAT_mips32_linux
35 #undef PLAT_mips64_linux
36 
37 #if defined(__APPLE__) && defined(__i386__)
38 #  define PLAT_x86_darwin 1
39 #elif defined(__APPLE__) && defined(__x86_64__)
40 #  define PLAT_amd64_darwin 1
41 #elif defined(__linux__) && defined(__i386__)
42 #  define PLAT_x86_linux 1
43 #elif defined(__linux__) && defined(__x86_64__)
44 #  define PLAT_amd64_linux 1
45 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__)
46 #  define PLAT_ppc32_linux 1
47 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__)
48 #  define PLAT_ppc64_linux 1
49 #elif defined(__linux__) && defined(__arm__) && !defined(__aarch64__)
50 #  define PLAT_arm_linux 1
51 #elif defined(__linux__) && defined(__aarch64__) && !defined(__arm__)
52 #  define PLAT_arm64_linux 1
53 #elif defined(__linux__) && defined(__s390x__)
54 #  define PLAT_s390x_linux 1
55 #elif defined(__linux__) && defined(__mips__)
56 #if (__mips==64)
57 #  define PLAT_mips64_linux 1
58 #else
59 #  define PLAT_mips32_linux 1
60 #endif
61 #endif
62 
63 
64 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \
65     || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin)
66 #  define INC(_lval,_lqual)	     \
67       __asm__ __volatile__ ( \
68       "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" )
69 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux)
70 #  define INC(_lval,_lqual)		  \
71    __asm__ __volatile__(                  \
72       "1:\n"                              \
73       "        lwarx 15,0,%0\n"           \
74       "        addi 15,15,1\n"            \
75       "        stwcx. 15,0,%0\n"          \
76       "        bne- 1b\n"                 \
77       : /*out*/ : /*in*/ "b"(&(_lval))    \
78       : /*trash*/ "r15", "cr0", "memory"  \
79    )
80 #elif defined(PLAT_arm_linux)
81 #  define INC(_lval,_lqual) \
82   __asm__ __volatile__( \
83       "1:\n"                                 \
84       "        ldrex r8, [%0, #0]\n"         \
85       "        add   r8, r8, #1\n"           \
86       "        strex r9, r8, [%0, #0]\n"     \
87       "        cmp   r9, #0\n"               \
88       "        bne   1b\n"                   \
89       : /*out*/ : /*in*/ "r"(&(_lval))       \
90       : /*trash*/ "r8", "r9", "cc", "memory" \
91   );
92 #elif defined(PLAT_arm64_linux)
93 #  define INC(_lval,_lqual) \
94   __asm__ __volatile__( \
95       "1:\n"                                 \
96       "        ldxr  w8, [%0, #0]\n"         \
97       "        add   w8, w8, #1\n"           \
98       "        stxr  w9, w8, [%0, #0]\n"     \
99       "        cmp   w9, #0\n"               \
100       "        bne   1b\n"                   \
101       : /*out*/ : /*in*/ "r"(&(_lval))       \
102       : /*trash*/ "x8", "x9", "cc", "memory" \
103   );
104 #elif defined(PLAT_s390x_linux)
105 #  define INC(_lval,_lqual) \
106    __asm__ __volatile__( \
107       "1: l     0,%0\n"                            \
108       "   lr    1,0\n"                             \
109       "   ahi   1,1\n"                             \
110       "   cs    0,1,%0\n"                          \
111       "   jl    1b\n"                              \
112       : "+m" (_lval) :: "cc", "0","1" \
113    )
114 #elif defined(PLAT_mips32_linux) || defined(PLAT_mips64_linux)
115 #  define INC(_lval,_lqual)                         \
116      __asm__ __volatile__ (                         \
117       "L1xyzzy1" _lqual":\n"                        \
118       "        move $t0, %0\n"                      \
119       "        ll   $t1, 0($t0)\n"                  \
120       "        addi $t1, $t1, 1\n"                  \
121       "        sc   $t1, 0($t0)\n"                  \
122       "        beqz $t1, L1xyzzy1" _lqual           \
123       : /*out*/ : /*in*/ "r"(&(_lval))              \
124       : /*trash*/ "t0", "t1", "memory"              \
125         )
126 #else
127 #  error "Fix Me for this platform"
128 #endif
129 
130 
131 
132 #define LIMIT 10
133 
134 volatile int x = 0;
135 
child_fn(void * arg)136 void* child_fn ( void* arg )
137 {
138    int q = 0;
139    int oldx = 0;
140    struct timespec ts = { 0, 1000 * 1000 };
141 
142    while (1) {
143       q = (x >= LIMIT);
144       if (x != oldx) {
145          oldx = x;
146          printf("child: new value %d\n", oldx);
147          fflush(stdout);
148       }
149       if (q) break;
150       nanosleep(&ts, 0);
151    }
152    return NULL;
153 }
154 
main(void)155 int main ( void )
156 {
157    pthread_t child;
158    int i;
159 
160    if (pthread_create(&child, NULL, child_fn, NULL)) {
161       perror("pthread_create");
162       exit(1);
163    }
164 
165    for (i = 0; i < LIMIT; i++) {
166       INC(x, "main");
167       if (i == 5) sleep(1); /* make sure child doesn't starve */
168    }
169 
170    if (pthread_join(child, NULL)) {
171       perror("pthread join");
172       exit(1);
173    }
174 
175    printf("done, x = %d\n", x);
176 
177    return 0;
178 }
179