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


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Wed Mar 17 2021 02:48:40