|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | This source file is subject to version 3.01 of the PHP license, | |
| 4 | + | that is bundled with this package in the file LICENSE, and is | |
| 5 | + | available through the world-wide-web at the following url: | |
| 6 | + | https://www.php.net/license/3_01.txt | |
| 7 | + | If you did not receive a copy of the PHP license and are unable to | |
| 8 | + | obtain it through the world-wide-web, please send a note to | |
| 9 | + | [email protected] so we can mail you a copy immediately. | |
| 10 | + +----------------------------------------------------------------------+ |
| 11 | + | Authors: Levi Morrison <[email protected]> | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + */ |
| 14 | + |
| 15 | +#ifndef ZEND_ATOMIC_H |
| 16 | +#define ZEND_ATOMIC_H |
| 17 | + |
| 18 | +#include "zend_portability.h" |
| 19 | + |
| 20 | +#include <stdbool.h> |
| 21 | + |
| 22 | +#define ZEND_GCC_PREREQ(x, y) \ |
| 23 | + ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || (__GNUC__ > (x))) |
| 24 | + |
| 25 | +/* Builtins are used to avoid library linkage */ |
| 26 | +#if __has_feature(c_atomic) |
| 27 | +#define HAVE_C11_ATOMICS 1 |
| 28 | +#elif ZEND_GCC_PREREQ(4, 7) |
| 29 | +#define HAVE_GNUC_ATOMICS 1 |
| 30 | +#elif defined(__GNUC__) |
| 31 | +#define HAVE_SYNC_ATOMICS 1 |
| 32 | +#elif !defined(ZEND_WIN32) |
| 33 | +#define HAVE_NO_ATOMICS 1 |
| 34 | +#endif |
| 35 | + |
| 36 | +#undef ZEND_GCC_PREREQ |
| 37 | + |
| 38 | +/* Treat zend_atomic_* types as opaque. They have definitions only for size |
| 39 | + * and alignment purposes. |
| 40 | + */ |
| 41 | + |
| 42 | +#if ZEND_WIN32 || HAVE_SYNC_ATOMICS |
| 43 | +typedef struct zend_atomic_bool_s { |
| 44 | + volatile char value; |
| 45 | +} zend_atomic_bool; |
| 46 | +#elif HAVE_C11_ATOMICS |
| 47 | +typedef struct zend_atomic_bool_s { |
| 48 | + _Atomic(bool) value; |
| 49 | +} zend_atomic_bool; |
| 50 | +#else |
| 51 | +typedef struct zend_atomic_bool_s { |
| 52 | + volatile bool value; |
| 53 | +} zend_atomic_bool; |
| 54 | +#endif |
| 55 | + |
| 56 | +BEGIN_EXTERN_C() |
| 57 | + |
| 58 | +#if ZEND_WIN32 |
| 59 | + |
| 60 | +#define ZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired)) |
| 61 | + |
| 62 | +static zend_always_inline bool zend_atomic_bool_exchange_ex(zend_atomic_bool *obj, bool desired) { |
| 63 | + return InterlockedExchange8(&obj->value, desired); |
| 64 | +} |
| 65 | + |
| 66 | +/* On this platform it is non-const due to Iterlocked API*/ |
| 67 | +static zend_always_inline bool zend_atomic_bool_load_ex(zend_atomic_bool *obj) { |
| 68 | + /* Or'ing with false won't change the value. */ |
| 69 | + return InterlockedOr8(&obj->value, false); |
| 70 | +} |
| 71 | + |
| 72 | +static zend_always_inline void zend_atomic_bool_store_ex(zend_atomic_bool *obj, bool desired) { |
| 73 | + (void)InterlockedExchange8(&obj->value, desired); |
| 74 | +} |
| 75 | + |
| 76 | +#elif HAVE_C11_ATOMICS |
| 77 | + |
| 78 | +#define ZEND_ATOMIC_BOOL_INIT(obj, desired) __c11_atomic_init(&(obj)->value, (desired)) |
| 79 | + |
| 80 | +static zend_always_inline bool zend_atomic_bool_exchange_ex(zend_atomic_bool *obj, bool desired) { |
| 81 | + return __c11_atomic_exchange(&obj->value, desired, __ATOMIC_SEQ_CST); |
| 82 | +} |
| 83 | + |
| 84 | +static zend_always_inline bool zend_atomic_bool_load_ex(const zend_atomic_bool *obj) { |
| 85 | + return __c11_atomic_load(&obj->value, __ATOMIC_SEQ_CST); |
| 86 | +} |
| 87 | + |
| 88 | +static zend_always_inline void zend_atomic_bool_store_ex(zend_atomic_bool *obj, bool desired) { |
| 89 | + __c11_atomic_store(&obj->value, desired, __ATOMIC_SEQ_CST); |
| 90 | +} |
| 91 | + |
| 92 | +#elif HAVE_GNUC_ATOMICS |
| 93 | + |
| 94 | +#define ZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired)) |
| 95 | + |
| 96 | +static zend_always_inline bool zend_atomic_bool_exchange_ex(zend_atomic_bool *obj, bool desired) { |
| 97 | + bool prev = false; |
| 98 | + __atomic_exchange(&obj->value, &desired, &prev, __ATOMIC_SEQ_CST); |
| 99 | + return prev; |
| 100 | +} |
| 101 | + |
| 102 | +static zend_always_inline bool zend_atomic_bool_load_ex(const zend_atomic_bool *obj) { |
| 103 | + bool prev = false; |
| 104 | + __atomic_load(&obj->value, &prev, __ATOMIC_SEQ_CST); |
| 105 | + return prev; |
| 106 | +} |
| 107 | + |
| 108 | +static zend_always_inline void zend_atomic_bool_store_ex(zend_atomic_bool *obj, bool desired) { |
| 109 | + __atomic_store(&obj->value, &desired, __ATOMIC_SEQ_CST); |
| 110 | +} |
| 111 | + |
| 112 | +#elif HAVE_SYNC_ATOMICS |
| 113 | + |
| 114 | +#define ZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired)) |
| 115 | + |
| 116 | +static zend_always_inline bool zend_atomic_bool_exchange_ex(zend_atomic_bool *obj, bool desired) { |
| 117 | + bool prev = __sync_lock_test_and_set(&obj->value, desired); |
| 118 | + |
| 119 | + /* __sync_lock_test_and_set only does an acquire barrier, so sync |
| 120 | + * immediately after. |
| 121 | + */ |
| 122 | + __sync_synchronize(); |
| 123 | + return prev; |
| 124 | +} |
| 125 | + |
| 126 | +static zend_always_inline bool zend_atomic_bool_load_ex(zend_atomic_bool *obj) { |
| 127 | + /* Or'ing false won't change the value */ |
| 128 | + return __sync_fetch_and_or(&obj->value, false); |
| 129 | +} |
| 130 | + |
| 131 | +static zend_always_inline void zend_atomic_bool_store_ex(zend_atomic_bool *obj, bool desired) { |
| 132 | + __sync_synchronize(); |
| 133 | + obj->value = desired; |
| 134 | + __sync_synchronize(); |
| 135 | +} |
| 136 | + |
| 137 | +#elif HAVE_NO_ATOMICS |
| 138 | + |
| 139 | +#warning No atomics support detected. Please open an issue with platform details. |
| 140 | + |
| 141 | +#define ZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired)) |
| 142 | + |
| 143 | +static zend_always_inline void zend_atomic_bool_store_ex(zend_atomic_bool *obj, bool desired) { |
| 144 | + obj->value = desired; |
| 145 | +} |
| 146 | + |
| 147 | +static zend_always_inline bool zend_atomic_bool_load_ex(const zend_atomic_bool *obj) { |
| 148 | + return obj->value; |
| 149 | +} |
| 150 | + |
| 151 | +static zend_always_inline bool zend_atomic_bool_exchange_ex(zend_atomic_bool *obj, bool desired) { |
| 152 | + bool prev = obj->value; |
| 153 | + obj->value = true; |
| 154 | + return prev; |
| 155 | +} |
| 156 | + |
| 157 | +#endif |
| 158 | + |
| 159 | +ZEND_API void zend_atomic_bool_init(zend_atomic_bool *obj, bool desired); |
| 160 | +ZEND_API bool zend_atomic_bool_exchange(zend_atomic_bool *obj, bool desired); |
| 161 | +ZEND_API void zend_atomic_bool_store(zend_atomic_bool *obj, bool desired); |
| 162 | + |
| 163 | +#if ZEND_WIN32 || HAVE_SYNC_ATOMICS |
| 164 | +/* On these platforms it is non-const due to underlying APIs. */ |
| 165 | +ZEND_API bool zend_atomic_bool_load(zend_atomic_bool *obj); |
| 166 | +#else |
| 167 | +ZEND_API bool zend_atomic_bool_load(const zend_atomic_bool *obj); |
| 168 | +#endif |
| 169 | + |
| 170 | +END_EXTERN_C() |
| 171 | + |
| 172 | +#endif |
0 commit comments