1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #include "base/condition_variable.h"
13 #include "base/lock.h"
14 #include "base/logging.h"
15 #include "base/time.h"
17 ConditionVariable::ConditionVariable(Lock
* user_lock
)
18 : user_mutex_(user_lock
->lock_
.native_handle()) {
20 // http://crbug.com/293736
21 // On older Android platform versions, monotonic clock based absolute
22 // deadlines are supported through the non-standard
23 // pthread_cond_timedwait_monotonic_np. Newer platform versions have
24 // pthread_condattr_setclock.
25 // Mac can use relative time deadlines.
26 #if !defined(XP_DARWIN) && \
27 !(defined(ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
28 pthread_condattr_t attrs
;
29 rv
= pthread_condattr_init(&attrs
);
31 pthread_condattr_setclock(&attrs
, CLOCK_MONOTONIC
);
32 rv
= pthread_cond_init(&condition_
, &attrs
);
33 pthread_condattr_destroy(&attrs
);
35 rv
= pthread_cond_init(&condition_
, NULL
);
40 ConditionVariable::~ConditionVariable() {
41 #if defined(XP_DARWIN)
42 // This hack is necessary to avoid a fatal pthreads subsystem bug in the
43 // Darwin kernel. http://crbug.com/517681.
50 pthread_cond_timedwait_relative_np(&condition_
, lock
.lock_
.native_handle(),
55 int rv
= pthread_cond_destroy(&condition_
);
59 void ConditionVariable::Wait() {
60 int rv
= pthread_cond_wait(&condition_
, user_mutex_
);
64 void ConditionVariable::TimedWait(const base::TimeDelta
& max_time
) {
65 int64_t usecs
= max_time
.InMicroseconds();
66 struct timespec relative_time
;
67 relative_time
.tv_sec
= usecs
/ base::Time::kMicrosecondsPerSecond
;
68 relative_time
.tv_nsec
= (usecs
% base::Time::kMicrosecondsPerSecond
) *
69 base::Time::kNanosecondsPerMicrosecond
;
71 #if defined(XP_DARWIN)
72 int rv
= pthread_cond_timedwait_relative_np(&condition_
, user_mutex_
,
75 // The timeout argument to pthread_cond_timedwait is in absolute time.
76 struct timespec absolute_time
;
78 clock_gettime(CLOCK_MONOTONIC
, &now
);
79 absolute_time
.tv_sec
= now
.tv_sec
;
80 absolute_time
.tv_nsec
= now
.tv_nsec
;
82 absolute_time
.tv_sec
+= relative_time
.tv_sec
;
83 absolute_time
.tv_nsec
+= relative_time
.tv_nsec
;
84 absolute_time
.tv_sec
+=
85 absolute_time
.tv_nsec
/ base::Time::kNanosecondsPerSecond
;
86 absolute_time
.tv_nsec
%= base::Time::kNanosecondsPerSecond
;
87 DCHECK_GE(absolute_time
.tv_sec
, now
.tv_sec
); // Overflow paranoia
89 # if defined(ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
90 int rv
= pthread_cond_timedwait_monotonic_np(&condition_
, user_mutex_
,
93 int rv
= pthread_cond_timedwait(&condition_
, user_mutex_
, &absolute_time
);
94 # endif // ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
97 // On failure, we only expect the CV to timeout. Any other error value means
98 // that we've unexpectedly woken up.
99 DCHECK(rv
== 0 || rv
== ETIMEDOUT
);
102 void ConditionVariable::Broadcast() {
103 int rv
= pthread_cond_broadcast(&condition_
);
107 void ConditionVariable::Signal() {
108 int rv
= pthread_cond_signal(&condition_
);