Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / base / synchronization / condition_variable_posix.cc
blobc9a2ec43f829e7fdbb73351e8739ad5e699150eb
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
3 // found in the LICENSE file.
5 #include "base/synchronization/condition_variable.h"
7 #include <errno.h>
8 #include <sys/time.h>
10 #include "base/logging.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/time/time.h"
15 namespace base {
17 ConditionVariable::ConditionVariable(Lock* user_lock)
18 : user_mutex_(user_lock->lock_.native_handle())
19 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
20 , user_lock_(user_lock)
21 #endif
23 int rv = 0;
24 // http://crbug.com/293736
25 // NaCl doesn't support monotonic clock based absolute deadlines.
26 // On older Android platform versions, it's supported through the
27 // non-standard pthread_cond_timedwait_monotonic_np. Newer platform
28 // versions have pthread_condattr_setclock.
29 // Mac can use relative time deadlines.
30 #if !defined(OS_MACOSX) && !defined(OS_NACL) && \
31 !(defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
32 pthread_condattr_t attrs;
33 rv = pthread_condattr_init(&attrs);
34 DCHECK_EQ(0, rv);
35 pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC);
36 rv = pthread_cond_init(&condition_, &attrs);
37 pthread_condattr_destroy(&attrs);
38 #else
39 rv = pthread_cond_init(&condition_, NULL);
40 #endif
41 DCHECK_EQ(0, rv);
44 ConditionVariable::~ConditionVariable() {
45 #if defined(OS_MACOSX)
46 // This hack is necessary to avoid a fatal pthreads subsystem bug in the
47 // Darwin kernel. http://crbug.com/517681.
49 base::Lock lock;
50 base::AutoLock l(lock);
51 struct timespec ts;
52 ts.tv_sec = 0;
53 ts.tv_nsec = 1;
54 pthread_cond_timedwait_relative_np(&condition_, lock.lock_.native_handle(),
55 &ts);
57 #endif
59 int rv = pthread_cond_destroy(&condition_);
60 DCHECK_EQ(0, rv);
63 void ConditionVariable::Wait() {
64 base::ThreadRestrictions::AssertWaitAllowed();
65 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
66 user_lock_->CheckHeldAndUnmark();
67 #endif
68 int rv = pthread_cond_wait(&condition_, user_mutex_);
69 DCHECK_EQ(0, rv);
70 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
71 user_lock_->CheckUnheldAndMark();
72 #endif
75 void ConditionVariable::TimedWait(const TimeDelta& max_time) {
76 base::ThreadRestrictions::AssertWaitAllowed();
77 int64 usecs = max_time.InMicroseconds();
78 struct timespec relative_time;
79 relative_time.tv_sec = usecs / Time::kMicrosecondsPerSecond;
80 relative_time.tv_nsec =
81 (usecs % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond;
83 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
84 user_lock_->CheckHeldAndUnmark();
85 #endif
87 #if defined(OS_MACOSX)
88 int rv = pthread_cond_timedwait_relative_np(
89 &condition_, user_mutex_, &relative_time);
90 #else
91 // The timeout argument to pthread_cond_timedwait is in absolute time.
92 struct timespec absolute_time;
93 #if defined(OS_NACL)
94 // See comment in constructor for why this is different in NaCl.
95 struct timeval now;
96 gettimeofday(&now, NULL);
97 absolute_time.tv_sec = now.tv_sec;
98 absolute_time.tv_nsec = now.tv_usec * Time::kNanosecondsPerMicrosecond;
99 #else
100 struct timespec now;
101 clock_gettime(CLOCK_MONOTONIC, &now);
102 absolute_time.tv_sec = now.tv_sec;
103 absolute_time.tv_nsec = now.tv_nsec;
104 #endif
106 absolute_time.tv_sec += relative_time.tv_sec;
107 absolute_time.tv_nsec += relative_time.tv_nsec;
108 absolute_time.tv_sec += absolute_time.tv_nsec / Time::kNanosecondsPerSecond;
109 absolute_time.tv_nsec %= Time::kNanosecondsPerSecond;
110 DCHECK_GE(absolute_time.tv_sec, now.tv_sec); // Overflow paranoia
112 #if defined(OS_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
113 int rv = pthread_cond_timedwait_monotonic_np(
114 &condition_, user_mutex_, &absolute_time);
115 #else
116 int rv = pthread_cond_timedwait(&condition_, user_mutex_, &absolute_time);
117 #endif // OS_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
118 #endif // OS_MACOSX
120 DCHECK(rv == 0 || rv == ETIMEDOUT);
121 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
122 user_lock_->CheckUnheldAndMark();
123 #endif
126 void ConditionVariable::Broadcast() {
127 int rv = pthread_cond_broadcast(&condition_);
128 DCHECK_EQ(0, rv);
131 void ConditionVariable::Signal() {
132 int rv = pthread_cond_signal(&condition_);
133 DCHECK_EQ(0, rv);
136 } // namespace base