GCSynch.h
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //  (c) 2006 by Basler Vision Technologies
00003 //  Section: Vision Components
00004 //  Project: GenApi
00005 //  Author:  Hartmut Nebelung
00006 //  $Header$
00007 //
00008 //  License: This file is published under the license of the EMVA GenICam  Standard Group.
00009 //  A text file describing the legal terms is included in  your installation as 'GenICam_license.pdf'.
00010 //  If for some reason you are missing  this file please contact the EMVA or visit the website
00011 //  (http://www.genicam.org) for a full copy.
00012 //
00013 //  THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
00014 //  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00015 //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00016 //  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD  GROUP
00017 //  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL,
00018 //  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  LIMITED TO,
00019 //  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  DATA, OR PROFITS;
00020 //  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  THEORY OF LIABILITY,
00021 //  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)
00022 //  ARISING IN ANY WAY OUT OF THE USE  OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00023 //  POSSIBILITY OF SUCH DAMAGE.
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     // CLock
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             // nothing implemented for Unix
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     // AutoLock
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     // template LockableObject<Object,ThreadingModel>
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         // creates a hashed name instead of the real name
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         // There are no named locks on VxWorks. While we could use a single global lock, we
00258         // will just rely on the caller to add their own locking in
00259 #else
00260 #       error No/unknown platform thread support
00261 #endif
00262 
00263         // This is for debugging/assertions only
00264         // the d'tor check whether this is 0 in debug builds
00265         // in release builds this is always 0
00266         mutable long m_DebugCount;
00267 
00268     private:
00269         // not copyable
00270         CGlobalLock(const CGlobalLock&);
00271         CGlobalLock& operator=(const CGlobalLock&);
00272     };
00273 
00274 
00283     //-----------------------------------------------------------------
00284     // unlocks the global lock object on destruction
00285     // this is for automatic UNLOCKING only.
00286     // we can't do automatic locking here since we don't get a returnvalue from the c'tor
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) // this allows the auto unlock to be turned off for messy code structures
00298         {
00299             // explicitly don't lock the object here since we want to do this manually and handle the return value properly
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 } // namespace GenICam
00323 
00324 #endif // GENAPI_GCSYNCH_H


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02