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