You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
9.0 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/synch/event.hpp
  3. *
  4. * Purpose: event class, based on Windows EVENT.
  5. *
  6. * Created: 3rd July 2003
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/synch/event.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::event class
  42. * (\ref group__library__synch "Synchronisation" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_SYNCH_HPP_EVENT
  45. #define WINSTL_INCL_WINSTL_SYNCH_HPP_EVENT
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_MAJOR 4
  48. # define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_MINOR 3
  49. # define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_REVISION 1
  50. # define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_EDIT 59
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. #ifndef STLSOFT_INCL_STLSOFT_SYNCH_HPP_CONCEPTS
  59. # include <stlsoft/synch/concepts.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_SYNCH_HPP_CONCEPTS */
  61. #ifndef WINSTL_INCL_WINSTL_SYNCH_ERROR_HPP_EXCEPTIONS
  62. # include <winstl/synch/error/exceptions.hpp>
  63. #endif /* !WINSTL_INCL_WINSTL_SYNCH_ERROR_HPP_EXCEPTIONS */
  64. /* /////////////////////////////////////////////////////////////////////////
  65. * Namespace
  66. */
  67. #ifndef _WINSTL_NO_NAMESPACE
  68. # if defined(_STLSOFT_NO_NAMESPACE) || \
  69. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  70. /* There is no stlsoft namespace, so must define ::winstl */
  71. namespace winstl
  72. {
  73. # else
  74. /* Define stlsoft::winstl_project */
  75. namespace stlsoft
  76. {
  77. namespace winstl_project
  78. {
  79. # endif /* _STLSOFT_NO_NAMESPACE */
  80. #endif /* !_WINSTL_NO_NAMESPACE */
  81. /* /////////////////////////////////////////////////////////////////////////
  82. * Classes
  83. */
  84. /** \brief Class which wraps the Win32 EVENT synchronisation object
  85. *
  86. * \ingroup group__library__synch
  87. */
  88. class event
  89. : public stlsoft_ns_qual(synchronisable_object_tag)
  90. {
  91. public:
  92. /// This type
  93. typedef event class_type;
  94. typedef HANDLE synch_handle_type;
  95. typedef HANDLE resource_type;
  96. /// \name Construction
  97. /// @{
  98. public:
  99. /// \brief Creates the event
  100. event(ws_bool_t bManualReset, ws_bool_t bInitialState)
  101. : m_ev(create_event_(NULL, bManualReset, bInitialState, static_cast<ws_char_a_t const*>(0)))
  102. , m_bOwnHandle(true)
  103. {}
  104. /// \brief Creates the event with the given name
  105. ss_explicit_k event(ws_char_a_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState)
  106. : m_ev(create_event_(NULL, bManualReset, bInitialState, name))
  107. , m_bOwnHandle(true)
  108. {}
  109. /// \brief Creates the event with the given name
  110. ss_explicit_k event(ws_char_w_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState)
  111. : m_ev(create_event_(NULL, bManualReset, bInitialState, name))
  112. , m_bOwnHandle(true)
  113. {}
  114. /// \brief Creates the event with the given name and security attributes
  115. ss_explicit_k event(ws_char_a_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState, LPSECURITY_ATTRIBUTES psa)
  116. : m_ev(create_event_(psa, bManualReset, bInitialState, name))
  117. , m_bOwnHandle(true)
  118. {}
  119. /// \brief Creates the event with the given name and security attributes
  120. ss_explicit_k event(ws_char_w_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState, LPSECURITY_ATTRIBUTES psa)
  121. : m_ev(create_event_(psa, bManualReset, bInitialState, name))
  122. , m_bOwnHandle(true)
  123. {}
  124. /// \brief Destroys the event instance
  125. ~event() stlsoft_throw_0()
  126. {
  127. if( NULL != m_ev &&
  128. m_bOwnHandle)
  129. {
  130. ::CloseHandle(m_ev);
  131. }
  132. }
  133. /// @}
  134. /// \name Operations
  135. /// @{
  136. public:
  137. /// \brief Sets the state of the event to signalled
  138. void set()
  139. {
  140. WINSTL_ASSERT(NULL != m_ev);
  141. if(!::SetEvent(m_ev))
  142. {
  143. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  144. STLSOFT_THROW_X(synchronisation_exception("event set operation failed", ::GetLastError()));
  145. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  146. }
  147. }
  148. /// \brief Sets the state of the event to signalled
  149. void reset()
  150. {
  151. WINSTL_ASSERT(NULL != m_ev);
  152. if(!::ResetEvent(m_ev))
  153. {
  154. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  155. STLSOFT_THROW_X(synchronisation_exception("event reset operation failed", ::GetLastError()));
  156. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  157. }
  158. }
  159. /// @}
  160. /// \name Accessors
  161. /// @{
  162. public:
  163. /// \brief The underlying kernel object handle
  164. HANDLE handle()
  165. {
  166. return m_ev;
  167. }
  168. /// \brief The underlying kernel object handle
  169. HANDLE get()
  170. {
  171. return m_ev;
  172. }
  173. /// @}
  174. // Implementation
  175. private:
  176. static HANDLE create_event_(LPSECURITY_ATTRIBUTES psa, ws_bool_t bManualReset, ws_bool_t bInitialState, ws_char_a_t const* name)
  177. {
  178. HANDLE h = ::CreateEventA(psa, bManualReset, bInitialState, name);
  179. if(NULL == h)
  180. {
  181. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  182. STLSOFT_THROW_X(synchronisation_exception("Failed to create kernel event object", ::GetLastError()));
  183. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  184. }
  185. return h;
  186. }
  187. static HANDLE create_event_(LPSECURITY_ATTRIBUTES psa, ws_bool_t bManualReset, ws_bool_t bInitialState, ws_char_w_t const* name)
  188. {
  189. HANDLE h = ::CreateEventW(psa, bManualReset, bInitialState, name);
  190. if(NULL == h)
  191. {
  192. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  193. STLSOFT_THROW_X(synchronisation_exception("Failed to create kernel event object", ::GetLastError()));
  194. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  195. }
  196. return h;
  197. }
  198. // Members
  199. private:
  200. HANDLE m_ev;
  201. const ws_bool_t m_bOwnHandle; // Does the instance own the handle?
  202. // Not to be implemented
  203. private:
  204. event(class_type const& rhs);
  205. event& operator =(class_type const& rhs);
  206. };
  207. /* /////////////////////////////////////////////////////////////////////////
  208. * Shims
  209. */
  210. /** \brief Overload of the form of the winstl::get_synch_handle() shim for
  211. * the winstl::event type.
  212. *
  213. * \param ev The winstl::event instance
  214. *
  215. * \retval The synchronisation handle of \c ev
  216. */
  217. inline HANDLE get_synch_handle(event &ev)
  218. {
  219. return ev.get();
  220. }
  221. /** \brief Overload of the form of the winstl::get_kernel_handle() shim for
  222. * the winstl::event type.
  223. *
  224. * \ingroup group__library__shims__kernel_handle_attribute
  225. *
  226. * \param ev The winstl::event instance
  227. *
  228. * \retval The synchronisation handle of \c ev
  229. */
  230. inline HANDLE get_kernel_handle(event &ev)
  231. {
  232. return ev.get();
  233. }
  234. ////////////////////////////////////////////////////////////////////////////
  235. // Unit-testing
  236. #ifdef STLSOFT_UNITTEST
  237. # include "./unittest/event_unittest_.h"
  238. #endif /* STLSOFT_UNITTEST */
  239. /* ////////////////////////////////////////////////////////////////////// */
  240. #ifndef _WINSTL_NO_NAMESPACE
  241. # if defined(_STLSOFT_NO_NAMESPACE) || \
  242. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  243. } // namespace winstl
  244. # else
  245. } // namespace winstl_project
  246. } // namespace stlsoft
  247. # endif /* _STLSOFT_NO_NAMESPACE */
  248. #endif /* !_WINSTL_NO_NAMESPACE */
  249. /* ////////////////////////////////////////////////////////////////////// */
  250. #endif /* !WINSTL_INCL_WINSTL_SYNCH_HPP_EVENT */
  251. /* ///////////////////////////// end of file //////////////////////////// */