-
Notifications
You must be signed in to change notification settings - Fork 32
/
atomic.h
227 lines (205 loc) · 7.56 KB
/
atomic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* This header supplies atomic operations. So far, we rely on GCC's
* atomic builtins. During configure, we check if atomic operatons are
* available. If they are not, I am making the necessary provisioning to live without them if
* they are not available. Please note that you should only use the macros
* here if you think you can actually live WITHOUT an explicit atomic operation,
* because in the non-presence of them, we simply do it without atomicitiy.
* Which, for word-aligned data types, usually (but only usually!) should work.
*
* We are using the functions described in
* http:/gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html
*
* THESE MACROS MUST ONLY BE USED WITH WORD-SIZED DATA TYPES!
*
* Note: this file was obtained at 2015-12-16 from the rsyslog project.
*
* Copyright 2008-2015 Rainer Gerhards and Adiscon GmbH.
*
* This file is part of the rsyslog runtime library.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* -or-
* see COPYING.ASL20 in the source distribution
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FJ_INCLUDED_ATOMIC_H
#define FJ_INCLUDED_ATOMIC_H
#ifdef HAVE_ATOMIC_BUILTINS
# define ATOMIC_SUB(data, val, phlpmut) __sync_fetch_and_sub(data, val)
# define ATOMIC_ADD(data, val) __sync_fetch_and_add(&(data), val)
# define ATOMIC_INC(data, phlpmut) ((void) __sync_fetch_and_add(data, 1))
# define ATOMIC_INC_AND_FETCH_int(data, phlpmut) __sync_fetch_and_add(data, 1)
# define ATOMIC_INC_AND_FETCH_unsigned(data, phlpmut) __sync_fetch_and_add(data, 1)
# define ATOMIC_DEC(data, phlpmut) ((void) __sync_sub_and_fetch(data, 1))
# define ATOMIC_DEC_AND_FETCH(data, phlpmut) __sync_sub_and_fetch(data, 1)
# define ATOMIC_FETCH_32BIT(data, phlpmut) ((unsigned) __sync_fetch_and_and(data, 0xffffffff))
# define ATOMIC_STORE_1_TO_32BIT(data) __sync_lock_test_and_set(&(data), 1)
# define ATOMIC_STORE_0_TO_INT(data, phlpmut) __sync_fetch_and_and(data, 0)
# define ATOMIC_STORE_1_TO_INT(data, phlpmut) __sync_fetch_and_or(data, 1)
# define ATOMIC_STORE_INT_TO_INT(data, val) __sync_fetch_and_or(&(data), (val))
# define ATOMIC_CAS(data, oldVal, newVal, phlpmut) __sync_bool_compare_and_swap(data, (oldVal), (newVal))
# define ATOMIC_CAS_time_t(data, oldVal, newVal, phlpmut) __sync_bool_compare_and_swap(data, (oldVal), (newVal))
# define ATOMIC_CAS_VAL(data, oldVal, newVal, phlpmut) __sync_val_compare_and_swap(data, (oldVal), (newVal));
/* functions below are not needed if we have atomics */
# define DEF_ATOMIC_HELPER_MUT(x)
# define INIT_ATOMIC_HELPER_MUT(x)
# define DESTROY_ATOMIC_HELPER_MUT(x)
/* the following operations should preferrably be done atomic, but it is
* not fatal if not -- that means we can live with some missed updates. So be
* sure to use these macros only if that really does not matter!
*/
# define PREFER_ATOMIC_INC(data) ((void) __sync_fetch_and_add(&(data), 1))
#else
/* note that we gained parctical proof that theoretical problems DO occur
* if we do not properly address them. See this blog post for details:
* http://blog.gerhards.net/2009/01/rsyslog-data-race-analysis.html
* The bottom line is that if there are no atomics available, we should NOT
* simply go ahead and do without them - use mutexes or other things.
* rgerhards, 2009-01-30
*/
#include <pthread.h>
# define ATOMIC_INC(data, phlpmut) { \
pthread_mutex_lock(phlpmut); \
++(*(data)); \
pthread_mutex_unlock(phlpmut); \
}
# define ATOMIC_STORE_0_TO_INT(data, hlpmut) { \
pthread_mutex_lock(hlpmut); \
*(data) = 0; \
pthread_mutex_unlock(hlpmut); \
}
# define ATOMIC_STORE_1_TO_INT(data, hlpmut) { \
pthread_mutex_lock(hlpmut); \
*(data) = 1; \
pthread_mutex_unlock(hlpmut); \
}
static inline int
ATOMIC_CAS(int *data, int oldVal, int newVal, pthread_mutex_t *phlpmut) {
int bSuccess;
pthread_mutex_lock(phlpmut);
if(*data == oldVal) {
*data = newVal;
bSuccess = 1;
} else {
bSuccess = 0;
}
pthread_mutex_unlock(phlpmut);
return(bSuccess);
}
static inline int
ATOMIC_CAS_time_t(time_t *data, time_t oldVal, time_t newVal, pthread_mutex_t *phlpmut) {
int bSuccess;
pthread_mutex_lock(phlpmut);
if(*data == oldVal) {
*data = newVal;
bSuccess = 1;
} else {
bSuccess = 0;
}
pthread_mutex_unlock(phlpmut);
return(bSuccess);
}
static inline int
ATOMIC_CAS_VAL(int *data, int oldVal, int newVal, pthread_mutex_t *phlpmut) {
int val;
pthread_mutex_lock(phlpmut);
if(*data == oldVal) {
*data = newVal;
}
val = *data;
pthread_mutex_unlock(phlpmut);
return(val);
}
# define ATOMIC_DEC(data, phlpmut) { \
pthread_mutex_lock(phlpmut); \
--(*(data)); \
pthread_mutex_unlock(phlpmut); \
}
static inline int
ATOMIC_INC_AND_FETCH_int(int *data, pthread_mutex_t *phlpmut) {
int val;
pthread_mutex_lock(phlpmut);
val = ++(*data);
pthread_mutex_unlock(phlpmut);
return(val);
}
static inline unsigned
ATOMIC_INC_AND_FETCH_unsigned(unsigned *data, pthread_mutex_t *phlpmut) {
unsigned val;
pthread_mutex_lock(phlpmut);
val = ++(*data);
pthread_mutex_unlock(phlpmut);
return(val);
}
static inline int
ATOMIC_DEC_AND_FETCH(int *data, pthread_mutex_t *phlpmut) {
int val;
pthread_mutex_lock(phlpmut);
val = --(*data);
pthread_mutex_unlock(phlpmut);
return(val);
}
static inline int
ATOMIC_FETCH_32BIT(int *data, pthread_mutex_t *phlpmut) {
int val;
pthread_mutex_lock(phlpmut);
val = (*data);
pthread_mutex_unlock(phlpmut);
return(val);
}
static inline void
ATOMIC_SUB(int *data, int val, pthread_mutex_t *phlpmut) {
pthread_mutex_lock(phlpmut);
(*data) -= val;
pthread_mutex_unlock(phlpmut);
}
# define DEF_ATOMIC_HELPER_MUT(x) pthread_mutex_t x;
# define INIT_ATOMIC_HELPER_MUT(x) pthread_mutex_init(&(x), NULL);
# define DESTROY_ATOMIC_HELPER_MUT(x) pthread_mutex_destroy(&(x));
# define PREFER_ATOMIC_INC(data) ((void) ++data)
#endif
/* we need to handle 64bit atomics seperately as some platforms have
* 32 bit atomics, but not 64 bit ones... -- rgerhards, 2010-12-01
*/
#if 0 /* currently disabled, we don't need it now and dont' have the data types present */
#ifdef HAVE_ATOMIC_BUILTINS64
# define ATOMIC_INC_uint64(data, phlpmut) ((void) __sync_fetch_and_add(data, 1))
# define ATOMIC_DEC_unit64(data, phlpmut) ((void) __sync_sub_and_fetch(data, 1))
# define ATOMIC_INC_AND_FETCH_uint64(data, phlpmut) __sync_fetch_and_add(data, 1)
# define DEF_ATOMIC_HELPER_MUT64(x)
# define INIT_ATOMIC_HELPER_MUT64(x)
# define DESTROY_ATOMIC_HELPER_MUT64(x)
#else
# define ATOMIC_INC_uint64(data, phlpmut) { \
pthread_mutex_lock(phlpmut); \
++(*(data)); \
pthread_mutex_unlock(phlpmut); \
}
# define ATOMIC_DEC_uint64(data, phlpmut) { \
pthread_mutex_lock(phlpmut); \
--(*(data)); \
pthread_mutex_unlock(phlpmut); \
}
static inline unsigned
ATOMIC_INC_AND_FETCH_uint64(uint64 *data, pthread_mutex_t *phlpmut) {
uint64 val;
pthread_mutex_lock(phlpmut);
val = ++(*data);
pthread_mutex_unlock(phlpmut);
return(val);
}
# define DEF_ATOMIC_HELPER_MUT64(x) pthread_mutex_t x;
# define INIT_ATOMIC_HELPER_MUT64(x) pthread_mutex_init(&(x), NULL)
# define DESTROY_ATOMIC_HELPER_MUT64(x) pthread_mutex_destroy(&(x))
#endif /* #ifdef HAVE_ATOMIC_BUILTINS64 */
#endif
#endif /* #ifndef INCLUDED_ATOMIC_H */