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/logging.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/time/time.h"
17 ConditionVariable::ConditionVariable(Lock
* user_lock
)
18 : user_mutex_(user_lock
->lock_
.native_handle())
20 , user_lock_(user_lock
)
24 // http://crbug.com/293736
25 // NaCl doesn't support monotonic clock based absolute deadlines.
26 // Android supports it through the non-standard
27 // pthread_cond_timedwait_monotonic_np.
28 // Mac can use relative time deadlines.
29 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID)
30 pthread_condattr_t attrs
;
31 rv
= pthread_condattr_init(&attrs
);
33 pthread_condattr_setclock(&attrs
, CLOCK_MONOTONIC
);
34 rv
= pthread_cond_init(&condition_
, &attrs
);
35 pthread_condattr_destroy(&attrs
);
37 rv
= pthread_cond_init(&condition_
, NULL
);
42 ConditionVariable::~ConditionVariable() {
43 int rv
= pthread_cond_destroy(&condition_
);
47 void ConditionVariable::Wait() {
48 base::ThreadRestrictions::AssertWaitAllowed();
50 user_lock_
->CheckHeldAndUnmark();
52 int rv
= pthread_cond_wait(&condition_
, user_mutex_
);
55 user_lock_
->CheckUnheldAndMark();
59 void ConditionVariable::TimedWait(const TimeDelta
& max_time
) {
60 base::ThreadRestrictions::AssertWaitAllowed();
61 int64 usecs
= max_time
.InMicroseconds();
62 struct timespec relative_time
;
63 relative_time
.tv_sec
= usecs
/ Time::kMicrosecondsPerSecond
;
64 relative_time
.tv_nsec
=
65 (usecs
% Time::kMicrosecondsPerSecond
) * Time::kNanosecondsPerMicrosecond
;
68 user_lock_
->CheckHeldAndUnmark();
71 #if defined(OS_MACOSX)
72 int rv
= pthread_cond_timedwait_relative_np(
73 &condition_
, user_mutex_
, &relative_time
);
75 // The timeout argument to pthread_cond_timedwait is in absolute time.
76 struct timespec absolute_time
;
78 // See comment in constructor for why this is different in NaCl.
80 gettimeofday(&now
, NULL
);
81 absolute_time
.tv_sec
= now
.tv_sec
;
82 absolute_time
.tv_nsec
= now
.tv_usec
* Time::kNanosecondsPerMicrosecond
;
85 clock_gettime(CLOCK_MONOTONIC
, &now
);
86 absolute_time
.tv_sec
= now
.tv_sec
;
87 absolute_time
.tv_nsec
= now
.tv_nsec
;
90 absolute_time
.tv_sec
+= relative_time
.tv_sec
;
91 absolute_time
.tv_nsec
+= relative_time
.tv_nsec
;
92 absolute_time
.tv_sec
+= absolute_time
.tv_nsec
/ Time::kNanosecondsPerSecond
;
93 absolute_time
.tv_nsec
%= Time::kNanosecondsPerSecond
;
94 DCHECK_GE(absolute_time
.tv_sec
, now
.tv_sec
); // Overflow paranoia
96 #if defined(OS_ANDROID)
97 int rv
= pthread_cond_timedwait_monotonic_np(
98 &condition_
, user_mutex_
, &absolute_time
);
100 int rv
= pthread_cond_timedwait(&condition_
, user_mutex_
, &absolute_time
);
104 DCHECK(rv
== 0 || rv
== ETIMEDOUT
);
106 user_lock_
->CheckUnheldAndMark();
110 void ConditionVariable::Broadcast() {
111 int rv
= pthread_cond_broadcast(&condition_
);
115 void ConditionVariable::Signal() {
116 int rv
= pthread_cond_signal(&condition_
);