|
1 // Copyright (c) 2006-2008 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. |
|
4 |
|
5 #include "base/condition_variable.h" |
|
6 |
|
7 #include <errno.h> |
|
8 #include <sys/time.h> |
|
9 |
|
10 #include "base/lock.h" |
|
11 #include "base/lock_impl.h" |
|
12 #include "base/logging.h" |
|
13 #include "base/time.h" |
|
14 |
|
15 using base::Time; |
|
16 using base::TimeDelta; |
|
17 |
|
18 ConditionVariable::ConditionVariable(Lock* user_lock) |
|
19 : user_mutex_(user_lock->lock_impl()->os_lock()) { |
|
20 int rv = 0; |
|
21 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
|
22 pthread_condattr_t attrs; |
|
23 rv = pthread_condattr_init(&attrs); |
|
24 DCHECK_EQ(0, rv); |
|
25 pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC); |
|
26 rv = pthread_cond_init(&condition_, &attrs); |
|
27 pthread_condattr_destroy(&attrs); |
|
28 #else |
|
29 rv = pthread_cond_init(&condition_, NULL); |
|
30 #endif |
|
31 DCHECK_EQ(0, rv); |
|
32 } |
|
33 |
|
34 ConditionVariable::~ConditionVariable() { |
|
35 int rv = pthread_cond_destroy(&condition_); |
|
36 DCHECK(rv == 0); |
|
37 } |
|
38 |
|
39 void ConditionVariable::Wait() { |
|
40 int rv = pthread_cond_wait(&condition_, user_mutex_); |
|
41 DCHECK(rv == 0); |
|
42 } |
|
43 |
|
44 void ConditionVariable::TimedWait(const TimeDelta& max_time) { |
|
45 int64_t usecs = max_time.InMicroseconds(); |
|
46 |
|
47 struct timespec relative_time; |
|
48 relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond; |
|
49 relative_time.tv_nsec = |
|
50 (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond; |
|
51 |
|
52 #if defined(OS_MACOSX) |
|
53 int rv = pthread_cond_timedwait_relative_np( |
|
54 &condition_, user_mutex_, &relative_time); |
|
55 #else |
|
56 // The timeout argument to pthread_cond_timedwait is in absolute time. |
|
57 struct timespec now; |
|
58 clock_gettime(CLOCK_MONOTONIC, &now); |
|
59 |
|
60 struct timespec absolute_time; |
|
61 absolute_time.tv_sec = now.tv_sec; |
|
62 absolute_time.tv_nsec = now.tv_nsec; |
|
63 absolute_time.tv_sec += relative_time.tv_sec; |
|
64 absolute_time.tv_nsec += relative_time.tv_nsec; |
|
65 absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond; |
|
66 absolute_time.tv_nsec %= Time::kNanosecondsPerSecond; |
|
67 DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia |
|
68 |
|
69 #if defined(OS_ANDROID) |
|
70 int rv = pthread_cond_timedwait_monotonic_np( |
|
71 &condition_, user_mutex_, &absolute_time); |
|
72 #else |
|
73 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time); |
|
74 #endif // OS_ANDROID |
|
75 #endif // OS_MACOSX |
|
76 |
|
77 DCHECK(rv == 0 || rv == ETIMEDOUT); |
|
78 } |
|
79 |
|
80 void ConditionVariable::Broadcast() { |
|
81 int rv = pthread_cond_broadcast(&condition_); |
|
82 DCHECK(rv == 0); |
|
83 } |
|
84 |
|
85 void ConditionVariable::Signal() { |
|
86 int rv = pthread_cond_signal(&condition_); |
|
87 DCHECK(rv == 0); |
|
88 } |