00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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(static_cast<volatile gint*>(&_val), (gint)val); }
00031
00032 inline AtomicInt(const AtomicInt& copy)
00033 { g_atomic_int_set(static_cast<volatile gint*>(&_val), (gint)copy.get()); }
00034
00035 inline int get() const
00036 { return g_atomic_int_get(static_cast<volatile gint*>(&_val)); }
00037
00038 inline void operator=(int val)
00039 { g_atomic_int_set(static_cast<volatile gint*>(&_val), (gint)val); }
00040
00041 inline void operator+=(int val)
00042 { g_atomic_int_add(static_cast<volatile gint*>(&_val), (gint)val); }
00043
00044 inline void operator-=(int val)
00045 { g_atomic_int_add(static_cast<volatile gint*>(&_val), (gint)-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++()
00054 { g_atomic_int_inc(static_cast<volatile gint*>(&_val)); return *this; }
00055
00056 inline AtomicInt& operator--()
00057 { g_atomic_int_add(static_cast<volatile gint*>(&_val), -1); return *this; }
00058
00062 inline bool compare_and_exchange(int old, int val)
00063 { return g_atomic_int_compare_and_exchange(static_cast<volatile gint*>(&_val), old, val); }
00064
00068 inline int exchange_and_add(int val)
00069 { return g_atomic_int_exchange_and_add(static_cast<volatile gint*>(&_val), val); }
00070
00074 inline bool decrement_and_test()
00075 { return g_atomic_int_dec_and_test(static_cast<volatile gint*>(&_val)); }
00076
00077 private:
00078 volatile mutable int _val;
00079 };
00080
00081
00082 }
00083
00084 #endif // RAUL_ATOMIC_INT_HPP