posix_event.hpp 4.3 KB

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