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 
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++() 
00056         { g_atomic_int_inc(static_cast<volatile gint*>(&_val)); return *this; }
00057 
00058     inline AtomicInt& operator--() 
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 } 
00085 
00086 #endif // RAUL_ATOMIC_INT_HPP