win_event.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // win_event.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_WIN_EVENT_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_EVENT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #if defined(BOOST_WINDOWS)
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/noncopyable.hpp>
  23. #include <boost/asio/detail/socket_types.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. #include <boost/assert.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include <boost/asio/detail/pop_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. class win_event
  32. : private noncopyable
  33. {
  34. public:
  35. // Constructor.
  36. win_event()
  37. : event_(::CreateEvent(0, true, false, 0))
  38. {
  39. if (!event_)
  40. {
  41. DWORD last_error = ::GetLastError();
  42. boost::system::system_error e(
  43. boost::system::error_code(last_error,
  44. boost::asio::error::get_system_category()),
  45. "event");
  46. boost::throw_exception(e);
  47. }
  48. }
  49. // Destructor.
  50. ~win_event()
  51. {
  52. ::CloseHandle(event_);
  53. }
  54. // Signal the event.
  55. template <typename Lock>
  56. void signal(Lock& lock)
  57. {
  58. BOOST_ASSERT(lock.locked());
  59. (void)lock;
  60. ::SetEvent(event_);
  61. }
  62. // Reset the event.
  63. template <typename Lock>
  64. void clear(Lock& lock)
  65. {
  66. BOOST_ASSERT(lock.locked());
  67. (void)lock;
  68. ::ResetEvent(event_);
  69. }
  70. // Wait for the event to become signalled.
  71. template <typename Lock>
  72. void wait(Lock& lock)
  73. {
  74. BOOST_ASSERT(lock.locked());
  75. lock.unlock();
  76. ::WaitForSingleObject(event_, INFINITE);
  77. lock.lock();
  78. }
  79. private:
  80. HANDLE event_;
  81. };
  82. } // namespace detail
  83. } // namespace asio
  84. } // namespace boost
  85. #endif // defined(BOOST_WINDOWS)
  86. #include <boost/asio/detail/pop_options.hpp>
  87. #endif // BOOST_ASIO_DETAIL_WIN_EVENT_HPP