summaryrefslogtreecommitdiff
path: root/lib/simple-atomic.c
blob: 656b4bdc19dcef4b83347225c67d58691c9e5be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Simple atomic operations for multithreading.
   Copyright (C) 2020-2024 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2021.  */

#include <config.h>

/* Specification.  */
#include "simple-atomic.h"

#if 0x590 <= __SUNPRO_C && __STDC__
# define asm __asm
#endif

#if defined _WIN32 && ! defined __CYGWIN__
/* Native Windows.  */

# include <windows.h>

void
memory_barrier (void)
{
  /* MemoryBarrier
     <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-memorybarrier>  */
  MemoryBarrier ();
}

unsigned int
atomic_compare_and_swap (unsigned int volatile *vp,
                         unsigned int cmp,
                         unsigned int newval)
{
  /* InterlockedCompareExchange
     <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchange>  */
  return InterlockedCompareExchange ((LONG volatile *) vp,
                                     (LONG) newval, (LONG) cmp);
}

uintptr_t
atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                             uintptr_t cmp,
                             uintptr_t newval)
{
  /* InterlockedCompareExchangePointer
     <https://docs.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedcompareexchangepointer>  */
  return InterlockedCompareExchangePointer ((void * volatile *) vp,
                                            (void *) newval, (void *) cmp);
}

#elif HAVE_PTHREAD_H
/* Some other platform that supports multi-threading.

   We don't use the C11 <stdatomic.h> (available in GCC >= 4.9) because it would
   require to link with -latomic.  */

# if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) \
       || __clang_major__ >= 3) \
      && HAVE_ATOMIC_COMPARE_AND_SWAP_GCC41)
/* Use GCC built-ins (available on many platforms with GCC >= 4.1 or
   clang >= 3.0).
   Documentation:
   <https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html>  */

void
memory_barrier (void)
{
  __sync_synchronize ();
}

unsigned int
atomic_compare_and_swap (unsigned int volatile *vp,
                         unsigned int cmp,
                         unsigned int newval)
{
  return __sync_val_compare_and_swap (vp, cmp, newval);
}

uintptr_t
atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                             uintptr_t cmp,
                             uintptr_t newval)
{
  return __sync_val_compare_and_swap (vp, cmp, newval);
}

# elif defined _AIX
/* AIX */
/* For older versions of GCC or xlc, use inline assembly.
   __compare_and_swap and __compare_and_swaplp are not sufficient here.  */

void
memory_barrier (void)
{
  asm volatile ("sync");
}

unsigned int
atomic_compare_and_swap (unsigned int volatile *vp,
                         unsigned int cmp,
                         unsigned int newval)
{
  asm volatile ("sync");

  unsigned int oldval;
  asm volatile (
#  if defined __GNUC__ || defined __clang__
                "1: lwarx %0,0,%1\n"
                  " cmpw 0,%0,%2\n"
                  " bne 0,2f\n"
                  " stwcx. %3,0,%1\n"
                  " bne 0,1b\n"
                "2:"
#  else /* another label syntax */
                ".L01: lwarx %0,0,%1\n"
                     " cmpw 0,%0,%2\n"
                     " bne 0,.L02\n"
                     " stwcx. %3,0,%1\n"
                     " bne 0,.L01\n"
                ".L02:"
#  endif
                : "=&r" (oldval)
                : "r" (vp), "r" (cmp), "r" (newval)
                : "cr0");

  asm volatile ("isync");
  return oldval;
}

uintptr_t
atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                             uintptr_t cmp,
                             uintptr_t newval)
{
  asm volatile ("sync");

  uintptr_t oldval;
  asm volatile (
#  if defined __GNUC__ || defined __clang__
#   if defined __powerpc64__ || defined __LP64__
                "1: ldarx %0,0,%1\n"
                  " cmpd 0,%0,%2\n"
                  " bne 0,2f\n"
                  " stdcx. %3,0,%1\n"
                  " bne 0,1b\n"
                "2:"
#   else
                "1: lwarx %0,0,%1\n"
                  " cmpw 0,%0,%2\n"
                  " bne 0,2f\n"
                  " stwcx. %3,0,%1\n"
                  " bne 0,1b\n"
                "2:"
#   endif
#  else /* another label syntax */
#   if defined __powerpc64__ || defined __LP64__
                ".L01: ldarx %0,0,%1\n"
                     " cmpd 0,%0,%2\n"
                     " bne 0,.L02\n"
                     " stdcx. %3,0,%1\n"
                     " bne 0,.L01\n"
                ".L02:"
#   else
                ".L01: lwarx %0,0,%1\n"
                     " cmpw 0,%0,%2\n"
                     " bne 0,.L02\n"
                     " stwcx. %3,0,%1\n"
                     " bne 0,.L01\n"
                ".L02:"
#   endif
#  endif
                : "=&r" (oldval)
                : "r" (vp), "r" (cmp), "r" (newval)
                : "cr0");

  asm volatile ("isync");
  return oldval;
}

# elif ((defined __GNUC__ || defined __clang__ || defined __SUNPRO_C) && (defined __sparc || defined __i386 || defined __x86_64__)) || (defined __TINYC__ && (defined __i386 || defined __x86_64__))
/* For older versions of GCC or clang, use inline assembly.
   GCC, clang, and the Oracle Studio C 12 compiler understand GCC's extended
   asm syntax, but the plain Oracle Studio C 11 compiler understands only
   simple asm.  */

void
memory_barrier (void)
{
#  if defined __GNUC__ || defined __clang__ || __SUNPRO_C >= 0x590 || defined __TINYC__
#   if defined __i386 || defined __x86_64__
#    if defined __TINYC__ && defined __i386
  /* Cannot use the SSE instruction "mfence" with this compiler.  */
  asm volatile ("lock orl $0,(%esp)");
#    else
  asm volatile ("mfence");
#    endif
#   endif
#   if defined __sparc
  asm volatile ("membar 2");
#   endif
#  else
#   if defined __i386 || defined __x86_64__
  asm ("mfence");
#   endif
#   if defined __sparc
  asm ("membar 2");
#   endif
#  endif
}

unsigned int
atomic_compare_and_swap (unsigned int volatile *vp,
                         unsigned int cmp,
                         unsigned int newval)
{
#  if defined __GNUC__ || defined __clang__ || __SUNPRO_C >= 0x590 || defined __TINYC__
  unsigned int oldval;
#   if defined __i386 || defined __x86_64__
  asm volatile (" lock\n cmpxchgl %3,(%1)"
                : "=a" (oldval) : "r" (vp), "a" (cmp), "r" (newval) : "memory");
#   endif
#   if defined __sparc
  asm volatile (" cas [%1],%2,%3\n"
                " mov %3,%0"
                : "=r" (oldval) : "r" (vp), "r" (cmp), "r" (newval) : "memory");
#   endif
  return oldval;
#  else /* __SUNPRO_C */
#   if defined __x86_64__
  asm (" movl %esi,%eax\n"
       " lock\n cmpxchgl %edx,(%rdi)");
#   elif defined __i386
  asm (" movl 16(%ebp),%ecx\n"
       " movl 12(%ebp),%eax\n"
       " movl 8(%ebp),%edx\n"
       " lock\n cmpxchgl %ecx,(%edx)");
#   endif
#   if defined __sparc
  asm (" cas [%i0],%i1,%i2\n"
       " mov %i2,%i0");
#   endif
#  endif
}

uintptr_t
atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                             uintptr_t cmp,
                             uintptr_t newval)
{
#  if defined __GNUC__ || defined __clang__ || __SUNPRO_C >= 0x590 || defined __TINYC__
  uintptr_t oldval;
#   if defined __x86_64__
  asm volatile (" lock\n cmpxchgq %3,(%1)"
                : "=a" (oldval) : "r" (vp), "a" (cmp), "r" (newval) : "memory");
#   elif defined __i386
  asm volatile (" lock\n cmpxchgl %3,(%1)"
                : "=a" (oldval) : "r" (vp), "a" (cmp), "r" (newval) : "memory");
#   endif
#   if defined __sparc && (defined __sparcv9 || defined __arch64__)
  asm volatile (" casx [%1],%2,%3\n"
                " mov %3,%0"
                : "=r" (oldval) : "r" (vp), "r" (cmp), "r" (newval) : "memory");
#   elif defined __sparc
  asm volatile (" cas [%1],%2,%3\n"
                " mov %3,%0"
                : "=r" (oldval) : "r" (vp), "r" (cmp), "r" (newval) : "memory");
#   endif
  return oldval;
#  else /* __SUNPRO_C */
#   if defined __x86_64__
  asm (" movq %rsi,%rax\n"
       " lock\n cmpxchgq %rdx,(%rdi)");
#   elif defined __i386
  asm (" movl 16(%ebp),%ecx\n"
       " movl 12(%ebp),%eax\n"
       " movl 8(%ebp),%edx\n"
       " lock\n cmpxchgl %ecx,(%edx)");
#   endif
#   if defined __sparc && (defined __sparcv9 || defined __arch64__)
  asm (" casx [%i0],%i1,%i2\n"
       " mov %i2,%i0");
#   elif defined __sparc
  asm (" cas [%i0],%i1,%i2\n"
       " mov %i2,%i0");
#   endif
#  endif
}

# else
/* Fallback code.  It has some race conditions.  The unit test will fail.  */

void
memory_barrier (void)
{
}

unsigned int
atomic_compare_and_swap (unsigned int volatile *vp,
                         unsigned int cmp,
                         unsigned int newval)
{
  unsigned int oldval = *vp;
  if (oldval == cmp)
    *vp = newval;
  return oldval;
}

uintptr_t
atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                             uintptr_t cmp,
                             uintptr_t newval)
{
  uintptr_t oldval = *vp;
  if (oldval == cmp)
    *vp = newval;
  return oldval;
}

# endif

#else
/* A platform that does not support multi-threading.  */

void
memory_barrier (void)
{
}

unsigned int
atomic_compare_and_swap (unsigned int volatile *vp,
                         unsigned int cmp,
                         unsigned int newval)
{
  unsigned int oldval = *vp;
  if (oldval == cmp)
    *vp = newval;
  return oldval;
}

uintptr_t
atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                             uintptr_t cmp,
                             uintptr_t newval)
{
  uintptr_t oldval = *vp;
  if (oldval == cmp)
    *vp = newval;
  return oldval;
}

#endif

Return to:

Send suggestions and report system problems to the System administrator.