condition_variable_posix.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/synchronization/condition_variable.h"
  5. #include <errno.h>
  6. #include <stdint.h>
  7. #include <sys/time.h>
  8. #include "base/synchronization/lock.h"
  9. #include "base/threading/scoped_blocking_call.h"
  10. #include "base/threading/thread_restrictions.h"
  11. #include "base/time/time.h"
  12. #include "build/build_config.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #if BUILDFLAG(IS_ANDROID) && __ANDROID_API__ < 21
  15. #define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC 1
  16. #endif
  17. namespace base {
  18. ConditionVariable::ConditionVariable(Lock* user_lock)
  19. : user_mutex_(user_lock->lock_.native_handle())
  20. #if DCHECK_IS_ON()
  21. , user_lock_(user_lock)
  22. #endif
  23. {
  24. int rv = 0;
  25. // http://crbug.com/293736
  26. // NaCl doesn't support monotonic clock based absolute deadlines.
  27. // On older Android platform versions, it's supported through the
  28. // non-standard pthread_cond_timedwait_monotonic_np. Newer platform
  29. // versions have pthread_condattr_setclock.
  30. // Mac can use relative time deadlines.
  31. #if !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_NACL) && \
  32. !defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
  33. pthread_condattr_t attrs;
  34. rv = pthread_condattr_init(&attrs);
  35. DCHECK_EQ(0, rv);
  36. pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC);
  37. rv = pthread_cond_init(&condition_, &attrs);
  38. pthread_condattr_destroy(&attrs);
  39. #else
  40. rv = pthread_cond_init(&condition_, NULL);
  41. #endif
  42. DCHECK_EQ(0, rv);
  43. }
  44. ConditionVariable::~ConditionVariable() {
  45. #if BUILDFLAG(IS_APPLE)
  46. // This hack is necessary to avoid a fatal pthreads subsystem bug in the
  47. // Darwin kernel. http://crbug.com/517681.
  48. {
  49. base::Lock lock;
  50. base::AutoLock l(lock);
  51. struct timespec ts;
  52. ts.tv_sec = 0;
  53. ts.tv_nsec = 1;
  54. pthread_cond_timedwait_relative_np(&condition_, lock.lock_.native_handle(),
  55. &ts);
  56. }
  57. #endif
  58. int rv = pthread_cond_destroy(&condition_);
  59. DCHECK_EQ(0, rv);
  60. }
  61. void ConditionVariable::Wait() {
  62. absl::optional<internal::ScopedBlockingCallWithBaseSyncPrimitives>
  63. scoped_blocking_call;
  64. if (waiting_is_blocking_)
  65. scoped_blocking_call.emplace(FROM_HERE, BlockingType::MAY_BLOCK);
  66. #if DCHECK_IS_ON()
  67. user_lock_->CheckHeldAndUnmark();
  68. #endif
  69. int rv = pthread_cond_wait(&condition_, user_mutex_);
  70. DCHECK_EQ(0, rv);
  71. #if DCHECK_IS_ON()
  72. user_lock_->CheckUnheldAndMark();
  73. #endif
  74. }
  75. void ConditionVariable::TimedWait(const TimeDelta& max_time) {
  76. absl::optional<internal::ScopedBlockingCallWithBaseSyncPrimitives>
  77. scoped_blocking_call;
  78. if (waiting_is_blocking_)
  79. scoped_blocking_call.emplace(FROM_HERE, BlockingType::MAY_BLOCK);
  80. int64_t usecs = max_time.InMicroseconds();
  81. struct timespec relative_time;
  82. relative_time.tv_sec =
  83. static_cast<time_t>(usecs / Time::kMicrosecondsPerSecond);
  84. relative_time.tv_nsec =
  85. (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond;
  86. #if DCHECK_IS_ON()
  87. user_lock_->CheckHeldAndUnmark();
  88. #endif
  89. #if BUILDFLAG(IS_APPLE)
  90. int rv = pthread_cond_timedwait_relative_np(
  91. &condition_, user_mutex_, &relative_time);
  92. #else
  93. // The timeout argument to pthread_cond_timedwait is in absolute time.
  94. struct timespec absolute_time;
  95. #if BUILDFLAG(IS_NACL)
  96. // See comment in constructor for why this is different in NaCl.
  97. struct timeval now;
  98. gettimeofday(&now, NULL);
  99. absolute_time.tv_sec = now.tv_sec;
  100. absolute_time.tv_nsec = now.tv_usec * Time::kNanosecondsPerMicrosecond;
  101. #else
  102. struct timespec now;
  103. clock_gettime(CLOCK_MONOTONIC, &now);
  104. absolute_time.tv_sec = now.tv_sec;
  105. absolute_time.tv_nsec = now.tv_nsec;
  106. #endif
  107. absolute_time.tv_sec += relative_time.tv_sec;
  108. absolute_time.tv_nsec += relative_time.tv_nsec;
  109. absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond;
  110. absolute_time.tv_nsec %= Time::kNanosecondsPerSecond;
  111. DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia
  112. #if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
  113. int rv = pthread_cond_timedwait_monotonic_np(
  114. &condition_, user_mutex_, &absolute_time);
  115. #else
  116. int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time);
  117. #endif // HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
  118. #endif // BUILDFLAG(IS_APPLE)
  119. // On failure, we only expect the CV to timeout. Any other error value means
  120. // that we've unexpectedly woken up.
  121. DCHECK(rv == 0 || rv == ETIMEDOUT);
  122. #if DCHECK_IS_ON()
  123. user_lock_->CheckUnheldAndMark();
  124. #endif
  125. }
  126. void ConditionVariable::Broadcast() {
  127. int rv = pthread_cond_broadcast(&condition_);
  128. DCHECK_EQ(0, rv);
  129. }
  130. void ConditionVariable::Signal() {
  131. int rv = pthread_cond_signal(&condition_);
  132. DCHECK_EQ(0, rv);
  133. }
  134. } // namespace base