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