Chromium Code Reviews| Index: base/synchronization/condition_variable_posix.cc |
| diff --git a/base/synchronization/condition_variable_posix.cc b/base/synchronization/condition_variable_posix.cc |
| index 92b479ede470f0a6c80e26b6e5b1b67fa94b6230..6e9c2eb538ab8206c2425b76bf92c1e24a368c5f 100644 |
| --- a/base/synchronization/condition_variable_posix.cc |
| +++ b/base/synchronization/condition_variable_posix.cc |
| @@ -20,7 +20,22 @@ ConditionVariable::ConditionVariable(Lock* user_lock) |
| , user_lock_(user_lock) |
| #endif |
| { |
| - int rv = pthread_cond_init(&condition_, NULL); |
| + int rv = 0; |
| + // http://crbug.com/293736 |
| + // NaCl doesn't support monotonic clock based absolute deadlines. |
| + // Android supports it through the non-standard |
| + // pthread_cond_timedwait_monotonic_np. |
| + // Mac can use relative time deadlines. |
| +#if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
| + pthread_condattr_t attrs; |
| + rv = pthread_condattr_init(&attrs); |
| + DCHECK_EQ(0, rv); |
| + pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC); |
| + rv = pthread_cond_init(&condition_, &attrs); |
| + pthread_condattr_destroy(&attrs); |
| +#else |
| + rv = pthread_cond_init(&condition_, NULL); |
| +#endif |
| DCHECK_EQ(0, rv); |
| } |
| @@ -44,23 +59,47 @@ void ConditionVariable::Wait() { |
| void ConditionVariable::TimedWait(const TimeDelta& max_time) { |
| base::ThreadRestrictions::AssertWaitAllowed(); |
| int64 usecs = max_time.InMicroseconds(); |
| + struct timespec relative_time; |
| + relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond; |
| + relative_time.tv_nsec = |
| + (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond; |
| + |
| +#if !defined(NDEBUG) |
| + user_lock_->CheckHeldAndUnmark(); |
| +#endif |
| +#if defined(OS_MACOSX) |
| + int rv = pthread_cond_timedwait_relative_np( |
| + &condition_, user_mutex_, &relative_time); |
| +#else |
| // The timeout argument to pthread_cond_timedwait is in absolute time. |
| + struct timespec absolute_time; |
| +#if defined(OS_NACL) |
|
Nico
2013/10/01 23:01:53
Maybe add a "// See comment in constructor for why
piman
2013/10/01 23:16:33
Done.
|
| struct timeval now; |
| gettimeofday(&now, NULL); |
| + absolute_time.tv_sec = now.tv_sec; |
| + absolute_time.tv_nsec = now.tv_usec * Time::kNanosecondsPerMicrosecond; |
| +#else |
| + struct timespec now; |
| + clock_gettime(CLOCK_MONOTONIC, &now); |
| + absolute_time.tv_sec = now.tv_sec; |
| + absolute_time.tv_nsec = now.tv_nsec; |
| +#endif |
| - struct timespec abstime; |
| - abstime.tv_sec = now.tv_sec + (usecs / Time::kMicrosecondsPerSecond); |
| - abstime.tv_nsec = (now.tv_usec + (usecs % Time::kMicrosecondsPerSecond)) * |
| - Time::kNanosecondsPerMicrosecond; |
| - abstime.tv_sec += abstime.tv_nsec / Time::kNanosecondsPerSecond; |
| - abstime.tv_nsec %= Time::kNanosecondsPerSecond; |
| - DCHECK_GE(abstime.tv_sec, now.tv_sec); // Overflow paranoia |
| + absolute_time.tv_sec += relative_time.tv_sec; |
| + absolute_time.tv_nsec += relative_time.tv_nsec; |
| + absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond; |
| + absolute_time.tv_nsec %= Time::kNanosecondsPerSecond; |
| + DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia |
| + |
| +#if defined(OS_ANDROID) |
| + int rv = pthread_cond_timedwait_monotonic_np( |
| + &condition_, user_mutex_, &absolute_time); |
| +#else |
| + int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); |
| +#endif // OS_ANDROID |
| +#endif // OS_MACOSX |
| -#if !defined(NDEBUG) |
| - user_lock_->CheckHeldAndUnmark(); |
| -#endif |
| - int rv = pthread_cond_timedwait(&condition_, user_mutex_, &abstime); |
| DCHECK(rv == 0 || rv == ETIMEDOUT); |
| #if !defined(NDEBUG) |
| user_lock_->CheckUnheldAndMark(); |