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