| 1 |
/* |
| 2 |
* Copyright (c) 2017 Ivan Maidanski |
| 3 |
* |
| 4 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 |
* |
| 7 |
* Permission is hereby granted to use or copy this program |
| 8 |
* for any purpose, provided the above notices are retained on all copies. |
| 9 |
* Permission to modify the code and to distribute modified code is granted, |
| 10 |
* provided the above notices are retained, and a notice that the code was |
| 11 |
* modified is included with the above copyright notice. |
| 12 |
*/ |
| 13 |
|
| 14 |
/* This is a private GC header which provides an implementation of */ |
| 15 |
/* libatomic_ops subset primitives sufficient for GC assuming that GCC */ |
| 16 |
/* atomic intrinsics are available (and have correct implementation). */ |
| 17 |
/* This is enabled by defining GC_BUILTIN_ATOMIC macro. Otherwise, */ |
| 18 |
/* libatomic_ops library is used to define the primitives. */ |
| 19 |
|
| 20 |
#ifndef GC_ATOMIC_OPS_H |
| 21 |
#define GC_ATOMIC_OPS_H |
| 22 |
|
| 23 |
#ifdef GC_BUILTIN_ATOMIC |
| 24 |
|
| 25 |
# include "gc.h" /* for GC_word */ |
| 26 |
|
| 27 |
# ifdef __cplusplus |
| 28 |
extern "C" { |
| 29 |
# endif |
| 30 |
|
| 31 |
typedef GC_word AO_t; |
| 32 |
|
| 33 |
# ifdef GC_PRIVATE_H /* have GC_INLINE */ |
| 34 |
# define AO_INLINE GC_INLINE |
| 35 |
# else |
| 36 |
# define AO_INLINE static __inline |
| 37 |
# endif |
| 38 |
|
| 39 |
typedef unsigned char AO_TS_t; |
| 40 |
# define AO_TS_CLEAR 0 |
| 41 |
# define AO_TS_INITIALIZER (AO_TS_t)AO_TS_CLEAR |
| 42 |
# if defined(__GCC_ATOMIC_TEST_AND_SET_TRUEVAL) && !defined(CPPCHECK) |
| 43 |
# define AO_TS_SET __GCC_ATOMIC_TEST_AND_SET_TRUEVAL |
| 44 |
# else |
| 45 |
# define AO_TS_SET (AO_TS_t)1 /* true */ |
| 46 |
# endif |
| 47 |
# define AO_CLEAR(p) __atomic_clear(p, __ATOMIC_RELEASE) |
| 48 |
# define AO_test_and_set_acquire(p) \ |
| 49 |
(__atomic_test_and_set(p, __ATOMIC_ACQUIRE) ? AO_TS_SET : AO_TS_CLEAR) |
| 50 |
# define AO_HAVE_test_and_set_acquire |
| 51 |
|
| 52 |
# define AO_compiler_barrier() __atomic_signal_fence(__ATOMIC_SEQ_CST) |
| 53 |
# define AO_nop_full() __atomic_thread_fence(__ATOMIC_SEQ_CST) |
| 54 |
# define AO_HAVE_nop_full |
| 55 |
|
| 56 |
# define AO_fetch_and_add(p, v) __atomic_fetch_add(p, v, __ATOMIC_RELAXED) |
| 57 |
# define AO_HAVE_fetch_and_add |
| 58 |
# define AO_fetch_and_add1(p) AO_fetch_and_add(p, 1) |
| 59 |
# define AO_HAVE_fetch_and_add1 |
| 60 |
|
| 61 |
# define AO_or(p, v) (void)__atomic_or_fetch(p, v, __ATOMIC_RELAXED) |
| 62 |
# define AO_HAVE_or |
| 63 |
|
| 64 |
# define AO_load(p) __atomic_load_n(p, __ATOMIC_RELAXED) |
| 65 |
# define AO_HAVE_load |
| 66 |
# define AO_load_acquire(p) __atomic_load_n(p, __ATOMIC_ACQUIRE) |
| 67 |
# define AO_HAVE_load_acquire |
| 68 |
# define AO_load_acquire_read(p) AO_load_acquire(p) |
| 69 |
# define AO_HAVE_load_acquire_read |
| 70 |
|
| 71 |
# define AO_store(p, v) __atomic_store_n(p, v, __ATOMIC_RELAXED) |
| 72 |
# define AO_HAVE_store |
| 73 |
# define AO_store_release(p, v) __atomic_store_n(p, v, __ATOMIC_RELEASE) |
| 74 |
# define AO_HAVE_store_release |
| 75 |
# define AO_store_release_write(p, v) AO_store_release(p, v) |
| 76 |
# define AO_HAVE_store_release_write |
| 77 |
|
| 78 |
# define AO_char_load(p) __atomic_load_n(p, __ATOMIC_RELAXED) |
| 79 |
# define AO_HAVE_char_load |
| 80 |
# define AO_char_store(p, v) __atomic_store_n(p, v, __ATOMIC_RELAXED) |
| 81 |
# define AO_HAVE_char_store |
| 82 |
|
| 83 |
# ifdef AO_REQUIRE_CAS |
| 84 |
AO_INLINE int |
| 85 |
AO_compare_and_swap(volatile AO_t *p, AO_t ov, AO_t nv) |
| 86 |
{ |
| 87 |
return (int)__atomic_compare_exchange_n(p, &ov, nv, 0, |
| 88 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED); |
| 89 |
} |
| 90 |
|
| 91 |
AO_INLINE int |
| 92 |
AO_compare_and_swap_release(volatile AO_t *p, AO_t ov, AO_t nv) |
| 93 |
{ |
| 94 |
return (int)__atomic_compare_exchange_n(p, &ov, nv, 0, |
| 95 |
__ATOMIC_RELEASE, __ATOMIC_RELAXED); |
| 96 |
} |
| 97 |
# define AO_HAVE_compare_and_swap_release |
| 98 |
# endif |
| 99 |
|
| 100 |
# ifdef __cplusplus |
| 101 |
} /* extern "C" */ |
| 102 |
# endif |
| 103 |
|
| 104 |
# ifndef NO_LOCKFREE_AO_OR |
| 105 |
/* __atomic_or_fetch is assumed to be lock-free. */ |
| 106 |
# define HAVE_LOCKFREE_AO_OR 1 |
| 107 |
# endif |
| 108 |
|
| 109 |
#else |
| 110 |
/* Fallback to libatomic_ops. */ |
| 111 |
# include "atomic_ops.h" |
| 112 |
|
| 113 |
/* AO_compiler_barrier, AO_load and AO_store should be defined for */ |
| 114 |
/* all targets; the rest of the primitives are guaranteed to exist */ |
| 115 |
/* only if AO_REQUIRE_CAS is defined (or if the corresponding */ |
| 116 |
/* AO_HAVE_x macro is defined). x86/x64 targets have AO_nop_full, */ |
| 117 |
/* AO_load_acquire, AO_store_release, at least. */ |
| 118 |
# if (!defined(AO_HAVE_load) || !defined(AO_HAVE_store)) && !defined(CPPCHECK) |
| 119 |
# error AO_load or AO_store is missing; probably old version of atomic_ops |
| 120 |
# endif |
| 121 |
|
| 122 |
#endif /* !GC_BUILTIN_ATOMIC */ |
| 123 |
|
| 124 |
#endif /* GC_ATOMIC_OPS_H */ |