00001 #ifndef QPID_SYS_ATOMICVALUE_GCC_H
00002 #define QPID_SYS_ATOMICVALUE_GCC_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #if !defined(QPID_SYS_ATOMICVALUE_H)
00026 #error "This file should only be included via AtomicValue.h."
00027 #endif
00028
00029 namespace qpid {
00030 namespace sys {
00031
00036 template <class T>
00037 class AtomicValue
00038 {
00039 public:
00040 AtomicValue(T init=0) : value(init) {}
00041
00042
00043 inline T operator+=(T n) { return __sync_add_and_fetch(&value, n); }
00044 inline T operator-=(T n) { return __sync_sub_and_fetch(&value, n); }
00045 inline T operator++() { return *this += 1; }
00046 inline T operator--() { return *this -= 1; }
00047
00048
00049 inline T fetchAndAdd(T n) { return __sync_fetch_and_add(&value, n); }
00050 inline T fetchAndSub(T n) { return __sync_fetch_and_sub(&value, n); }
00051 inline T operator++(int) { return fetchAndAdd(1); }
00052 inline T operator--(int) { return fetchAndSub(1); }
00053
00055 T valueCompareAndSwap(T testval, T newval) { return __sync_val_compare_and_swap(&value, testval, newval); }
00056
00058 bool boolCompareAndSwap(T testval, T newval) { return __sync_bool_compare_and_swap(&value, testval, newval); }
00059
00060 T get() const { return const_cast<AtomicValue<T>*>(this)->fetchAndAdd(static_cast<T>(0)); }
00061
00062 private:
00063 T value;
00064 };
00065
00066 }}
00067
00068 #endif