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 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++()
00053 { g_atomic_int_inc(static_cast<volatile gint*>(&_val)); return *this; }
00054
00055 inline AtomicInt& operator--()
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 }
00082
00083 #endif // RAUL_ATOMIC_INT_HPP