Marine Library  1.0
C++ library for Linux Networking Development
atomic_sync.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Zhao DAI <daidodo@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see accompanying file LICENSE.txt
16  * or <http://www.gnu.org/licenses/>.
17  */
18 
25 #ifndef DOZERG_ATOMIC_SYNC_H_20130117
26 #define DOZERG_ATOMIC_SYNC_H_20130117
27 
28 #include <cassert>
29 #include <cstddef> //NULL
30 #include "impl/environment.hh"
31 
32 NS_SERVER_BEGIN
33 
40 template<typename T>
42 {
43  typedef CAtomicSync<T> __Myt;
44 public:
45  typedef T value_type;
51  explicit CAtomicSync(value_type c = value_type()):v_(c){}
56  CAtomicSync(const __Myt & c):v_(c.load()){}
61  value_type load() const volatile{
62  return const_cast<__Myt *>(this)->add_fetch(0);
63  }
69  static value_type load(const value_type * v){
70  assert(v);
71  return add_fetch(const_cast<value_type *>(v), 0);
72  }
78  void store(value_type c) volatile{swap(c);}
85  static void store(value_type * v, value_type c){
86  assert(v);
87  swap(v, c);
88  }
95  value_type fetch_add(value_type c) volatile{return __sync_fetch_and_add(&v_, c);}
103  static value_type fetch_add(value_type * v, value_type c){
104  assert(v);
105  return __sync_fetch_and_add(v, c);
106  }
113  value_type add_fetch(value_type c) volatile{return __sync_add_and_fetch(&v_, c);}
121  static value_type add_fetch(value_type * v, value_type c){
122  assert(v);
123  return __sync_add_and_fetch (v, c);
124  }
131  value_type fetch_sub(value_type c) volatile{return __sync_fetch_and_sub(&v_, c);}
139  static value_type fetch_sub(value_type * v, value_type c){
140  assert(v);
141  return __sync_fetch_and_sub(v, c);
142  }
149  value_type sub_fetch(value_type c) volatile{return __sync_sub_and_fetch(&v_, c);}
157  static value_type sub_fetch(value_type * v, value_type c){
158  assert(v);
159  return __sync_sub_and_fetch (v, c);
160  }
167  value_type fetch_or(value_type c) volatile{return __sync_fetch_and_or(&v_, c);}
175  static value_type fetch_or(value_type * v, value_type c){
176  assert(v);
177  return __sync_fetch_and_or(v, c);
178  }
185  value_type or_fetch(value_type c) volatile{return __sync_or_and_fetch(&v_, c);}
193  static value_type or_fetch(value_type * v, value_type c){
194  assert(v);
195  return __sync_or_and_fetch (v, c);
196  }
203  value_type fetch_and(value_type c) volatile{return __sync_fetch_and_and(&v_, c);}
211  static value_type fetch_and(value_type * v, value_type c){
212  assert(v);
213  return __sync_fetch_and_and(v, c);
214  }
221  value_type and_fetch(value_type c) volatile{return __sync_and_and_fetch(&v_, c);}
229  static value_type and_fetch(value_type * v, value_type c){
230  assert(v);
231  return __sync_and_and_fetch (v, c);
232  }
239  value_type fetch_xor(value_type c) volatile{return __sync_fetch_and_xor(&v_, c);}
247  static value_type fetch_xor(value_type * v, value_type c){
248  assert(v);
249  return __sync_fetch_and_xor(v, c);
250  }
257  value_type xor_fetch(value_type c) volatile{return __sync_xor_and_fetch(&v_, c);}
265  static value_type xor_fetch(value_type * v, value_type c){
266  assert(v);
267  return __sync_xor_and_fetch (v, c);
268  }
275  value_type swap(value_type c) volatile{return __sync_lock_test_and_set(&v_, c);}
283  static value_type swap(value_type * v, value_type c){
284  assert(v);
285  return __sync_lock_test_and_set(v, c);
286  }
304  bool compare_swap(value_type expval, value_type newval, value_type * oldval = NULL) volatile{
305  if(NULL == oldval)
306  return __sync_bool_compare_and_swap(&v_, expval, newval);
307  *oldval = __sync_val_compare_and_swap(&v_, expval, newval);
308  return (*oldval == expval);
309  }
328  static bool compare_swap(value_type * val, value_type expval, value_type newval, value_type * oldval){
329  assert(val);
330  if(NULL == oldval)
331  return __sync_bool_compare_and_swap(val, expval, newval);
332  *oldval = __sync_val_compare_and_swap(val, expval, newval);
333  return (*oldval == expval);
334  }
340  value_type operator =(const __Myt & c) volatile{return (v_ = c.load());}
341  value_type operator =(value_type c) volatile{return (v_ = c);}
342  operator value_type() const volatile{return load();}
343  value_type operator ++() volatile{return add_fetch(1);}
344  value_type operator --() volatile{return sub_fetch(1);}
345  value_type operator ++(int) volatile{return fetch_add(1);}
346  value_type operator --(int) volatile{return fetch_sub(1);}
347  value_type operator +=(value_type c) volatile{return add_fetch(c);}
348  value_type operator -=(value_type c) volatile{return sub_fetch(c);}
349  value_type operator |=(value_type c) volatile{return or_fetch(c);}
350  value_type operator &=(value_type c) volatile{return and_fetch(c);}
351  value_type operator ^=(value_type c) volatile{return xor_fetch(c);}
353 private:
354  value_type v_;
355 };
356 
357 NS_SERVER_END
358 
359 #endif
360 
static value_type fetch_and(value_type *v, value_type c)
Get value and then AND atomically.
Definition: atomic_sync.hh:211
static value_type fetch_xor(value_type *v, value_type c)
Get value and then XOR atomically.
Definition: atomic_sync.hh:247
static value_type swap(value_type *v, value_type c)
Get value and then set atomically.
Definition: atomic_sync.hh:283
static value_type or_fetch(value_type *v, value_type c)
OR and then get value atomically.
Definition: atomic_sync.hh:193
value_type and_fetch(value_type c) volatile
AND and then get value atomically.
Definition: atomic_sync.hh:221
static value_type load(const value_type *v)
Get value atomically.
Definition: atomic_sync.hh:69
static value_type add_fetch(value_type *v, value_type c)
ADD and then get value atomically.
Definition: atomic_sync.hh:121
static bool compare_swap(value_type *val, value_type expval, value_type newval, value_type *oldval)
Compare and set atomically.
Definition: atomic_sync.hh:328
value_type swap(value_type c) volatile
Get value and then set atomically.
Definition: atomic_sync.hh:275
CAtomicSync(const __Myt &c)
Initialize from another CAtomicSync.
Definition: atomic_sync.hh:56
value_type or_fetch(value_type c) volatile
OR and then get value atomically.
Definition: atomic_sync.hh:185
value_type load() const volatile
Get value atomically.
Definition: atomic_sync.hh:61
CAtomicSync(value_type c=value_type())
Initialize with a value.
Definition: atomic_sync.hh:51
static value_type xor_fetch(value_type *v, value_type c)
XOR and then get value atomically.
Definition: atomic_sync.hh:265
static value_type fetch_sub(value_type *v, value_type c)
Get value and then SUBTRACT atomically.
Definition: atomic_sync.hh:139
value_type fetch_sub(value_type c) volatile
Get value and then SUBTRACT atomically.
Definition: atomic_sync.hh:131
value_type xor_fetch(value_type c) volatile
XOR and then get value atomically.
Definition: atomic_sync.hh:257
value_type fetch_xor(value_type c) volatile
Get value and then XOR atomically.
Definition: atomic_sync.hh:239
value_type fetch_or(value_type c) volatile
Get value and then OR atomically.
Definition: atomic_sync.hh:167
static value_type fetch_or(value_type *v, value_type c)
Get value and then OR atomically.
Definition: atomic_sync.hh:175
static value_type and_fetch(value_type *v, value_type c)
AND and then get value atomically.
Definition: atomic_sync.hh:229
void store(value_type c) volatile
Set value atomically.
Definition: atomic_sync.hh:78
static void store(value_type *v, value_type c)
Set value atomically.
Definition: atomic_sync.hh:85
static value_type sub_fetch(value_type *v, value_type c)
SUBTRACT and then get value atomically.
Definition: atomic_sync.hh:157
bool compare_swap(value_type expval, value_type newval, value_type *oldval=NULL) volatile
Compare and set atomically.
Definition: atomic_sync.hh:304
GCC __sync_XXX based atomic operations for integral types.
Definition: atomic_sync.hh:41
static value_type fetch_add(value_type *v, value_type c)
Get value and then ADD atomically.
Definition: atomic_sync.hh:103
value_type fetch_and(value_type c) volatile
Get value and then AND atomically.
Definition: atomic_sync.hh:203
value_type add_fetch(value_type c) volatile
ADD and then get value atomically.
Definition: atomic_sync.hh:113
value_type fetch_add(value_type c) volatile
Get value and then ADD atomically.
Definition: atomic_sync.hh:95
value_type sub_fetch(value_type c) volatile
SUBTRACT and then get value atomically.
Definition: atomic_sync.hh:149