win_event.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // detail/win_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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/config.hpp"
  16. #if defined(BOOST_WINDOWS)
  17. #include <boost/assert.hpp>
  18. #include "asio/detail/noncopyable.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. class win_event
  24. : private noncopyable
  25. {
  26. public:
  27. // Constructor.
  28. ASIO_DECL win_event();
  29. // Destructor.
  30. ~win_event()
  31. {
  32. ::CloseHandle(event_);
  33. }
  34. // Signal the event.
  35. template <typename Lock>
  36. void signal(Lock& lock)
  37. {
  38. BOOST_ASSERT(lock.locked());
  39. (void)lock;
  40. ::SetEvent(event_);
  41. }
  42. // Signal the event and unlock the mutex.
  43. template <typename Lock>
  44. void signal_and_unlock(Lock& lock)
  45. {
  46. BOOST_ASSERT(lock.locked());
  47. lock.unlock();
  48. ::SetEvent(event_);
  49. }
  50. // Reset the event.
  51. template <typename Lock>
  52. void clear(Lock& lock)
  53. {
  54. BOOST_ASSERT(lock.locked());
  55. (void)lock;
  56. ::ResetEvent(event_);
  57. }
  58. // Wait for the event to become signalled.
  59. template <typename Lock>
  60. void wait(Lock& lock)
  61. {
  62. BOOST_ASSERT(lock.locked());
  63. lock.unlock();
  64. ::WaitForSingleObject(event_, INFINITE);
  65. lock.lock();
  66. }
  67. private:
  68. HANDLE event_;
  69. };
  70. } // namespace detail
  71. } // namespace asio
  72. #include "asio/detail/pop_options.hpp"
  73. #if defined(ASIO_HEADER_ONLY)
  74. # include "asio/detail/impl/win_event.ipp"
  75. #endif // defined(ASIO_HEADER_ONLY)
  76. #endif // defined(BOOST_WINDOWS)
  77. #endif // ASIO_DETAIL_WIN_EVENT_HPP