posix_event.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // detail/posix_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_POSIX_EVENT_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_PTHREADS)
  17. #include <cstddef>
  18. #include <pthread.h>
  19. #include <boost/asio/detail/assert.hpp>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. class posix_event
  26. : private noncopyable
  27. {
  28. public:
  29. // Constructor.
  30. BOOST_ASIO_DECL posix_event();
  31. // Destructor.
  32. ~posix_event()
  33. {
  34. ::pthread_cond_destroy(&cond_);
  35. }
  36. // Signal the event. (Retained for backward compatibility.)
  37. template <typename Lock>
  38. void signal(Lock& lock)
  39. {
  40. this->signal_all(lock);
  41. }
  42. // Signal all waiters.
  43. template <typename Lock>
  44. void signal_all(Lock& lock)
  45. {
  46. BOOST_ASIO_ASSERT(lock.locked());
  47. (void)lock;
  48. state_ |= 1;
  49. ::pthread_cond_broadcast(&cond_); // Ignore EINVAL.
  50. }
  51. // Unlock the mutex and signal one waiter.
  52. template <typename Lock>
  53. void unlock_and_signal_one(Lock& lock)
  54. {
  55. BOOST_ASIO_ASSERT(lock.locked());
  56. state_ |= 1;
  57. bool have_waiters = (state_ > 1);
  58. lock.unlock();
  59. if (have_waiters)
  60. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  61. }
  62. // Unlock the mutex and signal one waiter who may destroy us.
  63. template <typename Lock>
  64. void unlock_and_signal_one_for_destruction(Lock& lock)
  65. {
  66. BOOST_ASIO_ASSERT(lock.locked());
  67. state_ |= 1;
  68. bool have_waiters = (state_ > 1);
  69. if (have_waiters)
  70. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  71. lock.unlock();
  72. }
  73. // If there's a waiter, unlock the mutex and signal it.
  74. template <typename Lock>
  75. bool maybe_unlock_and_signal_one(Lock& lock)
  76. {
  77. BOOST_ASIO_ASSERT(lock.locked());
  78. state_ |= 1;
  79. if (state_ > 1)
  80. {
  81. lock.unlock();
  82. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  83. return true;
  84. }
  85. return false;
  86. }
  87. // Reset the event.
  88. template <typename Lock>
  89. void clear(Lock& lock)
  90. {
  91. BOOST_ASIO_ASSERT(lock.locked());
  92. (void)lock;
  93. state_ &= ~std::size_t(1);
  94. }
  95. // Wait for the event to become signalled.
  96. template <typename Lock>
  97. void wait(Lock& lock)
  98. {
  99. BOOST_ASIO_ASSERT(lock.locked());
  100. while ((state_ & 1) == 0)
  101. {
  102. state_ += 2;
  103. ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
  104. state_ -= 2;
  105. }
  106. }
  107. // Timed wait for the event to become signalled.
  108. template <typename Lock>
  109. bool wait_for_usec(Lock& lock, long usec)
  110. {
  111. BOOST_ASIO_ASSERT(lock.locked());
  112. if ((state_ & 1) == 0)
  113. {
  114. state_ += 2;
  115. timespec ts;
  116. #if (defined(__MACH__) && defined(__APPLE__)) \
  117. || (defined(__ANDROID__) && (__ANDROID_API__ < 21) \
  118. && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
  119. ts.tv_sec = usec / 1000000;
  120. ts.tv_nsec = (usec % 1000000) * 1000;
  121. ::pthread_cond_timedwait_relative_np(
  122. &cond_, &lock.mutex().mutex_, &ts); // Ignore EINVAL.
  123. #else // (defined(__MACH__) && defined(__APPLE__))
  124. // || (defined(__ANDROID__) && (__ANDROID_API__ < 21)
  125. // && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
  126. if (::clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  127. {
  128. ts.tv_sec += usec / 1000000;
  129. ts.tv_nsec += (usec % 1000000) * 1000;
  130. ts.tv_sec += ts.tv_nsec / 1000000000;
  131. ts.tv_nsec = ts.tv_nsec % 1000000000;
  132. ::pthread_cond_timedwait(&cond_,
  133. &lock.mutex().mutex_, &ts); // Ignore EINVAL.
  134. }
  135. #endif // (defined(__MACH__) && defined(__APPLE__))
  136. // || (defined(__ANDROID__) && (__ANDROID_API__ < 21)
  137. // && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
  138. state_ -= 2;
  139. }
  140. return (state_ & 1) != 0;
  141. }
  142. private:
  143. ::pthread_cond_t cond_;
  144. std::size_t state_;
  145. };
  146. } // namespace detail
  147. } // namespace asio
  148. } // namespace boost
  149. #include <boost/asio/detail/pop_options.hpp>
  150. #if defined(BOOST_ASIO_HEADER_ONLY)
  151. # include <boost/asio/detail/impl/posix_event.ipp>
  152. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  153. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  154. #endif // BOOST_ASIO_DETAIL_POSIX_EVENT_HPP