Beamlib  3.0.1
This is the Beam C++ class library.
BAtomicCount.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BAtomicCount.h BAtomicCount Atomic Counter
3  * T.Barnaby, BEAM Ltd, 2008-06-17
4  * Copyright (c) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5  *******************************************************************************
6  *
7  * For license see LICENSE.txt at the root of the beamlib source tree.
8  */
9 #ifndef BAtomicCount_H
10 #define BAtomicCount_H 1
11 
12 #if TARGET_vdc
14 class BAtomicCount {
15 public:
16  BAtomicCount(long value = 0) : ovalue(value){}
17 
18  long getValue() const {
19  return __sync_fetch_and_add(&ovalue, 0);
20  }
21  long add(long value){
22  return __sync_fetch_and_add(&ovalue, value) + value;
23  }
24  long operator++(int){
25  return __sync_fetch_and_add(&ovalue, 1);
26  }
27  long operator++(){
28  return __sync_fetch_and_add(&ovalue, 1) + 1;
29  }
30  long operator--(int){
31  return __sync_fetch_and_add(&ovalue, -1);
32  }
33  long operator--(){
34  return __sync_fetch_and_add(&ovalue, -1) - 1;
35  }
36  operator long() const {
37  return getValue();
38  }
39 private:
40  mutable long ovalue;
41 };
42 #else
43 #if __GNUC__ >= 5 || __GNUC_MINOR__ >= 4
44 #include <ext/atomicity.h>
45 #else
46 #include <bits/atomicity.h>
47 #endif
48 
50 class BAtomicCount {
51 public:
52  BAtomicCount(long value = 0) : ovalue(value){}
53 
54  long getValue() const {
55  return __gnu_cxx::__exchange_and_add(&ovalue, 0);
56  }
57  long add(long value){
58  return __gnu_cxx::__exchange_and_add(&ovalue, value) + value;
59  }
60  long operator++(int){
61  return __gnu_cxx::__exchange_and_add(&ovalue, 1);
62  }
63  long operator++(){
64  return __gnu_cxx::__exchange_and_add(&ovalue, 1) + 1;
65  }
66  long operator--(int){
67  return __gnu_cxx::__exchange_and_add(&ovalue, -1);
68  }
69  long operator--(){
70  return __gnu_cxx::__exchange_and_add(&ovalue, -1) - 1;
71  }
72  operator long() const {
73  return getValue();
74  }
75 private:
76  mutable _Atomic_word ovalue;
77 };
78 #endif
79 
80 #endif
BAtomicCount class.
Definition: BAtomicCount.h:50
long getValue() const
Definition: BAtomicCount.h:54
long operator--(int)
Definition: BAtomicCount.h:66
long operator++()
Definition: BAtomicCount.h:63
long operator++(int)
Definition: BAtomicCount.h:60
long add(long value)
Definition: BAtomicCount.h:57
long operator--()
Definition: BAtomicCount.h:69
BAtomicCount(long value=0)
Definition: BAtomicCount.h:52