-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatomic_sync.hh
360 lines (352 loc) · 12.4 KB
/
atomic_sync.hh
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) 2016 Zhao DAI <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see accompanying file LICENSE.txt
* or <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Lock-free atomic operations for integral types.
* @author Zhao DAI
*/
#ifndef DOZERG_ATOMIC_SYNC_H_20130117
#define DOZERG_ATOMIC_SYNC_H_20130117
#include <cassert>
#include <cstddef> //NULL
#include "impl/environment.hh"
NS_SERVER_BEGIN
/**
* @brief GCC @c __sync_XXX based atomic operations for [integral types]
* (http://en.cppreference.com/w/cpp/types/is_integral).
* A common use of CAtomicSync is a high performance counter shared between threads or processes.
* @tparam T An integral type, i.e. int, long, etc.
*/
template<typename T>
class CAtomicSync
{
typedef CAtomicSync<T> __Myt;
public:
typedef T value_type;
/**
* @brief Initialize with a value.
* If no value is provided, default to 0.
* @param c A value
*/
explicit CAtomicSync(value_type c = value_type()):v_(c){}
/**
* @brief Initialize from another CAtomicSync.
* @param c Another CAtomicSync
*/
CAtomicSync(const __Myt & c):v_(c.load()){}
/**
* @brief Get value atomically.
* @return Current value, i.e. @c *this
*/
value_type load() const volatile{
return const_cast<__Myt *>(this)->add_fetch(0);
}
/**
* @brief Get value atomically.
* @param v Pointer to a value
* @return Current value of @c v, i.e. @c *v
*/
static value_type load(const value_type * v){
assert(v);
return add_fetch(const_cast<value_type *>(v), 0);
}
/**
* @brief Set value atomically.
* This function performs `{*this = c}`.
* @param c A Value
*/
void store(value_type c) volatile{swap(c);}
/**
* @brief Set value atomically.
* This function performs `{*v = c}`.
* @param v Pointer to a value
* @param c A Value
*/
static void store(value_type * v, value_type c){
assert(v);
swap(v, c);
}
/**
* @brief Get value and then ADD atomically.
* This function performs `{T old = *this; *this += c; return old;}`
* @param c A value
* @return Current value @em before ADD'ing
*/
value_type fetch_add(value_type c) volatile{return __sync_fetch_and_add(&v_, c);}
/**
* @brief Get value and then ADD atomically.
* This function performs `{T old = *v; *v += c; return old;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em before ADD'ing
*/
static value_type fetch_add(value_type * v, value_type c){
assert(v);
return __sync_fetch_and_add(v, c);
}
/**
* @brief ADD and then get value atomically.
* This function performs `{*this += c; return *this;}`
* @param c A value
* @return Current value @em after ADD'ing
*/
value_type add_fetch(value_type c) volatile{return __sync_add_and_fetch(&v_, c);}
/**
* @brief ADD and then get value atomically.
* This function performs `{*v += c; return *v;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em after ADD'ing
*/
static value_type add_fetch(value_type * v, value_type c){
assert(v);
return __sync_add_and_fetch (v, c);
}
/**
* @brief Get value and then SUBTRACT atomically.
* This function performs `{T old = *this; *this -= c; return old;}`
* @param c A value
* @return Current value @em before SUBTRACT'ing
*/
value_type fetch_sub(value_type c) volatile{return __sync_fetch_and_sub(&v_, c);}
/**
* @brief Get value and then SUBTRACT atomically.
* This function performs `{T old = *v; *v -= c; return old;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em before SUBTRACT'ing
*/
static value_type fetch_sub(value_type * v, value_type c){
assert(v);
return __sync_fetch_and_sub(v, c);
}
/**
* @brief SUBTRACT and then get value atomically.
* This function performs `{*this -= c; return *this;}`
* @param c A value
* @return Current value @em after SUBTRACT'ing
*/
value_type sub_fetch(value_type c) volatile{return __sync_sub_and_fetch(&v_, c);}
/**
* @brief SUBTRACT and then get value atomically.
* This function performs `{*v -= c; return *v;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em after SUBTRACT'ing
*/
static value_type sub_fetch(value_type * v, value_type c){
assert(v);
return __sync_sub_and_fetch (v, c);
}
/**
* @brief Get value and then OR atomically.
* This function performs `{T old = *this; *this |= c; return old;}`
* @param c A value
* @return Current value @em before OR'ing
*/
value_type fetch_or(value_type c) volatile{return __sync_fetch_and_or(&v_, c);}
/**
* @brief Get value and then OR atomically.
* This function performs `{T old = *v; *v |= c; return old;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em before OR'ing
*/
static value_type fetch_or(value_type * v, value_type c){
assert(v);
return __sync_fetch_and_or(v, c);
}
/**
* @brief OR and then get value atomically.
* This function performs `{*this |= c; return *this;}`
* @param c A value
* @return Current value @em after OR'ing
*/
value_type or_fetch(value_type c) volatile{return __sync_or_and_fetch(&v_, c);}
/**
* @brief OR and then get value atomically.
* This function performs `{*v |= c; return *v;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em after OR'ing
*/
static value_type or_fetch(value_type * v, value_type c){
assert(v);
return __sync_or_and_fetch (v, c);
}
/**
* @brief Get value and then AND atomically.
* This function performs `{T old = *this; *this &= c; return old;}`
* @param c A value
* @return Current value @em before AND'ing
*/
value_type fetch_and(value_type c) volatile{return __sync_fetch_and_and(&v_, c);}
/**
* @brief Get value and then AND atomically.
* This function performs `{T old = *v; *v &= c; return old;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em before AND'ing
*/
static value_type fetch_and(value_type * v, value_type c){
assert(v);
return __sync_fetch_and_and(v, c);
}
/**
* @brief AND and then get value atomically.
* This function performs `{*this &= c; return *this;}`
* @param c A value
* @return Current value @em after AND'ing
*/
value_type and_fetch(value_type c) volatile{return __sync_and_and_fetch(&v_, c);}
/**
* @brief AND and then get value atomically.
* This function performs `{*v &= c; return *v;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em after AND'ing
*/
static value_type and_fetch(value_type * v, value_type c){
assert(v);
return __sync_and_and_fetch (v, c);
}
/**
* @brief Get value and then XOR atomically.
* This function performs `{T old = *this; *this ^= c; return old;}`
* @param c A value
* @return Current value @em before XOR'ing
*/
value_type fetch_xor(value_type c) volatile{return __sync_fetch_and_xor(&v_, c);}
/**
* @brief Get value and then XOR atomically.
* This function performs `{T old = *v; *v ^= c; return old;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em before XOR'ing
*/
static value_type fetch_xor(value_type * v, value_type c){
assert(v);
return __sync_fetch_and_xor(v, c);
}
/**
* @brief XOR and then get value atomically.
* This function performs `{*this ^= c; return *this;}`
* @param c A value
* @return Current value @em after XOR'ing
*/
value_type xor_fetch(value_type c) volatile{return __sync_xor_and_fetch(&v_, c);}
/**
* @brief XOR and then get value atomically.
* This function performs `{*v ^= c; return *v;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em after XOR'ing
*/
static value_type xor_fetch(value_type * v, value_type c){
assert(v);
return __sync_xor_and_fetch (v, c);
}
/**
* @brief Get value and then set atomically.
* This function performs `{T old = *this; *this = c; return old;}`
* @param c A value
* @return Value @em before setting
*/
value_type swap(value_type c) volatile{return __sync_lock_test_and_set(&v_, c);}
/**
* @brief Get value and then set atomically.
* This function performs `{T old = *v; *v = c; return old;}`
* @param v Pointer to a value
* @param c A value
* @return Value @em before setting
*/
static value_type swap(value_type * v, value_type c){
assert(v);
return __sync_lock_test_and_set(v, c);
}
/**
* @brief Compare and set atomically.
* This function performs:
* @code{.cpp}
* if(*this == expval){
* *oldval = *this; *this = newval; return true;
* }else{
* *oldval = *this; return false;
* }
* @endcode
* @param[in] expval Expected value
* @param[in] newval New value to set
* @param[out] oldval Pointer to a value to receive the old value @em before setting; Or @c
* NULL if not interested
* @return @c true if `*this == expval` ; otherwise @c false
* @note Setting operation will be performed if and only if compare operation returns @c true.
*/
bool compare_swap(value_type expval, value_type newval, value_type * oldval = NULL) volatile{
if(NULL == oldval)
return __sync_bool_compare_and_swap(&v_, expval, newval);
*oldval = __sync_val_compare_and_swap(&v_, expval, newval);
return (*oldval == expval);
}
/**
* @brief Compare and set atomically.
* This function performs:
* @code{.cpp}
* if(*val == expval){
* *oldval = *val; *val = newval; return true;
* }else{
* *oldval = *val; return false;
* }
* @endcode
* @param[inout] val Pointer to a value
* @param[in] expval Expected value
* @param[in] newval New value to set
* @param[out] oldval Pointer to a value to receive the old value @em before setting; Or @c
* NULL if not interested
* @return @c true if `*this == expval` ; otherwise @c false
* @note Setting operation will be performed if and only if compare operation returns @c true.
*/
static bool compare_swap(value_type * val, value_type expval, value_type newval, value_type * oldval){
assert(val);
if(NULL == oldval)
return __sync_bool_compare_and_swap(val, expval, newval);
*oldval = __sync_val_compare_and_swap(val, expval, newval);
return (*oldval == expval);
}
/**
* @name Operators
* Those operators just act as what you've already known, except that they are all atomic
* operations.
* @{ */
value_type operator =(const __Myt & c) volatile{return (v_ = c.load());}
value_type operator =(value_type c) volatile{return (v_ = c);}
operator value_type() const volatile{return load();}
value_type operator ++() volatile{return add_fetch(1);}
value_type operator --() volatile{return sub_fetch(1);}
value_type operator ++(int) volatile{return fetch_add(1);}
value_type operator --(int) volatile{return fetch_sub(1);}
value_type operator +=(value_type c) volatile{return add_fetch(c);}
value_type operator -=(value_type c) volatile{return sub_fetch(c);}
value_type operator |=(value_type c) volatile{return or_fetch(c);}
value_type operator &=(value_type c) volatile{return and_fetch(c);}
value_type operator ^=(value_type c) volatile{return xor_fetch(c);}
/** @} */
private:
value_type v_;
};
NS_SERVER_END
#endif