00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00032
00033 #ifndef GROOVX_RUTZ_ATOMIC_MUTEX_H_UTC20070412044614_DEFINED
00034 #define GROOVX_RUTZ_ATOMIC_MUTEX_H_UTC20070412044614_DEFINED
00035
00036 #include <limits>
00037 #include <pthread.h>
00038
00039 namespace rutz
00040 {
00041
00043 class mutex_atomic_int
00044 {
00045 private:
00046 int x;
00047 pthread_mutex_t mut;
00048
00049 mutex_atomic_int(const mutex_atomic_int&);
00050 mutex_atomic_int& operator=(const mutex_atomic_int&);
00051
00052 public:
00054 mutex_atomic_int() : x(0)
00055 { pthread_mutex_init(&mut, NULL); }
00056
00058 static int max_value() { return std::numeric_limits<int>::max(); }
00059
00061 int atomic_get() const
00062 { return x; }
00063
00065 void atomic_set(int v)
00066 { pthread_mutex_lock(&mut); x = v; pthread_mutex_unlock(&mut); }
00067
00069 void atomic_add(int i)
00070 { pthread_mutex_lock(&mut); x += i; pthread_mutex_unlock(&mut); }
00071
00073 void atomic_sub(int i)
00074 { pthread_mutex_lock(&mut); x -= i; pthread_mutex_unlock(&mut); }
00075
00077 bool atomic_sub_test_zero(int i)
00078 {
00079 bool ret;
00080 pthread_mutex_lock(&mut);
00081 ret = bool((x -= i) == 0);
00082 pthread_mutex_unlock(&mut);
00083 return ret;
00084 }
00085
00087 void atomic_incr()
00088 { pthread_mutex_lock(&mut); ++x; pthread_mutex_unlock(&mut); }
00089
00091 void atomic_decr()
00092 { pthread_mutex_lock(&mut); --x; pthread_mutex_unlock(&mut); }
00093
00095 bool atomic_decr_test_zero()
00096 {
00097 bool ret;
00098 pthread_mutex_lock(&mut);
00099 ret = bool(--x == 0);
00100 pthread_mutex_unlock(&mut);
00101 return ret;
00102 }
00103
00105 bool atomic_incr_test_zero()
00106 {
00107 bool ret;
00108 pthread_mutex_lock(&mut);
00109 ret = bool(++x == 0);
00110 pthread_mutex_unlock(&mut);
00111 return ret;
00112 }
00113
00115 int atomic_add_return(int i)
00116 {
00117 int ret;
00118 pthread_mutex_lock(&mut);
00119 ret = (x += i);
00120 pthread_mutex_unlock(&mut);
00121 return ret;
00122 }
00123
00125 int atomic_sub_return(int i)
00126 {
00127 int ret;
00128 pthread_mutex_lock(&mut);
00129 ret = (x -= i);
00130 pthread_mutex_unlock(&mut);
00131 return ret;
00132 }
00133
00135 int atomic_incr_return()
00136 {
00137 int ret;
00138 pthread_mutex_lock(&mut);
00139 ret = ++x;
00140 pthread_mutex_unlock(&mut);
00141 return ret;
00142 }
00143
00145 int atomic_decr_return()
00146 {
00147 int ret;
00148 pthread_mutex_lock(&mut);
00149 ret = --x;
00150 pthread_mutex_unlock(&mut);
00151 return ret;
00152 }
00153 };
00154
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 static const char __attribute__((used)) vcid_groovx_rutz_atomic_mutex_h_utc20070412044614[] = "$Id: atomic_mutex.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00165 #endif // !GROOVX_RUTZ_ATOMIC_MUTEX_H_UTC20070412044614DEFINED