| 1 |
/* This header supplies atomic operations. So far, we rely on GCC's |
| 2 |
* atomic builtins. During configure, we check if atomic operatons are |
| 3 |
* available. If they are not, I am making the necessary provisioning to live without them if |
| 4 |
* they are not available. Please note that you should only use the macros |
| 5 |
* here if you think you can actually live WITHOUT an explicit atomic operation, |
| 6 |
* because in the non-presence of them, we simply do it without atomicitiy. |
| 7 |
* Which, for word-aligned data types, usually (but only usually!) should work. |
| 8 |
* |
| 9 |
* We are using the functions described in |
| 10 |
* http:/gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html |
| 11 |
* |
| 12 |
* THESE MACROS MUST ONLY BE USED WITH WORD-SIZED DATA TYPES! |
| 13 |
* |
| 14 |
* Usage guidance (important for portability and static analysis tools): |
| 15 |
* - Operations are provided as macros or inline functions. Prefer simple lvalues |
| 16 |
* for the "data" argument to avoid double-evaluation surprises. |
| 17 |
* - The "phlpmut" argument is a helper mutex used when atomics are unavailable. |
| 18 |
* If atomics are supported, these helper mutex macros become no-ops. |
| 19 |
* Callers must still pass a valid pointer where required to keep signatures |
| 20 |
* uniform across platforms. |
| 21 |
* - Some macros are "best effort" (PREFER_*). They may degrade to non-atomic |
| 22 |
* operations when no atomic support is available. Use them only for |
| 23 |
* statistics or counters where occasional lost updates are acceptable. |
| 24 |
* - The remaining ATOMIC_* helpers provide true atomicity when available and |
| 25 |
* fall back to mutex-based critical sections when not. |
| 26 |
* - Mixing atomic loads/stores with plain reads/writes to the same variable is |
| 27 |
* undefined on modern compilers. If you use ATOMIC_* on a variable, access it |
| 28 |
* exclusively via these helpers (or other synchronized paths). |
| 29 |
* |
| 30 |
* Copyright 2008-2012 Rainer Gerhards and Adiscon GmbH. |
| 31 |
* |
| 32 |
* This file is part of the rsyslog runtime library. |
| 33 |
* |
| 34 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 35 |
* you may not use this file except in compliance with the License. |
| 36 |
* You may obtain a copy of the License at |
| 37 |
* |
| 38 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 39 |
* -or- |
| 40 |
* see COPYING.ASL20 in the source distribution |
| 41 |
* |
| 42 |
* Unless required by applicable law or agreed to in writing, software |
| 43 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 44 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 45 |
* See the License for the specific language governing permissions and |
| 46 |
* limitations under the License. |
| 47 |
*/ |
| 48 |
#ifndef INCLUDED_ATOMIC_H |
| 49 |
#define INCLUDED_ATOMIC_H |
| 50 |
#include <time.h> |
| 51 |
#include "typedefs.h" |
| 52 |
|
| 53 |
/* for this release, we disable atomic calls because there seem to be some |
| 54 |
* portability problems and we can not fix that without destabilizing the build. |
| 55 |
* They simply came in too late. -- rgerhards, 2008-04-02 |
| 56 |
*/ |
| 57 |
#ifdef HAVE_ATOMIC_BUILTINS |
| 58 |
#define ATOMIC_SUB(data, val, phlpmut) __sync_fetch_and_sub(data, val) |
| 59 |
#define ATOMIC_SUB_unsigned(data, val, phlpmut) __sync_fetch_and_sub(data, val) |
| 60 |
#define ATOMIC_ADD(data, val) __sync_fetch_and_add(&(data), val) |
| 61 |
#define ATOMIC_INC(data, phlpmut) ((void)__sync_fetch_and_add(data, 1)) |
| 62 |
#define ATOMIC_INC_AND_FETCH_int(data, phlpmut) __sync_fetch_and_add(data, 1) |
| 63 |
#define ATOMIC_INC_AND_FETCH_unsigned(data, phlpmut) __sync_fetch_and_add(data, 1) |
| 64 |
#define ATOMIC_DEC(data, phlpmut) ((void)__sync_sub_and_fetch(data, 1)) |
| 65 |
#define ATOMIC_DEC_AND_FETCH(data, phlpmut) __sync_sub_and_fetch(data, 1) |
| 66 |
#define ATOMIC_FETCH_32BIT(data, phlpmut) ((int)__sync_fetch_and_and(data, 0xffffffff)) |
| 67 |
#define ATOMIC_FETCH_32BIT_unsigned(data, phlpmut) ((unsigned)__sync_fetch_and_and(data, 0xffffffff)) |
| 68 |
#define ATOMIC_STORE_1_TO_32BIT(data) __sync_lock_test_and_set(&(data), 1) |
| 69 |
#define ATOMIC_STORE_0_TO_INT(data, phlpmut) __sync_fetch_and_and(data, 0) |
| 70 |
#define ATOMIC_STORE_1_TO_INT(data, phlpmut) __sync_fetch_and_or(data, 1) |
| 71 |
#define ATOMIC_STORE_INT(data, phlpmut, val) ((void)__sync_lock_test_and_set((data), (val))) |
| 72 |
#define ATOMIC_OR_INT_TO_INT(data, phlpmut, val) __sync_fetch_and_or((data), (val)) |
| 73 |
#define ATOMIC_CAS(data, oldVal, newVal, phlpmut) __sync_bool_compare_and_swap(data, (oldVal), (newVal)) |
| 74 |
#define ATOMIC_CAS_time_t(data, oldVal, newVal, phlpmut) __sync_bool_compare_and_swap(data, (oldVal), (newVal)) |
| 75 |
#define ATOMIC_CAS_VAL(data, oldVal, newVal, phlpmut) __sync_val_compare_and_swap(data, (oldVal), (newVal)); |
| 76 |
/* Atomic operations for pointers (word-sized on all supported platforms) */ |
| 77 |
#define ATOMIC_FETCH_PTR(data, phlpmut) ((void *)__sync_fetch_and_add(data, 0)) |
| 78 |
#define ATOMIC_STORE_PTR(data, phlpmut, val) ((void)__sync_lock_test_and_set((data), (val))) |
| 79 |
#define ATOMIC_CAS_PTR(data, oldVal, newVal, phlpmut) __sync_bool_compare_and_swap(data, (oldVal), (newVal)) |
| 80 |
|
| 81 |
/* functions below are not needed if we have atomics */ |
| 82 |
#define DEF_ATOMIC_HELPER_MUT(x) |
| 83 |
#define INIT_ATOMIC_HELPER_MUT(x) |
| 84 |
#define DESTROY_ATOMIC_HELPER_MUT(x) |
| 85 |
|
| 86 |
/* the following operations should preferrably be done atomic, but it is |
| 87 |
* not fatal if not -- that means we can live with some missed updates. So be |
| 88 |
* sure to use these macros only if that really does not matter! |
| 89 |
*/ |
| 90 |
#define PREFER_ATOMIC_INC(data) ((void)__sync_fetch_and_add(&(data), 1)) |
| 91 |
#define PREFER_FETCH_32BIT(data) ((unsigned)__sync_fetch_and_and(&(data), 0xffffffff)) |
| 92 |
#define PREFER_STORE_0_TO_INT(data) __sync_fetch_and_and(data, 0) |
| 93 |
#define PREFER_STORE_1_TO_INT(data) __sync_fetch_and_or(data, 1) |
| 94 |
#else |
| 95 |
/* note that we gained parctical proof that theoretical problems DO occur |
| 96 |
* if we do not properly address them. See this blog post for details: |
| 97 |
* http://blog.gerhards.net/2009/01/rsyslog-data-race-analysis.html |
| 98 |
* The bottom line is that if there are no atomics available, we should NOT |
| 99 |
* simply go ahead and do without them - use mutexes or other things. The |
| 100 |
* code needs to be checked against all those cases. -- rgerhards, 2009-01-30 |
| 101 |
*/ |
| 102 |
#include <pthread.h> |
| 103 |
#define ATOMIC_INC(data, phlpmut) \ |
| 104 |
{ \ |
| 105 |
pthread_mutex_lock(phlpmut); \ |
| 106 |
++(*(data)); \ |
| 107 |
pthread_mutex_unlock(phlpmut); \ |
| 108 |
} |
| 109 |
|
| 110 |
#define ATOMIC_STORE_0_TO_INT(data, hlpmut) \ |
| 111 |
{ \ |
| 112 |
pthread_mutex_lock(hlpmut); \ |
| 113 |
*(data) = 0; \ |
| 114 |
pthread_mutex_unlock(hlpmut); \ |
| 115 |
} |
| 116 |
|
| 117 |
#define ATOMIC_STORE_1_TO_INT(data, hlpmut) \ |
| 118 |
{ \ |
| 119 |
pthread_mutex_lock(hlpmut); \ |
| 120 |
*(data) = 1; \ |
| 121 |
pthread_mutex_unlock(hlpmut); \ |
| 122 |
} |
| 123 |
|
| 124 |
#define ATOMIC_STORE_INT(data, hlpmut, val) \ |
| 125 |
do { \ |
| 126 |
pthread_mutex_lock(hlpmut); \ |
| 127 |
*(data) = (val); \ |
| 128 |
pthread_mutex_unlock(hlpmut); \ |
| 129 |
} while (0) |
| 130 |
|
| 131 |
#define ATOMIC_OR_INT_TO_INT(data, hlpmut, val) \ |
| 132 |
{ \ |
| 133 |
pthread_mutex_lock(hlpmut); \ |
| 134 |
*(data) = val; \ |
| 135 |
pthread_mutex_unlock(hlpmut); \ |
| 136 |
} |
| 137 |
|
| 138 |
static inline int ATOMIC_CAS(int *data, int oldVal, int newVal, pthread_mutex_t *phlpmut) { |
| 139 |
int bSuccess; |
| 140 |
pthread_mutex_lock(phlpmut); |
| 141 |
if (*data == oldVal) { |
| 142 |
*data = newVal; |
| 143 |
bSuccess = 1; |
| 144 |
} else { |
| 145 |
bSuccess = 0; |
| 146 |
} |
| 147 |
pthread_mutex_unlock(phlpmut); |
| 148 |
return (bSuccess); |
| 149 |
} |
| 150 |
|
| 151 |
static inline int ATOMIC_CAS_time_t(time_t *data, time_t oldVal, time_t newVal, pthread_mutex_t *phlpmut) { |
| 152 |
int bSuccess; |
| 153 |
pthread_mutex_lock(phlpmut); |
| 154 |
if (*data == oldVal) { |
| 155 |
*data = newVal; |
| 156 |
bSuccess = 1; |
| 157 |
} else { |
| 158 |
bSuccess = 0; |
| 159 |
} |
| 160 |
pthread_mutex_unlock(phlpmut); |
| 161 |
return (bSuccess); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
static inline int ATOMIC_CAS_VAL(int *data, int oldVal, int newVal, pthread_mutex_t *phlpmut) { |
| 166 |
int val; |
| 167 |
pthread_mutex_lock(phlpmut); |
| 168 |
if (*data == oldVal) { |
| 169 |
*data = newVal; |
| 170 |
} |
| 171 |
val = *data; |
| 172 |
pthread_mutex_unlock(phlpmut); |
| 173 |
return (val); |
| 174 |
} |
| 175 |
|
| 176 |
#define ATOMIC_DEC(data, phlpmut) \ |
| 177 |
{ \ |
| 178 |
pthread_mutex_lock(phlpmut); \ |
| 179 |
--(*(data)); \ |
| 180 |
pthread_mutex_unlock(phlpmut); \ |
| 181 |
} |
| 182 |
|
| 183 |
static inline int ATOMIC_INC_AND_FETCH_int(int *data, pthread_mutex_t *phlpmut) { |
| 184 |
int val; |
| 185 |
pthread_mutex_lock(phlpmut); |
| 186 |
val = ++(*data); |
| 187 |
pthread_mutex_unlock(phlpmut); |
| 188 |
return (val); |
| 189 |
} |
| 190 |
|
| 191 |
static inline unsigned ATOMIC_INC_AND_FETCH_unsigned(unsigned *data, pthread_mutex_t *phlpmut) { |
| 192 |
unsigned val; |
| 193 |
pthread_mutex_lock(phlpmut); |
| 194 |
val = ++(*data); |
| 195 |
pthread_mutex_unlock(phlpmut); |
| 196 |
return (val); |
| 197 |
} |
| 198 |
|
| 199 |
static inline int ATOMIC_DEC_AND_FETCH(int *data, pthread_mutex_t *phlpmut) { |
| 200 |
int val; |
| 201 |
pthread_mutex_lock(phlpmut); |
| 202 |
val = --(*data); |
| 203 |
pthread_mutex_unlock(phlpmut); |
| 204 |
return (val); |
| 205 |
} |
| 206 |
|
| 207 |
static inline int ATOMIC_FETCH_32BIT(int *data, pthread_mutex_t *phlpmut) { |
| 208 |
int val; |
| 209 |
pthread_mutex_lock(phlpmut); |
| 210 |
val = (*data); |
| 211 |
pthread_mutex_unlock(phlpmut); |
| 212 |
return (val); |
| 213 |
} |
| 214 |
|
| 215 |
static inline int ATOMIC_FETCH_32BIT_unsigned(unsigned *data, pthread_mutex_t *phlpmut) { |
| 216 |
int val; |
| 217 |
pthread_mutex_lock(phlpmut); |
| 218 |
val = (*data); |
| 219 |
pthread_mutex_unlock(phlpmut); |
| 220 |
return (val); |
| 221 |
} |
| 222 |
|
| 223 |
static inline void ATOMIC_SUB(int *data, int val, pthread_mutex_t *phlpmut) { |
| 224 |
pthread_mutex_lock(phlpmut); |
| 225 |
(*data) -= val; |
| 226 |
pthread_mutex_unlock(phlpmut); |
| 227 |
} |
| 228 |
|
| 229 |
static inline void ATOMIC_SUB_unsigned(unsigned *data, int val, pthread_mutex_t *phlpmut) { |
| 230 |
pthread_mutex_lock(phlpmut); |
| 231 |
(*data) -= val; |
| 232 |
pthread_mutex_unlock(phlpmut); |
| 233 |
} |
| 234 |
|
| 235 |
/* Atomic operations for pointers - fallback to mutex-protected operations */ |
| 236 |
static inline void *ATOMIC_FETCH_PTR(void **data, pthread_mutex_t *phlpmut) { |
| 237 |
void *val; |
| 238 |
pthread_mutex_lock(phlpmut); |
| 239 |
val = *data; |
| 240 |
pthread_mutex_unlock(phlpmut); |
| 241 |
return val; |
| 242 |
} |
| 243 |
|
| 244 |
static inline void ATOMIC_STORE_PTR(void **data, pthread_mutex_t *phlpmut, void *val) { |
| 245 |
pthread_mutex_lock(phlpmut); |
| 246 |
*data = val; |
| 247 |
pthread_mutex_unlock(phlpmut); |
| 248 |
} |
| 249 |
|
| 250 |
static inline int ATOMIC_CAS_PTR(void **data, void *oldVal, void *newVal, pthread_mutex_t *phlpmut) { |
| 251 |
int bSuccess; |
| 252 |
pthread_mutex_lock(phlpmut); |
| 253 |
if (*data == oldVal) { |
| 254 |
*data = newVal; |
| 255 |
bSuccess = 1; |
| 256 |
} else { |
| 257 |
bSuccess = 0; |
| 258 |
} |
| 259 |
pthread_mutex_unlock(phlpmut); |
| 260 |
return bSuccess; |
| 261 |
} |
| 262 |
|
| 263 |
#define DEF_ATOMIC_HELPER_MUT(x) pthread_mutex_t x |
| 264 |
#define INIT_ATOMIC_HELPER_MUT(x) pthread_mutex_init(&(x), NULL) |
| 265 |
#define DESTROY_ATOMIC_HELPER_MUT(x) pthread_mutex_destroy(&(x)) |
| 266 |
|
| 267 |
#define PREFER_ATOMIC_INC(data) ((void)++data) |
| 268 |
#define PREFER_FETCH_32BIT(data) ((unsigned)(data)) |
| 269 |
#define PREFER_STORE_0_TO_INT(data) (*(data) = 0) |
| 270 |
#define PREFER_STORE_1_TO_INT(data) (*(data) = 1) |
| 271 |
|
| 272 |
#endif |
| 273 |
|
| 274 |
/* we need to handle 64bit atomics seperately as some platforms have |
| 275 |
* 32 bit atomics, but not 64 bit ones... -- rgerhards, 2010-12-01 |
| 276 |
*/ |
| 277 |
#ifdef HAVE_ATOMIC_BUILTINS64 |
| 278 |
#define ATOMIC_INC_uint64(data, phlpmut) ((void)__sync_fetch_and_add(data, 1)) |
| 279 |
#define ATOMIC_ADD_uint64(data, phlpmut, value) ((void)__sync_fetch_and_add(data, value)) |
| 280 |
#define ATOMIC_DEC_uint64(data, phlpmut) ((void)__sync_sub_and_fetch(data, 1)) |
| 281 |
#define ATOMIC_INC_AND_FETCH_uint64(data, phlpmut) __sync_fetch_and_add(data, 1) |
| 282 |
|
| 283 |
#define DEF_ATOMIC_HELPER_MUT64(x) |
| 284 |
#define INIT_ATOMIC_HELPER_MUT64(x) |
| 285 |
#define DESTROY_ATOMIC_HELPER_MUT64(x) |
| 286 |
#else |
| 287 |
#define ATOMIC_INC_uint64(data, phlpmut) \ |
| 288 |
{ \ |
| 289 |
pthread_mutex_lock(phlpmut); \ |
| 290 |
++(*(data)); \ |
| 291 |
pthread_mutex_unlock(phlpmut); \ |
| 292 |
} |
| 293 |
#define ATOMIC_ADD_uint64(data, phlpmut, value) \ |
| 294 |
{ \ |
| 295 |
pthread_mutex_lock(phlpmut); \ |
| 296 |
*data += value; \ |
| 297 |
pthread_mutex_unlock(phlpmut); \ |
| 298 |
} |
| 299 |
#define ATOMIC_DEC_uint64(data, phlpmut) \ |
| 300 |
{ \ |
| 301 |
pthread_mutex_lock(phlpmut); \ |
| 302 |
--(*(data)); \ |
| 303 |
pthread_mutex_unlock(phlpmut); \ |
| 304 |
} |
| 305 |
|
| 306 |
static inline unsigned ATOMIC_INC_AND_FETCH_uint64(uint64 *data, pthread_mutex_t *phlpmut) { |
| 307 |
uint64 val; |
| 308 |
pthread_mutex_lock(phlpmut); |
| 309 |
val = ++(*data); |
| 310 |
pthread_mutex_unlock(phlpmut); |
| 311 |
return (val); |
| 312 |
} |
| 313 |
|
| 314 |
#define DEF_ATOMIC_HELPER_MUT64(x) pthread_mutex_t x |
| 315 |
#define INIT_ATOMIC_HELPER_MUT64(x) pthread_mutex_init(&(x), NULL) |
| 316 |
#define DESTROY_ATOMIC_HELPER_MUT64(x) pthread_mutex_destroy(&(x)) |
| 317 |
#endif /* #ifdef HAVE_ATOMIC_BUILTINS64 */ |
| 318 |
|
| 319 |
#endif /* #ifndef INCLUDED_ATOMIC_H */ |