|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana |
| 4 | + * University Research and Technology |
| 5 | + * Corporation. All rights reserved. |
| 6 | + * Copyright (c) 2004-2013 The University of Tennessee and The University |
| 7 | + * of Tennessee Research Foundation. All rights |
| 8 | + * reserved. |
| 9 | + * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, |
| 10 | + * University of Stuttgart. All rights reserved. |
| 11 | + * Copyright (c) 2004-2005 The Regents of the University of California. |
| 12 | + * All rights reserved. |
| 13 | + * Copyright (c) 2011 Sandia National Laboratories. All rights reserved. |
| 14 | + * Copyright (c) 2014-2016 Los Alamos National Security, LLC. All rights |
| 15 | + * reserved. |
| 16 | + * $COPYRIGHT$ |
| 17 | + * |
| 18 | + * Additional copyrights may follow |
| 19 | + * |
| 20 | + * $HEADER$ |
| 21 | + */ |
| 22 | + |
| 23 | +#ifndef OPAL_SYS_ARCH_ATOMIC_H |
| 24 | +#define OPAL_SYS_ARCH_ATOMIC_H 1 |
| 25 | + |
| 26 | +#include <stdbool.h> |
| 27 | + |
| 28 | +/********************************************************************** |
| 29 | + * |
| 30 | + * Memory Barriers |
| 31 | + * |
| 32 | + *********************************************************************/ |
| 33 | +#define OPAL_HAVE_ATOMIC_MEM_BARRIER 1 |
| 34 | + |
| 35 | +#define OPAL_HAVE_ATOMIC_MATH_32 1 |
| 36 | +#define OPAL_HAVE_ATOMIC_CMPSET_32 1 |
| 37 | +#define OPAL_HAVE_ATOMIC_ADD_32 1 |
| 38 | +#define OPAL_HAVE_ATOMIC_SUB_32 1 |
| 39 | +#define OPAL_HAVE_ATOMIC_SWAP_32 1 |
| 40 | +#define OPAL_HAVE_ATOMIC_MATH_64 1 |
| 41 | +#define OPAL_HAVE_ATOMIC_CMPSET_64 1 |
| 42 | +#define OPAL_HAVE_ATOMIC_ADD_64 1 |
| 43 | +#define OPAL_HAVE_ATOMIC_SUB_64 1 |
| 44 | +#define OPAL_HAVE_ATOMIC_SWAP_64 1 |
| 45 | + |
| 46 | + |
| 47 | +static inline void opal_atomic_mb(void) |
| 48 | +{ |
| 49 | + __atomic_thread_fence (__ATOMIC_SEQ_CST); |
| 50 | +} |
| 51 | + |
| 52 | +static inline void opal_atomic_rmb(void) |
| 53 | +{ |
| 54 | + __atomic_thread_fence (__ATOMIC_ACQUIRE); |
| 55 | +} |
| 56 | + |
| 57 | +static inline void opal_atomic_wmb(void) |
| 58 | +{ |
| 59 | + __atomic_thread_fence (__ATOMIC_RELEASE); |
| 60 | +} |
| 61 | + |
| 62 | +#define MB() opal_atomic_mb() |
| 63 | + |
| 64 | +/********************************************************************** |
| 65 | + * |
| 66 | + * Atomic math operations |
| 67 | + * |
| 68 | + *********************************************************************/ |
| 69 | + |
| 70 | +static inline int opal_atomic_cmpset_acq_32( volatile int32_t *addr, |
| 71 | + int32_t oldval, int32_t newval) |
| 72 | +{ |
| 73 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 74 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +static inline int opal_atomic_cmpset_rel_32( volatile int32_t *addr, |
| 79 | + int32_t oldval, int32_t newval) |
| 80 | +{ |
| 81 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 82 | + __ATOMIC_RELEASE, __ATOMIC_RELAXED); |
| 83 | +} |
| 84 | + |
| 85 | +static inline int opal_atomic_cmpset_32( volatile int32_t *addr, |
| 86 | + int32_t oldval, int32_t newval) |
| 87 | +{ |
| 88 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 89 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); |
| 90 | +} |
| 91 | + |
| 92 | +static inline int32_t opal_atomic_swap_32 (volatile int32_t *addr, int32_t newval) |
| 93 | +{ |
| 94 | + int32_t oldval; |
| 95 | + __atomic_exchange (addr, &newval, &oldval, __ATOMIC_RELAXED); |
| 96 | + return oldval; |
| 97 | +} |
| 98 | + |
| 99 | +static inline int32_t opal_atomic_add_32(volatile int32_t *addr, int32_t delta) |
| 100 | +{ |
| 101 | + return __atomic_add_fetch (addr, delta, __ATOMIC_RELAXED); |
| 102 | +} |
| 103 | + |
| 104 | +static inline int32_t opal_atomic_sub_32(volatile int32_t *addr, int32_t delta) |
| 105 | +{ |
| 106 | + return __atomic_sub_fetch (addr, delta, __ATOMIC_RELAXED); |
| 107 | +} |
| 108 | + |
| 109 | +static inline int opal_atomic_cmpset_acq_64( volatile int64_t *addr, |
| 110 | + int64_t oldval, int64_t newval) |
| 111 | +{ |
| 112 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 113 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); |
| 114 | +} |
| 115 | + |
| 116 | +static inline int opal_atomic_cmpset_rel_64( volatile int64_t *addr, |
| 117 | + int64_t oldval, int64_t newval) |
| 118 | +{ |
| 119 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 120 | + __ATOMIC_RELEASE, __ATOMIC_RELAXED); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +static inline int opal_atomic_cmpset_64( volatile int64_t *addr, |
| 125 | + int64_t oldval, int64_t newval) |
| 126 | +{ |
| 127 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 128 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); |
| 129 | +} |
| 130 | + |
| 131 | +static inline int64_t opal_atomic_swap_64 (volatile int64_t *addr, int64_t newval) |
| 132 | +{ |
| 133 | + int64_t oldval; |
| 134 | + __atomic_exchange (addr, &newval, &oldval, __ATOMIC_RELAXED); |
| 135 | + return oldval; |
| 136 | +} |
| 137 | + |
| 138 | +static inline int64_t opal_atomic_add_64(volatile int64_t *addr, int64_t delta) |
| 139 | +{ |
| 140 | + return __atomic_add_fetch (addr, delta, __ATOMIC_RELAXED); |
| 141 | +} |
| 142 | + |
| 143 | +static inline int64_t opal_atomic_sub_64(volatile int64_t *addr, int64_t delta) |
| 144 | +{ |
| 145 | + return __atomic_sub_fetch (addr, delta, __ATOMIC_RELAXED); |
| 146 | +} |
| 147 | + |
| 148 | +#if OPAL_HAVE_GCC_BUILTIN_CSWAP_INT128 |
| 149 | + |
| 150 | +#define OPAL_HAVE_ATOMIC_CMPSET_128 1 |
| 151 | + |
| 152 | +static inline int opal_atomic_cmpset_128 (volatile opal_int128_t *addr, |
| 153 | + opal_int128_t oldval, opal_int128_t newval) |
| 154 | +{ |
| 155 | + return __atomic_compare_exchange_n (addr, &oldval, newval, false, |
| 156 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); |
| 157 | +} |
| 158 | + |
| 159 | +#endif |
| 160 | + |
| 161 | +#if defined(__HLE__) |
| 162 | + |
| 163 | +#include <immintrin.h> |
| 164 | + |
| 165 | +#define OPAL_HAVE_ATOMIC_SPINLOCKS 1 |
| 166 | + |
| 167 | +static inline void opal_atomic_init (opal_atomic_lock_t* lock, int32_t value) |
| 168 | +{ |
| 169 | + lock->u.lock = value; |
| 170 | +} |
| 171 | + |
| 172 | +static inline int opal_atomic_trylock(opal_atomic_lock_t *lock) |
| 173 | +{ |
| 174 | + int ret = __atomic_exchange_n (&lock->u.lock, OPAL_ATOMIC_LOCKED, |
| 175 | + __ATOMIC_ACQUIRE | __ATOMIC_HLE_ACQUIRE); |
| 176 | + if (OPAL_ATOMIC_LOCKED == ret) { |
| 177 | + /* abort the transaction */ |
| 178 | + _mm_pause (); |
| 179 | + return 1; |
| 180 | + } |
| 181 | + |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +static inline void opal_atomic_lock (opal_atomic_lock_t *lock) |
| 186 | +{ |
| 187 | + while (OPAL_ATOMIC_LOCKED == __atomic_exchange_n (&lock->u.lock, OPAL_ATOMIC_LOCKED, |
| 188 | + __ATOMIC_ACQUIRE | __ATOMIC_HLE_ACQUIRE)) { |
| 189 | + /* abort the transaction */ |
| 190 | + _mm_pause (); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +static inline void opal_atomic_unlock (opal_atomic_lock_t *lock) |
| 195 | +{ |
| 196 | + __atomic_store_n (&lock->u.lock, OPAL_ATOMIC_UNLOCKED, |
| 197 | + __ATOMIC_RELEASE | __ATOMIC_HLE_RELEASE); |
| 198 | +} |
| 199 | + |
| 200 | +#endif |
| 201 | + |
| 202 | +#endif /* ! OPAL_SYS_ARCH_ATOMIC_H */ |
0 commit comments