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