ardour
atomic_counter.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Tim Mayberry
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #ifndef PBD_ATOMIC_COUNTER_H
21 #define PBD_ATOMIC_COUNTER_H
22 
23 #include <glib.h>
24 
25 namespace PBD {
26 
28 {
34 
35 public:
36 
37  atomic_counter (gint value = 0)
38  :
39  m_value(value)
40  { }
41 
42  gint get() const
43  {
44  return g_atomic_int_get (&m_value);
45  }
46 
47  void set (gint new_value)
48  {
49  g_atomic_int_set (&m_value, new_value);
50  }
51 
52  void increment ()
53  {
54  g_atomic_int_inc (&m_value);
55  }
56 
57  void operator++ ()
58  {
59  increment ();
60  }
61 
63  {
64  return g_atomic_int_dec_and_test (&m_value);
65  }
66 
67  bool operator-- ()
68  {
69  return decrement_and_test ();
70  }
71 
72  bool compare_and_exchange (gint old_value, gint new_value)
73  {
74  return g_atomic_int_compare_and_exchange
75  (
76  &m_value,
77  old_value,
78  new_value
79  );
80  }
81 
85  bool cas (gint old_value, gint new_value)
86  {
87  return compare_and_exchange (old_value, new_value);
88  }
89 
90 private:
91 
92  // Has to be mutable when using the apple version of gcc.
93  mutable volatile gint m_value;
94 
95 };
96 
97 } // namespace PBD
98 
99 #endif // PBD_ATOMIC_COUNTER_H
atomic_counter(gint value=0)
bool cas(gint old_value, gint new_value)
void set(gint new_value)
bool compare_and_exchange(gint old_value, gint new_value)
Definition: debug.h:30
atomic_counter & operator=(const atomic_counter &)
volatile gint m_value
atomic_counter(const atomic_counter &)