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.
4
5#include "base/synchronization/condition_variable.h"
6
7#include <errno.h>
8#include <sys/time.h>
9
10#include "base/logging.h"
11#include "base/synchronization/lock.h"
12#include "base/threading/thread_restrictions.h"
13#include "base/time/time.h"
14
15namespace base {
16
17ConditionVariable::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)
21#endif
22{
23  int rv = 0;
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);
34  DCHECK_EQ(0, rv);
35  pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC);
36  rv = pthread_cond_init(&condition_, &attrs);
37  pthread_condattr_destroy(&attrs);
38#else
39  rv = pthread_cond_init(&condition_, NULL);
40#endif
41  DCHECK_EQ(0, rv);
42}
43
44ConditionVariable::~ConditionVariable() {
45  int rv = pthread_cond_destroy(&condition_);
46  DCHECK_EQ(0, rv);
47}
48
49void ConditionVariable::Wait() {
50  base::ThreadRestrictions::AssertWaitAllowed();
51#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
52  user_lock_->CheckHeldAndUnmark();
53#endif
54  int rv = pthread_cond_wait(&condition_, user_mutex_);
55  DCHECK_EQ(0, rv);
56#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
57  user_lock_->CheckUnheldAndMark();
58#endif
59}
60
61void 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;
68
69#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
70  user_lock_->CheckHeldAndUnmark();
71#endif
72
73#if defined(OS_MACOSX)
74  int rv = pthread_cond_timedwait_relative_np(
75      &condition_, user_mutex_, &relative_time);
76#else
77  // The timeout argument to pthread_cond_timedwait is in absolute time.
78  struct timespec absolute_time;
79#if defined(OS_NACL)
80  // See comment in constructor for why this is different in NaCl.
81  struct timeval now;
82  gettimeofday(&now, NULL);
83  absolute_time.tv_sec = now.tv_sec;
84  absolute_time.tv_nsec = now.tv_usec * Time::kNanosecondsPerMicrosecond;
85#else
86  struct timespec now;
87  clock_gettime(CLOCK_MONOTONIC, &now);
88  absolute_time.tv_sec = now.tv_sec;
89  absolute_time.tv_nsec = now.tv_nsec;
90#endif
91
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
97
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);
101#else
102  int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time);
103#endif  // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
104#endif  // OS_MACOSX
105
106  DCHECK(rv == 0 || rv == ETIMEDOUT);
107#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
108  user_lock_->CheckUnheldAndMark();
109#endif
110}
111
112void ConditionVariable::Broadcast() {
113  int rv = pthread_cond_broadcast(&condition_);
114  DCHECK_EQ(0, rv);
115}
116
117void ConditionVariable::Signal() {
118  int rv = pthread_cond_signal(&condition_);
119  DCHECK_EQ(0, rv);
120}
121
122}  // namespace base
123