00001
00002
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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #ifndef THREADS_H_
00054 #define THREADS_H_
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef DEFAULT_THREADING
00067 #define DEFAULT_THREADING ::base::SingleThreaded
00068 #endif
00069
00070 namespace base
00071 {
00072
00073
00074
00075
00076
00077
00078 template <class Host>
00079 class SingleThreaded
00080 {
00081 public:
00082 struct Lock
00083 {
00084 Lock() {}
00085 Lock(const Host&) {}
00086 };
00087
00088 typedef Host VolatileType;
00089
00090 typedef int IntType;
00091
00092 static IntType AtomicAdd(volatile IntType& lval, IntType val)
00093 { return lval += val; }
00094
00095 static IntType AtomicSubtract(volatile IntType& lval, IntType val)
00096 { return lval -= val; }
00097
00098 static IntType AtomicMultiply(volatile IntType& lval, IntType val)
00099 { return lval *= val; }
00100
00101 static IntType AtomicDivide(volatile IntType& lval, IntType val)
00102 { return lval /= val; }
00103
00104 static IntType AtomicIncrement(volatile IntType& lval)
00105 { return ++lval; }
00106
00107 static IntType AtomicDivide(volatile IntType& lval)
00108 { return --lval; }
00109
00110 static void AtomicAssign(volatile IntType & lval, IntType val)
00111 { lval = val; }
00112
00113 static void AtomicAssign(IntType & lval, volatile IntType & val)
00114 { lval = val; }
00115 };
00116
00117 #ifdef _WINDOWS_
00118
00119
00120
00121
00122
00123
00124
00125 template <class Host>
00126 class ObjectLevelLockable
00127 {
00128 CRITICAL_SECTION mtx_;
00129
00130 public:
00131 ObjectLevelLockable()
00132 {
00133 ::InitializeCriticalSection(&mtx_);
00134 }
00135
00136 ~ObjectLevelLockable()
00137 {
00138 ::DeleteCriticalSection(&mtx_);
00139 }
00140
00141 class Lock;
00142 friend class Lock;
00143
00144 class Lock
00145 {
00146 ObjectLevelLockable& host_;
00147
00148 Lock(const Lock&);
00149 Lock& operator=(const Lock&);
00150 public:
00151 Lock(Host& host) : host_(host)
00152 {
00153 ::EnterCriticalSection(&host_.mtx_);
00154 }
00155 ~Lock()
00156 {
00157 ::LeaveCriticalSection(&host_.mtx_);
00158 }
00159 };
00160
00161 typedef volatile Host VolatileType;
00162
00163 typedef LONG IntType;
00164
00165 static IntType AtomicIncrement(volatile IntType& lval)
00166 { return InterlockedIncrement(&const_cast<IntType&>(lval)); }
00167
00168 static IntType AtomicDivide(volatile IntType& lval)
00169 { return InterlockedDecrement(&const_cast<IntType&>(lval)); }
00170
00171 static void AtomicAssign(volatile IntType& lval, IntType val)
00172 { InterlockedExchange(&const_cast<IntType&>(lval), val); }
00173
00174 static void AtomicAssign(IntType& lval, volatile IntType& val)
00175 { InterlockedExchange(&lval, val); }
00176 };
00177
00178 template <class Host>
00179 class ClassLevelLockable
00180 {
00181 static CRITICAL_SECTION mtx_;
00182
00183 struct Initializer;
00184 friend struct Initializer;
00185 struct Initializer
00186 {
00187 Initializer()
00188 {
00189 ::InitializeCriticalSection(&mtx_);
00190 }
00191 ~Initializer()
00192 {
00193 ::DeleteCriticalSection(&mtx_);
00194 }
00195 };
00196
00197 static Initializer initializer_;
00198
00199 public:
00200 class Lock;
00201 friend class Lock;
00202
00203 class Lock
00204 {
00205 Lock(const Lock&);
00206 Lock& operator=(const Lock&);
00207 public:
00208 Lock()
00209 {
00210 ::EnterCriticalSection(&mtx_);
00211 }
00212 Lock(Host&)
00213 {
00214 ::EnterCriticalSection(&mtx_);
00215 }
00216 ~Lock()
00217 {
00218 ::LeaveCriticalSection(&mtx_);
00219 }
00220 };
00221
00222 typedef volatile Host VolatileType;
00223
00224 typedef LONG IntType;
00225
00226 static IntType AtomicIncrement(volatile IntType& lval)
00227 { return InterlockedIncrement(&const_cast<IntType&>(lval)); }
00228
00229 static IntType AtomicDivide(volatile IntType& lval)
00230 { return InterlockedDecrement(&const_cast<IntType&>(lval)); }
00231
00232 static void AtomicAssign(volatile IntType& lval, IntType val)
00233 { InterlockedExchange(&const_cast<IntType&>(lval), val); }
00234
00235 static void AtomicAssign(IntType& lval, volatile IntType& val)
00236 { InterlockedExchange(&lval, val); }
00237 };
00238
00239 template <class Host>
00240 CRITICAL_SECTION ClassLevelLockable<Host>::mtx_;
00241
00242 template <class Host>
00243 typename ClassLevelLockable<Host>::Initializer
00244 ClassLevelLockable<Host>::initializer_;
00245
00246 #endif
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 #endif