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