Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / base / synchronization / condition_variable_posix.cc
blob0e4668feb2a79be8f6aa7d4bdb5d3eff80877865
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.
5 #include "base/synchronization/condition_variable.h"
7 #include <errno.h>
8 #include <sys/time.h>
10 #include "base/synchronization/lock.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "base/time/time.h"
14 namespace base {
16 ConditionVariable::ConditionVariable(Lock* user_lock)
17 : user_mutex_(user_lock->lock_.native_handle())
18 #if DCHECK_IS_ON()
19 , user_lock_(user_lock)
20 #endif
22 int rv = 0;
23 // http://crbug.com/293736
24 // NaCl doesn't support monotonic clock based absolute deadlines.
25 // On older Android platform versions, it's supported through the
26 // non-standard pthread_cond_timedwait_monotonic_np. Newer platform
27 // versions have pthread_condattr_setclock.
28 // Mac can use relative time deadlines.
29 #if !defined(OS_MACOSX) && !defined(OS_NACL) && \
30 !(defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
31 pthread_condattr_t attrs;
32 rv = pthread_condattr_init(&attrs);
33 DCHECK_EQ(0, rv);
34 pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC);
35 rv = pthread_cond_init(&condition_, &attrs);
36 pthread_condattr_destroy(&attrs);
37 #else
38 rv = pthread_cond_init(&condition_, NULL);
39 #endif
40 DCHECK_EQ(0, rv);
43 ConditionVariable::~ConditionVariable() {
44 #if defined(OS_MACOSX)
45 // This hack is necessary to avoid a fatal pthreads subsystem bug in the
46 // Darwin kernel. http://crbug.com/517681.
48 base::Lock lock;
49 base::AutoLock l(lock);
50 struct timespec ts;
51 ts.tv_sec = 0;
52 ts.tv_nsec = 1;
53 pthread_cond_timedwait_relative_np(&condition_, lock.lock_.native_handle(),
54 &ts);
56 #endif
58 int rv = pthread_cond_destroy(&condition_);
59 DCHECK_EQ(0, rv);
62 void ConditionVariable::Wait() {
63 base::ThreadRestrictions::AssertWaitAllowed();
64 #if DCHECK_IS_ON()
65 user_lock_->CheckHeldAndUnmark();
66 #endif
67 int rv = pthread_cond_wait(&condition_, user_mutex_);
68 DCHECK_EQ(0, rv);
69 #if DCHECK_IS_ON()
70 user_lock_->CheckUnheldAndMark();
71 #endif
74 void ConditionVariable::TimedWait(const TimeDelta& max_time) {
75 base::ThreadRestrictions::AssertWaitAllowed();
76 int64 usecs = max_time.InMicroseconds();
77 struct timespec relative_time;
78 relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond;
79 relative_time.tv_nsec =
80 (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond;
82 #if DCHECK_IS_ON()
83 user_lock_->CheckHeldAndUnmark();
84 #endif
86 #if defined(OS_MACOSX)
87 int rv = pthread_cond_timedwait_relative_np(
88 &condition_, user_mutex_, &relative_time);
89 #else
90 // The timeout argument to pthread_cond_timedwait is in absolute time.
91 struct timespec absolute_time;
92 #if defined(OS_NACL)
93 // See comment in constructor for why this is different in NaCl.
94 struct timeval now;
95 gettimeofday(&now, NULL);
96 absolute_time.tv_sec = now.tv_sec;
97 absolute_time.tv_nsec = now.tv_usec * Time::kNanosecondsPerMicrosecond;
98 #else
99 struct timespec now;
100 clock_gettime(CLOCK_MONOTONIC, &now);
101 absolute_time.tv_sec = now.tv_sec;
102 absolute_time.tv_nsec = now.tv_nsec;
103 #endif
105 absolute_time.tv_sec += relative_time.tv_sec;
106 absolute_time.tv_nsec += relative_time.tv_nsec;
107 absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond;
108 absolute_time.tv_nsec %= Time::kNanosecondsPerSecond;
109 DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia
111 #if defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
112 int rv = pthread_cond_timedwait_monotonic_np(
113 &condition_, user_mutex_, &absolute_time);
114 #else
115 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time);
116 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
117 #endif // OS_MACOSX
119 DCHECK(rv == 0 || rv == ETIMEDOUT);
120 #if DCHECK_IS_ON()
121 user_lock_->CheckUnheldAndMark();
122 #endif
125 void ConditionVariable::Broadcast() {
126 int rv = pthread_cond_broadcast(&condition_);
127 DCHECK_EQ(0, rv);
130 void ConditionVariable::Signal() {
131 int rv = pthread_cond_signal(&condition_);
132 DCHECK_EQ(0, rv);
135 } // namespace base