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