OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/synchronization/condition_variable.h" | 5 #include "base/synchronization/condition_variable.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 ConditionVariable::ConditionVariable(Lock* user_lock) | 17 ConditionVariable::ConditionVariable(Lock* user_lock) |
18 : user_mutex_(user_lock->lock_.native_handle()) | 18 : user_mutex_(user_lock->lock_.native_handle()) |
19 #if !defined(NDEBUG) | 19 #if !defined(NDEBUG) |
20 , user_lock_(user_lock) | 20 , user_lock_(user_lock) |
21 #endif | 21 #endif |
22 { | 22 { |
23 int rv = 0; | 23 int rv = 0; |
24 // http://crbug.com/293736 | 24 // http://crbug.com/293736 |
25 // NaCl doesn't support monotonic clock based absolute deadlines. | 25 // NaCl doesn't support monotonic clock based absolute deadlines. |
26 // Android supports it through the non-standard | 26 // On older Android platform versions, it's supported through the |
27 // pthread_cond_timedwait_monotonic_np. | 27 // non-standard pthread_cond_timedwait_monotonic_np. Newer platform |
| 28 // versions have pthread_condattr_setclock. |
28 // Mac can use relative time deadlines. | 29 // Mac can use relative time deadlines. |
29 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) | 30 #if !defined(OS_MACOSX) && !defined(OS_NACL) && \ |
| 31 !(defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) |
30 pthread_condattr_t attrs; | 32 pthread_condattr_t attrs; |
31 rv = pthread_condattr_init(&attrs); | 33 rv = pthread_condattr_init(&attrs); |
32 DCHECK_EQ(0, rv); | 34 DCHECK_EQ(0, rv); |
33 pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC); | 35 pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC); |
34 rv = pthread_cond_init(&condition_, &attrs); | 36 rv = pthread_cond_init(&condition_, &attrs); |
35 pthread_condattr_destroy(&attrs); | 37 pthread_condattr_destroy(&attrs); |
36 #else | 38 #else |
37 rv = pthread_cond_init(&condition_, NULL); | 39 rv = pthread_cond_init(&condition_, NULL); |
38 #endif | 40 #endif |
39 DCHECK_EQ(0, rv); | 41 DCHECK_EQ(0, rv); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 absolute_time.tv_sec = now.tv_sec; | 88 absolute_time.tv_sec = now.tv_sec; |
87 absolute_time.tv_nsec = now.tv_nsec; | 89 absolute_time.tv_nsec = now.tv_nsec; |
88 #endif | 90 #endif |
89 | 91 |
90 absolute_time.tv_sec += relative_time.tv_sec; | 92 absolute_time.tv_sec += relative_time.tv_sec; |
91 absolute_time.tv_nsec += relative_time.tv_nsec; | 93 absolute_time.tv_nsec += relative_time.tv_nsec; |
92 absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond; | 94 absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond; |
93 absolute_time.tv_nsec %= Time::kNanosecondsPerSecond; | 95 absolute_time.tv_nsec %= Time::kNanosecondsPerSecond; |
94 DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia | 96 DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia |
95 | 97 |
96 #if defined(OS_ANDROID) | 98 #if defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) |
97 int rv = pthread_cond_timedwait_monotonic_np( | 99 int rv = pthread_cond_timedwait_monotonic_np( |
98 &condition_, user_mutex_, &absolute_time); | 100 &condition_, user_mutex_, &absolute_time); |
99 #else | 101 #else |
100 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); | 102 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); |
101 #endif // OS_ANDROID | 103 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
102 #endif // OS_MACOSX | 104 #endif // OS_MACOSX |
103 | 105 |
104 DCHECK(rv == 0 || rv == ETIMEDOUT); | 106 DCHECK(rv == 0 || rv == ETIMEDOUT); |
105 #if !defined(NDEBUG) | 107 #if !defined(NDEBUG) |
106 user_lock_->CheckUnheldAndMark(); | 108 user_lock_->CheckUnheldAndMark(); |
107 #endif | 109 #endif |
108 } | 110 } |
109 | 111 |
110 void ConditionVariable::Broadcast() { | 112 void ConditionVariable::Broadcast() { |
111 int rv = pthread_cond_broadcast(&condition_); | 113 int rv = pthread_cond_broadcast(&condition_); |
112 DCHECK_EQ(0, rv); | 114 DCHECK_EQ(0, rv); |
113 } | 115 } |
114 | 116 |
115 void ConditionVariable::Signal() { | 117 void ConditionVariable::Signal() { |
116 int rv = pthread_cond_signal(&condition_); | 118 int rv = pthread_cond_signal(&condition_); |
117 DCHECK_EQ(0, rv); | 119 DCHECK_EQ(0, rv); |
118 } | 120 } |
119 | 121 |
120 } // namespace base | 122 } // namespace base |
OLD | NEW |