Synch.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. //-----------------------------------------------------------------------------
  25. /*!
  26. \file
  27. \brief Definition of Lock classes
  28. \ingroup GenApi_PublicImpl
  29. */
  30. #ifndef GENAPI_SYNCH_H
  31. #define GENAPI_SYNCH_H
  32. #include <GenApi/GenApiDll.h>
  33. #include <Base/GCException.h>
  34. #if defined (_WIN32)
  35. # include <windows.h>
  36. # include <winbase.h>
  37. #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
  38. # include <pthread.h>
  39. # include <errno.h>
  40. # include <list>
  41. #elif defined(VXWORKS)
  42. #include <vxworks.h>
  43. #include <intLib.h>
  44. #include <taskLib.h>
  45. #else
  46. # error No/unknown platform thread support
  47. #endif
  48. namespace GENAPI_NAMESPACE
  49. {
  50. //-----------------------------------------------------------------
  51. // CLock
  52. //-----------------------------------------------------------------
  53. /**
  54. \brief A lock class
  55. \ingroup GenApi_PublicImpl
  56. */
  57. class GENAPI_DECL CLock
  58. {
  59. public:
  60. //! Constructor
  61. CLock();
  62. //! Destructor
  63. ~CLock();
  64. //! tries to enter the critical section; returns true if successful
  65. bool TryLock();
  66. //! enters the critical section (may block)
  67. void Lock();
  68. //! leaves the critical section
  69. void Unlock();
  70. private:
  71. //! no copy constructor
  72. CLock( const CLock& );
  73. //! no assignment operator
  74. CLock& operator=( const CLock& );
  75. protected:
  76. #if defined (_WIN32)
  77. //! The critical section object
  78. CRITICAL_SECTION m_csObject;
  79. #elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__))
  80. //! the mutex object
  81. pthread_mutex_t m_mtxObject;
  82. #elif defined(VXWORKS)
  83. SEM_ID m_sem;
  84. #else
  85. # error No/unknown platform thread support
  86. #endif
  87. };
  88. //! This class is for testing purposes only. It should not be used for
  89. //! client code because it exists only for Windows but not for Linux
  90. //! since it uses internal data structures of a Win32 object
  91. class GENAPI_DECL CLockEx : public CLock
  92. {
  93. public:
  94. # if defined (_WIN32)
  95. //! Gives access to internal data member for test and purposes
  96. int64_t GetLockCount();
  97. //! Gives access to internal data member for test and purposes
  98. int64_t GetRecursionCount();
  99. # elif defined (__GNUC__) && (defined (__linux__) || defined (__APPLE__) || defined(VXWORKS))
  100. // nothing implemented for Unix
  101. # else
  102. # error No/unknown platform support
  103. # endif
  104. private:
  105. //! no copy constructor
  106. CLockEx( const CLockEx& );
  107. //! no assignment operator
  108. CLockEx& operator=( const CLockEx& );
  109. };
  110. //-----------------------------------------------------------------
  111. // AutoLock
  112. //-----------------------------------------------------------------
  113. class AutoLock
  114. {
  115. CLock& m_Lock;
  116. public:
  117. AutoLock(CLock& lock)
  118. : m_Lock(lock)
  119. {
  120. m_Lock.Lock();
  121. }
  122. ~AutoLock()
  123. {
  124. m_Lock.Unlock();
  125. }
  126. private:
  127. AutoLock& operator=(const AutoLock&);
  128. AutoLock(const AutoLock&);
  129. };
  130. //-----------------------------------------------------------------
  131. // template LockableObject<Object,ThreadingModel>
  132. //-----------------------------------------------------------------
  133. /**
  134. \brief Instance-Lock for an object
  135. \ingroup GenApi_PublicImpl
  136. */
  137. template< class Object>
  138. class LockableObject
  139. {
  140. public:
  141. mutable CLock m_Lock;
  142. class Lock;
  143. friend class Lock;
  144. /*! A scopelevel Lock class.
  145. * Automatically acquires the lock when created and releases
  146. * it when destroyed.
  147. */
  148. class Lock
  149. {
  150. /// Reference to outer object
  151. const LockableObject<Object> &m_Object;
  152. public:
  153. Lock( const LockableObject<Object>& obj) : m_Object(obj) {
  154. m_Object.m_Lock.Lock();
  155. }
  156. ~Lock(){
  157. m_Object.m_Lock.Unlock();
  158. }
  159. private:
  160. Lock& operator=( const Lock& );
  161. };
  162. /// Get a new lock
  163. Lock GetLock() const
  164. {
  165. return Lock( *this );
  166. }
  167. };
  168. } // namespace GenApi
  169. #endif // GENAPI_SYNCH_H