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