win_event.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // win_event.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 ASIO_DETAIL_WIN_EVENT_HPP
  11. #define 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 "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include "asio/detail/pop_options.hpp"
  19. #if defined(BOOST_WINDOWS)
  20. #include "asio/error.hpp"
  21. #include "asio/system_error.hpp"
  22. #include "asio/detail/noncopyable.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. #include <boost/assert.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include "asio/detail/pop_options.hpp"
  28. namespace asio {
  29. namespace detail {
  30. class win_event
  31. : private noncopyable
  32. {
  33. public:
  34. // Constructor.
  35. win_event()
  36. : event_(::CreateEvent(0, true, false, 0))
  37. {
  38. if (!event_)
  39. {
  40. DWORD last_error = ::GetLastError();
  41. asio::system_error e(
  42. asio::error_code(last_error,
  43. asio::error::get_system_category()),
  44. "event");
  45. boost::throw_exception(e);
  46. }
  47. }
  48. // Destructor.
  49. ~win_event()
  50. {
  51. ::CloseHandle(event_);
  52. }
  53. // Signal the event.
  54. template <typename Lock>
  55. void signal(Lock& lock)
  56. {
  57. BOOST_ASSERT(lock.locked());
  58. (void)lock;
  59. ::SetEvent(event_);
  60. }
  61. // Signal the event and unlock the mutex.
  62. template <typename Lock>
  63. void signal_and_unlock(Lock& lock)
  64. {
  65. BOOST_ASSERT(lock.locked());
  66. lock.unlock();
  67. ::SetEvent(event_);
  68. }
  69. // Reset the event.
  70. template <typename Lock>
  71. void clear(Lock& lock)
  72. {
  73. BOOST_ASSERT(lock.locked());
  74. (void)lock;
  75. ::ResetEvent(event_);
  76. }
  77. // Wait for the event to become signalled.
  78. template <typename Lock>
  79. void wait(Lock& lock)
  80. {
  81. BOOST_ASSERT(lock.locked());
  82. lock.unlock();
  83. ::WaitForSingleObject(event_, INFINITE);
  84. lock.lock();
  85. }
  86. private:
  87. HANDLE event_;
  88. };
  89. } // namespace detail
  90. } // namespace asio
  91. #endif // defined(BOOST_WINDOWS)
  92. #include "asio/detail/pop_options.hpp"
  93. #endif // ASIO_DETAIL_WIN_EVENT_HPP