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) || defined(DCHECK_ALWAYS_ON) |
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 // On older Android platform versions, it's supported through the | 26 // On older Android platform versions, it's supported through the |
27 // non-standard pthread_cond_timedwait_monotonic_np. Newer platform | 27 // non-standard pthread_cond_timedwait_monotonic_np. Newer platform |
28 // versions have pthread_condattr_setclock. | 28 // versions have pthread_condattr_setclock. |
29 // Mac can use relative time deadlines. | 29 // Mac can use relative time deadlines. |
(...skipping 11 matching lines...) Expand all Loading... |
41 DCHECK_EQ(0, rv); | 41 DCHECK_EQ(0, rv); |
42 } | 42 } |
43 | 43 |
44 ConditionVariable::~ConditionVariable() { | 44 ConditionVariable::~ConditionVariable() { |
45 int rv = pthread_cond_destroy(&condition_); | 45 int rv = pthread_cond_destroy(&condition_); |
46 DCHECK_EQ(0, rv); | 46 DCHECK_EQ(0, rv); |
47 } | 47 } |
48 | 48 |
49 void ConditionVariable::Wait() { | 49 void ConditionVariable::Wait() { |
50 base::ThreadRestrictions::AssertWaitAllowed(); | 50 base::ThreadRestrictions::AssertWaitAllowed(); |
51 #if !defined(NDEBUG) | 51 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
52 user_lock_->CheckHeldAndUnmark(); | 52 user_lock_->CheckHeldAndUnmark(); |
53 #endif | 53 #endif |
54 int rv = pthread_cond_wait(&condition_, user_mutex_); | 54 int rv = pthread_cond_wait(&condition_, user_mutex_); |
55 DCHECK_EQ(0, rv); | 55 DCHECK_EQ(0, rv); |
56 #if !defined(NDEBUG) | 56 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
57 user_lock_->CheckUnheldAndMark(); | 57 user_lock_->CheckUnheldAndMark(); |
58 #endif | 58 #endif |
59 } | 59 } |
60 | 60 |
61 void ConditionVariable::TimedWait(const TimeDelta& max_time) { | 61 void ConditionVariable::TimedWait(const TimeDelta& max_time) { |
62 base::ThreadRestrictions::AssertWaitAllowed(); | 62 base::ThreadRestrictions::AssertWaitAllowed(); |
63 int64 usecs = max_time.InMicroseconds(); | 63 int64 usecs = max_time.InMicroseconds(); |
64 struct timespec relative_time; | 64 struct timespec relative_time; |
65 relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond; | 65 relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond; |
66 relative_time.tv_nsec = | 66 relative_time.tv_nsec = |
67 (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond; | 67 (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond; |
68 | 68 |
69 #if !defined(NDEBUG) | 69 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
70 user_lock_->CheckHeldAndUnmark(); | 70 user_lock_->CheckHeldAndUnmark(); |
71 #endif | 71 #endif |
72 | 72 |
73 #if defined(OS_MACOSX) | 73 #if defined(OS_MACOSX) |
74 int rv = pthread_cond_timedwait_relative_np( | 74 int rv = pthread_cond_timedwait_relative_np( |
75 &condition_, user_mutex_, &relative_time); | 75 &condition_, user_mutex_, &relative_time); |
76 #else | 76 #else |
77 // The timeout argument to pthread_cond_timedwait is in absolute time. | 77 // The timeout argument to pthread_cond_timedwait is in absolute time. |
78 struct timespec absolute_time; | 78 struct timespec absolute_time; |
79 #if defined(OS_NACL) | 79 #if defined(OS_NACL) |
(...skipping 17 matching lines...) Expand all Loading... |
97 | 97 |
98 #if defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) | 98 #if defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) |
99 int rv = pthread_cond_timedwait_monotonic_np( | 99 int rv = pthread_cond_timedwait_monotonic_np( |
100 &condition_, user_mutex_, &absolute_time); | 100 &condition_, user_mutex_, &absolute_time); |
101 #else | 101 #else |
102 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); | 102 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); |
103 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC | 103 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
104 #endif // OS_MACOSX | 104 #endif // OS_MACOSX |
105 | 105 |
106 DCHECK(rv == 0 || rv == ETIMEDOUT); | 106 DCHECK(rv == 0 || rv == ETIMEDOUT); |
107 #if !defined(NDEBUG) | 107 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
108 user_lock_->CheckUnheldAndMark(); | 108 user_lock_->CheckUnheldAndMark(); |
109 #endif | 109 #endif |
110 } | 110 } |
111 | 111 |
112 void ConditionVariable::Broadcast() { | 112 void ConditionVariable::Broadcast() { |
113 int rv = pthread_cond_broadcast(&condition_); | 113 int rv = pthread_cond_broadcast(&condition_); |
114 DCHECK_EQ(0, rv); | 114 DCHECK_EQ(0, rv); |
115 } | 115 } |
116 | 116 |
117 void ConditionVariable::Signal() { | 117 void ConditionVariable::Signal() { |
118 int rv = pthread_cond_signal(&condition_); | 118 int rv = pthread_cond_signal(&condition_); |
119 DCHECK_EQ(0, rv); | 119 DCHECK_EQ(0, rv); |
120 } | 120 } |
121 | 121 |
122 } // namespace base | 122 } // namespace base |
OLD | NEW |