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 | |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 22 | #include "atomic.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 23 | #include "base/logging.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 24 | #include "cutils/atomic.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 25 | #include "cutils/atomic-inline.h" |
| 26 | #include "mutex-inl.h" |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 27 | #include "runtime.h" |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change.h" |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 29 | #include "thread-inl.h" |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 30 | #include "utils.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 34 | #if ART_USE_FUTEXES |
| 35 | static bool ComputeRelativeTimeSpec(timespec* result_ts, const timespec& lhs, const timespec& rhs) { |
Brian Carlstrom | fb6996f | 2013-07-18 18:21:14 -0700 | [diff] [blame] | 36 | const int32_t one_sec = 1000 * 1000 * 1000; // one second in nanoseconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 37 | result_ts->tv_sec = lhs.tv_sec - rhs.tv_sec; |
| 38 | result_ts->tv_nsec = lhs.tv_nsec - rhs.tv_nsec; |
| 39 | if (result_ts->tv_nsec < 0) { |
| 40 | result_ts->tv_sec--; |
| 41 | result_ts->tv_nsec += one_sec; |
| 42 | } else if (result_ts->tv_nsec > one_sec) { |
| 43 | result_ts->tv_sec++; |
| 44 | result_ts->tv_nsec -= one_sec; |
| 45 | } |
| 46 | return result_ts->tv_sec < 0; |
| 47 | } |
| 48 | #endif |
| 49 | |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 50 | struct AllMutexData { |
| 51 | // A guard for all_mutexes_ that's not a mutex (Mutexes must CAS to acquire and busy wait). |
| 52 | AtomicInteger all_mutexes_guard; |
| 53 | // All created mutexes guarded by all_mutexes_guard_. |
| 54 | std::set<BaseMutex*>* all_mutexes; |
| 55 | AllMutexData() : all_mutexes(NULL) {} |
| 56 | }; |
| 57 | static struct AllMutexData all_mutex_data[kAllMutexDataSize]; |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 58 | |
| 59 | class ScopedAllMutexesLock { |
| 60 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 61 | explicit ScopedAllMutexesLock(const BaseMutex* mutex) : mutex_(mutex) { |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 62 | while (!all_mutex_data->all_mutexes_guard.compare_and_swap(0, reinterpret_cast<int32_t>(mutex))) { |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 63 | NanoSleep(100); |
| 64 | } |
| 65 | } |
| 66 | ~ScopedAllMutexesLock() { |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 67 | while (!all_mutex_data->all_mutexes_guard.compare_and_swap(reinterpret_cast<int32_t>(mutex_), 0)) { |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 68 | NanoSleep(100); |
| 69 | } |
| 70 | } |
| 71 | private: |
| 72 | const BaseMutex* const mutex_; |
| 73 | }; |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 74 | |
| 75 | BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) { |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 76 | if (kLogLockContentions) { |
| 77 | ScopedAllMutexesLock mu(this); |
| 78 | std::set<BaseMutex*>** all_mutexes_ptr = &all_mutex_data->all_mutexes; |
| 79 | if (*all_mutexes_ptr == NULL) { |
| 80 | // We leak the global set of all mutexes to avoid ordering issues in global variable |
| 81 | // construction/destruction. |
| 82 | *all_mutexes_ptr = new std::set<BaseMutex*>(); |
| 83 | } |
| 84 | (*all_mutexes_ptr)->insert(this); |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 85 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | BaseMutex::~BaseMutex() { |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 89 | if (kLogLockContentions) { |
| 90 | ScopedAllMutexesLock mu(this); |
| 91 | all_mutex_data->all_mutexes->erase(this); |
| 92 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void BaseMutex::DumpAll(std::ostream& os) { |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 96 | if (kLogLockContentions) { |
| 97 | os << "Mutex logging:\n"; |
| 98 | ScopedAllMutexesLock mu(reinterpret_cast<const BaseMutex*>(-1)); |
| 99 | std::set<BaseMutex*>* all_mutexes = all_mutex_data->all_mutexes; |
| 100 | if (all_mutexes == NULL) { |
| 101 | // No mutexes have been created yet during at startup. |
| 102 | return; |
| 103 | } |
| 104 | typedef std::set<BaseMutex*>::const_iterator It; |
| 105 | os << "(Contented)\n"; |
| 106 | for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) { |
| 107 | BaseMutex* mutex = *it; |
| 108 | if (mutex->HasEverContended()) { |
| 109 | mutex->Dump(os); |
| 110 | os << "\n"; |
| 111 | } |
| 112 | } |
| 113 | os << "(Never contented)\n"; |
| 114 | for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) { |
| 115 | BaseMutex* mutex = *it; |
| 116 | if (!mutex->HasEverContended()) { |
| 117 | mutex->Dump(os); |
| 118 | os << "\n"; |
| 119 | } |
| 120 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 121 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 122 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 123 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 124 | void BaseMutex::CheckSafeToWait(Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | if (self == NULL) { |
| 126 | CheckUnattachedThread(level_); |
| 127 | return; |
| 128 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 129 | if (kDebugLocking) { |
| 130 | CHECK(self->GetHeldMutex(level_) == this) << "Waiting on unacquired mutex: " << name_; |
| 131 | bool bad_mutexes_held = false; |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 132 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 133 | if (i != level_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 134 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 135 | if (held_mutex != NULL) { |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 136 | LOG(ERROR) << "Holding \"" << held_mutex->name_ << "\" " |
| 137 | << "(level " << LockLevel(i) << ") while performing wait on " |
| 138 | << "\"" << name_ << "\" (level " << level_ << ")"; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 139 | bad_mutexes_held = true; |
| 140 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 143 | CHECK(!bad_mutexes_held); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 144 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 147 | inline void BaseMutex::ContentionLogData::AddToWaitTime(uint64_t value) { |
| 148 | if (kLogLockContentions) { |
| 149 | // Atomically add value to wait_time. |
| 150 | uint64_t new_val, old_val; |
| 151 | volatile int64_t* addr = reinterpret_cast<volatile int64_t*>(&wait_time); |
| 152 | volatile const int64_t* caddr = const_cast<volatile const int64_t*>(addr); |
| 153 | do { |
| 154 | old_val = static_cast<uint64_t>(QuasiAtomic::Read64(caddr)); |
| 155 | new_val = old_val + value; |
| 156 | } while (!QuasiAtomic::Cas64(static_cast<int64_t>(old_val), static_cast<int64_t>(new_val), addr)); |
| 157 | } |
| 158 | } |
| 159 | |
Brian Carlstrom | 0de7985 | 2013-07-25 22:29:58 -0700 | [diff] [blame] | 160 | void BaseMutex::RecordContention(uint64_t blocked_tid, |
| 161 | uint64_t owner_tid, |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 162 | uint64_t nano_time_blocked) { |
| 163 | if (kLogLockContentions) { |
| 164 | ContentionLogData* data = contetion_log_data_; |
| 165 | ++(data->contention_count); |
| 166 | data->AddToWaitTime(nano_time_blocked); |
| 167 | ContentionLogEntry* log = data->contention_log; |
| 168 | // This code is intentionally racy as it is only used for diagnostics. |
| 169 | uint32_t slot = data->cur_content_log_entry; |
| 170 | if (log[slot].blocked_tid == blocked_tid && |
| 171 | log[slot].owner_tid == blocked_tid) { |
| 172 | ++log[slot].count; |
| 173 | } else { |
| 174 | uint32_t new_slot; |
| 175 | do { |
| 176 | slot = data->cur_content_log_entry; |
| 177 | new_slot = (slot + 1) % kContentionLogSize; |
| 178 | } while (!data->cur_content_log_entry.compare_and_swap(slot, new_slot)); |
| 179 | log[new_slot].blocked_tid = blocked_tid; |
| 180 | log[new_slot].owner_tid = owner_tid; |
| 181 | log[new_slot].count = 1; |
| 182 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 183 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 186 | void BaseMutex::DumpContention(std::ostream& os) const { |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 187 | if (kLogLockContentions) { |
| 188 | const ContentionLogData* data = contetion_log_data_; |
| 189 | const ContentionLogEntry* log = data->contention_log; |
| 190 | uint64_t wait_time = data->wait_time; |
| 191 | uint32_t contention_count = data->contention_count; |
| 192 | if (contention_count == 0) { |
| 193 | os << "never contended"; |
| 194 | } else { |
| 195 | os << "contended " << contention_count |
| 196 | << " times, average wait of contender " << PrettyDuration(wait_time / contention_count); |
| 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 = log[i].blocked_tid; |
| 202 | uint64_t owner_tid = log[i].owner_tid; |
| 203 | uint32_t count = 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 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 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 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 226 | } |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 227 | if (max_tid != 0) { |
| 228 | os << " sample shows most blocked tid=" << max_tid; |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 229 | } |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 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 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 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_; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 301 | if (LIKELY(cur_state == 0)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 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. |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 306 | ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid()); |
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) { |
Brian Carlstrom | 0de7985 | 2013-07-25 22:29:58 -0700 | [diff] [blame] | 309 | // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning. |
| 310 | // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock. |
| 311 | if ((errno != EAGAIN) && (errno != EINTR)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 312 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 313 | } |
| 314 | } |
| 315 | android_atomic_dec(&num_contenders_); |
| 316 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 317 | } while (!done); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 318 | DCHECK_EQ(state_, 1); |
| 319 | exclusive_owner_ = SafeGetTid(self); |
| 320 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 321 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 322 | #endif |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 323 | RegisterAsLocked(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 324 | } |
| 325 | recursion_count_++; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 326 | if (kDebugLocking) { |
| 327 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 328 | << name_ << " " << recursion_count_; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 329 | AssertHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 330 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 333 | bool Mutex::ExclusiveTryLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 334 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 335 | if (kDebugLocking && !recursive_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 336 | AssertNotHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 337 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 338 | if (!recursive_ || !IsExclusiveHeld(self)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 339 | #if ART_USE_FUTEXES |
| 340 | bool done = false; |
| 341 | do { |
| 342 | int32_t cur_state = state_; |
| 343 | if (cur_state == 0) { |
| 344 | // Change state from 0 to 1. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 345 | done = android_atomic_acquire_cas(0, 1, &state_) == 0; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 346 | } else { |
| 347 | return false; |
| 348 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 349 | } while (!done); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 350 | DCHECK_EQ(state_, 1); |
| 351 | exclusive_owner_ = SafeGetTid(self); |
| 352 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 353 | int result = pthread_mutex_trylock(&mutex_); |
| 354 | if (result == EBUSY) { |
| 355 | return false; |
| 356 | } |
| 357 | if (result != 0) { |
| 358 | errno = result; |
| 359 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 360 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 361 | #endif |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 362 | RegisterAsLocked(self); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 363 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 364 | recursion_count_++; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 365 | if (kDebugLocking) { |
| 366 | CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: " |
| 367 | << name_ << " " << recursion_count_; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 368 | AssertHeld(self); |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 369 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 373 | void Mutex::ExclusiveUnlock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 374 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 375 | AssertHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 376 | recursion_count_--; |
| 377 | if (!recursive_ || recursion_count_ == 0) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 378 | if (kDebugLocking) { |
| 379 | CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: " |
| 380 | << name_ << " " << recursion_count_; |
| 381 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 382 | RegisterAsUnlocked(self); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 383 | #if ART_USE_FUTEXES |
| 384 | bool done = false; |
| 385 | do { |
| 386 | int32_t cur_state = state_; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 387 | if (LIKELY(cur_state == 1)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 388 | // We're no longer the owner. |
| 389 | exclusive_owner_ = 0; |
| 390 | // Change state to 0. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 391 | done = android_atomic_release_cas(cur_state, 0, &state_) == 0; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 392 | if (LIKELY(done)) { // Spurious fail? |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 393 | // Wake a contender |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 394 | if (UNLIKELY(num_contenders_ > 0)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 395 | futex(&state_, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 396 | } |
| 397 | } |
| 398 | } else { |
Ian Rogers | c4ee12e | 2013-05-16 11:19:53 -0700 | [diff] [blame] | 399 | // Logging acquires the logging lock, avoid infinite recursion in that case. |
| 400 | if (this != Locks::logging_lock_) { |
| 401 | LOG(FATAL) << "Unexpected state_ in unlock " << cur_state << " for " << name_; |
| 402 | } else { |
| 403 | LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1); |
| 404 | LogMessage::LogLine(data, StringPrintf("Unexpected state_ %d in unlock for %s", |
| 405 | cur_state, name_).c_str()); |
| 406 | _exit(1); |
| 407 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 408 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 409 | } while (!done); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 410 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 411 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 412 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 413 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 416 | void Mutex::Dump(std::ostream& os) const { |
| 417 | os << (recursive_ ? "recursive " : "non-recursive ") |
| 418 | << name_ |
| 419 | << " level=" << static_cast<int>(level_) |
| 420 | << " rec=" << recursion_count_ |
| 421 | << " owner=" << GetExclusiveOwnerTid() << " "; |
| 422 | DumpContention(os); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | std::ostream& operator<<(std::ostream& os, const Mutex& mu) { |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 426 | mu.Dump(os); |
| 427 | return os; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 430 | ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level) |
| 431 | : BaseMutex(name, level) |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 432 | #if ART_USE_FUTEXES |
| 433 | , state_(0), exclusive_owner_(0), num_pending_readers_(0), num_pending_writers_(0) |
| 434 | #endif |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 435 | { // NOLINT(whitespace/braces) |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 436 | #if !ART_USE_FUTEXES |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 437 | CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 438 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | ReaderWriterMutex::~ReaderWriterMutex() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 442 | #if ART_USE_FUTEXES |
| 443 | CHECK_EQ(state_, 0); |
| 444 | CHECK_EQ(exclusive_owner_, 0U); |
| 445 | CHECK_EQ(num_pending_readers_, 0); |
| 446 | CHECK_EQ(num_pending_writers_, 0); |
| 447 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 448 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 449 | // may still be using locks. |
| 450 | int rc = pthread_rwlock_destroy(&rwlock_); |
| 451 | if (rc != 0) { |
| 452 | errno = rc; |
| 453 | // 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] | 454 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 455 | Runtime* runtime = Runtime::Current(); |
| 456 | bool shutting_down = runtime == NULL || runtime->IsShuttingDown(); |
| 457 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_rwlock_destroy failed for " << name_; |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 458 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 459 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 462 | void ReaderWriterMutex::ExclusiveLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 463 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 464 | AssertNotExclusiveHeld(self); |
| 465 | #if ART_USE_FUTEXES |
| 466 | bool done = false; |
| 467 | do { |
| 468 | int32_t cur_state = state_; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 469 | if (LIKELY(cur_state == 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 470 | // Change state from 0 to -1. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 471 | done = android_atomic_acquire_cas(0, -1, &state_) == 0; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 472 | } else { |
| 473 | // Failed to acquire, hang up. |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 474 | ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 475 | android_atomic_inc(&num_pending_writers_); |
| 476 | if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) { |
Brian Carlstrom | 0de7985 | 2013-07-25 22:29:58 -0700 | [diff] [blame] | 477 | // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning. |
| 478 | // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock. |
| 479 | if ((errno != EAGAIN) && (errno != EINTR)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 480 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 481 | } |
| 482 | } |
| 483 | android_atomic_dec(&num_pending_writers_); |
| 484 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 485 | } while (!done); |
Ian Rogers | ab47016 | 2012-09-29 23:06:53 -0700 | [diff] [blame] | 486 | DCHECK_EQ(state_, -1); |
| 487 | exclusive_owner_ = SafeGetTid(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 488 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 489 | CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 490 | #endif |
| 491 | RegisterAsLocked(self); |
| 492 | AssertExclusiveHeld(self); |
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::ExclusiveUnlock(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 | AssertExclusiveHeld(self); |
| 498 | RegisterAsUnlocked(self); |
| 499 | #if ART_USE_FUTEXES |
| 500 | bool done = false; |
| 501 | do { |
| 502 | int32_t cur_state = state_; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 503 | if (LIKELY(cur_state == -1)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 504 | // We're no longer the owner. |
| 505 | exclusive_owner_ = 0; |
| 506 | // Change state from -1 to 0. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 507 | done = android_atomic_release_cas(-1, 0, &state_) == 0; |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 508 | if (LIKELY(done)) { // cmpxchg may fail due to noise? |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 509 | // Wake any waiters. |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame^] | 510 | if (UNLIKELY(num_pending_readers_ > 0 || num_pending_writers_ > 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 511 | futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0); |
| 512 | } |
| 513 | } |
| 514 | } else { |
| 515 | LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_; |
| 516 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 517 | } while (!done); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 518 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 519 | CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_)); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 520 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 523 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 524 | bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 525 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 526 | #if ART_USE_FUTEXES |
| 527 | bool done = false; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 528 | timespec end_abs_ts; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 529 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &end_abs_ts); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 530 | do { |
| 531 | int32_t cur_state = state_; |
| 532 | if (cur_state == 0) { |
| 533 | // Change state from 0 to -1. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 534 | done = android_atomic_acquire_cas(0, -1, &state_) == 0; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 535 | } else { |
| 536 | // Failed to acquire, hang up. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 537 | timespec now_abs_ts; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 538 | InitTimeSpec(true, CLOCK_REALTIME, 0, 0, &now_abs_ts); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 539 | timespec rel_ts; |
| 540 | if (ComputeRelativeTimeSpec(&rel_ts, end_abs_ts, now_abs_ts)) { |
| 541 | return false; // Timed out. |
| 542 | } |
Hiroshi Yamauchi | b373308 | 2013-08-12 17:28:49 -0700 | [diff] [blame] | 543 | ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 544 | android_atomic_inc(&num_pending_writers_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 545 | if (futex(&state_, FUTEX_WAIT, cur_state, &rel_ts, NULL, 0) != 0) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 546 | if (errno == ETIMEDOUT) { |
| 547 | android_atomic_dec(&num_pending_writers_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 548 | return false; // Timed out. |
Brian Carlstrom | 0de7985 | 2013-07-25 22:29:58 -0700 | [diff] [blame] | 549 | } else if ((errno != EAGAIN) && (errno != EINTR)) { |
| 550 | // EAGAIN and EINTR both indicate a spurious failure, |
| 551 | // recompute the relative time out from now and try again. |
| 552 | // We don't use TEMP_FAILURE_RETRY so we can recompute rel_ts; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 553 | PLOG(FATAL) << "timed futex wait failed for " << name_; |
| 554 | } |
| 555 | } |
| 556 | android_atomic_dec(&num_pending_writers_); |
| 557 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 558 | } while (!done); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 559 | exclusive_owner_ = SafeGetTid(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 560 | #else |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 561 | timespec ts; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 562 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 563 | int result = pthread_rwlock_timedwrlock(&rwlock_, &ts); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 564 | if (result == ETIMEDOUT) { |
| 565 | return false; |
| 566 | } |
| 567 | if (result != 0) { |
| 568 | errno = result; |
Ian Rogers | a5acfd3 | 2012-08-15 11:50:10 -0700 | [diff] [blame] | 569 | PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 570 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 571 | #endif |
| 572 | RegisterAsLocked(self); |
| 573 | AssertSharedHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 574 | return true; |
| 575 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 576 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 577 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 578 | bool ReaderWriterMutex::SharedTryLock(Thread* self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 579 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 580 | #if ART_USE_FUTEXES |
| 581 | bool done = false; |
| 582 | do { |
| 583 | int32_t cur_state = state_; |
| 584 | if (cur_state >= 0) { |
| 585 | // Add as an extra reader. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 586 | done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 587 | } else { |
| 588 | // Owner holds it exclusively. |
| 589 | return false; |
| 590 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 591 | } while (!done); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 592 | #else |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 593 | int result = pthread_rwlock_tryrdlock(&rwlock_); |
| 594 | if (result == EBUSY) { |
| 595 | return false; |
| 596 | } |
| 597 | if (result != 0) { |
| 598 | errno = result; |
| 599 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
| 600 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 601 | #endif |
| 602 | RegisterAsLocked(self); |
| 603 | AssertSharedHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 604 | return true; |
| 605 | } |
| 606 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 607 | bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 608 | DCHECK(self == NULL || self == Thread::Current()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 609 | bool result; |
| 610 | if (UNLIKELY(self == NULL)) { // Handle unattached threads. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 611 | result = IsExclusiveHeld(self); // TODO: a better best effort here. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 612 | } else { |
| 613 | result = (self->GetHeldMutex(level_) == this); |
| 614 | } |
| 615 | return result; |
| 616 | } |
| 617 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 618 | void ReaderWriterMutex::Dump(std::ostream& os) const { |
| 619 | os << name_ |
| 620 | << " level=" << static_cast<int>(level_) |
| 621 | << " owner=" << GetExclusiveOwnerTid() << " "; |
| 622 | DumpContention(os); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) { |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 626 | mu.Dump(os); |
| 627 | return os; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Ian Rogers | 23055dc | 2013-04-18 16:29:16 -0700 | [diff] [blame] | 630 | ConditionVariable::ConditionVariable(const char* name, Mutex& guard) |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 631 | : name_(name), guard_(guard) { |
| 632 | #if ART_USE_FUTEXES |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 633 | sequence_ = 0; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 634 | num_waiters_ = 0; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 635 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 636 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 637 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | ConditionVariable::~ConditionVariable() { |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 641 | #if ART_USE_FUTEXES |
| 642 | if (num_waiters_!= 0) { |
| 643 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 644 | Runtime* runtime = Runtime::Current(); |
| 645 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 646 | LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_ |
| 647 | << " called with " << num_waiters_ << " waiters."; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 648 | } |
| 649 | #else |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 650 | // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread |
| 651 | // may still be using condition variables. |
| 652 | int rc = pthread_cond_destroy(&cond_); |
| 653 | if (rc != 0) { |
| 654 | errno = rc; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 655 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 656 | Runtime* runtime = Runtime::Current(); |
| 657 | bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(); |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 658 | PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_; |
| 659 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 660 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 663 | void ConditionVariable::Broadcast(Thread* self) { |
| 664 | DCHECK(self == NULL || self == Thread::Current()); |
| 665 | // TODO: enable below, there's a race in thread creation that causes false failures currently. |
| 666 | // guard_.AssertExclusiveHeld(self); |
Mathieu Chartier | e46cd75 | 2012-10-31 16:56:18 -0700 | [diff] [blame] | 667 | DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 668 | #if ART_USE_FUTEXES |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 669 | if (num_waiters_ > 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 670 | android_atomic_inc(&sequence_); // Indicate the broadcast occurred. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 671 | bool done = false; |
| 672 | do { |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 673 | int32_t cur_sequence = sequence_; |
| 674 | // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring |
| 675 | // mutex unlocks will awaken the requeued waiter thread. |
| 676 | done = futex(&sequence_, FUTEX_CMP_REQUEUE, 0, |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 677 | reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()), |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 678 | &guard_.state_, cur_sequence) != -1; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 679 | if (!done) { |
| 680 | if (errno != EAGAIN) { |
| 681 | PLOG(FATAL) << "futex cmp requeue failed for " << name_; |
| 682 | } |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 683 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 684 | } while (!done); |
| 685 | } |
| 686 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 687 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 688 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 691 | void ConditionVariable::Signal(Thread* self) { |
| 692 | DCHECK(self == NULL || self == Thread::Current()); |
| 693 | guard_.AssertExclusiveHeld(self); |
| 694 | #if ART_USE_FUTEXES |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 695 | if (num_waiters_ > 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 696 | android_atomic_inc(&sequence_); // Indicate a signal occurred. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 697 | // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them |
| 698 | // to avoid this, however, requeueing can only move all waiters. |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 699 | int num_woken = futex(&sequence_, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 700 | // 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] | 701 | CHECK((num_woken == 0) || (num_woken == 1)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 702 | } |
| 703 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 704 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 705 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 708 | void ConditionVariable::Wait(Thread* self) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 709 | guard_.CheckSafeToWait(self); |
| 710 | WaitHoldingLocks(self); |
| 711 | } |
| 712 | |
| 713 | void ConditionVariable::WaitHoldingLocks(Thread* self) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 714 | DCHECK(self == NULL || self == Thread::Current()); |
| 715 | guard_.AssertExclusiveHeld(self); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 716 | unsigned int old_recursion_count = guard_.recursion_count_; |
| 717 | #if ART_USE_FUTEXES |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 718 | num_waiters_++; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 719 | // Ensure the Mutex is contended so that requeued threads are awoken. |
| 720 | android_atomic_inc(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 721 | guard_.recursion_count_ = 1; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 722 | int32_t cur_sequence = sequence_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 723 | guard_.ExclusiveUnlock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 724 | if (futex(&sequence_, FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) { |
| 725 | // Futex failed, check it is an expected error. |
| 726 | // EAGAIN == EWOULDBLK, so we let the caller try again. |
| 727 | // EINTR implies a signal was sent to this thread. |
| 728 | if ((errno != EINTR) && (errno != EAGAIN)) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 729 | PLOG(FATAL) << "futex wait failed for " << name_; |
| 730 | } |
| 731 | } |
| 732 | guard_.ExclusiveLock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 733 | CHECK_GE(num_waiters_, 0); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 734 | num_waiters_--; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 735 | // We awoke and so no longer require awakes from the guard_'s unlock. |
| 736 | CHECK_GE(guard_.num_contenders_, 0); |
| 737 | android_atomic_dec(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 738 | #else |
| 739 | guard_.recursion_count_ = 0; |
| 740 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_)); |
| 741 | #endif |
| 742 | guard_.recursion_count_ = old_recursion_count; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 745 | void ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) { |
| 746 | DCHECK(self == NULL || self == Thread::Current()); |
| 747 | guard_.AssertExclusiveHeld(self); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 748 | guard_.CheckSafeToWait(self); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 749 | unsigned int old_recursion_count = guard_.recursion_count_; |
| 750 | #if ART_USE_FUTEXES |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 751 | timespec rel_ts; |
Ian Rogers | 5bd97c4 | 2012-11-27 02:38:26 -0800 | [diff] [blame] | 752 | InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 753 | num_waiters_++; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 754 | // Ensure the Mutex is contended so that requeued threads are awoken. |
| 755 | android_atomic_inc(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 756 | guard_.recursion_count_ = 1; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 757 | int32_t cur_sequence = sequence_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 758 | guard_.ExclusiveUnlock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 759 | if (futex(&sequence_, FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 760 | if (errno == ETIMEDOUT) { |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 761 | // Timed out we're done. |
Brian Carlstrom | 0de7985 | 2013-07-25 22:29:58 -0700 | [diff] [blame] | 762 | } else if ((errno == EAGAIN) || (errno == EINTR)) { |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 763 | // A signal or ConditionVariable::Signal/Broadcast has come in. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 764 | } else { |
| 765 | PLOG(FATAL) << "timed futex wait failed for " << name_; |
| 766 | } |
| 767 | } |
| 768 | guard_.ExclusiveLock(self); |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 769 | CHECK_GE(num_waiters_, 0); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 770 | num_waiters_--; |
Ian Rogers | d45f201 | 2012-11-28 11:46:23 -0800 | [diff] [blame] | 771 | // We awoke and so no longer require awakes from the guard_'s unlock. |
| 772 | CHECK_GE(guard_.num_contenders_, 0); |
| 773 | android_atomic_dec(&guard_.num_contenders_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 774 | #else |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 775 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 776 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 777 | int clock = CLOCK_MONOTONIC; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 778 | #else |
| 779 | #define TIMEDWAIT pthread_cond_timedwait |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 780 | int clock = CLOCK_REALTIME; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 781 | #endif |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 782 | guard_.recursion_count_ = 0; |
| 783 | timespec ts; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 784 | InitTimeSpec(true, clock, ms, ns, &ts); |
| 785 | int rc = TEMP_FAILURE_RETRY(TIMEDWAIT(&cond_, &guard_.mutex_, &ts)); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 786 | if (rc != 0 && rc != ETIMEDOUT) { |
| 787 | errno = rc; |
| 788 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 789 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 790 | #endif |
| 791 | guard_.recursion_count_ = old_recursion_count; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 794 | } // namespace art |