RAUL  0.5.1
AtomicInt.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_ATOMIC_INT_HPP
19 #define RAUL_ATOMIC_INT_HPP
20 
21 #include <glib.h>
22 
23 namespace Raul {
24 
25 
26 class AtomicInt {
27 public:
28 
29  inline AtomicInt(int val)
30  { g_atomic_int_set(&_val, val); }
31 
32  inline AtomicInt(const AtomicInt& copy)
33  { g_atomic_int_set(&_val, copy.get()); }
34 
35  inline int get() const
36  { return g_atomic_int_get(&_val); }
37 
38  inline void operator=(int val)
39  { g_atomic_int_set(&_val, val); }
40 
41  inline void operator+=(int val)
42  { g_atomic_int_add(&_val, val); }
43 
44  inline void operator-=(int val)
45  { g_atomic_int_add(&_val, -val); }
46 
47  inline bool operator==(int val) const
48  { return get() == val; }
49 
50  inline int operator+(int val) const
51  { return get() + val; }
52 
53  inline AtomicInt& operator++() // prefix
54  { g_atomic_int_inc(&_val); return *this; }
55 
56  inline AtomicInt& operator--() // prefix
57  { g_atomic_int_add(&_val, -1); return *this; }
58 
62  inline bool compare_and_exchange(int oldval, int newval)
63  { return g_atomic_int_compare_and_exchange(&_val, oldval, newval); }
64 
68  inline int exchange_and_add(int val)
69  { return g_atomic_int_exchange_and_add(&_val, val); }
70 
74  inline bool decrement_and_test()
75  { return g_atomic_int_dec_and_test(&_val); }
76 
77 private:
78  volatile int _val;
79 };
80 
81 
82 } // namespace Raul
83 
84 #endif // RAUL_ATOMIC_INT_HPP
Definition: Array.hpp:26