GenApi  3.5.1
GCSynch.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // (c) 2006 by Basler Vision Technologies
3 // Author: Hartmut Nebelung
4 //
5 // License: This file is published under the license of the EMVA GenICam Standard Group.
6 // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
7 // If for some reason you are missing this file please contact the EMVA or visit the website
8 // (http://www.genicam.org) for a full copy.
9 //
10 // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
11 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
13 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
14 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
17 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20 // POSSIBILITY OF SUCH DAMAGE.
21 //
22 //-----------------------------------------------------------------------------
29 #ifndef GENAPI_GCSYNCH_H
30 #define GENAPI_GCSYNCH_H
31 
32 #include <Base/GCTypes.h>
33 #include <Base/GCException.h>
34 
35 #if defined (_WIN32)
36 # include <windows.h>
37 # include <winbase.h>
38 #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
39 # include <semaphore.h>
40 # include <pthread.h>
41 # include <errno.h>
42 # include <list>
43 #elif defined(VXWORKS)
44  #include <vxworks.h>
45  #include <taskLib.h>
46 #else
47 # error No/unknown platform thread support
48 #endif
49 
50 namespace GENICAM_NAMESPACE
51 {
52 
53  //-----------------------------------------------------------------
54  // CLock
55  //-----------------------------------------------------------------
56 
61  class GCBASE_API CLock
62  {
63  public:
65  CLock();
66 
68  ~CLock();
69 
71  bool TryLock();
72 
74  void Lock();
75 
77  void Unlock();
78 
79  private:
81  CLock( const CLock& );
82 
84  CLock& operator=( const CLock& );
85 
86  protected:
87 
88 #if defined (_WIN32)
89  CRITICAL_SECTION m_csObject;
91 #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
92  pthread_mutex_t m_mtxObject;
94 #elif defined(VXWORKS)
95  SEM_ID m_sem;
96 #else
97 # error No/unknown platform thread support
98 #endif
99 
100  };
101 
102 
106  class GCBASE_API CLockEx : public CLock
107  {
108  public:
109 
110 # if defined (_WIN32)
111 
113  int64_t GetLockCount();
114 
116  int64_t GetRecursionCount();
117 
118 # elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__) || defined(VXWORKS))
119  // nothing implemented for Unix
120 # else
121 # error No/unknown platform support
122 # endif
123 
124  private:
126  CLockEx( const CLockEx& );
127 
129  CLockEx& operator=( const CLockEx& );
130 
131  };
132 
133 
134  //-----------------------------------------------------------------
135  // AutoLock
136  //-----------------------------------------------------------------
137  class AutoLock
138  {
139  CLock& m_Lock;
140  public:
141  AutoLock(CLock& lock)
142  : m_Lock(lock)
143  {
144  m_Lock.Lock();
145  }
146 
147  ~AutoLock()
148  {
149  m_Lock.Unlock();
150  }
151 
152  private:
153  AutoLock& operator=(const AutoLock&);
154  AutoLock(const AutoLock&);
155  };
156 
157 
158  //-----------------------------------------------------------------
159  // template LockableObject<Object,ThreadingModel>
160  //-----------------------------------------------------------------
161 
166  template< class Object>
168  {
169  public:
170  mutable CLock m_Lock;
171 
172  class Lock;
173  friend class Lock;
174 
179  class Lock
180  {
182  const LockableObject<Object> &m_Object;
183  public:
184  Lock( const LockableObject<Object>& obj) : m_Object(obj) {
185  m_Object.m_Lock.Lock();
186  }
187 
188  ~Lock(){
189  m_Object.m_Lock.Unlock();
190  }
191  private:
192  Lock& operator=( const Lock& );
193  };
194 
196  Lock GetLock() const
197  {
198  return Lock( *this );
199  }
200  };
201 
202 
207  class GCBASE_API CGlobalLock
208  {
209  public:
214  explicit CGlobalLock(const char* pszName);
215 #if defined(_WIN32) && ! defined(PHARLAP_WIN32)
216  explicit CGlobalLock(const wchar_t* pszName);
221 #endif
222  explicit CGlobalLock(const GENICAM_NAMESPACE::gcstring& strName);
227 
228  ~CGlobalLock();
229 
230  public:
232  bool IsValid(void) const;
233 
235  bool Lock(unsigned int timeout_ms);
236 
238  bool TryLock(void);
239 
241  void Unlock(void);
242 
243 #if defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
244  // creates a hashed name instead of the real name
245  void HashSemName(const GENICAM_NAMESPACE::gcstring& strName);
246 #endif
247 
248  protected:
249 #if defined(_WIN32)
250  HANDLE m_handle;
251 #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
252  GENICAM_NAMESPACE::gcstring m_semName;
253  sem_t* m_handle;
254 #elif VXWORKS
255  // There are no named locks on VxWorks. While we could use a single global lock, we
256  // will just rely on the caller to add their own locking in
257 #else
258 # error No/unknown platform thread support
259 #endif
260 
261  // This is for debugging/assertions only
262  // the d'tor check whether this is 0 in debug builds
263  // in release builds this is always 0
264  mutable long m_DebugCount;
265 
266  private:
267  // not copyable
268  CGlobalLock(const CGlobalLock&);
269  CGlobalLock& operator=(const CGlobalLock&);
270  };
271 
272 
281  //-----------------------------------------------------------------
282  // unlocks the global lock object on destruction
283  // this is for automatic UNLOCKING only.
284  // we can't do automatic locking here since we don't get a returnvalue from the c'tor
285  //-----------------------------------------------------------------
286  class GCBASE_API CGlobalLockUnlocker
287  {
288  protected:
289  CGlobalLock& m_Lock;
290  bool m_enabled;
291 
292  public:
294  : m_Lock(lock)
295  , m_enabled(true) // this allows the auto unlock to be turned off for messy code structures
296  {
297  // explicitly don't lock the object here since we want to do this manually and handle the return value properly
298  }
299 
301  {
302  UnlockEarly();
303  }
304 
306  void UnlockEarly(void)
307  {
308  if (m_enabled)
309  {
310  m_enabled = false;
311  m_Lock.Unlock();
312  }
313  }
314 
315  private:
316  CGlobalLockUnlocker& operator=(const CGlobalLockUnlocker&);
318  };
319 
320 } // namespace GenICam
321 
322 #endif // GENAPI_GCSYNCH_H
Instance-Lock for an object.
Definition: GCSynch.h:167
unlocks the global lock object on destruction
Definition: GCSynch.h:286
void UnlockEarly(void)
This function allows to unlock the object early before the object is destroyed.
Definition: GCSynch.h:306
Definition: GCArray.h:33
void Unlock(void)
leaves the lock
void Lock()
enters the critical section (may block)
Definition: GCSynch.h:106
Named global lock which can be used over process boundaries.
Definition: GCSynch.h:207
A lock class.
Definition: GCSynch.h:61
Lock GetLock() const
Get a new lock.
Definition: GCSynch.h:196
Standard GenICam Exceptions.
A string class which is a clone of std::string.
Definition: GCString.h:50
void Unlock()
leaves the critical section
Platform-dependent type definitions.