You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/event.h"
  11. #if defined(WEBRTC_WIN)
  12. #include <windows.h>
  13. #elif defined(WEBRTC_POSIX)
  14. #include <errno.h>
  15. #include <pthread.h>
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #else
  19. #error "Must define either WEBRTC_WIN or WEBRTC_POSIX."
  20. #endif
  21. #include "absl/types/optional.h"
  22. #include "rtc_base/checks.h"
  23. #include "rtc_base/synchronization/yield_policy.h"
  24. #include "rtc_base/system/warn_current_thread_is_deadlocked.h"
  25. namespace rtc {
  26. Event::Event() : Event(false, false) {}
  27. #if defined(WEBRTC_WIN)
  28. Event::Event(bool manual_reset, bool initially_signaled) {
  29. event_handle_ = ::CreateEvent(nullptr, // Security attributes.
  30. manual_reset, initially_signaled,
  31. nullptr); // Name.
  32. RTC_CHECK(event_handle_);
  33. }
  34. Event::~Event() {
  35. CloseHandle(event_handle_);
  36. }
  37. void Event::Set() {
  38. SetEvent(event_handle_);
  39. }
  40. void Event::Reset() {
  41. ResetEvent(event_handle_);
  42. }
  43. bool Event::Wait(const int give_up_after_ms, int /*warn_after_ms*/) {
  44. ScopedYieldPolicy::YieldExecution();
  45. const DWORD ms = give_up_after_ms == kForever ? INFINITE : give_up_after_ms;
  46. return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
  47. }
  48. #elif defined(WEBRTC_POSIX)
  49. // On MacOS, clock_gettime is available from version 10.12, and on
  50. // iOS, from version 10.0. So we can't use it yet.
  51. #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
  52. #define USE_CLOCK_GETTIME 0
  53. #define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
  54. // On Android, pthread_condattr_setclock is available from version 21. By
  55. // default, we target a new enough version for 64-bit platforms but not for
  56. // 32-bit platforms. For older versions, use
  57. // pthread_cond_timedwait_monotonic_np.
  58. #elif defined(WEBRTC_ANDROID) && (__ANDROID_API__ < 21)
  59. #define USE_CLOCK_GETTIME 1
  60. #define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 1
  61. #else
  62. #define USE_CLOCK_GETTIME 1
  63. #define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
  64. #endif
  65. Event::Event(bool manual_reset, bool initially_signaled)
  66. : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
  67. RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
  68. pthread_condattr_t cond_attr;
  69. RTC_CHECK(pthread_condattr_init(&cond_attr) == 0);
  70. #if USE_CLOCK_GETTIME && !USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
  71. RTC_CHECK(pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC) == 0);
  72. #endif
  73. RTC_CHECK(pthread_cond_init(&event_cond_, &cond_attr) == 0);
  74. pthread_condattr_destroy(&cond_attr);
  75. }
  76. Event::~Event() {
  77. pthread_mutex_destroy(&event_mutex_);
  78. pthread_cond_destroy(&event_cond_);
  79. }
  80. void Event::Set() {
  81. pthread_mutex_lock(&event_mutex_);
  82. event_status_ = true;
  83. pthread_cond_broadcast(&event_cond_);
  84. pthread_mutex_unlock(&event_mutex_);
  85. }
  86. void Event::Reset() {
  87. pthread_mutex_lock(&event_mutex_);
  88. event_status_ = false;
  89. pthread_mutex_unlock(&event_mutex_);
  90. }
  91. namespace {
  92. timespec GetTimespec(const int milliseconds_from_now) {
  93. timespec ts;
  94. // Get the current time.
  95. #if USE_CLOCK_GETTIME
  96. clock_gettime(CLOCK_MONOTONIC, &ts);
  97. #else
  98. timeval tv;
  99. gettimeofday(&tv, nullptr);
  100. ts.tv_sec = tv.tv_sec;
  101. ts.tv_nsec = tv.tv_usec * 1000;
  102. #endif
  103. // Add the specified number of milliseconds to it.
  104. ts.tv_sec += (milliseconds_from_now / 1000);
  105. ts.tv_nsec += (milliseconds_from_now % 1000) * 1000000;
  106. // Normalize.
  107. if (ts.tv_nsec >= 1000000000) {
  108. ts.tv_sec++;
  109. ts.tv_nsec -= 1000000000;
  110. }
  111. return ts;
  112. }
  113. } // namespace
  114. bool Event::Wait(const int give_up_after_ms, const int warn_after_ms) {
  115. // Instant when we'll log a warning message (because we've been waiting so
  116. // long it might be a bug), but not yet give up waiting. nullopt if we
  117. // shouldn't log a warning.
  118. const absl::optional<timespec> warn_ts =
  119. warn_after_ms == kForever ||
  120. (give_up_after_ms != kForever && warn_after_ms > give_up_after_ms)
  121. ? absl::nullopt
  122. : absl::make_optional(GetTimespec(warn_after_ms));
  123. // Instant when we'll stop waiting and return an error. nullopt if we should
  124. // never give up.
  125. const absl::optional<timespec> give_up_ts =
  126. give_up_after_ms == kForever
  127. ? absl::nullopt
  128. : absl::make_optional(GetTimespec(give_up_after_ms));
  129. ScopedYieldPolicy::YieldExecution();
  130. pthread_mutex_lock(&event_mutex_);
  131. // Wait for `event_cond_` to trigger and `event_status_` to be set, with the
  132. // given timeout (or without a timeout if none is given).
  133. const auto wait = [&](const absl::optional<timespec> timeout_ts) {
  134. int error = 0;
  135. while (!event_status_ && error == 0) {
  136. if (timeout_ts == absl::nullopt) {
  137. error = pthread_cond_wait(&event_cond_, &event_mutex_);
  138. } else {
  139. #if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
  140. error = pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_,
  141. &*timeout_ts);
  142. #else
  143. error =
  144. pthread_cond_timedwait(&event_cond_, &event_mutex_, &*timeout_ts);
  145. #endif
  146. }
  147. }
  148. return error;
  149. };
  150. int error;
  151. if (warn_ts == absl::nullopt) {
  152. error = wait(give_up_ts);
  153. } else {
  154. error = wait(warn_ts);
  155. if (error == ETIMEDOUT) {
  156. webrtc::WarnThatTheCurrentThreadIsProbablyDeadlocked();
  157. error = wait(give_up_ts);
  158. }
  159. }
  160. // NOTE(liulk): Exactly one thread will auto-reset this event. All
  161. // the other threads will think it's unsignaled. This seems to be
  162. // consistent with auto-reset events in WEBRTC_WIN
  163. if (error == 0 && !is_manual_reset_)
  164. event_status_ = false;
  165. pthread_mutex_unlock(&event_mutex_);
  166. return (error == 0);
  167. }
  168. #endif
  169. } // namespace rtc