| 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/time.h" | 13 #include "base/time.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 ConditionVariable::ConditionVariable(Lock* user_lock) | 17 ConditionVariable::ConditionVariable(Lock* user_lock) |
| 17 : user_mutex_(user_lock->lock_.os_lock()) | 18 : user_mutex_(user_lock->lock_.os_lock()) |
| 18 #if !defined(NDEBUG) | 19 #if !defined(NDEBUG) |
| 19 , user_lock_(user_lock) | 20 , user_lock_(user_lock) |
| 20 #endif | 21 #endif |
| 21 { | 22 { |
| 22 int rv = pthread_cond_init(&condition_, NULL); | 23 int rv = pthread_cond_init(&condition_, NULL); |
| 23 DCHECK_EQ(0, rv); | 24 DCHECK_EQ(0, rv); |
| 24 } | 25 } |
| 25 | 26 |
| 26 ConditionVariable::~ConditionVariable() { | 27 ConditionVariable::~ConditionVariable() { |
| 27 int rv = pthread_cond_destroy(&condition_); | 28 int rv = pthread_cond_destroy(&condition_); |
| 28 DCHECK_EQ(0, rv); | 29 DCHECK_EQ(0, rv); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void ConditionVariable::Wait() { | 32 void ConditionVariable::Wait() { |
| 33 base::ThreadRestrictions::AssertWaitAllowed(); |
| 32 #if !defined(NDEBUG) | 34 #if !defined(NDEBUG) |
| 33 user_lock_->CheckHeldAndUnmark(); | 35 user_lock_->CheckHeldAndUnmark(); |
| 34 #endif | 36 #endif |
| 35 int rv = pthread_cond_wait(&condition_, user_mutex_); | 37 int rv = pthread_cond_wait(&condition_, user_mutex_); |
| 36 DCHECK_EQ(0, rv); | 38 DCHECK_EQ(0, rv); |
| 37 #if !defined(NDEBUG) | 39 #if !defined(NDEBUG) |
| 38 user_lock_->CheckUnheldAndMark(); | 40 user_lock_->CheckUnheldAndMark(); |
| 39 #endif | 41 #endif |
| 40 } | 42 } |
| 41 | 43 |
| 42 void ConditionVariable::TimedWait(const TimeDelta& max_time) { | 44 void ConditionVariable::TimedWait(const TimeDelta& max_time) { |
| 45 base::ThreadRestrictions::AssertWaitAllowed(); |
| 43 int64 usecs = max_time.InMicroseconds(); | 46 int64 usecs = max_time.InMicroseconds(); |
| 44 | 47 |
| 45 // The timeout argument to pthread_cond_timedwait is in absolute time. | 48 // The timeout argument to pthread_cond_timedwait is in absolute time. |
| 46 struct timeval now; | 49 struct timeval now; |
| 47 gettimeofday(&now, NULL); | 50 gettimeofday(&now, NULL); |
| 48 | 51 |
| 49 struct timespec abstime; | 52 struct timespec abstime; |
| 50 abstime.tv_sec = now.tv_sec + (usecs / Time::kMicrosecondsPerSecond); | 53 abstime.tv_sec = now.tv_sec + (usecs / Time::kMicrosecondsPerSecond); |
| 51 abstime.tv_nsec = (now.tv_usec + (usecs % Time::kMicrosecondsPerSecond)) * | 54 abstime.tv_nsec = (now.tv_usec + (usecs % Time::kMicrosecondsPerSecond)) * |
| 52 Time::kNanosecondsPerMicrosecond; | 55 Time::kNanosecondsPerMicrosecond; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 68 int rv = pthread_cond_broadcast(&condition_); | 71 int rv = pthread_cond_broadcast(&condition_); |
| 69 DCHECK_EQ(0, rv); | 72 DCHECK_EQ(0, rv); |
| 70 } | 73 } |
| 71 | 74 |
| 72 void ConditionVariable::Signal() { | 75 void ConditionVariable::Signal() { |
| 73 int rv = pthread_cond_signal(&condition_); | 76 int rv = pthread_cond_signal(&condition_); |
| 74 DCHECK_EQ(0, rv); | 77 DCHECK_EQ(0, rv); |
| 75 } | 78 } |
| 76 | 79 |
| 77 } // namespace base | 80 } // namespace base |
| OLD | NEW |