• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "config.h"
3 #include <pthread.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <assert.h>
7 
8 /* Simple test program, no race.  Parent and child both modify x and
9    use the hardware bus lock (implicitly, since XCHG r,m on x86/amd64
10    does not require an explicit LOCK prefix.). */
11 
12 #undef PLAT_x86_darwin
13 #undef PLAT_amd64_darwin
14 #undef PLAT_x86_linux
15 #undef PLAT_amd64_linux
16 #undef PLAT_ppc32_linux
17 #undef PLAT_ppc64be_linux
18 #undef PLAT_arm_linux
19 #undef PLAT_s390x_linux
20 #undef PLAT_mips32_linux
21 #undef PLAT_tilegx_linux
22 #undef PLAT_x86_solaris
23 #undef PLAT_amd64_solaris
24 
25 #if defined(__APPLE__) && defined(__i386__)
26 #  define PLAT_x86_darwin 1
27 #elif defined(__APPLE__) && defined(__x86_64__)
28 #  define PLAT_amd64_darwin 1
29 #elif defined(__linux__) && defined(__i386__)
30 #  define PLAT_x86_linux 1
31 #elif defined(__linux__) && defined(__x86_64__)
32 #  define PLAT_amd64_linux 1
33 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__)
34 #  define PLAT_ppc32_linux 1
35 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__)
36 #  define PLAT_ppc64_linux 1
37 #elif defined(__linux__) && defined(__arm__) && !defined(__aarch64__)
38 #  define PLAT_arm_linux 1
39 #elif defined(__linux__) && defined(__aarch64__) && !defined(__arm__)
40 #  define PLAT_arm64_linux 1
41 #elif defined(__linux__) && defined(__s390x__)
42 #  define PLAT_s390x_linux 1
43 #elif defined(__linux__) && defined(__mips__)
44 #  define PLAT_mips32_linux 1
45 #elif defined(__linux__) && defined(__tilegx__)
46 #  define PLAT_tilegx_linux 1
47 #elif defined(__sun__) && defined(__i386__)
48 #  define PLAT_x86_solaris 1
49 #elif defined(__sun__) && defined(__x86_64__)
50 #  define PLAT_amd64_solaris 1
51 #endif
52 
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 #  define XCHG_M_R(_addr,_lval) \
58      __asm__ __volatile__( \
59         "xchgl %0, %1" \
60         : /*out*/ "+r"(_lval) \
61         : /*in*/  "m"(_addr) \
62         : "memory", "cc" \
63      )
64 #  define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \
65      __asm__ __volatile__( \
66         "lock xchgl %0, %1" \
67         : /*out*/ "+r"(_lval) \
68         : /*in*/  "m"(_addr) \
69         : "memory", "cc" \
70      )
71 
72 #elif defined(PLAT_s390x_linux)
73 #  define XCHG_M_R(_addr,_lval)                              \
74      do {                                                    \
75         __asm__ __volatile__(                                \
76            "0: l   0,%[global]\n\t"                          \
77            "   cs  0,%[local],%[global]\n\t"                 \
78            "   bne 0b\n\t"                                   \
79            "   lr  %[local],0\n\t"                           \
80            : /*out*/ [global]"+m"(_addr), [local]"+d"(_lval) \
81            : /*in*/                                          \
82            : "0", "memory", "cc"                             \
83         );                                                   \
84      } while (0)
85 
86 #  define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \
87       XCHG_M_R(_addr,_lval)
88 
89 #elif defined(PLAT_mips32_linux) || defined(PLAT_mips64_linux)
90 #  define XCHG_M_R(_addr,_lval)                              \
91      __asm__ __volatile__(                                   \
92         "move $12, %2\n"                                     \
93         "move $13, %1\n"                                     \
94         "ll $14, 0($13)\n"                                   \
95         "sc $12, 0($13)\n"                                   \
96         "move %0, $14\n"                                     \
97         : /*out*/ "=r"(_lval)                                \
98         : /*in*/  "r"(&_addr), "r"(_lval)                    \
99         : "$12", "$13", "$14", "memory", "cc"                \
100      )
101 
102 #  define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \
103       XCHG_M_R(_addr,_lval)
104 
105 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) \
106       || defined(PLAT_arm_linux) || defined(PLAT_arm64_linux)
107 #  if defined(HAVE_BUILTIN_ATOMIC)
108 #    define XCHG_M_R(_addr,_lval)                                           \
109         do {                                                                \
110           int tmp;                                                          \
111           while ((tmp = *(int*)(& _addr)),                                  \
112                  ! __sync_bool_compare_and_swap((int*)&_addr, tmp, _lval))  \
113             ;                                                               \
114           _lval = tmp;                                                      \
115         } while (0)
116 #  else
117 #    warning "XCHG_M_R() implementation is missing. Either" \
118              "provide one or use a newer gcc version."
119 #    define XCHG_M_R(_addr,_lval) \
120         do { int tmp = *(int*)(& _addr); \
121              *(int*)(& _addr) = (_lval); \
122              _lval = tmp; \
123         } while (0)
124 #  endif
125 #  define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \
126       XCHG_M_R(_addr,_lval)
127 
128 #elif defined(PLAT_tilegx_linux)
129 #  define XCHG_M_R(_addr,_lval) \
130   _lval = __insn_exch4(&_addr, _lval)
131 
132 #  define XCHG_M_R_with_redundant_LOCK(_addr,_lval) \
133   XCHG_M_R(_addr, _lval)
134 #else
135 #  error "Unsupported architecture"
136 
137 #endif
138 
139 int x = 0;
140 
child_fn(void * arg)141 void* child_fn ( void* arg )
142 {
143    int v = 12345;
144    XCHG_M_R_with_redundant_LOCK(x, v);
145    assert(v == 0 || v == 6789);
146    return NULL;
147 }
148 
main(void)149 int main ( void )
150 {
151    int v = 6789;
152    pthread_t child;
153 
154    if (pthread_create(&child, NULL, child_fn, NULL)) {
155       perror("pthread_create");
156       exit(1);
157    }
158 
159    XCHG_M_R(x, v);
160    assert(v == 0 || v == 12345);
161 
162    if (pthread_join(child, NULL)) {
163       perror("pthread join");
164       exit(1);
165    }
166 
167    if (v == 0 || v == 12345)
168       printf("success\n");
169    else
170       printf("failure\n");
171 
172    return v;
173 }
174