1 // Copyright (c) 2006-2008 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/condition_variable.h"
10 #include "base/lock.h"
11 #include "base/lock_impl.h"
12 #include "base/logging.h"
14 ConditionVariable::ConditionVariable(Lock
* user_lock
)
15 : user_mutex_(user_lock
->lock_impl()->os_lock()) {
16 int rv
= pthread_cond_init(&condition_
, NULL
);
20 ConditionVariable::~ConditionVariable() {
21 int rv
= pthread_cond_destroy(&condition_
);
25 void ConditionVariable::Wait() {
26 int rv
= pthread_cond_wait(&condition_
, user_mutex_
);
30 void ConditionVariable::TimedWait(const TimeDelta
& max_time
) {
31 int64 usecs
= max_time
.InMicroseconds();
33 // The timeout argument to pthread_cond_timedwait is in absolute time.
35 gettimeofday(&now
, NULL
);
37 struct timespec abstime
;
38 abstime
.tv_sec
= now
.tv_sec
+ (usecs
/ Time::kMicrosecondsPerSecond
);
39 abstime
.tv_nsec
= (now
.tv_usec
+ (usecs
% Time::kMicrosecondsPerSecond
)) *
40 Time::kNanosecondsPerMicrosecond
;
41 abstime
.tv_sec
+= abstime
.tv_nsec
/ Time::kNanosecondsPerSecond
;
42 abstime
.tv_nsec
%= Time::kNanosecondsPerSecond
;
43 DCHECK(abstime
.tv_sec
>= now
.tv_sec
); // Overflow paranoia
45 int rv
= pthread_cond_timedwait(&condition_
, user_mutex_
, &abstime
);
46 DCHECK(rv
== 0 || rv
== ETIMEDOUT
);
49 void ConditionVariable::Broadcast() {
50 int rv
= pthread_cond_broadcast(&condition_
);
54 void ConditionVariable::Signal() {
55 int rv
= pthread_cond_signal(&condition_
);