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