strusBase  0.17
atomic.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_ATOMIC_HPP_INCLUDED
10 #define _STRUS_ATOMIC_HPP_INCLUDED
11 
13 #if defined __GNUC__
14 #define STRUS_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
15 #endif
16 
17 #undef STRUS_USE_STD_ATOMIC
18 #if __cplusplus >= 201103L
19 #if defined __clang__
20 #define STRUS_USE_STD_ATOMIC
21 #elif defined __GNUC__
22 #if STRUS_GCC_VERSION >= 40900
23 #define STRUS_USE_STD_ATOMIC
24 #endif // STRUS_GCC_VERSION
25 #endif // __clang__
26 #endif // __cplusplus
27 
28 #ifdef STRUS_USE_STD_ATOMIC
29 #include <memory>
30 #include <atomic>
31 
32 namespace strus {
33 
34 template <class X>
35 class atomic
36  :public std::atomic<X>
37 {
38 public:
39  atomic( X value)
40  :std::atomic<X>(value){}
41  atomic( const atomic& o)
42  :std::atomic<X>(o){}
43 };
44 
45 }//namespace
46 
47 #define STRUS_MEMORY_ORDER_ATOMIC_COUNTER std::memory_order_acquire
48 
49 #else //STRUS_USE_STD_ATOMIC
50 #include <boost/atomic.hpp>
51 
52 namespace strus {
53 
54 template <class X>
55 class atomic
56  :public boost::atomic<X>
57 {
58 public:
59  atomic( X value)
60  :boost::atomic<X>(value){}
61  atomic( const atomic& o)
62  :boost::atomic<X>(o){}
63 };
64 
65 }//namespace
66 
67 #define STRUS_MEMORY_ORDER_ATOMIC_COUNTER boost::memory_order_acquire
68 
69 #endif //STRUS_USE_STD_ATOMIC
70 
71 namespace strus {
72 
73 template <typename IntegralCounterType>
75  :public strus::atomic<IntegralCounterType>
76 {
77 public:
79  AtomicCounter( IntegralCounterType initialValue_=0)
80  :strus::atomic<IntegralCounterType>(initialValue_)
81  {}
82 
84  void increment( IntegralCounterType val = 1)
85  {
87  }
88 
90  void decrement( IntegralCounterType val = 1)
91  {
93  }
94 
97  IntegralCounterType allocIncrement( IntegralCounterType val = 1)
98  {
100  }
101 
104  IntegralCounterType value() const
105  {
107  }
108 
111  void set( const IntegralCounterType& val)
112  {
114  }
115 
120  bool test_and_set( IntegralCounterType testval, IntegralCounterType newval)
121  {
123  }
124 };
125 
127  :public strus::atomic<bool>
128 {
129 public:
131  AtomicFlag( bool initialValue_=false)
132  :strus::atomic<bool>(initialValue_)
133  {}
134 
137  bool set( bool val)
138  {
139  bool prev_val = !val;
141  }
142 
145  bool test() const
146  {
148  }
149 };
150 
151 }//namespace
152 #endif
153 
void set(const IntegralCounterType &val)
Initialization of the counter.
Definition: atomic.hpp:111
bool set(bool val)
Set the flag, if the new value changes the current value.
Definition: atomic.hpp:137
Definition: atomic.hpp:74
atomic(const atomic &o)
Definition: atomic.hpp:61
void decrement(IntegralCounterType val=1)
Decrement of the counter.
Definition: atomic.hpp:90
IntegralCounterType allocIncrement(IntegralCounterType val=1)
Increment of the counter.
Definition: atomic.hpp:97
atomic(X value)
Definition: atomic.hpp:59
bool test() const
Evaluate the current value.
Definition: atomic.hpp:145
bool test_and_set(IntegralCounterType testval, IntegralCounterType newval)
Compare current value with 'testval', change it to 'newval' if matches.
Definition: atomic.hpp:120
Definition: atomic.hpp:126
#define STRUS_MEMORY_ORDER_ATOMIC_COUNTER
Definition: atomic.hpp:67
Definition: atomic.hpp:55
IntegralCounterType value() const
Increment of the counter.
Definition: atomic.hpp:104
AtomicCounter(IntegralCounterType initialValue_=0)
Constructor.
Definition: atomic.hpp:79
AtomicFlag(bool initialValue_=false)
Constructor.
Definition: atomic.hpp:131
void increment(IntegralCounterType val=1)
Increment of the counter.
Definition: atomic.hpp:84