"Make Chrome your default browser" should now appear as a checkbox at the bottom...
[chromium-blink-merge.git] / base / condition_variable_posix.cc
blobc6f5f073cda6baae03e2ba2a03fb71863a959dda
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"
7 #include <errno.h>
8 #include <sys/time.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);
17 DCHECK(rv == 0);
20 ConditionVariable::~ConditionVariable() {
21 int rv = pthread_cond_destroy(&condition_);
22 DCHECK(rv == 0);
25 void ConditionVariable::Wait() {
26 int rv = pthread_cond_wait(&condition_, user_mutex_);
27 DCHECK(rv == 0);
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.
34 struct timeval now;
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_);
51 DCHECK(rv == 0);
54 void ConditionVariable::Signal() {
55 int rv = pthread_cond_signal(&condition_);
56 DCHECK(rv == 0);