2 Copyright (C) 2010 Tim Mayberry
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.
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.
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.
20 #ifndef PBD_ATOMIC_COUNTER_H
21 #define PBD_ATOMIC_COUNTER_H
30 * Prevent copying and assignment
32 atomic_counter (const atomic_counter&);
33 atomic_counter& operator= (const atomic_counter&);
37 atomic_counter (gint value = 0)
44 return g_atomic_int_get (&m_value);
47 void set (gint new_value)
49 g_atomic_int_set (&m_value, new_value);
54 g_atomic_int_inc (&m_value);
62 bool decrement_and_test ()
64 return g_atomic_int_dec_and_test (&m_value);
69 return decrement_and_test ();
72 bool compare_and_exchange (gint old_value, gint new_value)
74 return g_atomic_int_compare_and_exchange
83 * convenience method, \see compare_and_exchange
85 bool cas (gint old_value, gint new_value)
87 return compare_and_exchange (old_value, new_value);
92 // Has to be mutable when using the apple version of gcc.
93 mutable volatile gint m_value;
99 #endif // PBD_ATOMIC_COUNTER_H