Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/condition_variable.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <errno.h> |
michael@0 | 8 | #include <sys/time.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/lock.h" |
michael@0 | 11 | #include "base/lock_impl.h" |
michael@0 | 12 | #include "base/logging.h" |
michael@0 | 13 | #include "base/time.h" |
michael@0 | 14 | |
michael@0 | 15 | using base::Time; |
michael@0 | 16 | using base::TimeDelta; |
michael@0 | 17 | |
michael@0 | 18 | ConditionVariable::ConditionVariable(Lock* user_lock) |
michael@0 | 19 | : user_mutex_(user_lock->lock_impl()->os_lock()) { |
michael@0 | 20 | int rv = 0; |
michael@0 | 21 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
michael@0 | 22 | pthread_condattr_t attrs; |
michael@0 | 23 | rv = pthread_condattr_init(&attrs); |
michael@0 | 24 | DCHECK_EQ(0, rv); |
michael@0 | 25 | pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC); |
michael@0 | 26 | rv = pthread_cond_init(&condition_, &attrs); |
michael@0 | 27 | pthread_condattr_destroy(&attrs); |
michael@0 | 28 | #else |
michael@0 | 29 | rv = pthread_cond_init(&condition_, NULL); |
michael@0 | 30 | #endif |
michael@0 | 31 | DCHECK_EQ(0, rv); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | ConditionVariable::~ConditionVariable() { |
michael@0 | 35 | int rv = pthread_cond_destroy(&condition_); |
michael@0 | 36 | DCHECK(rv == 0); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | void ConditionVariable::Wait() { |
michael@0 | 40 | int rv = pthread_cond_wait(&condition_, user_mutex_); |
michael@0 | 41 | DCHECK(rv == 0); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | void ConditionVariable::TimedWait(const TimeDelta& max_time) { |
michael@0 | 45 | int64_t usecs = max_time.InMicroseconds(); |
michael@0 | 46 | |
michael@0 | 47 | struct timespec relative_time; |
michael@0 | 48 | relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond; |
michael@0 | 49 | relative_time.tv_nsec = |
michael@0 | 50 | (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond; |
michael@0 | 51 | |
michael@0 | 52 | #if defined(OS_MACOSX) |
michael@0 | 53 | int rv = pthread_cond_timedwait_relative_np( |
michael@0 | 54 | &condition_, user_mutex_, &relative_time); |
michael@0 | 55 | #else |
michael@0 | 56 | // The timeout argument to pthread_cond_timedwait is in absolute time. |
michael@0 | 57 | struct timespec now; |
michael@0 | 58 | clock_gettime(CLOCK_MONOTONIC, &now); |
michael@0 | 59 | |
michael@0 | 60 | struct timespec absolute_time; |
michael@0 | 61 | absolute_time.tv_sec = now.tv_sec; |
michael@0 | 62 | absolute_time.tv_nsec = now.tv_nsec; |
michael@0 | 63 | absolute_time.tv_sec += relative_time.tv_sec; |
michael@0 | 64 | absolute_time.tv_nsec += relative_time.tv_nsec; |
michael@0 | 65 | absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond; |
michael@0 | 66 | absolute_time.tv_nsec %= Time::kNanosecondsPerSecond; |
michael@0 | 67 | DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia |
michael@0 | 68 | |
michael@0 | 69 | #if defined(OS_ANDROID) |
michael@0 | 70 | int rv = pthread_cond_timedwait_monotonic_np( |
michael@0 | 71 | &condition_, user_mutex_, &absolute_time); |
michael@0 | 72 | #else |
michael@0 | 73 | int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); |
michael@0 | 74 | #endif // OS_ANDROID |
michael@0 | 75 | #endif // OS_MACOSX |
michael@0 | 76 | |
michael@0 | 77 | DCHECK(rv == 0 || rv == ETIMEDOUT); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | void ConditionVariable::Broadcast() { |
michael@0 | 81 | int rv = pthread_cond_broadcast(&condition_); |
michael@0 | 82 | DCHECK(rv == 0); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void ConditionVariable::Signal() { |
michael@0 | 86 | int rv = pthread_cond_signal(&condition_); |
michael@0 | 87 | DCHECK(rv == 0); |
michael@0 | 88 | } |