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"
10 #include "base/synchronization/lock.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "base/time/time.h"
16 ConditionVariable::ConditionVariable(Lock
* user_lock
)
17 : user_mutex_(user_lock
->lock_
.native_handle())
19 , user_lock_(user_lock
)
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
);
34 pthread_condattr_setclock(&attrs
, CLOCK_MONOTONIC
);
35 rv
= pthread_cond_init(&condition_
, &attrs
);
36 pthread_condattr_destroy(&attrs
);
38 rv
= pthread_cond_init(&condition_
, NULL
);
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.
49 base::AutoLock
l(lock
);
53 pthread_cond_timedwait_relative_np(&condition_
, lock
.lock_
.native_handle(),
58 int rv
= pthread_cond_destroy(&condition_
);
62 void ConditionVariable::Wait() {
63 base::ThreadRestrictions::AssertWaitAllowed();
65 user_lock_
->CheckHeldAndUnmark();
67 int rv
= pthread_cond_wait(&condition_
, user_mutex_
);
70 user_lock_
->CheckUnheldAndMark();
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
;
83 user_lock_
->CheckHeldAndUnmark();
86 #if defined(OS_MACOSX)
87 int rv
= pthread_cond_timedwait_relative_np(
88 &condition_
, user_mutex_
, &relative_time
);
90 // The timeout argument to pthread_cond_timedwait is in absolute time.
91 struct timespec absolute_time
;
93 // See comment in constructor for why this is different in NaCl.
95 gettimeofday(&now
, NULL
);
96 absolute_time
.tv_sec
= now
.tv_sec
;
97 absolute_time
.tv_nsec
= now
.tv_usec
* Time::kNanosecondsPerMicrosecond
;
100 clock_gettime(CLOCK_MONOTONIC
, &now
);
101 absolute_time
.tv_sec
= now
.tv_sec
;
102 absolute_time
.tv_nsec
= now
.tv_nsec
;
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
);
115 int rv
= pthread_cond_timedwait(&condition_
, user_mutex_
, &absolute_time
);
116 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
119 DCHECK(rv
== 0 || rv
== ETIMEDOUT
);
121 user_lock_
->CheckUnheldAndMark();
125 void ConditionVariable::Broadcast() {
126 int rv
= pthread_cond_broadcast(&condition_
);
130 void ConditionVariable::Signal() {
131 int rv
= pthread_cond_signal(&condition_
);