Refactored not to expose raw pointers on ProxyList class.
[chromium-blink-merge.git] / base / synchronization / condition_variable_posix.cc
blob013284c888f72993b81a75f0b56a28bd653e6e3a
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/time.h"
15 namespace base {
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)
21 #endif
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);
44 ConditionVariable::~ConditionVariable() {
45 int rv = pthread_cond_destroy(&condition_);
46 DCHECK_EQ(0, rv);
49 void 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
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();
71 #endif
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
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);
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
106 DCHECK(rv == 0 || rv == ETIMEDOUT);
107 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
108 user_lock_->CheckUnheldAndMark();
109 #endif
112 void ConditionVariable::Broadcast() {
113 int rv = pthread_cond_broadcast(&condition_);
114 DCHECK_EQ(0, rv);
117 void ConditionVariable::Signal() {
118 int rv = pthread_cond_signal(&condition_);
119 DCHECK_EQ(0, rv);
122 } // namespace base