Fix iOS build for XCode 4.6.
[chromium-blink-merge.git] / base / synchronization / condition_variable_posix.cc
blob25edf168599dc2dc45d76a64be5cf567cdc97e88
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"
7 #include <errno.h>
8 #include <sys/time.h>
10 #include "base/logging.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/time.h"
15 namespace base {
17 ConditionVariable::ConditionVariable(Lock* user_lock)
18 : user_mutex_(user_lock->lock_.os_lock())
19 #if !defined(NDEBUG)
20 , user_lock_(user_lock)
21 #endif
23 int rv = pthread_cond_init(&condition_, NULL);
24 DCHECK_EQ(0, rv);
27 ConditionVariable::~ConditionVariable() {
28 int rv = pthread_cond_destroy(&condition_);
29 DCHECK_EQ(0, rv);
32 void ConditionVariable::Wait() {
33 base::ThreadRestrictions::AssertWaitAllowed();
34 #if !defined(NDEBUG)
35 user_lock_->CheckHeldAndUnmark();
36 #endif
37 int rv = pthread_cond_wait(&condition_, user_mutex_);
38 DCHECK_EQ(0, rv);
39 #if !defined(NDEBUG)
40 user_lock_->CheckUnheldAndMark();
41 #endif
44 void ConditionVariable::TimedWait(const TimeDelta& max_time) {
45 base::ThreadRestrictions::AssertWaitAllowed();
46 int64 usecs = max_time.InMicroseconds();
48 // The timeout argument to pthread_cond_timedwait is in absolute time.
49 struct timeval now;
50 gettimeofday(&now, NULL);
52 struct timespec abstime;
53 abstime.tv_sec = now.tv_sec + (usecs / Time::kMicrosecondsPerSecond);
54 abstime.tv_nsec = (now.tv_usec + (usecs % Time::kMicrosecondsPerSecond)) *
55 Time::kNanosecondsPerMicrosecond;
56 abstime.tv_sec += abstime.tv_nsec / Time::kNanosecondsPerSecond;
57 abstime.tv_nsec %= Time::kNanosecondsPerSecond;
58 DCHECK_GE(abstime.tv_sec, now.tv_sec); // Overflow paranoia
60 #if !defined(NDEBUG)
61 user_lock_->CheckHeldAndUnmark();
62 #endif
63 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &abstime);
64 DCHECK(rv == 0 || rv == ETIMEDOUT);
65 #if !defined(NDEBUG)
66 user_lock_->CheckUnheldAndMark();
67 #endif
70 void ConditionVariable::Broadcast() {
71 int rv = pthread_cond_broadcast(&condition_);
72 DCHECK_EQ(0, rv);
75 void ConditionVariable::Signal() {
76 int rv = pthread_cond_signal(&condition_);
77 DCHECK_EQ(0, rv);
80 } // namespace base