ARGOBOTS
abtd_atomic.h
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #ifndef ABTD_ATOMIC_H_INCLUDED
7 #define ABTD_ATOMIC_H_INCLUDED
8 
9 #include <stdint.h>
10 
11 typedef struct ABTD_atomic_bool {
12  uint8_t val;
13 } ABTD_atomic_bool;
14 
15 typedef struct ABTD_atomic_int {
16  int val;
17 } ABTD_atomic_int;
18 
19 typedef struct ABTD_atomic_int32 {
20  int32_t val;
21 } ABTD_atomic_int32;
22 
23 typedef struct ABTD_atomic_uint32 {
24  uint32_t val;
25 } ABTD_atomic_uint32;
26 
27 typedef struct ABTD_atomic_int64 {
28  int64_t val;
29 } ABTD_atomic_int64;
30 
31 typedef struct ABTD_atomic_uint64 {
32  uint64_t val;
33 } ABTD_atomic_uint64;
34 
35 typedef struct ABTD_atomic_ptr {
36  void *val;
37 } ABTD_atomic_ptr;
38 
39 #define ABTD_ATOMIC_BOOL_STATIC_INITIALIZER(val) \
40  { \
41  (val) \
42  }
43 #define ABTD_ATOMIC_INT_STATIC_INITIALIZER(val) \
44  { \
45  (val) \
46  }
47 #define ABTD_ATOMIC_INT32_STATIC_INITIALIZER(val) \
48  { \
49  (val) \
50  }
51 #define ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(val) \
52  { \
53  (val) \
54  }
55 #define ABTD_ATOMIC_INT64_STATIC_INITIALIZER(val) \
56  { \
57  (val) \
58  }
59 #define ABTD_ATOMIC_UINT64_STATIC_INITIALIZER(val) \
60  { \
61  (val) \
62  }
63 #define ABTD_ATOMIC_PTR_STATIC_INITIALIZER(val) \
64  { \
65  (val) \
66  }
67 
68 static inline int ABTDI_atomic_val_cas_int(ABTD_atomic_int *ptr, int oldv,
69  int newv, int weak)
70 {
71 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
72  int tmp_oldv = oldv;
73  int ret = __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
74  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
75  return ret ? tmp_oldv : oldv;
76 #else
77  return __sync_val_compare_and_swap(&ptr->val, oldv, newv);
78 #endif
79 }
80 
81 static inline int32_t ABTDI_atomic_val_cas_int32(ABTD_atomic_int32 *ptr,
82  int32_t oldv, int32_t newv,
83  int weak)
84 {
85 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
86  int32_t tmp_oldv = oldv;
87  int ret = __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
88  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
89  return ret ? tmp_oldv : oldv;
90 #else
91  return __sync_val_compare_and_swap(&ptr->val, oldv, newv);
92 #endif
93 }
94 
95 static inline uint32_t ABTDI_atomic_val_cas_uint32(ABTD_atomic_uint32 *ptr,
96  uint32_t oldv, uint32_t newv,
97  int weak)
98 {
99 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
100  uint32_t tmp_oldv = oldv;
101  int ret = __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
102  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
103  return ret ? tmp_oldv : oldv;
104 #else
105  return __sync_val_compare_and_swap(&ptr->val, oldv, newv);
106 #endif
107 }
108 
109 static inline int64_t ABTDI_atomic_val_cas_int64(ABTD_atomic_int64 *ptr,
110  int64_t oldv, int64_t newv,
111  int weak)
112 {
113 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
114  int64_t tmp_oldv = oldv;
115  int ret = __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
116  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
117  return ret ? tmp_oldv : oldv;
118 #else
119  return __sync_val_compare_and_swap(&ptr->val, oldv, newv);
120 #endif
121 }
122 
123 static inline uint64_t ABTDI_atomic_val_cas_uint64(ABTD_atomic_uint64 *ptr,
124  uint64_t oldv, uint64_t newv,
125  int weak)
126 {
127 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
128  uint64_t tmp_oldv = oldv;
129  int ret = __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
130  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
131  return ret ? tmp_oldv : oldv;
132 #else
133  return __sync_val_compare_and_swap(&ptr->val, oldv, newv);
134 #endif
135 }
136 
137 static inline void *ABTDI_atomic_val_cas_ptr(ABTD_atomic_ptr *ptr, void *oldv,
138  void *newv, int weak)
139 {
140 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
141  void *tmp_oldv = oldv;
142  int ret = __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
143  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
144  return ret ? tmp_oldv : oldv;
145 #else
146  return __sync_val_compare_and_swap(&ptr->val, oldv, newv);
147 #endif
148 }
149 
150 static inline int ABTDI_atomic_bool_cas_int(ABTD_atomic_int *ptr, int oldv,
151  int newv, int weak)
152 {
153 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
154  return __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
155  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
156 #else
157  return __sync_bool_compare_and_swap(&ptr->val, oldv, newv);
158 #endif
159 }
160 
161 static inline int ABTDI_atomic_bool_cas_int32(ABTD_atomic_int32 *ptr,
162  int32_t oldv, int32_t newv,
163  int weak)
164 {
165 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
166  return __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
167  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
168 #else
169  return __sync_bool_compare_and_swap(&ptr->val, oldv, newv);
170 #endif
171 }
172 
173 static inline int ABTDI_atomic_bool_cas_uint32(ABTD_atomic_uint32 *ptr,
174  uint32_t oldv, uint32_t newv,
175  int weak)
176 {
177 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
178  return __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
179  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
180 #else
181  return __sync_bool_compare_and_swap(&ptr->val, oldv, newv);
182 #endif
183 }
184 
185 static inline int ABTDI_atomic_bool_cas_int64(ABTD_atomic_int64 *ptr,
186  int64_t oldv, int64_t newv,
187  int weak)
188 {
189 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
190  return __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
191  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
192 #else
193  return __sync_bool_compare_and_swap(&ptr->val, oldv, newv);
194 #endif
195 }
196 
197 static inline int ABTDI_atomic_bool_cas_uint64(ABTD_atomic_uint64 *ptr,
198  uint64_t oldv, uint64_t newv,
199  int weak)
200 {
201 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
202  return __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
203  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
204 #else
205  return __sync_bool_compare_and_swap(&ptr->val, oldv, newv);
206 #endif
207 }
208 
209 static inline int ABTDI_atomic_bool_cas_ptr(ABTD_atomic_ptr *ptr, void *oldv,
210  void *newv, int weak)
211 {
212 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
213  return __atomic_compare_exchange_n(&ptr->val, &oldv, newv, weak,
214  __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
215 #else
216  return __sync_bool_compare_and_swap(&ptr->val, oldv, newv);
217 #endif
218 }
219 
220 static inline int ABTD_atomic_val_cas_weak_int(ABTD_atomic_int *ptr, int oldv,
221  int newv)
222 {
223  return ABTDI_atomic_val_cas_int(ptr, oldv, newv, 1);
224 }
225 
226 static inline int32_t ABTD_atomic_val_cas_weak_int32(ABTD_atomic_int32 *ptr,
227  int32_t oldv, int32_t newv)
228 {
229  return ABTDI_atomic_val_cas_int32(ptr, oldv, newv, 1);
230 }
231 
232 static inline uint32_t ABTD_atomic_val_cas_weak_uint32(ABTD_atomic_uint32 *ptr,
233  uint32_t oldv,
234  uint32_t newv)
235 {
236  return ABTDI_atomic_val_cas_uint32(ptr, oldv, newv, 1);
237 }
238 
239 static inline int64_t ABTD_atomic_val_cas_weak_int64(ABTD_atomic_int64 *ptr,
240  int64_t oldv, int64_t newv)
241 {
242  return ABTDI_atomic_val_cas_int64(ptr, oldv, newv, 1);
243 }
244 
245 static inline uint64_t ABTD_atomic_val_cas_weak_uint64(ABTD_atomic_uint64 *ptr,
246  uint64_t oldv,
247  uint64_t newv)
248 {
249  return ABTDI_atomic_val_cas_uint64(ptr, oldv, newv, 1);
250 }
251 
252 static inline void *ABTD_atomic_val_cas_weak_ptr(ABTD_atomic_ptr *ptr,
253  void *oldv, void *newv)
254 {
255  return ABTDI_atomic_val_cas_ptr(ptr, oldv, newv, 1);
256 }
257 
258 static inline int ABTD_atomic_val_cas_strong_int(ABTD_atomic_int *ptr, int oldv,
259  int newv)
260 {
261  return ABTDI_atomic_val_cas_int(ptr, oldv, newv, 0);
262 }
263 
264 static inline int32_t ABTD_atomic_val_cas_strong_int32(ABTD_atomic_int32 *ptr,
265  int32_t oldv,
266  int32_t newv)
267 {
268  return ABTDI_atomic_val_cas_int32(ptr, oldv, newv, 0);
269 }
270 
271 static inline uint32_t
272 ABTD_atomic_val_cas_strong_uint32(ABTD_atomic_uint32 *ptr, uint32_t oldv,
273  uint32_t newv)
274 {
275  return ABTDI_atomic_val_cas_uint32(ptr, oldv, newv, 0);
276 }
277 
278 static inline int64_t ABTD_atomic_val_cas_strong_int64(ABTD_atomic_int64 *ptr,
279  int64_t oldv,
280  int64_t newv)
281 {
282  return ABTDI_atomic_val_cas_int64(ptr, oldv, newv, 0);
283 }
284 
285 static inline uint64_t
286 ABTD_atomic_val_cas_strong_uint64(ABTD_atomic_uint64 *ptr, uint64_t oldv,
287  uint64_t newv)
288 {
289  return ABTDI_atomic_val_cas_uint64(ptr, oldv, newv, 0);
290 }
291 
292 static inline void *ABTD_atomic_val_cas_strong_ptr(ABTD_atomic_ptr *ptr,
293  void *oldv, void *newv)
294 {
295  return ABTDI_atomic_val_cas_ptr(ptr, oldv, newv, 0);
296 }
297 
298 static inline int ABTD_atomic_bool_cas_weak_int(ABTD_atomic_int *ptr, int oldv,
299  int newv)
300 {
301  return ABTDI_atomic_bool_cas_int(ptr, oldv, newv, 1);
302 }
303 
304 static inline int ABTD_atomic_bool_cas_weak_int32(ABTD_atomic_int32 *ptr,
305  int32_t oldv, int32_t newv)
306 {
307  return ABTDI_atomic_bool_cas_int32(ptr, oldv, newv, 1);
308 }
309 
310 static inline int ABTD_atomic_bool_cas_weak_uint32(ABTD_atomic_uint32 *ptr,
311  uint32_t oldv, uint32_t newv)
312 {
313  return ABTDI_atomic_bool_cas_uint32(ptr, oldv, newv, 1);
314 }
315 
316 static inline int ABTD_atomic_bool_cas_weak_int64(ABTD_atomic_int64 *ptr,
317  int64_t oldv, int64_t newv)
318 {
319  return ABTDI_atomic_bool_cas_int64(ptr, oldv, newv, 1);
320 }
321 
322 static inline int ABTD_atomic_bool_cas_weak_uint64(ABTD_atomic_uint64 *ptr,
323  uint64_t oldv, uint64_t newv)
324 {
325  return ABTDI_atomic_bool_cas_uint64(ptr, oldv, newv, 1);
326 }
327 
328 static inline int ABTD_atomic_bool_cas_weak_ptr(ABTD_atomic_ptr *ptr,
329  void *oldv, void *newv)
330 {
331  return ABTDI_atomic_bool_cas_ptr(ptr, oldv, newv, 1);
332 }
333 
334 static inline int ABTD_atomic_bool_cas_strong_int(ABTD_atomic_int *ptr,
335  int oldv, int newv)
336 {
337  return ABTDI_atomic_bool_cas_int(ptr, oldv, newv, 0);
338 }
339 
340 static inline int ABTD_atomic_bool_cas_strong_int32(ABTD_atomic_int32 *ptr,
341  int32_t oldv, int32_t newv)
342 {
343  return ABTDI_atomic_bool_cas_int32(ptr, oldv, newv, 0);
344 }
345 
346 static inline int ABTD_atomic_bool_cas_strong_uint32(ABTD_atomic_uint32 *ptr,
347  uint32_t oldv,
348  uint32_t newv)
349 {
350  return ABTDI_atomic_bool_cas_uint32(ptr, oldv, newv, 0);
351 }
352 
353 static inline int ABTD_atomic_bool_cas_strong_int64(ABTD_atomic_int64 *ptr,
354  int64_t oldv, int64_t newv)
355 {
356  return ABTDI_atomic_bool_cas_int64(ptr, oldv, newv, 0);
357 }
358 
359 static inline int ABTD_atomic_bool_cas_strong_uint64(ABTD_atomic_uint64 *ptr,
360  uint64_t oldv,
361  uint64_t newv)
362 {
363  return ABTDI_atomic_bool_cas_uint64(ptr, oldv, newv, 0);
364 }
365 
366 static inline int ABTD_atomic_bool_cas_strong_ptr(ABTD_atomic_ptr *ptr,
367  void *oldv, void *newv)
368 {
369  return ABTDI_atomic_bool_cas_ptr(ptr, oldv, newv, 0);
370 }
371 
372 static inline int ABTD_atomic_fetch_add_int(ABTD_atomic_int *ptr, int v)
373 {
374 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
375  return __atomic_fetch_add(&ptr->val, v, __ATOMIC_ACQ_REL);
376 #else
377  return __sync_fetch_and_add(&ptr->val, v);
378 #endif
379 }
380 
381 static inline int32_t ABTD_atomic_fetch_add_int32(ABTD_atomic_int32 *ptr,
382  int32_t v)
383 {
384 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
385  return __atomic_fetch_add(&ptr->val, v, __ATOMIC_ACQ_REL);
386 #else
387  return __sync_fetch_and_add(&ptr->val, v);
388 #endif
389 }
390 
391 static inline uint32_t ABTD_atomic_fetch_add_uint32(ABTD_atomic_uint32 *ptr,
392  uint32_t v)
393 {
394 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
395  return __atomic_fetch_add(&ptr->val, v, __ATOMIC_ACQ_REL);
396 #else
397  return __sync_fetch_and_add(&ptr->val, v);
398 #endif
399 }
400 
401 static inline int64_t ABTD_atomic_fetch_add_int64(ABTD_atomic_int64 *ptr,
402  int64_t v)
403 {
404 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
405  return __atomic_fetch_add(&ptr->val, v, __ATOMIC_ACQ_REL);
406 #else
407  return __sync_fetch_and_add(&ptr->val, v);
408 #endif
409 }
410 
411 static inline uint64_t ABTD_atomic_fetch_add_uint64(ABTD_atomic_uint64 *ptr,
412  uint64_t v)
413 {
414 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
415  return __atomic_fetch_add(&ptr->val, v, __ATOMIC_ACQ_REL);
416 #else
417  return __sync_fetch_and_add(&ptr->val, v);
418 #endif
419 }
420 
421 static inline int ABTD_atomic_fetch_sub_int(ABTD_atomic_int *ptr, int v)
422 {
423 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
424  return __atomic_fetch_sub(&ptr->val, v, __ATOMIC_ACQ_REL);
425 #else
426  return __sync_fetch_and_sub(&ptr->val, v);
427 #endif
428 }
429 
430 static inline int32_t ABTD_atomic_fetch_sub_int32(ABTD_atomic_int32 *ptr,
431  int32_t v)
432 {
433 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
434  return __atomic_fetch_sub(&ptr->val, v, __ATOMIC_ACQ_REL);
435 #else
436  return __sync_fetch_and_sub(&ptr->val, v);
437 #endif
438 }
439 
440 static inline uint32_t ABTD_atomic_fetch_sub_uint32(ABTD_atomic_uint32 *ptr,
441  uint32_t v)
442 {
443 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
444  return __atomic_fetch_sub(&ptr->val, v, __ATOMIC_ACQ_REL);
445 #else
446  return __sync_fetch_and_sub(&ptr->val, v);
447 #endif
448 }
449 
450 static inline int64_t ABTD_atomic_fetch_sub_int64(ABTD_atomic_int64 *ptr,
451  int64_t v)
452 {
453 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
454  return __atomic_fetch_sub(&ptr->val, v, __ATOMIC_ACQ_REL);
455 #else
456  return __sync_fetch_and_sub(&ptr->val, v);
457 #endif
458 }
459 
460 static inline uint64_t ABTD_atomic_fetch_sub_uint64(ABTD_atomic_uint64 *ptr,
461  uint64_t v)
462 {
463 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
464  return __atomic_fetch_sub(&ptr->val, v, __ATOMIC_ACQ_REL);
465 #else
466  return __sync_fetch_and_sub(&ptr->val, v);
467 #endif
468 }
469 
470 static inline int ABTD_atomic_fetch_and_int(ABTD_atomic_int *ptr, int v)
471 {
472 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
473  return __atomic_fetch_and(&ptr->val, v, __ATOMIC_ACQ_REL);
474 #else
475  return __sync_fetch_and_and(&ptr->val, v);
476 #endif
477 }
478 
479 static inline int32_t ABTD_atomic_fetch_and_int32(ABTD_atomic_int32 *ptr,
480  int32_t v)
481 {
482 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
483  return __atomic_fetch_and(&ptr->val, v, __ATOMIC_ACQ_REL);
484 #else
485  return __sync_fetch_and_and(&ptr->val, v);
486 #endif
487 }
488 
489 static inline uint32_t ABTD_atomic_fetch_and_uint32(ABTD_atomic_uint32 *ptr,
490  uint32_t v)
491 {
492 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
493  return __atomic_fetch_and(&ptr->val, v, __ATOMIC_ACQ_REL);
494 #else
495  return __sync_fetch_and_and(&ptr->val, v);
496 #endif
497 }
498 
499 static inline int64_t ABTD_atomic_fetch_and_int64(ABTD_atomic_int64 *ptr,
500  int64_t v)
501 {
502 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
503  return __atomic_fetch_and(&ptr->val, v, __ATOMIC_ACQ_REL);
504 #else
505  return __sync_fetch_and_and(&ptr->val, v);
506 #endif
507 }
508 
509 static inline uint64_t ABTD_atomic_fetch_and_uint64(ABTD_atomic_uint64 *ptr,
510  uint64_t v)
511 {
512 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
513  return __atomic_fetch_and(&ptr->val, v, __ATOMIC_ACQ_REL);
514 #else
515  return __sync_fetch_and_and(&ptr->val, v);
516 #endif
517 }
518 
519 static inline int ABTD_atomic_fetch_or_int(ABTD_atomic_int *ptr, int v)
520 {
521 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
522  return __atomic_fetch_or(&ptr->val, v, __ATOMIC_ACQ_REL);
523 #else
524  return __sync_fetch_and_or(&ptr->val, v);
525 #endif
526 }
527 
528 static inline int32_t ABTD_atomic_fetch_or_int32(ABTD_atomic_int32 *ptr,
529  int32_t v)
530 {
531 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
532  return __atomic_fetch_or(&ptr->val, v, __ATOMIC_ACQ_REL);
533 #else
534  return __sync_fetch_and_or(&ptr->val, v);
535 #endif
536 }
537 
538 static inline uint32_t ABTD_atomic_fetch_or_uint32(ABTD_atomic_uint32 *ptr,
539  uint32_t v)
540 {
541 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
542  return __atomic_fetch_or(&ptr->val, v, __ATOMIC_ACQ_REL);
543 #else
544  return __sync_fetch_and_or(&ptr->val, v);
545 #endif
546 }
547 
548 static inline int64_t ABTD_atomic_fetch_or_int64(ABTD_atomic_int64 *ptr,
549  int64_t v)
550 {
551 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
552  return __atomic_fetch_or(&ptr->val, v, __ATOMIC_ACQ_REL);
553 #else
554  return __sync_fetch_and_or(&ptr->val, v);
555 #endif
556 }
557 
558 static inline uint64_t ABTD_atomic_fetch_or_uint64(ABTD_atomic_uint64 *ptr,
559  uint64_t v)
560 {
561 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
562  return __atomic_fetch_or(&ptr->val, v, __ATOMIC_ACQ_REL);
563 #else
564  return __sync_fetch_and_or(&ptr->val, v);
565 #endif
566 }
567 
568 static inline int ABTD_atomic_fetch_xor_int(ABTD_atomic_int *ptr, int v)
569 {
570 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
571  return __atomic_fetch_xor(&ptr->val, v, __ATOMIC_ACQ_REL);
572 #else
573  return __sync_fetch_and_xor(&ptr->val, v);
574 #endif
575 }
576 
577 static inline int32_t ABTD_atomic_fetch_xor_int32(ABTD_atomic_int32 *ptr,
578  int32_t v)
579 {
580 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
581  return __atomic_fetch_xor(&ptr->val, v, __ATOMIC_ACQ_REL);
582 #else
583  return __sync_fetch_and_xor(&ptr->val, v);
584 #endif
585 }
586 
587 static inline uint32_t ABTD_atomic_fetch_xor_uint32(ABTD_atomic_uint32 *ptr,
588  uint32_t v)
589 {
590 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
591  return __atomic_fetch_xor(&ptr->val, v, __ATOMIC_ACQ_REL);
592 #else
593  return __sync_fetch_and_xor(&ptr->val, v);
594 #endif
595 }
596 
597 static inline int64_t ABTD_atomic_fetch_xor_int64(ABTD_atomic_int64 *ptr,
598  int64_t v)
599 {
600 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
601  return __atomic_fetch_xor(&ptr->val, v, __ATOMIC_ACQ_REL);
602 #else
603  return __sync_fetch_and_xor(&ptr->val, v);
604 #endif
605 }
606 
607 static inline uint64_t ABTD_atomic_fetch_xor_uint64(ABTD_atomic_uint64 *ptr,
608  uint64_t v)
609 {
610 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
611  return __atomic_fetch_xor(&ptr->val, v, __ATOMIC_ACQ_REL);
612 #else
613  return __sync_fetch_and_xor(&ptr->val, v);
614 #endif
615 }
616 
617 static inline uint16_t ABTD_atomic_test_and_set_bool(ABTD_atomic_bool *ptr)
618 {
619  /* return 0 if this test_and_set succeeds to set a value. */
620 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
621  return __atomic_test_and_set(&ptr->val, __ATOMIC_ACQUIRE);
622 #else
623  return __sync_lock_test_and_set(&ptr->val, 1);
624 #endif
625 }
626 
627 static inline void ABTD_atomic_relaxed_clear_bool(ABTD_atomic_bool *ptr)
628 {
629 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
630  __atomic_clear(&ptr->val, __ATOMIC_RELAXED);
631 #else
632  *(volatile uint8_t *)&ptr->val = 0;
633 #endif
634 }
635 
636 static inline void ABTD_atomic_release_clear_bool(ABTD_atomic_bool *ptr)
637 {
638 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
639  __atomic_clear(&ptr->val, __ATOMIC_RELEASE);
640 #else
641  __sync_lock_release(&ptr->val);
642 #endif
643 }
644 
645 static inline ABT_bool
646 ABTD_atomic_relaxed_load_bool(const ABTD_atomic_bool *ptr)
647 {
648 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
649  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED) ? ABT_TRUE : ABT_FALSE;
650 #else
651  return (*(volatile uint8_t *)&ptr->val) ? ABT_TRUE : ABT_FALSE;
652 #endif
653 }
654 
655 static inline int ABTD_atomic_relaxed_load_int(const ABTD_atomic_int *ptr)
656 {
657 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
658  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED);
659 #else
660  return *(volatile int *)&ptr->val;
661 #endif
662 }
663 
664 static inline int32_t
665 ABTD_atomic_relaxed_load_int32(const ABTD_atomic_int32 *ptr)
666 {
667 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
668  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED);
669 #else
670  return *(volatile int32_t *)&ptr->val;
671 #endif
672 }
673 
674 static inline uint32_t
675 ABTD_atomic_relaxed_load_uint32(const ABTD_atomic_uint32 *ptr)
676 {
677 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
678  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED);
679 #else
680  return *(volatile uint32_t *)&ptr->val;
681 #endif
682 }
683 
684 static inline int64_t
685 ABTD_atomic_relaxed_load_int64(const ABTD_atomic_int64 *ptr)
686 {
687 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
688  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED);
689 #else
690  return *(volatile int64_t *)&ptr->val;
691 #endif
692 }
693 
694 static inline uint64_t
695 ABTD_atomic_relaxed_load_uint64(const ABTD_atomic_uint64 *ptr)
696 {
697  /* return 0 if this test_and_set succeeds to set a value. */
698 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
699  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED);
700 #else
701  return *(volatile uint64_t *)&ptr->val;
702 #endif
703 }
704 
705 static inline void *ABTD_atomic_relaxed_load_ptr(const ABTD_atomic_ptr *ptr)
706 {
707  /* return 0 if this test_and_set succeeds to set a value. */
708 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
709  return __atomic_load_n(&ptr->val, __ATOMIC_RELAXED);
710 #else
711  return *(void *volatile *)&ptr->val;
712 #endif
713 }
714 
715 static inline ABT_bool
716 ABTD_atomic_acquire_load_bool(const ABTD_atomic_bool *ptr)
717 {
718 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
719  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE) ? ABT_TRUE : ABT_FALSE;
720 #else
721  __sync_synchronize();
722  ABT_bool val = *(volatile uint8_t *)&ptr->val ? ABT_TRUE : ABT_FALSE;
723  __sync_synchronize();
724  return val;
725 #endif
726 }
727 
728 static inline int ABTD_atomic_acquire_load_int(const ABTD_atomic_int *ptr)
729 {
730 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
731  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE);
732 #else
733  __sync_synchronize();
734  int val = *(volatile int *)&ptr->val;
735  __sync_synchronize();
736  return val;
737 #endif
738 }
739 
740 static inline int32_t
741 ABTD_atomic_acquire_load_int32(const ABTD_atomic_int32 *ptr)
742 {
743 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
744  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE);
745 #else
746  __sync_synchronize();
747  int32_t val = *(volatile int32_t *)&ptr->val;
748  __sync_synchronize();
749  return val;
750 #endif
751 }
752 
753 static inline uint32_t
754 ABTD_atomic_acquire_load_uint32(const ABTD_atomic_uint32 *ptr)
755 {
756 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
757  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE);
758 #else
759  __sync_synchronize();
760  uint32_t val = *(volatile uint32_t *)&ptr->val;
761  __sync_synchronize();
762  return val;
763 #endif
764 }
765 
766 static inline int64_t
767 ABTD_atomic_acquire_load_int64(const ABTD_atomic_int64 *ptr)
768 {
769 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
770  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE);
771 #else
772  __sync_synchronize();
773  int64_t val = *(volatile int64_t *)&ptr->val;
774  __sync_synchronize();
775  return val;
776 #endif
777 }
778 
779 static inline uint64_t
780 ABTD_atomic_acquire_load_uint64(const ABTD_atomic_uint64 *ptr)
781 {
782  /* return 0 if this test_and_set succeeds to set a value. */
783 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
784  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE);
785 #else
786  __sync_synchronize();
787  uint64_t val = *(volatile uint64_t *)&ptr->val;
788  __sync_synchronize();
789  return val;
790 #endif
791 }
792 
793 static inline void *ABTD_atomic_acquire_load_ptr(const ABTD_atomic_ptr *ptr)
794 {
795  /* return 0 if this test_and_set succeeds to set a value. */
796 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
797  return __atomic_load_n(&ptr->val, __ATOMIC_ACQUIRE);
798 #else
799  __sync_synchronize();
800  void *val = *(void *volatile *)&ptr->val;
801  __sync_synchronize();
802  return val;
803 #endif
804 }
805 
806 static inline void ABTD_atomic_relaxed_store_int(ABTD_atomic_int *ptr, int val)
807 {
808 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
809  __atomic_store_n(&ptr->val, val, __ATOMIC_RELAXED);
810 #else
811  *(volatile int *)&ptr->val = val;
812 #endif
813 }
814 
815 static inline void ABTD_atomic_relaxed_store_int32(ABTD_atomic_int32 *ptr,
816  int32_t val)
817 {
818 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
819  __atomic_store_n(&ptr->val, val, __ATOMIC_RELAXED);
820 #else
821  *(volatile int32_t *)&ptr->val = val;
822 #endif
823 }
824 
825 static inline void ABTD_atomic_relaxed_store_uint32(ABTD_atomic_uint32 *ptr,
826  uint32_t val)
827 {
828 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
829  __atomic_store_n(&ptr->val, val, __ATOMIC_RELAXED);
830 #else
831  *(volatile uint32_t *)&ptr->val = val;
832 #endif
833 }
834 
835 static inline void ABTD_atomic_relaxed_store_int64(ABTD_atomic_int64 *ptr,
836  int64_t val)
837 {
838 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
839  __atomic_store_n(&ptr->val, val, __ATOMIC_RELAXED);
840 #else
841  *(volatile int64_t *)&ptr->val = val;
842 #endif
843 }
844 
845 static inline void ABTD_atomic_relaxed_store_uint64(ABTD_atomic_uint64 *ptr,
846  uint64_t val)
847 {
848 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
849  __atomic_store_n(&ptr->val, val, __ATOMIC_RELAXED);
850 #else
851  *(volatile uint64_t *)&ptr->val = val;
852 #endif
853 }
854 
855 static inline void ABTD_atomic_relaxed_store_ptr(ABTD_atomic_ptr *ptr,
856  void *val)
857 {
858 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
859  __atomic_store_n(&ptr->val, val, __ATOMIC_RELAXED);
860 #else
861  *(void *volatile *)&ptr->val = val;
862 #endif
863 }
864 
865 static inline void ABTD_atomic_release_store_int(ABTD_atomic_int *ptr, int val)
866 {
867 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
868  __atomic_store_n(&ptr->val, val, __ATOMIC_RELEASE);
869 #else
870  __sync_synchronize();
871  *(volatile int *)&ptr->val = val;
872  __sync_synchronize();
873 #endif
874 }
875 
876 static inline void ABTD_atomic_release_store_int32(ABTD_atomic_int32 *ptr,
877  int32_t val)
878 {
879 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
880  __atomic_store_n(&ptr->val, val, __ATOMIC_RELEASE);
881 #else
882  __sync_synchronize();
883  *(volatile int32_t *)&ptr->val = val;
884  __sync_synchronize();
885 #endif
886 }
887 
888 static inline void ABTD_atomic_release_store_uint32(ABTD_atomic_uint32 *ptr,
889  uint32_t val)
890 {
891 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
892  __atomic_store_n(&ptr->val, val, __ATOMIC_RELEASE);
893 #else
894  __sync_synchronize();
895  *(volatile uint32_t *)&ptr->val = val;
896  __sync_synchronize();
897 #endif
898 }
899 
900 static inline void ABTD_atomic_release_store_int64(ABTD_atomic_int64 *ptr,
901  int64_t val)
902 {
903 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
904  __atomic_store_n(&ptr->val, val, __ATOMIC_RELEASE);
905 #else
906  __sync_synchronize();
907  *(volatile int64_t *)&ptr->val = val;
908  __sync_synchronize();
909 #endif
910 }
911 
912 static inline void ABTD_atomic_release_store_uint64(ABTD_atomic_uint64 *ptr,
913  uint64_t val)
914 {
915 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
916  __atomic_store_n(&ptr->val, val, __ATOMIC_RELEASE);
917 #else
918  __sync_synchronize();
919  *(volatile uint64_t *)&ptr->val = val;
920  __sync_synchronize();
921 #endif
922 }
923 
924 static inline void ABTD_atomic_release_store_ptr(ABTD_atomic_ptr *ptr,
925  void *val)
926 {
927 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
928  __atomic_store_n(&ptr->val, val, __ATOMIC_RELEASE);
929 #else
930  __sync_synchronize();
931  *(void *volatile *)&ptr->val = val;
932  __sync_synchronize();
933 #endif
934 }
935 
936 static inline int ABTD_atomic_exchange_int(ABTD_atomic_int *ptr, int v)
937 {
938 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
939  return __atomic_exchange_n(&ptr->val, v, __ATOMIC_ACQ_REL);
940 #else
941  int val;
942  do {
943  val = ABTD_atomic_acquire_load_int(ptr);
944  } while (!ABTD_atomic_bool_cas_weak_int(ptr, val, v));
945  return val;
946 #endif
947 }
948 
949 static inline int32_t ABTD_atomic_exchange_int32(ABTD_atomic_int32 *ptr,
950  int32_t v)
951 {
952 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
953  return __atomic_exchange_n(&ptr->val, v, __ATOMIC_ACQ_REL);
954 #else
955  int32_t val;
956  do {
957  val = ABTD_atomic_acquire_load_int32(ptr);
958  } while (!ABTD_atomic_bool_cas_weak_int32(ptr, val, v));
959  return val;
960 #endif
961 }
962 
963 static inline uint32_t ABTD_atomic_exchange_uint32(ABTD_atomic_uint32 *ptr,
964  uint32_t v)
965 {
966 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
967  return __atomic_exchange_n(&ptr->val, v, __ATOMIC_ACQ_REL);
968 #else
969  uint32_t val;
970  do {
971  val = ABTD_atomic_acquire_load_uint32(ptr);
972  } while (!ABTD_atomic_bool_cas_weak_uint32(ptr, val, v));
973  return val;
974 #endif
975 }
976 
977 static inline int64_t ABTD_atomic_exchange_int64(ABTD_atomic_int64 *ptr,
978  int64_t v)
979 {
980 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
981  return __atomic_exchange_n(&ptr->val, v, __ATOMIC_ACQ_REL);
982 #else
983  int64_t val;
984  do {
985  val = ABTD_atomic_acquire_load_int64(ptr);
986  } while (!ABTD_atomic_bool_cas_weak_int64(ptr, val, v));
987  return val;
988 #endif
989 }
990 
991 static inline uint64_t ABTD_atomic_exchange_uint64(ABTD_atomic_uint64 *ptr,
992  uint64_t v)
993 {
994 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
995  return __atomic_exchange_n(&ptr->val, v, __ATOMIC_ACQ_REL);
996 #else
997  uint64_t val;
998  do {
999  val = ABTD_atomic_acquire_load_uint64(ptr);
1000  } while (!ABTD_atomic_bool_cas_weak_uint64(ptr, val, v));
1001  return val;
1002 #endif
1003 }
1004 
1005 static inline void *ABTD_atomic_exchange_ptr(ABTD_atomic_ptr *ptr, void *v)
1006 {
1007 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
1008  return __atomic_exchange_n(&ptr->val, v, __ATOMIC_ACQ_REL);
1009 #else
1010  void *val;
1011  do {
1012  val = ABTD_atomic_acquire_load_ptr(ptr);
1013  } while (!ABTD_atomic_bool_cas_weak_ptr(ptr, val, v));
1014  return val;
1015 #endif
1016 }
1017 
1018 static inline void ABTD_atomic_mem_barrier(void)
1019 {
1020 #ifdef ABT_CONFIG_HAVE_ATOMIC_BUILTIN
1021  __atomic_thread_fence(__ATOMIC_ACQ_REL);
1022 #else
1023  __sync_synchronize();
1024 #endif
1025 }
1026 
1027 static inline void ABTD_compiler_barrier(void)
1028 {
1029  __asm__ __volatile__("" ::: "memory");
1030 }
1031 
1032 static inline void ABTD_atomic_pause(void)
1033 {
1034 #ifdef __x86_64__
1035  __asm__ __volatile__("pause" ::: "memory");
1036 #endif
1037 }
1038 
1039 #endif /* ABTD_ATOMIC_H_INCLUDED */
static int ABTDI_atomic_bool_cas_int32(ABTD_atomic_int32 *ptr, int32_t oldv, int32_t newv, int weak)
Definition: abtd_atomic.h:161
static int ABTDI_atomic_bool_cas_int(ABTD_atomic_int *ptr, int oldv, int newv, int weak)
Definition: abtd_atomic.h:150
int ABT_bool
Definition: abt.h:309
static uint64_t ABTDI_atomic_val_cas_uint64(ABTD_atomic_uint64 *ptr, uint64_t oldv, uint64_t newv, int weak)
Definition: abtd_atomic.h:123
static int ABTDI_atomic_bool_cas_uint32(ABTD_atomic_uint32 *ptr, uint32_t oldv, uint32_t newv, int weak)
Definition: abtd_atomic.h:173
#define ABT_FALSE
Definition: abt.h:224
static int64_t ABTDI_atomic_val_cas_int64(ABTD_atomic_int64 *ptr, int64_t oldv, int64_t newv, int weak)
Definition: abtd_atomic.h:109
static int32_t ABTDI_atomic_val_cas_int32(ABTD_atomic_int32 *ptr, int32_t oldv, int32_t newv, int weak)
Definition: abtd_atomic.h:81
#define ABT_TRUE
Definition: abt.h:223
static int ABTDI_atomic_val_cas_int(ABTD_atomic_int *ptr, int oldv, int newv, int weak)
Definition: abtd_atomic.h:68
static uint32_t ABTDI_atomic_val_cas_uint32(ABTD_atomic_uint32 *ptr, uint32_t oldv, uint32_t newv, int weak)
Definition: abtd_atomic.h:95
static void * ABTDI_atomic_val_cas_ptr(ABTD_atomic_ptr *ptr, void *oldv, void *newv, int weak)
Definition: abtd_atomic.h:137
static int ABTDI_atomic_bool_cas_int64(ABTD_atomic_int64 *ptr, int64_t oldv, int64_t newv, int weak)
Definition: abtd_atomic.h:185
static int ABTDI_atomic_bool_cas_ptr(ABTD_atomic_ptr *ptr, void *oldv, void *newv, int weak)
Definition: abtd_atomic.h:209
static int ABTDI_atomic_bool_cas_uint64(ABTD_atomic_uint64 *ptr, uint64_t oldv, uint64_t newv, int weak)
Definition: abtd_atomic.h:197