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