RAUL 0.8.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 00029 class AtomicInt { 00030 public: 00031 inline AtomicInt(int val) 00032 { g_atomic_int_set(static_cast<volatile gint*>(&_val), val); } 00033 00034 inline AtomicInt(const AtomicInt& copy) 00035 { g_atomic_int_set(static_cast<volatile gint*>(&_val), copy.get()); } 00036 00037 inline int get() const 00038 { return g_atomic_int_get(static_cast<volatile gint*>(&_val)); } 00039 00040 inline void operator=(int val) 00041 { g_atomic_int_set(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 void operator-=(int val) 00047 { g_atomic_int_add(static_cast<volatile gint*>(&_val), -val); } 00048 00049 inline bool operator==(int val) const 00050 { return get() == val; } 00051 00052 inline int operator+(int val) const 00053 { return get() + val; } 00054 00055 inline AtomicInt& operator++() // prefix 00056 { g_atomic_int_inc(static_cast<volatile gint*>(&_val)); return *this; } 00057 00058 inline AtomicInt& operator--() // prefix 00059 { g_atomic_int_add(static_cast<volatile gint*>(&_val), -1); return *this; } 00060 00064 inline bool compare_and_exchange(int old, int val) 00065 { return g_atomic_int_compare_and_exchange(static_cast<volatile gint*>(&_val), old, val); } 00066 00070 inline int exchange_and_add(int val) 00071 { return g_atomic_int_exchange_and_add(static_cast<volatile gint*>(&_val), val); } 00072 00076 inline bool decrement_and_test() 00077 { return g_atomic_int_dec_and_test(static_cast<volatile gint*>(&_val)); } 00078 00079 private: 00080 volatile mutable int _val; 00081 }; 00082 00083 00084 } // namespace Raul 00085 00086 #endif // RAUL_ATOMIC_INT_HPP