Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(867)

Unified Diff: base/synchronization/condition_variable_posix.cc

Issue 24158005: Fix WaitableEvent and ConditionVariable::TimedWait to use monotonic time on non-mac posix (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add test Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/synchronization/condition_variable_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..1c7838a5fb6f6ab2c4328d4dca6e3f20274ad301 100644
--- a/base/synchronization/condition_variable_posix.cc
+++ b/base/synchronization/condition_variable_posix.cc
@@ -20,7 +20,21 @@ ConditionVariable::ConditionVariable(Lock* user_lock)
, user_lock_(user_lock)
#endif
{
- int rv = pthread_cond_init(&condition_, NULL);
+ int rv = 0;
+ // http://crbug.com/293736
+ // Mac and NaCl don't support monotonic clock based deadlines.
+ // Android supports it through the non-standard
+ // pthread_cond_timedwait_monotonic_np.
+#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);
}
@@ -46,13 +60,22 @@ void ConditionVariable::TimedWait(const TimeDelta& max_time) {
int64 usecs = max_time.InMicroseconds();
// The timeout argument to pthread_cond_timedwait is in absolute time.
+ struct timespec abstime;
+#if defined(OS_MACOSX) || defined(OS_NACL)
struct timeval now;
gettimeofday(&now, NULL);
+ abstime.tv_sec = now.tv_sec;
+ abstime.tv_nsec = now.tv_usec * Time::kNanosecondsPerMicrosecond;
+#else
+ struct timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ abstime.tv_sec = now.tv_sec;
+ abstime.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 += usecs / Time::kMicrosecondsPerSecond;
+ abstime.tv_nsec +=
+ (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
@@ -60,7 +83,12 @@ void ConditionVariable::TimedWait(const TimeDelta& max_time) {
#if !defined(NDEBUG)
user_lock_->CheckHeldAndUnmark();
#endif
+#if defined(OS_ANDROID)
+ int rv = pthread_cond_timedwait_monotonic_np(
+ &condition_, user_mutex_, &abstime);
Nico 2013/10/01 21:20:42 I haven't used it before, but from some googling a
+#else
int rv = pthread_cond_timedwait(&condition_, user_mutex_, &abstime);
+#endif
DCHECK(rv == 0 || rv == ETIMEDOUT);
#if !defined(NDEBUG)
user_lock_->CheckUnheldAndMark();
« no previous file with comments | « no previous file | base/synchronization/condition_variable_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698