Go to the documentation of this file.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 #ifndef __CXUTILS_MUTEX_H
00042 #define __CXUTILS_MUTEX_H
00043
00044
00045 #ifdef WIN32
00046
00047 #else
00048 #include <sys/sem.h>
00049 #include <sys/types.h>
00050 #include <sys/stat.h>
00051 #include <unistd.h>
00052 #include <pthread.h>
00053 #endif
00054
00055 #include <string>
00056 #include "cxbase.h"
00057
00058 #ifndef INFINITE
00059 #define INFINITE 0xFFFFFFFF
00060 #endif
00061
00062 namespace CxUtils
00063 {
00075 class CX_UTILS_DLL Mutex
00076 {
00077 public:
00091 template <class T>
00092 class ScopedPtr
00093 {
00094 public:
00106 ScopedPtr(T* data, const Mutex* mutex, const bool forceUnlock = false) : mpData(data),
00107 mpNullSafety(NULL),
00108 mpMutex(mutex),
00109 mUnlockFlag(true)
00110
00111 {
00112 if(mpMutex && mpData)
00113 {
00114 if(mpMutex->IsLocked())
00115 {
00116 if(forceUnlock == false)
00117 {
00118 mUnlockFlag = false;
00119 }
00120 }
00121 else
00122 {
00123 mpMutex->Lock();
00124 }
00125 }
00126 }
00127 virtual ~ScopedPtr()
00128 {
00129 if(mpMutex && mpData)
00130 {
00131 mpData = NULL;
00132 if(mUnlockFlag)
00133 {
00134 mpMutex->Unlock();
00135 }
00136 }
00137 if(mpNullSafety)
00138 {
00139 delete mpNullSafety;
00140 mpNullSafety = NULL;
00141 }
00142 }
00143
00144 inline T* operator->() { return Data(); }
00145 inline const T* operator->() const { return Data(); }
00146 T* Data()
00147 {
00148 if(mpMutex && mpData)
00149 {
00150 return mpData;
00151 }
00152 else
00153 {
00154 if(NULL == mpNullSafety)
00155 {
00156 mpNullSafety = new T();
00157 }
00158 return mpNullSafety;
00159 }
00160 }
00161 const T* Data() const
00162 {
00163 if(mpMutex && mpData)
00164 {
00165 return mpData;
00166 }
00167 else
00168 {
00169 if(NULL == mpNullSafety)
00170 {
00171 T** temp = ((T**)(&mpNullSafety));
00172 *temp = new T();
00173 }
00174 return mpNullSafety;
00175 }
00176 }
00177
00178 bool IsLocked() const
00179 {
00180 if(mpMutex)
00181 {
00182 return mpMutex->IsLocked();
00183 }
00184 return false;
00185 }
00186
00187 bool IsValid() const
00188 {
00189 if(mpMutex && mpData)
00190 {
00191 return true;
00192 }
00193 return false;
00194 }
00195 bool operator==(const T* x) const
00196 {
00197 return mpData == x;
00198 }
00199 bool operator!=(const T* x) const
00200 {
00201 return mpData != x;
00202 }
00203 private:
00204 ScopedPtr<T>& operator=(const ScopedPtr<T>& ptr) { return *this; }
00205 T* mpData;
00206 T* mpNullSafety;
00207 const Mutex* mpMutex;
00208 bool mUnlockFlag;
00209 };
00216 template <class T>
00217 class CScopedPtr
00218 {
00219 public:
00231 CScopedPtr(const T* data, const Mutex* mutex, const bool forceUnlock = false) : mpData(data),
00232 mpNullSafety(NULL),
00233 mpMutex(mutex),
00234 mUnlockFlag(true)
00235
00236 {
00237 if(mpMutex && mpData)
00238 {
00239 if(mpMutex->IsLocked())
00240 {
00241 if(forceUnlock == false)
00242 {
00243 mUnlockFlag = false;
00244 }
00245 }
00246 else
00247 {
00248 mpMutex->Lock();
00249 }
00250 }
00251 }
00252 virtual ~CScopedPtr()
00253 {
00254 if(mpMutex && mpData)
00255 {
00256 mpData = NULL;
00257 if(mUnlockFlag)
00258 {
00259 mpMutex->Unlock();
00260 }
00261 }
00262 if(mpNullSafety)
00263 {
00264 delete mpNullSafety;
00265 }
00266 }
00267
00268 inline const T* operator->() const { return Data(); }
00269
00270 const T* Data() const
00271 {
00272 if(mpMutex && mpData)
00273 {
00274 return mpData;
00275 }
00276 else
00277 {
00278 if(NULL == mpNullSafety)
00279 {
00280 T** temp = ((T**)(&mpNullSafety));
00281 *temp = new T();
00282 }
00283 return mpNullSafety;
00284 }
00285 }
00286
00287 bool IsLocked() const
00288 {
00289 if(mpMutex)
00290 {
00291 return mpMutex->IsLocked();
00292 }
00293 return false;
00294 }
00295
00296 bool IsValid() const
00297 {
00298 if(mpMutex && mpData)
00299 {
00300 return true;
00301 }
00302 return false;
00303 }
00304 bool operator==(const T* x) const
00305 {
00306 return mpData == x;
00307 }
00308 bool operator!=(const T* x) const
00309 {
00310 return mpData != x;
00311 }
00312 private:
00313 const T* mpData;
00314 const T* mpNullSafety;
00315 const Mutex* mpMutex;
00316 bool mUnlockFlag;
00317 };
00330 class CX_UTILS_DLL ScopedLock
00331 {
00332 public:
00333 ScopedLock(const Mutex* mutex);
00334 ~ScopedLock();
00335 private:
00336 bool mUnlockFlag;
00337 const Mutex* mpMutex;
00338 };
00339 Mutex(const std::string& name = std::string());
00340 ~Mutex();
00341 void Create(const std::string& name);
00342 bool Lock(unsigned long wait = INFINITE) const;
00343 void Unlock() const;
00344 bool IsLocked() const;
00345 protected:
00346 #ifdef WIN32
00347 void* mMutex;
00348 #else
00349
00350
00351
00352
00353
00354
00355 union SemUnion
00356 {
00357 int mVal;
00358 struct semid_ds *mpBuff;
00359 unsigned short *mpArray;
00360 struct seminfo *mpBuffIPC;
00361 };
00362 bool mCreateFlag;
00363 std::string mName;
00364 int mSemID;
00365 key_t mSemKey;
00366 union Mutex::SemUnion mSemUnion;
00367 pthread_mutex_t mMutex;
00368 #endif
00369 #ifdef WIN32
00370 typedef unsigned long ID;
00371 #else
00372 typedef pthread_t ID;
00373 #endif
00374 volatile ID mThreadID;
00375 static unsigned int mCount;
00376
00377 };
00378
00379 }
00380
00381 #endif
00382