| 1 |
/* |
| 2 |
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved. |
| 3 |
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved. |
| 4 |
* Copyright (c) 2003-2011 Hewlett-Packard Development Company, L.P. |
| 5 |
* |
| 6 |
* |
| 7 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 8 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 9 |
* |
| 10 |
* Permission is hereby granted to use or copy this program |
| 11 |
* for any purpose, provided the above notices are retained on all copies. |
| 12 |
* Permission to modify the code and to distribute modified code is granted, |
| 13 |
* provided the above notices are retained, and a notice that the code was |
| 14 |
* modified is included with the above copyright notice. |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
#if !defined(AO_GCC_HAVE_XSIZE_SYNC_CAS) || !defined(AO_PREFER_GENERALIZED) |
| 19 |
|
| 20 |
AO_INLINE XCTYPE |
| 21 |
AO_XSIZE_load(const volatile XCTYPE *addr) |
| 22 |
{ |
| 23 |
return __atomic_load_n(addr, __ATOMIC_RELAXED); |
| 24 |
} |
| 25 |
#define AO_HAVE_XSIZE_load |
| 26 |
|
| 27 |
AO_INLINE XCTYPE |
| 28 |
AO_XSIZE_load_acquire(const volatile XCTYPE *addr) |
| 29 |
{ |
| 30 |
return __atomic_load_n(addr, __ATOMIC_ACQUIRE); |
| 31 |
} |
| 32 |
#define AO_HAVE_XSIZE_load_acquire |
| 33 |
|
| 34 |
/* XSIZE_load_read is defined using load and nop_read. */ |
| 35 |
/* TODO: Map it to ACQUIRE. We should be strengthening the read and */ |
| 36 |
/* write stuff to the more general acquire/release versions. It almost */ |
| 37 |
/* never makes a difference and is much less error-prone. */ |
| 38 |
|
| 39 |
/* XSIZE_load_full is generalized using load and nop_full. */ |
| 40 |
/* TODO: Map it to SEQ_CST and clarify the documentation. */ |
| 41 |
|
| 42 |
/* TODO: Map load_dd_acquire_read to ACQUIRE. Ideally it should be */ |
| 43 |
/* mapped to CONSUME, but the latter is currently broken. */ |
| 44 |
|
| 45 |
/* XSIZE_store_full definition is omitted similar to load_full reason. */ |
| 46 |
|
| 47 |
/* TODO: Map store_write to RELEASE. */ |
| 48 |
|
| 49 |
#ifndef AO_SKIPATOMIC_XSIZE_store |
| 50 |
AO_INLINE void |
| 51 |
AO_XSIZE_store(volatile XCTYPE *addr, XCTYPE value) |
| 52 |
{ |
| 53 |
__atomic_store_n(addr, value, __ATOMIC_RELAXED); |
| 54 |
} |
| 55 |
# define AO_HAVE_XSIZE_store |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifndef AO_SKIPATOMIC_XSIZE_store_release |
| 59 |
AO_INLINE void |
| 60 |
AO_XSIZE_store_release(volatile XCTYPE *addr, XCTYPE value) |
| 61 |
{ |
| 62 |
__atomic_store_n(addr, value, __ATOMIC_RELEASE); |
| 63 |
} |
| 64 |
# define AO_HAVE_XSIZE_store_release |
| 65 |
#endif |
| 66 |
|
| 67 |
#endif /* !AO_GCC_HAVE_XSIZE_SYNC_CAS || !AO_PREFER_GENERALIZED */ |
| 68 |
|
| 69 |
#ifdef AO_GCC_HAVE_XSIZE_SYNC_CAS |
| 70 |
|
| 71 |
AO_INLINE XCTYPE |
| 72 |
AO_XSIZE_fetch_compare_and_swap(volatile XCTYPE *addr, |
| 73 |
XCTYPE old_val, XCTYPE new_val) |
| 74 |
{ |
| 75 |
(void)__atomic_compare_exchange_n(addr, |
| 76 |
&old_val /* p_expected */, |
| 77 |
new_val /* desired */, |
| 78 |
0 /* is_weak: false */, |
| 79 |
__ATOMIC_RELAXED /* success */, |
| 80 |
__ATOMIC_RELAXED /* failure */); |
| 81 |
return old_val; |
| 82 |
} |
| 83 |
# define AO_HAVE_XSIZE_fetch_compare_and_swap |
| 84 |
|
| 85 |
AO_INLINE XCTYPE |
| 86 |
AO_XSIZE_fetch_compare_and_swap_acquire(volatile XCTYPE *addr, |
| 87 |
XCTYPE old_val, XCTYPE new_val) |
| 88 |
{ |
| 89 |
(void)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 90 |
__ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE); |
| 91 |
return old_val; |
| 92 |
} |
| 93 |
# define AO_HAVE_XSIZE_fetch_compare_and_swap_acquire |
| 94 |
|
| 95 |
AO_INLINE XCTYPE |
| 96 |
AO_XSIZE_fetch_compare_and_swap_release(volatile XCTYPE *addr, |
| 97 |
XCTYPE old_val, XCTYPE new_val) |
| 98 |
{ |
| 99 |
(void)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 100 |
__ATOMIC_RELEASE, |
| 101 |
__ATOMIC_RELAXED /* failure */); |
| 102 |
return old_val; |
| 103 |
} |
| 104 |
# define AO_HAVE_XSIZE_fetch_compare_and_swap_release |
| 105 |
|
| 106 |
AO_INLINE XCTYPE |
| 107 |
AO_XSIZE_fetch_compare_and_swap_full(volatile XCTYPE *addr, |
| 108 |
XCTYPE old_val, XCTYPE new_val) |
| 109 |
{ |
| 110 |
(void)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 111 |
__ATOMIC_ACQ_REL, |
| 112 |
__ATOMIC_ACQUIRE /* failure */); |
| 113 |
return old_val; |
| 114 |
} |
| 115 |
# define AO_HAVE_XSIZE_fetch_compare_and_swap_full |
| 116 |
|
| 117 |
# ifndef AO_GENERALIZE_ASM_BOOL_CAS |
| 118 |
AO_INLINE int |
| 119 |
AO_XSIZE_compare_and_swap(volatile XCTYPE *addr, |
| 120 |
XCTYPE old_val, XCTYPE new_val) |
| 121 |
{ |
| 122 |
return (int)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 123 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED); |
| 124 |
} |
| 125 |
# define AO_HAVE_XSIZE_compare_and_swap |
| 126 |
|
| 127 |
AO_INLINE int |
| 128 |
AO_XSIZE_compare_and_swap_acquire(volatile XCTYPE *addr, |
| 129 |
XCTYPE old_val, XCTYPE new_val) |
| 130 |
{ |
| 131 |
return (int)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 132 |
__ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE); |
| 133 |
} |
| 134 |
# define AO_HAVE_XSIZE_compare_and_swap_acquire |
| 135 |
|
| 136 |
AO_INLINE int |
| 137 |
AO_XSIZE_compare_and_swap_release(volatile XCTYPE *addr, |
| 138 |
XCTYPE old_val, XCTYPE new_val) |
| 139 |
{ |
| 140 |
return (int)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 141 |
__ATOMIC_RELEASE, |
| 142 |
__ATOMIC_RELAXED /* failure */); |
| 143 |
} |
| 144 |
# define AO_HAVE_XSIZE_compare_and_swap_release |
| 145 |
|
| 146 |
AO_INLINE int |
| 147 |
AO_XSIZE_compare_and_swap_full(volatile XCTYPE *addr, |
| 148 |
XCTYPE old_val, XCTYPE new_val) |
| 149 |
{ |
| 150 |
return (int)__atomic_compare_exchange_n(addr, &old_val, new_val, 0, |
| 151 |
__ATOMIC_ACQ_REL, |
| 152 |
__ATOMIC_ACQUIRE /* failure */); |
| 153 |
} |
| 154 |
# define AO_HAVE_XSIZE_compare_and_swap_full |
| 155 |
|
| 156 |
# endif /* !AO_GENERALIZE_ASM_BOOL_CAS */ |
| 157 |
|
| 158 |
#endif /* AO_GCC_HAVE_XSIZE_SYNC_CAS */ |