Ardour  8.12
atomic_counter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Tim Mayberry <mojofunk@gmail.com>
3  * Copyright (C) 2013-2015 Paul Davis <paul@linuxaudiosystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef PBD_ATOMIC_COUNTER_H
21 #define PBD_ATOMIC_COUNTER_H
22 
23 #include <atomic>
24 
25 #include <glib.h>
26 
27 namespace PBD {
28 
30 {
36 
37 public:
38 
39  atomic_counter (gint value = 0)
40  {
41  m_value.store (value);
42  }
43 
44  gint get() const
45  {
46  return m_value.load ();
47  }
48 
49  void set (gint new_value)
50  {
51  m_value.store (new_value);
52  }
53 
54  void increment ()
55  {
56  m_value.fetch_add (1);
57  }
58 
59  void operator++ ()
60  {
61  increment ();
62  }
63 
65  {
67  }
68 
69  bool operator-- ()
70  {
71  return decrement_and_test ();
72  }
73 
74  bool compare_and_exchange (gint old_value, gint new_value)
75  {
76  return m_value.compare_exchange_strong (old_value, new_value);
77  }
78 
82  bool cas (gint old_value, gint new_value)
83  {
84  return compare_and_exchange (old_value, new_value);
85  }
86 
87 private:
88  mutable std::atomic<int> m_value;
89 };
90 
91 } // namespace PBD
92 
93 #endif // PBD_ATOMIC_COUNTER_H
bool compare_and_exchange(gint old_value, gint new_value)
bool cas(gint old_value, gint new_value)
atomic_counter & operator=(const atomic_counter &)
void set(gint new_value)
std::atomic< int > m_value
atomic_counter(gint value=0)
atomic_counter(const atomic_counter &)
Definition: axis_view.h:42
bool atomic_dec_and_test(std::atomic< T > &aval)
Definition: atomic.h:27