RAUL
0.7.0
|
00001 /* This file is part of Raul. 00002 * Copyright (C) 2007-2009 David 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 inline AtomicInt(int val) 00029 { g_atomic_int_set(static_cast<volatile gint*>(&_val), val); } 00030 00031 inline AtomicInt(const AtomicInt& copy) 00032 { g_atomic_int_set(static_cast<volatile gint*>(&_val), copy.get()); } 00033 00034 inline int get() const 00035 { return g_atomic_int_get(static_cast<volatile gint*>(&_val)); } 00036 00037 inline void operator=(int val) 00038 { g_atomic_int_set(static_cast<volatile gint*>(&_val), val); } 00039 00040 inline void operator+=(int val) 00041 { g_atomic_int_add(static_cast<volatile gint*>(&_val), val); } 00042 00043 inline void operator-=(int val) 00044 { g_atomic_int_add(static_cast<volatile gint*>(&_val), -val); } 00045 00046 inline bool operator==(int val) const 00047 { return get() == val; } 00048 00049 inline int operator+(int val) const 00050 { return get() + val; } 00051 00052 inline AtomicInt& operator++() // prefix 00053 { g_atomic_int_inc(static_cast<volatile gint*>(&_val)); return *this; } 00054 00055 inline AtomicInt& operator--() // prefix 00056 { g_atomic_int_add(static_cast<volatile gint*>(&_val), -1); return *this; } 00057 00061 inline bool compare_and_exchange(int old, int val) 00062 { return g_atomic_int_compare_and_exchange(static_cast<volatile gint*>(&_val), old, val); } 00063 00067 inline int exchange_and_add(int val) 00068 { return g_atomic_int_exchange_and_add(static_cast<volatile gint*>(&_val), val); } 00069 00073 inline bool decrement_and_test() 00074 { return g_atomic_int_dec_and_test(static_cast<volatile gint*>(&_val)); } 00075 00076 private: 00077 volatile mutable int _val; 00078 }; 00079 00080 00081 } // namespace Raul 00082 00083 #endif // RAUL_ATOMIC_INT_HPP