/builddir/build/BUILD/raul-0.4.0/raul/AtomicInt.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_ATOMIC_INT_HPP
00019 #define RAUL_ATOMIC_INT_HPP
00020 
00021 #include <glib.h>
00022 
00023 namespace Raul {
00024 
00025 
00026 class AtomicInt {
00027 public:
00028     
00029     inline AtomicInt(int val)
00030         { g_atomic_int_set(&_val, val); }
00031 
00032     inline AtomicInt(const AtomicInt& copy)
00033         { g_atomic_int_set(&_val, copy.get()); }
00034     
00035     inline int get() const
00036         { return g_atomic_int_get(&_val); }
00037 
00038     inline void operator=(int val)
00039         { g_atomic_int_set(&_val, val); }
00040 
00041     inline void operator+=(int val)
00042         { g_atomic_int_add(&_val, val); }
00043     
00044     inline void operator-=(int val)
00045         { g_atomic_int_add(&_val, -val); }
00046     
00047     inline bool operator==(int val) const
00048         { return get() == val; }
00049 
00050     inline int operator+(int val) const
00051         { return get() + val; }
00052 
00053     inline AtomicInt& operator++() // prefix
00054         { g_atomic_int_inc(&_val); return *this; }
00055     
00056     inline AtomicInt& operator--() // prefix
00057         { g_atomic_int_add(&_val, -1); return *this; }
00058     
00062     inline bool compare_and_exchange(int oldval, int newval)
00063         { return g_atomic_int_compare_and_exchange(&_val, oldval, newval); }
00064 
00068     inline int exchange_and_add(int val)
00069         { return g_atomic_int_exchange_and_add(&_val, val); }
00070 
00074     inline bool decrement_and_test()
00075         { return g_atomic_int_dec_and_test(&_val); }
00076 
00077 private:
00078     volatile int _val;
00079 };
00080 
00081 
00082 } // namespace Raul
00083 
00084 #endif // RAUL_ATOMIC_INT_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1