Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "mutex.h" |
| 18 | |
| 19 | #include <errno.h> |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 20 | #include <sys/time.h> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 21 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame^] | 22 | #include "base/logging.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 23 | #include "cutils/atomic.h" |
| 24 | #include "cutils/atomic-inline.h" |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 25 | #include "runtime.h" |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 27 | #include "thread.h" |
| 28 | #include "utils.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 30 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 31 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 32 | extern int pthread_mutex_lock(pthread_mutex_t* mutex) EXCLUSIVE_LOCK_FUNCTION(mutex); |
| 33 | extern int pthread_mutex_unlock(pthread_mutex_t* mutex) UNLOCK_FUNCTION(1); |
| 34 | extern int pthread_mutex_trylock(pthread_mutex_t* mutex) EXCLUSIVE_TRYLOCK_FUNCTION(0, mutex); |
| 35 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 36 | #if ART_USE_FUTEXES |
Ian Rogers | acc46d6 | 2012-09-27 21:39:40 -0700 | [diff] [blame] | 37 | #include "linux/futex.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 38 | #include "sys/syscall.h" |
| 39 | #ifndef SYS_futex |
| 40 | #define SYS_futex __NR_futex |
| 41 | #endif |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 42 | int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) { |
| 43 | return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 44 | } |
| 45 | #endif // ART_USE_FUTEXES |
| 46 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 47 | namespace art { |
| 48 | |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 49 | // This works on Mac OS 10.6 but hasn't been tested on older releases. |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 50 | struct __attribute__((__may_alias__)) darwin_pthread_mutex_t { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 51 | long padding0; |
| 52 | int padding1; |
| 53 | uint32_t padding2; |
| 54 | int16_t padding3; |
| 55 | int16_t padding4; |
| 56 | uint32_t padding5; |
| 57 | pthread_t darwin_pthread_mutex_owner; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 58 | // ...other stuff we don't care about. |
| 59 | }; |
| 60 | |
| 61 | struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 62 | long padding0; |
| 63 | pthread_mutex_t padding1; |
| 64 | int padding2; |
| 65 | pthread_cond_t padding3; |
| 66 | pthread_cond_t padding4; |
| 67 | int padding5; |
| 68 | int padding6; |
| 69 | pthread_t darwin_pthread_rwlock_owner; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 70 | // ...other stuff we don't care about. |
| 71 | }; |
| 72 | |
| 73 | struct __attribute__((__may_alias__)) glibc_pthread_mutex_t { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | int32_t padding0[2]; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 75 | int owner; |
| 76 | // ...other stuff we don't care about. |
| 77 | }; |
| 78 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 79 | struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t { |
| 80 | #ifdef __LP64__ |
| 81 | int32_t padding0[6]; |
| 82 | #else |
| 83 | int32_t padding0[7]; |
| 84 | #endif |
| 85 | int writer; |
| 86 | // ...other stuff we don't care about. |
| 87 | }; |
| 88 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 89 | static uint64_t SafeGetTid(const Thread* self) { |
| 90 | if (self != NULL) { |
| 91 | return static_cast<uint64_t>(self->GetTid()); |
| 92 | } else { |
| 93 | return static_cast<uint64_t>(GetTid()); |
| 94 | } |
| 95 | } |
| 96 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 97 | #if ART_USE_FUTEXES |
| 98 | static bool ComputeRelativeTimeSpec(timespec* result_ts, const timespec& lhs, const timespec& rhs) { |
| 99 | const long int one_sec = 1000 * 1000 * 1000; // one second in nanoseconds. |
| 100 | result_ts->tv_sec = lhs.tv_sec - rhs.tv_sec; |
| 101 | result_ts->tv_nsec = lhs.tv_nsec - rhs.tv_nsec; |
| 102 | if (result_ts->tv_nsec < 0) { |
| 103 | result_ts->tv_sec--; |
| 104 | result_ts->tv_nsec += one_sec; |
| 105 | } else if (result_ts->tv_nsec > one_sec) { |
| 106 | result_ts->tv_sec++; |
| 107 | result_ts->tv_nsec -= one_sec; |
| 108 | } |
| 109 | return result_ts->tv_sec < 0; |
| 110 | } |
| 111 | #endif |
| 112 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 113 | BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) {} |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 114 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 115 | static void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | // The check below enumerates the cases where we expect not to be able to sanity check locks |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 117 | // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock. |
| 118 | // TODO: tighten this check. |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 119 | if (kDebugLocking) { |
| 120 | Runtime* runtime = Runtime::Current(); |
| 121 | CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() || |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 122 | level == kDefaultMutexLevel || level == kRuntimeShutdownLock || |
| 123 | level == kThreadListLock || level == kLoggingLock || level == kAbortLock); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 124 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 127 | void BaseMutex::RegisterAsLocked(Thread* self) { |
| 128 | if (UNLIKELY(self == NULL)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | CheckUnattachedThread(level_); |
| 130 | return; |
| 131 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 132 | if (kDebugLocking) { |
| 133 | // Check if a bad Mutex of this level or lower is held. |
| 134 | bool bad_mutexes_held = false; |
| 135 | for (int i = level_; i >= 0; --i) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 136 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 137 | if (UNLIKELY(held_mutex != NULL)) { |
| 138 | LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" (level " << i |
| 139 | << ") while locking \"" << name_ << "\" (level " << static_cast<int>(level_) << ")"; |
| 140 | if (i > kAbortLock) { |
| 141 | // Only abort in the check below if this is more than abort level lock. |
| 142 | bad_mutexes_held = true; |
| 143 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 146 | CHECK(!bad_mutexes_held); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 147 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | // Don't record monitors as they are outside the scope of analysis. They may be inspected off of |
| 149 | // the monitor list. |
| 150 | if (level_ != kMonitorLock) { |
| 151 | self->SetHeldMutex(level_, this); |
| 152 | } |
| 153 | } |
| 154 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 155 | void BaseMutex::RegisterAsUnlocked(Thread* self) { |
| 156 | if (UNLIKELY(self == NULL)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 157 | CheckUnattachedThread(level_); |
| 158 | return; |
| 159 | } |
| 160 | if (level_ != kMonitorLock) { |
Brian Carlstrom | 760c943 | 2012-11-29 16:46:27 -0800 | [diff] [blame] | 161 | if (kDebugLocking && !gAborting) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 162 | CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_; |
| 163 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 164 | self->SetHeldMutex(level_, NULL); |
| 165 | } |
| 166 | } |
| 167 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 168 | void BaseMutex::CheckSafeToWait(Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 169 | if (self == NULL) { |
| 170 | CheckUnattachedThread(level_); |
| 171 | return; |
| 172 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 173 | if (kDebugLocking) { |
| 174 | CHECK(self->GetHeldMutex(level_) == this) << "Waiting on unacquired mutex: " << name_; |
| 175 | bool bad_mutexes_held = false; |
| 176 | for (int i = kMaxMutexLevel; i >= 0; --i) { |
| 177 | if (i != level_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 178 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 179 | if (held_mutex != NULL) { |
| 180 | LOG(ERROR) << "Holding " << held_mutex->name_ << " (level " << i |
| 181 | << ") while performing wait on: " |
| 182 | << name_ << " (level " << static_cast<int>(level_) << ")"; |
| 183 | bad_mutexes_held = true; |
| 184 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 187 | CHECK(!bad_mutexes_held); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 188 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 191 | Mutex::Mutex(const char* name, LockLevel level, bool recursive) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 192 | : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 193 | #if ART_USE_FUTEXES |
| 194 | state_ = 0; |
| 195 | exclusive_owner_ = 0; |
| 196 | num_contenders_ = 0; |
| 197 | #elif defined(__BIONIC__) || defined(__APPLE__) |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 198 | // Use recursive mutexes for bionic and Apple otherwise the |
| 199 | // non-recursive mutexes don't have TIDs to check lock ownership of. |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 200 | pthread_mutexattr_t attributes; |
| 201 | CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes)); |
| 202 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE)); |
| 203 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes)); |
| 204 | CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 205 | #else |
| 206 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL)); |
| 207 | #endif |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | Mutex::~Mutex() { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 211 | #if ART_USE_FUTEXES |
| 212 | if (state_ != 0) { |
| 213 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 214 | Runtime* runtime = Runtime::Current(); |
| 215 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
| 216 | LOG(shutting_down ? WARNING : FATAL) << "destroying mutex with owner: " << exclusive_owner_; |
| 217 | } else { |
| 218 | CHECK_EQ(exclusive_owner_, 0U) << "unexpectedly found an owner on unlocked mutex " << name_; |
| 219 | CHECK_EQ(num_contenders_, 0) << "unexpectedly found a contender on mutex " << name_; |
| 220 | } |
| 221 | #else |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 222 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 223 | // may still be using locks. |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 224 | int rc = pthread_mutex_destroy(&mutex_); |
| 225 | if (rc != 0) { |
| 226 | errno = rc; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 227 | // TODO: should we just not log at all if shutting down? this could be the logging mutex! |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 228 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 229 | Runtime* runtime = Runtime::Current(); |
| 230 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 231 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_; |
| 232 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 233 | #endif |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 236 | void Mutex::ExclusiveLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 237 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 238 | if (kDebugLocking && !recursive_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 239 | AssertNotHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 240 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 241 | if (!recursive_ || !IsExclusiveHeld(self)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 242 | #if ART_USE_FUTEXES |
| 243 | bool done = false; |
| 244 | do { |
| 245 | int32_t cur_state = state_; |
| 246 | if (cur_state == 0) { |
| 247 | // Change state from 0 to 1. |
| 248 | done = android_atomic_cmpxchg(0, 1, &state_) == 0; |
| 249 | } else { |
| 250 | // Failed to acquire, hang up. |
| 251 | android_atomic_inc(&num_contenders_); |
| 252 | if (futex(&state_, FUTEX_WAIT, 1, NULL, NULL, 0) != 0) { |
| 253 | if (errno != EAGAIN) { |
| 254 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 255 | } |
| 256 | } |
| 257 | android_atomic_dec(&num_contenders_); |
| 258 | } |
| 259 | } while(!done); |
| 260 | DCHECK_EQ(state_, 1); |
| 261 | exclusive_owner_ = SafeGetTid(self); |
| 262 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 263 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 264 | #endif |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 265 | RegisterAsLocked(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 266 | } |
| 267 | recursion_count_++; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 268 | if (kDebugLocking) { |
| 269 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 270 | << name_ << " " << recursion_count_; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 271 | AssertHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 272 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 275 | bool Mutex::ExclusiveTryLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 276 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 277 | if (kDebugLocking && !recursive_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 278 | AssertNotHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 279 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 280 | if (!recursive_ || !IsExclusiveHeld(self)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 281 | #if ART_USE_FUTEXES |
| 282 | bool done = false; |
| 283 | do { |
| 284 | int32_t cur_state = state_; |
| 285 | if (cur_state == 0) { |
| 286 | // Change state from 0 to 1. |
| 287 | done = android_atomic_cmpxchg(0, 1, &state_) == 0; |
| 288 | } else { |
| 289 | return false; |
| 290 | } |
| 291 | } while(!done); |
| 292 | DCHECK_EQ(state_, 1); |
| 293 | exclusive_owner_ = SafeGetTid(self); |
| 294 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 295 | int result = pthread_mutex_trylock(&mutex_); |
| 296 | if (result == EBUSY) { |
| 297 | return false; |
| 298 | } |
| 299 | if (result != 0) { |
| 300 | errno = result; |
| 301 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 302 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 303 | #endif |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 304 | RegisterAsLocked(self); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 305 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 306 | recursion_count_++; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 307 | if (kDebugLocking) { |
| 308 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 309 | << name_ << " " << recursion_count_; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 310 | AssertHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 311 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 315 | void Mutex::ExclusiveUnlock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 316 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 317 | AssertHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 318 | recursion_count_--; |
| 319 | if (!recursive_ || recursion_count_ == 0) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 320 | if (kDebugLocking) { |
| 321 | CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: " |
| 322 | << name_ << " " << recursion_count_; |
| 323 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 324 | RegisterAsUnlocked(self); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 325 | #if ART_USE_FUTEXES |
| 326 | bool done = false; |
| 327 | do { |
| 328 | int32_t cur_state = state_; |
| 329 | if (cur_state == 1) { |
| 330 | // We're no longer the owner. |
| 331 | exclusive_owner_ = 0; |
| 332 | // Change state to 0. |
| 333 | done = android_atomic_cmpxchg(cur_state, 0, &state_) == 0; |
| 334 | if (done) { // Spurious fail? |
| 335 | // Wake a contender |
| 336 | if (num_contenders_ > 0) { |
| 337 | futex(&state_, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 338 | } |
| 339 | } |
| 340 | } else { |
| 341 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 342 | } |
| 343 | } while(!done); |
| 344 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 345 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 346 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 347 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 350 | bool Mutex::IsExclusiveHeld(const Thread* self) const { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 351 | DCHECK(self == NULL || self == Thread::Current()); |
| 352 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
| 353 | if (kDebugLocking) { |
| 354 | // Sanity debug check that if we think it is locked we have it in our held mutexes. |
Brian Carlstrom | 9e419ca | 2012-11-27 11:31:49 -0800 | [diff] [blame] | 355 | if (result && self != NULL && level_ != kMonitorLock && !gAborting) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 356 | CHECK_EQ(self->GetHeldMutex(level_), this); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 357 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 358 | } |
| 359 | return result; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 362 | uint64_t Mutex::GetExclusiveOwnerTid() const { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 363 | #if ART_USE_FUTEXES |
| 364 | return exclusive_owner_; |
| 365 | #elif defined(__BIONIC__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 366 | return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff); |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 367 | #elif defined(__GLIBC__) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 368 | return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner; |
Elliott Hughes | cf04431 | 2012-01-23 18:48:51 -0800 | [diff] [blame] | 369 | #elif defined(__APPLE__) |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 370 | const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_); |
| 371 | pthread_t owner = dpmutex->darwin_pthread_mutex_owner; |
Brian Carlstrom | bd93c30 | 2012-08-27 16:58:28 -0700 | [diff] [blame] | 372 | // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING |
| 373 | // TODO: should we make darwin_pthread_mutex_owner volatile and recheck until not -1? |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 374 | if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) { |
| 375 | return 0; |
| 376 | } |
| 377 | uint64_t tid; |
| 378 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 379 | return tid; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 380 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 381 | #error unsupported C library |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 382 | #endif |
| 383 | } |
| 384 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 385 | std::string Mutex::Dump() const { |
| 386 | return StringPrintf("%s %s level=%d count=%d owner=%llx", |
| 387 | recursive_ ? "recursive" : "non-recursive", |
| 388 | name_.c_str(), |
| 389 | static_cast<int>(level_), |
| 390 | recursion_count_, |
| 391 | GetExclusiveOwnerTid()); |
| 392 | } |
| 393 | |
| 394 | std::ostream& operator<<(std::ostream& os, const Mutex& mu) { |
| 395 | return os << mu.Dump(); |
| 396 | } |
| 397 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 398 | ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level) : |
| 399 | BaseMutex(name, level) |
| 400 | #if ART_USE_FUTEXES |
| 401 | , state_(0), exclusive_owner_(0), num_pending_readers_(0), num_pending_writers_(0) |
| 402 | #endif |
| 403 | { |
| 404 | #if !ART_USE_FUTEXES |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 405 | CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 406 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | ReaderWriterMutex::~ReaderWriterMutex() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 410 | #if ART_USE_FUTEXES |
| 411 | CHECK_EQ(state_, 0); |
| 412 | CHECK_EQ(exclusive_owner_, 0U); |
| 413 | CHECK_EQ(num_pending_readers_, 0); |
| 414 | CHECK_EQ(num_pending_writers_, 0); |
| 415 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 416 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 417 | // may still be using locks. |
| 418 | int rc = pthread_rwlock_destroy(&rwlock_); |
| 419 | if (rc != 0) { |
| 420 | errno = rc; |
| 421 | // TODO: should we just not log at all if shutting down? this could be the logging mutex! |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 422 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 423 | Runtime* runtime = Runtime::Current(); |
| 424 | bool shutting_down = runtime == NULL || runtime->IsShuttingDown(); |
| 425 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_rwlock_destroy failed for " << name_; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 426 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 427 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 430 | void ReaderWriterMutex::ExclusiveLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 431 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 432 | AssertNotExclusiveHeld(self); |
| 433 | #if ART_USE_FUTEXES |
| 434 | bool done = false; |
| 435 | do { |
| 436 | int32_t cur_state = state_; |
| 437 | if (cur_state == 0) { |
| 438 | // Change state from 0 to -1. |
| 439 | done = android_atomic_cmpxchg(0, -1, &state_) == 0; |
| 440 | } else { |
| 441 | // Failed to acquire, hang up. |
| 442 | android_atomic_inc(&num_pending_writers_); |
| 443 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
| 444 | if (errno != EAGAIN) { |
| 445 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 446 | } |
| 447 | } |
| 448 | android_atomic_dec(&num_pending_writers_); |
| 449 | } |
| 450 | } while(!done); |
Ian Rogers | ab47016 | 2012-09-29 23:06:53 -0700 | [diff] [blame] | 451 | DCHECK_EQ(state_, -1); |
| 452 | exclusive_owner_ = SafeGetTid(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 453 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 454 | CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 455 | #endif |
| 456 | RegisterAsLocked(self); |
| 457 | AssertExclusiveHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 460 | void ReaderWriterMutex::ExclusiveUnlock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 461 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 462 | AssertExclusiveHeld(self); |
| 463 | RegisterAsUnlocked(self); |
| 464 | #if ART_USE_FUTEXES |
| 465 | bool done = false; |
| 466 | do { |
| 467 | int32_t cur_state = state_; |
| 468 | if (cur_state == -1) { |
| 469 | // We're no longer the owner. |
| 470 | exclusive_owner_ = 0; |
| 471 | // Change state from -1 to 0. |
| 472 | done = android_atomic_cmpxchg(-1, 0, &state_) == 0; |
| 473 | if (done) { // cmpxchg may fail due to noise? |
| 474 | // Wake any waiters. |
| 475 | if (num_pending_readers_ > 0 || num_pending_writers_ > 0) { |
| 476 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 477 | } |
| 478 | } |
| 479 | } else { |
| 480 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 481 | } |
| 482 | } while(!done); |
| 483 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 484 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 485 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 488 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 489 | bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 490 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 491 | #if ART_USE_FUTEXES |
| 492 | bool done = false; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 493 | timespec end_abs_ts; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 494 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &end_abs_ts); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 495 | do { |
| 496 | int32_t cur_state = state_; |
| 497 | if (cur_state == 0) { |
| 498 | // Change state from 0 to -1. |
| 499 | done = android_atomic_cmpxchg(0, -1, &state_) == 0; |
| 500 | } else { |
| 501 | // Failed to acquire, hang up. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 502 | timespec now_abs_ts; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 503 | InitTimeSpec(true, CLOCK_REALTIME, 0, 0, &now_abs_ts); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 504 | timespec rel_ts; |
| 505 | if (ComputeRelativeTimeSpec(&rel_ts, end_abs_ts, now_abs_ts)) { |
| 506 | return false; // Timed out. |
| 507 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 508 | android_atomic_inc(&num_pending_writers_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 509 | if (futex(&state_, FUTEX_WAIT, cur_state, &rel_ts, NULL, 0) != 0) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 510 | if (errno == ETIMEDOUT) { |
| 511 | android_atomic_dec(&num_pending_writers_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 512 | return false; // Timed out. |
| 513 | } else if (errno != EAGAIN && errno != EINTR) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 514 | PLOG(FATAL) << "timed futex wait failed for " << name_; |
| 515 | } |
| 516 | } |
| 517 | android_atomic_dec(&num_pending_writers_); |
| 518 | } |
| 519 | } while(!done); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 520 | exclusive_owner_ = SafeGetTid(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 521 | #else |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 522 | timespec ts; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 523 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 524 | int result = pthread_rwlock_timedwrlock(&rwlock_, &ts); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 525 | if (result == ETIMEDOUT) { |
| 526 | return false; |
| 527 | } |
| 528 | if (result != 0) { |
| 529 | errno = result; |
Ian Rogers | a5acfd3 | 2012-08-15 11:50:10 -0700 | [diff] [blame] | 530 | PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 531 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 532 | #endif |
| 533 | RegisterAsLocked(self); |
| 534 | AssertSharedHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 535 | return true; |
| 536 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 537 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 538 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 539 | void ReaderWriterMutex::SharedLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 540 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 541 | #if ART_USE_FUTEXES |
| 542 | bool done = false; |
| 543 | do { |
| 544 | int32_t cur_state = state_; |
| 545 | if (cur_state >= 0) { |
| 546 | // Add as an extra reader. |
| 547 | done = android_atomic_cmpxchg(cur_state, cur_state + 1, &state_) == 0; |
| 548 | } else { |
| 549 | // Owner holds it exclusively, hang up. |
| 550 | android_atomic_inc(&num_pending_readers_); |
| 551 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
| 552 | if (errno != EAGAIN) { |
| 553 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 554 | } |
| 555 | } |
| 556 | android_atomic_dec(&num_pending_readers_); |
| 557 | } |
| 558 | } while(!done); |
| 559 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 560 | CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 561 | #endif |
| 562 | RegisterAsLocked(self); |
| 563 | AssertSharedHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 566 | bool ReaderWriterMutex::SharedTryLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 567 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 568 | #if ART_USE_FUTEXES |
| 569 | bool done = false; |
| 570 | do { |
| 571 | int32_t cur_state = state_; |
| 572 | if (cur_state >= 0) { |
| 573 | // Add as an extra reader. |
| 574 | done = android_atomic_cmpxchg(cur_state, cur_state + 1, &state_) == 0; |
| 575 | } else { |
| 576 | // Owner holds it exclusively. |
| 577 | return false; |
| 578 | } |
| 579 | } while(!done); |
| 580 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 581 | int result = pthread_rwlock_tryrdlock(&rwlock_); |
| 582 | if (result == EBUSY) { |
| 583 | return false; |
| 584 | } |
| 585 | if (result != 0) { |
| 586 | errno = result; |
| 587 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 588 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 589 | #endif |
| 590 | RegisterAsLocked(self); |
| 591 | AssertSharedHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 592 | return true; |
| 593 | } |
| 594 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 595 | void ReaderWriterMutex::SharedUnlock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 596 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 597 | AssertSharedHeld(self); |
| 598 | RegisterAsUnlocked(self); |
| 599 | #if ART_USE_FUTEXES |
| 600 | bool done = false; |
| 601 | do { |
| 602 | int32_t cur_state = state_; |
| 603 | if (LIKELY(cur_state > 0)) { |
| 604 | // Reduce state by 1. |
| 605 | done = android_atomic_cmpxchg(cur_state, cur_state - 1, &state_) == 0; |
| 606 | if (done && (cur_state - 1) == 0) { // cmpxchg may fail due to noise? |
| 607 | if (num_pending_writers_ > 0 || num_pending_readers_ > 0) { |
| 608 | // Wake any exclusive waiters as there are now no readers. |
| 609 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 610 | } |
| 611 | } |
| 612 | } else { |
| 613 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 614 | } |
| 615 | } while(!done); |
| 616 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 617 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 618 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 621 | bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 622 | DCHECK(self == NULL || self == Thread::Current()); |
| 623 | bool result = (GetExclusiveOwnerTid() == SafeGetTid(self)); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 624 | if (kDebugLocking) { |
| 625 | // Sanity that if the pthread thinks we own the lock the Thread agrees. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 626 | if (self != NULL && result) { |
| 627 | CHECK_EQ(self->GetHeldMutex(level_), this); |
| 628 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 629 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 630 | return result; |
| 631 | } |
| 632 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 633 | bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 634 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 635 | bool result; |
| 636 | if (UNLIKELY(self == NULL)) { // Handle unattached threads. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 637 | result = IsExclusiveHeld(self); // TODO: a better best effort here. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 638 | } else { |
| 639 | result = (self->GetHeldMutex(level_) == this); |
| 640 | } |
| 641 | return result; |
| 642 | } |
| 643 | |
| 644 | uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 645 | #if ART_USE_FUTEXES |
| 646 | return exclusive_owner_; |
| 647 | #else |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 648 | #if defined(__BIONIC__) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 649 | return rwlock_.writerThreadId; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 650 | #elif defined(__GLIBC__) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 651 | return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 652 | #elif defined(__APPLE__) |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 653 | const darwin_pthread_rwlock_t* |
| 654 | dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_); |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 655 | pthread_t owner = dprwlock->darwin_pthread_rwlock_owner; |
| 656 | if (owner == (pthread_t)0) { |
| 657 | return 0; |
| 658 | } |
| 659 | uint64_t tid; |
| 660 | CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6 |
| 661 | return tid; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 662 | #else |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 663 | #error unsupported C library |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 664 | #endif |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 665 | #endif |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 668 | std::string ReaderWriterMutex::Dump() const { |
| 669 | return StringPrintf("%s level=%d owner=%llx", |
| 670 | name_.c_str(), |
| 671 | static_cast<int>(level_), |
| 672 | GetExclusiveOwnerTid()); |
| 673 | } |
| 674 | |
| 675 | std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) { |
| 676 | return os << mu.Dump(); |
| 677 | } |
| 678 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 679 | ConditionVariable::ConditionVariable(const std::string& name, Mutex& guard) |
| 680 | : name_(name), guard_(guard) { |
| 681 | #if ART_USE_FUTEXES |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 682 | sequence_ = 0; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 683 | num_waiters_ = 0; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 684 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 685 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 686 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | ConditionVariable::~ConditionVariable() { |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 690 | #if ART_USE_FUTEXES |
| 691 | if (num_waiters_!= 0) { |
| 692 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 693 | Runtime* runtime = Runtime::Current(); |
| 694 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 695 | LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_ |
| 696 | << " called with " << num_waiters_ << " waiters."; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 697 | } |
| 698 | #else |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 699 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 700 | // may still be using condition variables. |
| 701 | int rc = pthread_cond_destroy(&cond_); |
| 702 | if (rc != 0) { |
| 703 | errno = rc; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 704 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 705 | Runtime* runtime = Runtime::Current(); |
| 706 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 707 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_; |
| 708 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 709 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 712 | void ConditionVariable::Broadcast(Thread* self) { |
| 713 | DCHECK(self == NULL || self == Thread::Current()); |
| 714 | // TODO: enable below, there's a race in thread creation that causes false failures currently. |
| 715 | // guard_.AssertExclusiveHeld(self); |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 716 | DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 717 | #if ART_USE_FUTEXES |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 718 | if (num_waiters_ > 0) { |
| 719 | android_atomic_inc(&sequence_); // Indicate the broadcast occurred. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 720 | bool done = false; |
| 721 | do { |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 722 | int32_t cur_sequence = sequence_; |
| 723 | // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring |
| 724 | // mutex unlocks will awaken the requeued waiter thread. |
| 725 | done = futex(&sequence_, FUTEX_CMP_REQUEUE, 0, |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 726 | reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()), |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 727 | &guard_.state_, cur_sequence) != -1; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 728 | if (!done) { |
| 729 | if (errno != EAGAIN) { |
| 730 | PLOG(FATAL) << "futex cmp requeue failed for " << name_; |
| 731 | } |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 732 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 733 | } while (!done); |
| 734 | } |
| 735 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 736 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 737 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 740 | void ConditionVariable::Signal(Thread* self) { |
| 741 | DCHECK(self == NULL || self == Thread::Current()); |
| 742 | guard_.AssertExclusiveHeld(self); |
| 743 | #if ART_USE_FUTEXES |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 744 | if (num_waiters_ > 0) { |
| 745 | android_atomic_inc(&sequence_); // Indicate a signal occurred. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 746 | // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them |
| 747 | // to avoid this, however, requeueing can only move all waiters. |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 748 | int num_woken = futex(&sequence_, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 749 | // Check something was woken or else we changed sequence_ before they had chance to wait. |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 750 | CHECK((num_woken == 0) || (num_woken == 1)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 751 | } |
| 752 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 753 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 754 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 757 | void ConditionVariable::Wait(Thread* self) { |
| 758 | DCHECK(self == NULL || self == Thread::Current()); |
| 759 | guard_.AssertExclusiveHeld(self); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 760 | unsigned int old_recursion_count = guard_.recursion_count_; |
| 761 | #if ART_USE_FUTEXES |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 762 | num_waiters_++; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 763 | // Ensure the Mutex is contended so that requeued threads are awoken. |
| 764 | android_atomic_inc(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 765 | guard_.recursion_count_ = 1; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 766 | int32_t cur_sequence = sequence_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 767 | guard_.ExclusiveUnlock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 768 | if (futex(&sequence_, FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) { |
| 769 | // Futex failed, check it is an expected error. |
| 770 | // EAGAIN == EWOULDBLK, so we let the caller try again. |
| 771 | // EINTR implies a signal was sent to this thread. |
| 772 | if ((errno != EINTR) && (errno != EAGAIN)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 773 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 774 | } |
| 775 | } |
| 776 | guard_.ExclusiveLock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 777 | CHECK_GE(num_waiters_, 0); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 778 | num_waiters_--; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 779 | // We awoke and so no longer require awakes from the guard_'s unlock. |
| 780 | CHECK_GE(guard_.num_contenders_, 0); |
| 781 | android_atomic_dec(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 782 | #else |
| 783 | guard_.recursion_count_ = 0; |
| 784 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_)); |
| 785 | #endif |
| 786 | guard_.recursion_count_ = old_recursion_count; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 789 | void ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) { |
| 790 | DCHECK(self == NULL || self == Thread::Current()); |
| 791 | guard_.AssertExclusiveHeld(self); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 792 | unsigned int old_recursion_count = guard_.recursion_count_; |
| 793 | #if ART_USE_FUTEXES |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 794 | timespec rel_ts; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 795 | InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 796 | num_waiters_++; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 797 | // Ensure the Mutex is contended so that requeued threads are awoken. |
| 798 | android_atomic_inc(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 799 | guard_.recursion_count_ = 1; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 800 | int32_t cur_sequence = sequence_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 801 | guard_.ExclusiveUnlock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 802 | if (futex(&sequence_, FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 803 | if (errno == ETIMEDOUT) { |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 804 | // Timed out we're done. |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 805 | } else if ((errno == EINTR) || (errno == EAGAIN)) { |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 806 | // A signal or ConditionVariable::Signal/Broadcast has come in. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 807 | } else { |
| 808 | PLOG(FATAL) << "timed futex wait failed for " << name_; |
| 809 | } |
| 810 | } |
| 811 | guard_.ExclusiveLock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 812 | CHECK_GE(num_waiters_, 0); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 813 | num_waiters_--; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 814 | // We awoke and so no longer require awakes from the guard_'s unlock. |
| 815 | CHECK_GE(guard_.num_contenders_, 0); |
| 816 | android_atomic_dec(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 817 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 818 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 819 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 820 | int clock = CLOCK_MONOTONIC; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 821 | #else |
| 822 | #define TIMEDWAIT pthread_cond_timedwait |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 823 | int clock = CLOCK_REALTIME; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 824 | #endif |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 825 | guard_.recursion_count_ = 0; |
| 826 | timespec ts; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 827 | InitTimeSpec(true, clock, ms, ns, &ts); |
| 828 | int rc = TEMP_FAILURE_RETRY(TIMEDWAIT(&cond_, &guard_.mutex_, &ts)); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 829 | if (rc != 0 && rc != ETIMEDOUT) { |
| 830 | errno = rc; |
| 831 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 832 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 833 | #endif |
| 834 | guard_.recursion_count_ = old_recursion_count; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 837 | } // namespace art |