WaitObject.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //-----------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2006-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: Urs Helmig
  6. //-----------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Declaration of WaitObject classes
  10. */
  11. #ifndef __WAITOBJECT_H__
  12. #define __WAITOBJECT_H__
  13. #if _MSC_VER > 1000
  14. #pragma once
  15. #endif // _MSC_VER > 1000
  16. #include <pylon/Platform.h>
  17. #ifdef _MSC_VER
  18. # pragma pack(push, PYLON_PACKING)
  19. #endif /* _MSC_VER */
  20. #include <pylon/stdinclude.h>
  21. #include <pylon/PylonBase.h>
  22. #if defined (PYLON_UNIX_BUILD)
  23. struct _WaitObjectPosix_t;
  24. #endif
  25. namespace Pylon
  26. {
  27. // -------------------------------------------------------------------------
  28. // enum EWaitExResult
  29. // ----------------------------------------------------------------------
  30. /*!
  31. \brief The return value of the Pylon::WaitObject::WaitEx() and Pylon::WaitObjectEx::WaitEx() methods
  32. \ingroup PYLON_PUBLIC
  33. */
  34. // ----------------------------------------------------------------------
  35. enum EWaitExResult
  36. {
  37. waitex_timeout = 0, //!< The time-out interval elapsed
  38. waitex_signaled = 1, //!< The wait operation completed successfully
  39. waitex_abandoned = 2, //!< Windows only (see MSDN for more information)
  40. waitex_alerted = -1 //!< The wait was interrupted (Windows: queued APC or I/O completion routine; UNIX: signal)
  41. };
  42. #if defined(PYLON_WIN_BUILD)
  43. typedef HANDLE WaitObject_t;
  44. #elif defined(PYLON_UNIX_BUILD)
  45. typedef struct _WaitObjectPosix_t* WaitObject_t;
  46. #endif
  47. #if defined (PYLON_UNIX_BUILD)
  48. // -------------------------------------------------------------------------
  49. // enum ready_mask_t
  50. // ----------------------------------------------------------------------
  51. /*!
  52. \brief File descriptor ready mask
  53. Return value of the Pylon::WaitObject::GetReadyMask() method. Indicates to which type of file descriptor set a wait object's file descriptor
  54. is to be added when using the file descriptor as input for the select() or poll() function.
  55. Example: When the ready_read bit of a wait object's GetReadyMask() value is set, add the wait object's file descriptor to the read file descriptor set.
  56. \ingroup PYLON_PUBLIC
  57. */
  58. // ----------------------------------------------------------------------
  59. enum ready_mask_t
  60. {
  61. ready_none = 0x00, //!< The file descriptor is not valid
  62. ready_read = 0x01, //!< The file desriptor is to be added to a read file descriptor set
  63. ready_write = 0x02, //!< The file descriptor is to be added to a write file descriptor set
  64. ready_except = 0x04 //!< The file descriptor is to be added to a exception file descriptor set
  65. };
  66. #endif
  67. // ----------------------------------------------------------------------
  68. const unsigned int waitForever = 0xFFFFFFFF;
  69. #if defined (PYLON_UNIX_BUILD)
  70. # ifndef INFINITE
  71. # define INFINITE waitForever
  72. # endif
  73. #endif
  74. // ----------------------------------------------------------------------
  75. /*!
  76. \brief A platform independent wait object.
  77. Wait objects are used by the Pylon::IStreamGrabber and Pylon::IEventGrabber interfaces to provide a platform independent mechanism for
  78. allowing an application to wait for data buffers to be filled.
  79. For the Windows version of pylon, WaitObjects are wrappers for Win32 objects that can be used with \c WaitForSingleObject() and \c WaitForMultipleObjects().
  80. For the Linux version of pylon, WaitObjects are implemented based on file descriptors. The wait operation is implemented using the \c poll() function.
  81. Although the class provides a default constructor, the default constructor doesn't create a "usable" wait objects wrapping a handle resp. file descriptor.
  82. Valid instances of Pylon::WaitObject cannot be created by the application, instead the pylon libraries return fully created wait objects.
  83. The Pylon::WaitObjectEx class can be used to create wait objects that can be controlled by the application.
  84. The Pylon::WaitObject class provides access to the wrapped handle resp. file descriptor. This allows to use to allow use pylon wait objects as input for
  85. "native" APIs like \c WaitForMultipleObjects() (Windows), and \c poll() (Linux).
  86. Multiple Pylon::WaitObjects can be put in the Pylon::WaitObjects container class allowing to wait "simultaneously" for multiple events.
  87. \ingroup PYLON_PUBLIC
  88. */
  89. // ----------------------------------------------------------------------
  90. class PYLONBASE_API WaitObject
  91. {
  92. // -------------------------------
  93. // Platform independent
  94. // -------------------------------
  95. public:
  96. /// Constructs an "empty" wait object, i.e., the wait object is not attached to a platform dependent wait object (IsValid() == false)
  97. /** The Pylon::WaitObjectEx class can be used to create wait objects controllable by an application. */
  98. WaitObject();
  99. /// Destructor
  100. virtual ~WaitObject();
  101. /// Copy constructor (duplicates the wrapped handle/file descriptor)
  102. WaitObject( const WaitObject& );
  103. /// Assignment operator (duplicates the wrapped handle/file descriptor)
  104. WaitObject& operator=( const WaitObject& );
  105. /// Suspend calling thread for specified time.
  106. /**
  107. \param ms wait time in ms
  108. */
  109. static void Sleep( unsigned long ms );
  110. public:
  111. /// Checks if the wait object is valid.
  112. /**
  113. Don't call the Wait methods() for an invalid wait object. Wait objects returned by the pylon libraries are valid.
  114. \return true if the object contains a valid handle/file descriptor
  115. */
  116. bool IsValid() const;
  117. public:
  118. /// Wait for the object to be signaled
  119. /**
  120. \param timeout timeout in ms
  121. \return false when the timeout has been expired, true when the waiting was successful before
  122. the timeout has been expired.
  123. */
  124. bool Wait( unsigned int timeout ) const;
  125. /// Wait for the object to be signaled (interruptible)
  126. /**
  127. \param timeout timeout in ms
  128. \param bAlertable When the bAlertable parameter is set to true, the function waits until either the timeout elapses, the object enters
  129. the signaled state, or the wait operation has been interrupted.
  130. For Windows, the wait operation is interrupted by queued APCs or I/O completion routines.
  131. For Linux, the wait operation can be interrupted by signals.
  132. \return The returned Pylon::EWaitExResult value indicates the result of the wait operation.
  133. */
  134. EWaitExResult WaitEx( unsigned int timeout, bool bAlertable ) const;
  135. // -------------------------------
  136. // Windows-specific stuff
  137. // -------------------------------
  138. public:
  139. /// Constructor taking existing handle (duplicate=false -> take ownership like std:auto_ptr)
  140. /** This method allows to wrap an existing windows handle that can be used with the \c WaitForSingleObject() and
  141. \c WaitForMultipleObjects methods. */
  142. WaitObject( WaitObject_t h, bool duplicate = true );
  143. #if defined(PYLON_WIN_BUILD)
  144. /// conversion operator
  145. /** \return the native Win32 handle wrapped by the WaitObject. (Not supported by pylon4Linux) */
  146. operator WaitObject_t() const;
  147. protected:
  148. WaitObject_t Duplicate( WaitObject_t h ) const;
  149. protected:
  150. WaitObject_t m_Native;
  151. #endif
  152. #if defined (PYLON_UNIX_BUILD)
  153. /// Invalidates the internal wait object.
  154. void Destroy();
  155. protected:
  156. WaitObject_t m_pWaitObjectPosix;
  157. // -------------------------------
  158. // Linux specific stuff
  159. // -------------------------------
  160. public:
  161. /// Getter for file descriptor
  162. /** \return the file descriptor wrapped by the WaitObject. When the file descriptor is added to the file descriptor set indicated by the
  163. GetReadyMask() method, the \c select() or poll() function can be used to wait for pylon and non-pylon events simultaneously.
  164. */
  165. int GetFd( void ) const;
  166. /// Indicates to what kind of file descriptor set the wrapped file descriptor must be put for usage with the \c select() or poll() function.
  167. ready_mask_t GetReadyMask() const;
  168. /// conversion operator
  169. /** \return the internal wait object handle. (Not supported by pylon4Windows) */
  170. operator struct _WaitObjectPosix_t* () const;
  171. /// Accessor to internal wait object (not supported by pylon for Windows)
  172. WaitObject_t& GetWaitObject();
  173. #endif
  174. };
  175. // ----------------------------------------------------------------------
  176. /*!
  177. \brief A wait object that the user may signal
  178. \ingroup PYLON_PUBLIC
  179. */
  180. // ----------------------------------------------------------------------
  181. class PYLONBASE_API WaitObjectEx : public WaitObject
  182. {
  183. // -------------------------------
  184. // Platform independent
  185. // -------------------------------
  186. public:
  187. /// Creates an event object (manual reset event)
  188. static WaitObjectEx Create( bool initiallySignalled = false );
  189. /// Constructs an "empty" wait object, i.e., the wait object is not attached to a platform dependent wait object (IsValid() == false)
  190. /** Use the static WaitObjectEx::Create() method to create instances of the WaitObjectEx class instead. */
  191. WaitObjectEx();
  192. /// Destroys the waitobject
  193. virtual ~WaitObjectEx()
  194. {
  195. }
  196. public:
  197. /// Set the object to signaled state
  198. void Signal();
  199. /// Reset the object to unsignaled state
  200. void Reset();
  201. public:
  202. // -------------------------------
  203. // Windows and OSX specific stuff
  204. // -------------------------------
  205. #if defined(PYLON_WIN_BUILD) || defined(PYLON_OSX_BUILD)
  206. /// Constructor using an existing windows handle (duplicate=false -> take ownership like std:auto_ptr)
  207. explicit WaitObjectEx( WaitObject_t h, bool duplicate = true );
  208. #endif
  209. // -------------------------------
  210. // Linux specific stuff
  211. // -------------------------------
  212. #if defined(PYLON_UNIX_BUILD)
  213. /// Constructor using an existing Linux file descriptor
  214. explicit WaitObjectEx( int fd );
  215. #endif
  216. };
  217. }
  218. #ifdef _MSC_VER
  219. # pragma pack(pop)
  220. #endif /* _MSC_VER */
  221. #endif //__WAITOBJECT_H__