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
00031 #ifndef GENAPI_GCSYNCH_H
00032 #define GENAPI_GCSYNCH_H
00033
00034 #include <Base/GCTypes.h>
00035 #include <Base/GCException.h>
00036
00037 #if defined (_WIN32)
00038 # include <windows.h>
00039 # include <winbase.h>
00040 #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
00041 # include <semaphore.h>
00042 # include <pthread.h>
00043 # include <errno.h>
00044 # include <list>
00045 #elif defined(VXWORKS)
00046 #include <vxworks.h>
00047 #include <taskLib.h>
00048 #else
00049 # error No/unknown platform thread support
00050 #endif
00051
00052 namespace GENICAM_NAMESPACE
00053 {
00054
00055
00056
00057
00058
00063 class GCBASE_API CLock
00064 {
00065 public:
00067 CLock();
00068
00070 ~CLock();
00071
00073 bool TryLock();
00074
00076 void Lock();
00077
00079 void Unlock();
00080
00081 private:
00083 CLock( const CLock& );
00084
00086 CLock& operator=( const CLock& );
00087
00088 protected:
00089
00090 #if defined (_WIN32)
00091
00092 CRITICAL_SECTION m_csObject;
00093 #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
00094
00095 pthread_mutex_t m_mtxObject;
00096 #elif defined(VXWORKS)
00097 SEM_ID m_sem;
00098 #else
00099 # error No/unknown platform thread support
00100 #endif
00101
00102 };
00103
00104
00108 class GCBASE_API CLockEx : public CLock
00109 {
00110 public:
00111
00112 # if defined (_WIN32)
00113
00115 int64_t GetLockCount();
00116
00118 int64_t GetRecursionCount();
00119
00120 # elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__) || defined(VXWORKS))
00121
00122 # else
00123 # error No/unknown platform support
00124 # endif
00125
00126 private:
00128 CLockEx( const CLockEx& );
00129
00131 CLockEx& operator=( const CLockEx& );
00132
00133 };
00134
00135
00136
00137
00138
00139 class AutoLock
00140 {
00141 CLock& m_Lock;
00142 public:
00143 AutoLock(CLock& lock)
00144 : m_Lock(lock)
00145 {
00146 m_Lock.Lock();
00147 }
00148
00149 ~AutoLock()
00150 {
00151 m_Lock.Unlock();
00152 }
00153
00154 private:
00155 AutoLock& operator=(const AutoLock&);
00156 AutoLock(const AutoLock&);
00157 };
00158
00159
00160
00161
00162
00163
00168 template< class Object>
00169 class LockableObject
00170 {
00171 public:
00172 mutable CLock m_Lock;
00173
00174 class Lock;
00175 friend class Lock;
00176
00181 class Lock
00182 {
00184 const LockableObject<Object> &m_Object;
00185 public:
00186 Lock( const LockableObject<Object>& obj) : m_Object(obj) {
00187 m_Object.m_Lock.Lock();
00188 }
00189
00190 ~Lock(){
00191 m_Object.m_Lock.Unlock();
00192 }
00193 private:
00194 Lock& operator=( const Lock& );
00195 };
00196
00198 Lock GetLock() const
00199 {
00200 return Lock( *this );
00201 }
00202 };
00203
00204
00209 class GCBASE_API CGlobalLock
00210 {
00211 public:
00216 explicit CGlobalLock(const char* pszName);
00217 #if defined(_WIN32) && ! defined(PHARLAP_WIN32)
00218
00219
00220
00221
00222 explicit CGlobalLock(const wchar_t* pszName);
00223 #endif
00224
00225
00226
00227
00228 explicit CGlobalLock(const GENICAM_NAMESPACE::gcstring& strName);
00229
00230 ~CGlobalLock();
00231
00232 public:
00234 bool IsValid(void) const;
00235
00237 bool Lock(unsigned int timeout_ms);
00238
00240 bool TryLock(void);
00241
00243 void Unlock(void);
00244
00245 #if defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
00246
00247 void HashSemName(const GENICAM_NAMESPACE::gcstring& strName);
00248 #endif
00249
00250 protected:
00251 #if defined(_WIN32)
00252 HANDLE m_handle;
00253 #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
00254 GENICAM_NAMESPACE::gcstring m_semName;
00255 sem_t* m_handle;
00256 #elif VXWORKS
00257
00258
00259 #else
00260 # error No/unknown platform thread support
00261 #endif
00262
00263
00264
00265
00266 mutable long m_DebugCount;
00267
00268 private:
00269
00270 CGlobalLock(const CGlobalLock&);
00271 CGlobalLock& operator=(const CGlobalLock&);
00272 };
00273
00274
00283
00284
00285
00286
00287
00288 class GCBASE_API CGlobalLockUnlocker
00289 {
00290 protected:
00291 CGlobalLock& m_Lock;
00292 bool m_enabled;
00293
00294 public:
00295 CGlobalLockUnlocker(CGlobalLock& lock)
00296 : m_Lock(lock)
00297 , m_enabled(true)
00298 {
00299
00300 }
00301
00302 ~CGlobalLockUnlocker()
00303 {
00304 UnlockEarly();
00305 }
00306
00308 void UnlockEarly(void)
00309 {
00310 if (m_enabled)
00311 {
00312 m_enabled = false;
00313 m_Lock.Unlock();
00314 }
00315 }
00316
00317 private:
00318 CGlobalLockUnlocker& operator=(const CGlobalLockUnlocker&);
00319 CGlobalLockUnlocker(const CGlobalLockUnlocker&);
00320 };
00321
00322 }
00323
00324 #endif // GENAPI_GCSYNCH_H