posix_event.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // posix_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_POSIX_EVENT_HPP
  11. #define ASIO_DETAIL_POSIX_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_HAS_PTHREADS)
  20. #include "asio/detail/push_options.hpp"
  21. #include <boost/assert.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <pthread.h>
  24. #include "asio/detail/pop_options.hpp"
  25. #include "asio/error.hpp"
  26. #include "asio/system_error.hpp"
  27. #include "asio/detail/noncopyable.hpp"
  28. namespace asio {
  29. namespace detail {
  30. class posix_event
  31. : private noncopyable
  32. {
  33. public:
  34. // Constructor.
  35. posix_event()
  36. : signalled_(false)
  37. {
  38. int error = ::pthread_cond_init(&cond_, 0);
  39. if (error != 0)
  40. {
  41. asio::system_error e(
  42. asio::error_code(error,
  43. asio::error::get_system_category()),
  44. "event");
  45. boost::throw_exception(e);
  46. }
  47. }
  48. // Destructor.
  49. ~posix_event()
  50. {
  51. ::pthread_cond_destroy(&cond_);
  52. }
  53. // Signal the event.
  54. template <typename Lock>
  55. void signal(Lock& lock)
  56. {
  57. BOOST_ASSERT(lock.locked());
  58. (void)lock;
  59. signalled_ = true;
  60. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  61. }
  62. // Signal the event and unlock the mutex.
  63. template <typename Lock>
  64. void signal_and_unlock(Lock& lock)
  65. {
  66. BOOST_ASSERT(lock.locked());
  67. signalled_ = true;
  68. lock.unlock();
  69. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  70. }
  71. // Reset the event.
  72. template <typename Lock>
  73. void clear(Lock& lock)
  74. {
  75. BOOST_ASSERT(lock.locked());
  76. (void)lock;
  77. signalled_ = false;
  78. }
  79. // Wait for the event to become signalled.
  80. template <typename Lock>
  81. void wait(Lock& lock)
  82. {
  83. BOOST_ASSERT(lock.locked());
  84. while (!signalled_)
  85. ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
  86. }
  87. private:
  88. ::pthread_cond_t cond_;
  89. bool signalled_;
  90. };
  91. } // namespace detail
  92. } // namespace asio
  93. #endif // defined(BOOST_HAS_PTHREADS)
  94. #include "asio/detail/pop_options.hpp"
  95. #endif // ASIO_DETAIL_POSIX_EVENT_HPP