BDS Public
Beam-lib  2.16.3
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 #ifndef BAtomicCount_H
8 #define BAtomicCount_H 1
9 
10 #if TARGET_vdc
11 class BAtomicCount {
13 public:
14  BAtomicCount(long value = 0) : ovalue(value){}
15 
16  long getValue() const {
17  return __sync_fetch_and_add(&ovalue, 0);
18  }
19  long add(long value){
20  return __sync_fetch_and_add(&ovalue, value) + value;
21  }
22  long operator++(int){
23  return __sync_fetch_and_add(&ovalue, 1);
24  }
25  long operator++(){
26  return __sync_fetch_and_add(&ovalue, 1) + 1;
27  }
28  long operator--(int){
29  return __sync_fetch_and_add(&ovalue, -1);
30  }
31  long operator--(){
32  return __sync_fetch_and_add(&ovalue, -1) - 1;
33  }
34  operator long() const {
35  return getValue();
36  }
37 private:
38  mutable long ovalue;
39 };
40 #else
41 #if __GNUC__ >= 5 || __GNUC_MINOR__ >= 4
42 #include <ext/atomicity.h>
43 #else
44 #include <bits/atomicity.h>
45 #endif
46 
48 class BAtomicCount {
49 public:
50  BAtomicCount(long value = 0) : ovalue(value){}
51 
52  long getValue() const {
53  return __gnu_cxx::__exchange_and_add(&ovalue, 0);
54  }
55  long add(long value){
56  return __gnu_cxx::__exchange_and_add(&ovalue, value) + value;
57  }
58  long operator++(int){
59  return __gnu_cxx::__exchange_and_add(&ovalue, 1);
60  }
61  long operator++(){
62  return __gnu_cxx::__exchange_and_add(&ovalue, 1) + 1;
63  }
64  long operator--(int){
65  return __gnu_cxx::__exchange_and_add(&ovalue, -1);
66  }
67  long operator--(){
68  return __gnu_cxx::__exchange_and_add(&ovalue, -1) - 1;
69  }
70  operator long() const {
71  return getValue();
72  }
73 private:
74  mutable _Atomic_word ovalue;
75 };
76 #endif
77 
78 #endif
long operator--()
Definition: BAtomicCount.h:67
BAtomicCount(long value=0)
Definition: BAtomicCount.h:50
long operator++(int)
Definition: BAtomicCount.h:58
long operator++()
Definition: BAtomicCount.h:61
BAtomicCount class.
Definition: BAtomicCount.h:48
long add(long value)
Definition: BAtomicCount.h:55
long getValue() const
Definition: BAtomicCount.h:52
long operator--(int)
Definition: BAtomicCount.h:64