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())
19 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
20 , user_lock_(user_lock
)
24 // http://crbug.com/293736
25 // NaCl doesn't support monotonic clock based absolute deadlines.
26 // On older Android platform versions, it's supported through the
27 // non-standard pthread_cond_timedwait_monotonic_np. Newer platform
28 // versions have pthread_condattr_setclock.
29 // Mac can use relative time deadlines.
30 #if !defined(OS_MACOSX) && !defined(OS_NACL) && \
31 !(defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
32 pthread_condattr_t attrs
;
33 rv
= pthread_condattr_init(&attrs
);
35 pthread_condattr_setclock(&attrs
, CLOCK_MONOTONIC
);
36 rv
= pthread_cond_init(&condition_
, &attrs
);
37 pthread_condattr_destroy(&attrs
);
39 rv
= pthread_cond_init(&condition_
, NULL
);
44 ConditionVariable::~ConditionVariable() {
45 int rv
= pthread_cond_destroy(&condition_
);
49 void ConditionVariable::Wait() {
50 base::ThreadRestrictions::AssertWaitAllowed();
51 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
52 user_lock_
->CheckHeldAndUnmark();
54 int rv
= pthread_cond_wait(&condition_
, user_mutex_
);
56 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
57 user_lock_
->CheckUnheldAndMark();
61 void ConditionVariable::TimedWait(const TimeDelta
& max_time
) {
62 base::ThreadRestrictions::AssertWaitAllowed();
63 int64 usecs
= max_time
.InMicroseconds();
64 struct timespec relative_time
;
65 relative_time
.tv_sec
= usecs
/ Time::kMicrosecondsPerSecond
;
66 relative_time
.tv_nsec
=
67 (usecs
% Time::kMicrosecondsPerSecond
) * Time::kNanosecondsPerMicrosecond
;
69 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
70 user_lock_
->CheckHeldAndUnmark();
73 #if defined(OS_MACOSX)
74 int rv
= pthread_cond_timedwait_relative_np(
75 &condition_
, user_mutex_
, &relative_time
);
77 // The timeout argument to pthread_cond_timedwait is in absolute time.
78 struct timespec absolute_time
;
80 // See comment in constructor for why this is different in NaCl.
82 gettimeofday(&now
, NULL
);
83 absolute_time
.tv_sec
= now
.tv_sec
;
84 absolute_time
.tv_nsec
= now
.tv_usec
* Time::kNanosecondsPerMicrosecond
;
87 clock_gettime(CLOCK_MONOTONIC
, &now
);
88 absolute_time
.tv_sec
= now
.tv_sec
;
89 absolute_time
.tv_nsec
= now
.tv_nsec
;
92 absolute_time
.tv_sec
+= relative_time
.tv_sec
;
93 absolute_time
.tv_nsec
+= relative_time
.tv_nsec
;
94 absolute_time
.tv_sec
+= absolute_time
.tv_nsec
/ Time::kNanosecondsPerSecond
;
95 absolute_time
.tv_nsec
%= Time::kNanosecondsPerSecond
;
96 DCHECK_GE(absolute_time
.tv_sec
, now
.tv_sec
); // Overflow paranoia
98 #if defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
99 int rv
= pthread_cond_timedwait_monotonic_np(
100 &condition_
, user_mutex_
, &absolute_time
);
102 int rv
= pthread_cond_timedwait(&condition_
, user_mutex_
, &absolute_time
);
103 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
106 DCHECK(rv
== 0 || rv
== ETIMEDOUT
);
107 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
108 user_lock_
->CheckUnheldAndMark();
112 void ConditionVariable::Broadcast() {
113 int rv
= pthread_cond_broadcast(&condition_
);
117 void ConditionVariable::Signal() {
118 int rv
= pthread_cond_signal(&condition_
);