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_SYNCH_H
00032 #define GENAPI_SYNCH_H
00033
00034 #include <GenApi/GenApiDll.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 <pthread.h>
00042 # include <errno.h>
00043 # include <list>
00044 #elif defined(VXWORKS)
00045 #include <vxworks.h>
00046 #include <intLib.h>
00047 #include <taskLib.h>
00048 #else
00049 # error No/unknown platform thread support
00050 #endif
00051
00052 namespace GENAPI_NAMESPACE
00053 {
00054
00055
00056
00057
00058
00063 class GENAPI_DECL 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 GENAPI_DECL 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 }
00205
00206 #endif // GENAPI_SYNCH_H