Thomas Hellstrom | fc1ef97 | 2009-03-16 11:40:18 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Many similar implementations exist. See for example libwsbm |
| 3 | * or the linux kernel include/atomic.h |
| 4 | * |
| 5 | * No copyright claimed on this file. |
| 6 | * |
| 7 | */ |
| 8 | |
José Fonseca | bfb4db8 | 2014-12-11 22:14:14 +0000 | [diff] [blame] | 9 | #include "no_extern_c.h" |
| 10 | |
José Fonseca | 2aaca1d | 2010-02-02 15:18:01 +0000 | [diff] [blame] | 11 | #ifndef U_ATOMIC_H |
| 12 | #define U_ATOMIC_H |
Thomas Hellstrom | fc1ef97 | 2009-03-16 11:40:18 +0100 | [diff] [blame] | 13 | |
Matt Turner | 41b5858 | 2014-11-21 16:44:43 -0800 | [diff] [blame] | 14 | #include <stdbool.h> |
| 15 | |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 16 | /* Favor OS-provided implementations. |
Keith Whitwell | 687f331 | 2009-04-17 11:01:22 +0100 | [diff] [blame] | 17 | * |
| 18 | * Where no OS-provided implementation is available, fall back to |
| 19 | * locally coded assembly, compiler intrinsic or ultimately a |
| 20 | * mutex-based implementation. |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 21 | */ |
Matt Turner | 504062b | 2014-11-21 15:50:58 -0800 | [diff] [blame] | 22 | #if defined(__sun) |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 23 | #define PIPE_ATOMIC_OS_SOLARIS |
Matt Turner | 504062b | 2014-11-21 15:50:58 -0800 | [diff] [blame] | 24 | #elif defined(_MSC_VER) |
José Fonseca | 57839d1 | 2010-02-01 16:12:44 +0000 | [diff] [blame] | 25 | #define PIPE_ATOMIC_MSVC_INTRINSIC |
Timothy Arceri | e801fbb | 2014-12-12 20:47:43 +1100 | [diff] [blame] | 26 | #elif defined(__GNUC__) |
Matt Turner | 024db25 | 2014-11-21 14:29:41 -0800 | [diff] [blame] | 27 | #define PIPE_ATOMIC_GCC_INTRINSIC |
Keith Whitwell | 687f331 | 2009-04-17 11:01:22 +0100 | [diff] [blame] | 28 | #else |
José Fonseca | 7827bc7 | 2010-02-01 14:23:36 +0000 | [diff] [blame] | 29 | #error "Unsupported platform" |
Keith Whitwell | 687f331 | 2009-04-17 11:01:22 +0100 | [diff] [blame] | 30 | #endif |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 31 | |
| 32 | |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 33 | /* Implementation using GCC-provided synchronization intrinsics |
| 34 | */ |
Michel Dänzer | 3b76072 | 2009-04-17 17:02:34 +0200 | [diff] [blame] | 35 | #if defined(PIPE_ATOMIC_GCC_INTRINSIC) |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 36 | |
| 37 | #define PIPE_ATOMIC "GCC Sync Intrinsics" |
| 38 | |
José Fonseca | 38f6f23 | 2010-02-02 14:43:48 +0000 | [diff] [blame] | 39 | #define p_atomic_set(_v, _i) (*(_v) = (_i)) |
| 40 | #define p_atomic_read(_v) (*(_v)) |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 41 | #define p_atomic_dec_zero(v) (__sync_sub_and_fetch((v), 1) == 0) |
| 42 | #define p_atomic_inc(v) (void) __sync_add_and_fetch((v), 1) |
| 43 | #define p_atomic_dec(v) (void) __sync_sub_and_fetch((v), 1) |
Carl Worth | b16de0b | 2015-02-05 15:36:59 -0800 | [diff] [blame] | 44 | #define p_atomic_add(v, i) (void) __sync_add_and_fetch((v), (i)) |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 45 | #define p_atomic_inc_return(v) __sync_add_and_fetch((v), 1) |
| 46 | #define p_atomic_dec_return(v) __sync_sub_and_fetch((v), 1) |
| 47 | #define p_atomic_cmpxchg(v, old, _new) \ |
| 48 | __sync_val_compare_and_swap((v), (old), (_new)) |
José Fonseca | e0da333 | 2010-02-03 22:15:53 +0000 | [diff] [blame] | 49 | |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | |
| 53 | |
| 54 | /* Unlocked version for single threaded environments, such as some |
| 55 | * windows kernel modules. |
| 56 | */ |
Keith Whitwell | 687f331 | 2009-04-17 11:01:22 +0100 | [diff] [blame] | 57 | #if defined(PIPE_ATOMIC_OS_UNLOCKED) |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 58 | |
| 59 | #define PIPE_ATOMIC "Unlocked" |
Michal Krol | 1e23dac | 2009-03-17 10:54:08 +0100 | [diff] [blame] | 60 | |
José Fonseca | 38f6f23 | 2010-02-02 14:43:48 +0000 | [diff] [blame] | 61 | #define p_atomic_set(_v, _i) (*(_v) = (_i)) |
| 62 | #define p_atomic_read(_v) (*(_v)) |
José Fonseca | a5299e9 | 2014-11-25 14:25:28 +0000 | [diff] [blame] | 63 | #define p_atomic_dec_zero(_v) (p_atomic_dec_return(_v) == 0) |
| 64 | #define p_atomic_inc(_v) ((void) p_atomic_inc_return(_v)) |
| 65 | #define p_atomic_dec(_v) ((void) p_atomic_dec_return(_v)) |
Carl Worth | b16de0b | 2015-02-05 15:36:59 -0800 | [diff] [blame] | 66 | #define p_atomic_add(_v, _i) (*(_v) = *(_v) + (_i)) |
José Fonseca | a5299e9 | 2014-11-25 14:25:28 +0000 | [diff] [blame] | 67 | #define p_atomic_inc_return(_v) (++(*(_v))) |
| 68 | #define p_atomic_dec_return(_v) (--(*(_v))) |
| 69 | #define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v)) |
Michal Krol | 1e23dac | 2009-03-17 10:54:08 +0100 | [diff] [blame] | 70 | |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | |
José Fonseca | 57839d1 | 2010-02-01 16:12:44 +0000 | [diff] [blame] | 74 | #if defined(PIPE_ATOMIC_MSVC_INTRINSIC) |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 75 | |
José Fonseca | 57839d1 | 2010-02-01 16:12:44 +0000 | [diff] [blame] | 76 | #define PIPE_ATOMIC "MSVC Intrinsics" |
Michal Krol | a7d42e1 | 2009-03-16 13:07:22 +0100 | [diff] [blame] | 77 | |
José Fonseca | d7f2dfb | 2015-01-20 23:34:26 +0000 | [diff] [blame] | 78 | /* We use the Windows header's Interlocked*64 functions instead of the |
| 79 | * _Interlocked*64 intrinsics wherever we can, as support for the latter varies |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 80 | * with target CPU, whereas Windows headers take care of all portability |
| 81 | * issues: using intrinsics where available, falling back to library |
| 82 | * implementations where not. |
| 83 | */ |
| 84 | #ifndef WIN32_LEAN_AND_MEAN |
| 85 | #define WIN32_LEAN_AND_MEAN 1 |
José Fonseca | e0da333 | 2010-02-03 22:15:53 +0000 | [diff] [blame] | 86 | #endif |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 87 | #include <windows.h> |
| 88 | #include <intrin.h> |
| 89 | #include <assert.h> |
| 90 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 91 | /* MSVC supports decltype keyword, but it's only supported on C++ and doesn't |
| 92 | * quite work here; and if a C++-only solution is worthwhile, then it would be |
| 93 | * better to use templates / function overloading, instead of decltype magic. |
| 94 | * Therefore, we rely on implicit casting to LONGLONG for the functions that return |
| 95 | */ |
José Fonseca | e0da333 | 2010-02-03 22:15:53 +0000 | [diff] [blame] | 96 | |
José Fonseca | 38f6f23 | 2010-02-02 14:43:48 +0000 | [diff] [blame] | 97 | #define p_atomic_set(_v, _i) (*(_v) = (_i)) |
| 98 | #define p_atomic_read(_v) (*(_v)) |
Michal Krol | a7d42e1 | 2009-03-16 13:07:22 +0100 | [diff] [blame] | 99 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 100 | #define p_atomic_dec_zero(_v) \ |
| 101 | (p_atomic_dec_return(_v) == 0) |
Michal Krol | a7d42e1 | 2009-03-16 13:07:22 +0100 | [diff] [blame] | 102 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 103 | #define p_atomic_inc(_v) \ |
| 104 | ((void) p_atomic_inc_return(_v)) |
Michal Krol | a7d42e1 | 2009-03-16 13:07:22 +0100 | [diff] [blame] | 105 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 106 | #define p_atomic_inc_return(_v) (\ |
José Fonseca | d7f2dfb | 2015-01-20 23:34:26 +0000 | [diff] [blame] | 107 | sizeof *(_v) == sizeof(short) ? _InterlockedIncrement16((short *) (_v)) : \ |
| 108 | sizeof *(_v) == sizeof(long) ? _InterlockedIncrement ((long *) (_v)) : \ |
| 109 | sizeof *(_v) == sizeof(__int64) ? InterlockedIncrement64 ((__int64 *)(_v)) : \ |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 110 | (assert(!"should not get here"), 0)) |
Christoph Bumiller | cb49132 | 2014-11-17 20:05:53 +0100 | [diff] [blame] | 111 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 112 | #define p_atomic_dec(_v) \ |
| 113 | ((void) p_atomic_dec_return(_v)) |
Michal Krol | a7d42e1 | 2009-03-16 13:07:22 +0100 | [diff] [blame] | 114 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 115 | #define p_atomic_dec_return(_v) (\ |
José Fonseca | d7f2dfb | 2015-01-20 23:34:26 +0000 | [diff] [blame] | 116 | sizeof *(_v) == sizeof(short) ? _InterlockedDecrement16((short *) (_v)) : \ |
| 117 | sizeof *(_v) == sizeof(long) ? _InterlockedDecrement ((long *) (_v)) : \ |
| 118 | sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \ |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 119 | (assert(!"should not get here"), 0)) |
Christoph Bumiller | cb49132 | 2014-11-17 20:05:53 +0100 | [diff] [blame] | 120 | |
Carl Worth | b16de0b | 2015-02-05 15:36:59 -0800 | [diff] [blame] | 121 | #define p_atomic_add(_v, _i) (\ |
| 122 | sizeof *(_v) == sizeof(char) ? _InterlockedExchangeAdd8 ((char *) (_v), (_i)) : \ |
| 123 | sizeof *(_v) == sizeof(short) ? _InterlockedExchangeAdd16((short *) (_v), (_i)) : \ |
| 124 | sizeof *(_v) == sizeof(long) ? _InterlockedExchangeAdd ((long *) (_v), (_i)) : \ |
| 125 | sizeof *(_v) == sizeof(__int64) ? InterlockedExchangeAdd64((__int64 *)(_v), (_i)) : \ |
| 126 | (assert(!"should not get here"), 0)) |
| 127 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 128 | #define p_atomic_cmpxchg(_v, _old, _new) (\ |
José Fonseca | d7f2dfb | 2015-01-20 23:34:26 +0000 | [diff] [blame] | 129 | sizeof *(_v) == sizeof(char) ? _InterlockedCompareExchange8 ((char *) (_v), (char) (_new), (char) (_old)) : \ |
| 130 | sizeof *(_v) == sizeof(short) ? _InterlockedCompareExchange16((short *) (_v), (short) (_new), (short) (_old)) : \ |
| 131 | sizeof *(_v) == sizeof(long) ? _InterlockedCompareExchange ((long *) (_v), (long) (_new), (long) (_old)) : \ |
| 132 | sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \ |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 133 | (assert(!"should not get here"), 0)) |
José Fonseca | e0da333 | 2010-02-03 22:15:53 +0000 | [diff] [blame] | 134 | |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 137 | #if defined(PIPE_ATOMIC_OS_SOLARIS) |
| 138 | |
| 139 | #define PIPE_ATOMIC "Solaris OS atomic functions" |
| 140 | |
| 141 | #include <atomic.h> |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 142 | #include <assert.h> |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 143 | |
| 144 | #define p_atomic_set(_v, _i) (*(_v) = (_i)) |
| 145 | #define p_atomic_read(_v) (*(_v)) |
| 146 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 147 | #define p_atomic_dec_zero(v) (\ |
| 148 | sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) == 0 : \ |
| 149 | sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \ |
| 150 | sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \ |
| 151 | sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \ |
| 152 | (assert(!"should not get here"), 0)) |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 153 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 154 | #define p_atomic_inc(v) (void) (\ |
| 155 | sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8 ((uint8_t *)(v)) : \ |
| 156 | sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \ |
| 157 | sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \ |
| 158 | sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \ |
| 159 | (assert(!"should not get here"), 0)) |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 160 | |
Alan Coopersmith | 4671dca | 2015-02-15 16:19:06 -0800 | [diff] [blame] | 161 | #define p_atomic_inc_return(v) ((__typeof(*v)) \ |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 162 | sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8_nv ((uint8_t *)(v)) : \ |
| 163 | sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \ |
| 164 | sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \ |
| 165 | sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \ |
| 166 | (assert(!"should not get here"), 0)) |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 167 | |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 168 | #define p_atomic_dec(v) ((void) \ |
| 169 | sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8 ((uint8_t *)(v)) : \ |
| 170 | sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \ |
| 171 | sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \ |
| 172 | sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \ |
| 173 | (assert(!"should not get here"), 0)) |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 174 | |
Alan Coopersmith | 4671dca | 2015-02-15 16:19:06 -0800 | [diff] [blame] | 175 | #define p_atomic_dec_return(v) ((__typeof(*v)) \ |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 176 | sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) : \ |
| 177 | sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \ |
| 178 | sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \ |
| 179 | sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \ |
| 180 | (assert(!"should not get here"), 0)) |
| 181 | |
Carl Worth | b16de0b | 2015-02-05 15:36:59 -0800 | [diff] [blame] | 182 | #define p_atomic_add(v, i) ((void) \ |
| 183 | sizeof(*v) == sizeof(uint8_t) ? atomic_add_8 ((uint8_t *)(v), (i)) : \ |
| 184 | sizeof(*v) == sizeof(uint16_t) ? atomic_add_16((uint16_t *)(v), (i)) : \ |
| 185 | sizeof(*v) == sizeof(uint32_t) ? atomic_add_32((uint32_t *)(v), (i)) : \ |
| 186 | sizeof(*v) == sizeof(uint64_t) ? atomic_add_64((uint64_t *)(v), (i)) : \ |
| 187 | (assert(!"should not get here"), 0)) |
| 188 | |
Alan Coopersmith | 4671dca | 2015-02-15 16:19:06 -0800 | [diff] [blame] | 189 | #define p_atomic_cmpxchg(v, old, _new) ((__typeof(*v)) \ |
Matt Turner | 6df72e9 | 2014-11-21 16:33:40 -0800 | [diff] [blame] | 190 | sizeof(*v) == sizeof(uint8_t) ? atomic_cas_8 ((uint8_t *)(v), (uint8_t )(old), (uint8_t )(_new)) : \ |
| 191 | sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \ |
| 192 | sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new)) : \ |
| 193 | sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \ |
| 194 | (assert(!"should not get here"), 0)) |
Alan Coopersmith | 78572eb | 2010-02-05 19:36:42 -0800 | [diff] [blame] | 195 | |
| 196 | #endif |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 197 | |
Keith Whitwell | eddfad3 | 2009-03-18 11:29:01 +0000 | [diff] [blame] | 198 | #ifndef PIPE_ATOMIC |
| 199 | #error "No pipe_atomic implementation selected" |
| 200 | #endif |
| 201 | |
| 202 | |
Thomas Hellstrom | fc1ef97 | 2009-03-16 11:40:18 +0100 | [diff] [blame] | 203 | |
José Fonseca | 2aaca1d | 2010-02-02 15:18:01 +0000 | [diff] [blame] | 204 | #endif /* U_ATOMIC_H */ |