Ardour  9.0-pre0-350-gf17a656217
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 "pbd/atomic.h"
24 
25 #include <atomic>
26 
27 #include <glib.h>
28 
29 namespace PBD {
30 
32 {
38 
39 public:
40 
41  atomic_counter (gint value = 0)
42  {
43  m_value.store (value);
44  }
45 
46  gint get() const
47  {
48  return m_value.load ();
49  }
50 
51  void set (gint new_value)
52  {
53  m_value.store (new_value);
54  }
55 
56  void increment ()
57  {
58  m_value.fetch_add (1);
59  }
60 
61  void operator++ ()
62  {
63  increment ();
64  }
65 
67  {
69  }
70 
71  bool operator-- ()
72  {
73  return decrement_and_test ();
74  }
75 
76  bool compare_and_exchange (gint old_value, gint new_value)
77  {
78  return m_value.compare_exchange_strong (old_value, new_value);
79  }
80 
84  bool cas (gint old_value, gint new_value)
85  {
86  return compare_and_exchange (old_value, new_value);
87  }
88 
89 private:
90  mutable std::atomic<int> m_value;
91 };
92 
93 } // namespace PBD
94 
95 #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:26