blob: 70b6f7e295bd439d15524bdc2a1928ca1124c91c [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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 Rogersc604d732012-10-14 16:09:54 -070020#include <sys/time.h>
Elliott Hughes8daa0922011-09-11 13:46:25 -070021
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070022#include "atomic.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers693ff612013-02-01 10:56:12 -080024#include "mutex-inl.h"
Elliott Hughes6b355752012-01-13 16:49:08 -080025#include "runtime.h"
Ian Rogersc604d732012-10-14 16:09:54 -070026#include "scoped_thread_state_change.h"
Ian Rogers04d7aa92013-03-16 14:29:17 -070027#include "thread-inl.h"
Elliott Hughesffb465f2012-03-01 18:46:05 -080028#include "utils.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070029
30namespace art {
31
Ian Rogers719d1a32014-03-06 12:13:39 -080032Mutex* Locks::abort_lock_ = nullptr;
Brian Carlstrom306db812014-09-05 13:01:41 -070033Mutex* Locks::alloc_tracker_lock_ = nullptr;
Andreas Gampe74240812014-04-17 10:35:09 -070034Mutex* Locks::allocated_monitor_ids_lock_ = nullptr;
Chao-ying Fu9e369312014-05-21 11:20:52 -070035Mutex* Locks::allocated_thread_ids_lock_ = nullptr;
Sebastien Hertzed2be172014-08-19 15:33:43 +020036ReaderWriterMutex* Locks::breakpoint_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080037ReaderWriterMutex* Locks::classlinker_classes_lock_ = nullptr;
Brian Carlstrom306db812014-09-05 13:01:41 -070038Mutex* Locks::deoptimization_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080039ReaderWriterMutex* Locks::heap_bitmap_lock_ = nullptr;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -070040Mutex* Locks::instrument_entrypoints_lock_ = nullptr;
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070041Mutex* Locks::intern_table_lock_ = nullptr;
Ian Rogers68d8b422014-07-17 11:09:10 -070042Mutex* Locks::jni_libraries_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080043Mutex* Locks::logging_lock_ = nullptr;
Hiroshi Yamauchi3eed93d2014-06-04 11:43:59 -070044Mutex* Locks::mem_maps_lock_ = nullptr;
Chao-ying Fu9e369312014-05-21 11:20:52 -070045Mutex* Locks::modify_ldt_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080046ReaderWriterMutex* Locks::mutator_lock_ = nullptr;
Brian Carlstrom306db812014-09-05 13:01:41 -070047Mutex* Locks::profiler_lock_ = nullptr;
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070048Mutex* Locks::reference_processor_lock_ = nullptr;
49Mutex* Locks::reference_queue_cleared_references_lock_ = nullptr;
50Mutex* Locks::reference_queue_finalizer_references_lock_ = nullptr;
51Mutex* Locks::reference_queue_phantom_references_lock_ = nullptr;
52Mutex* Locks::reference_queue_soft_references_lock_ = nullptr;
53Mutex* Locks::reference_queue_weak_references_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080054Mutex* Locks::runtime_shutdown_lock_ = nullptr;
55Mutex* Locks::thread_list_lock_ = nullptr;
Ian Rogersf3d874c2014-07-17 18:52:42 -070056Mutex* Locks::thread_list_suspend_thread_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080057Mutex* Locks::thread_suspend_count_lock_ = nullptr;
58Mutex* Locks::trace_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080059Mutex* Locks::unexpected_signal_lock_ = nullptr;
Ian Rogers719d1a32014-03-06 12:13:39 -080060
61struct 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};
68static struct AllMutexData gAllMutexData[kAllMutexDataSize];
69
Ian Rogersc604d732012-10-14 16:09:54 -070070#if ART_USE_FUTEXES
71static bool ComputeRelativeTimeSpec(timespec* result_ts, const timespec& lhs, const timespec& rhs) {
Brian Carlstromfb6996f2013-07-18 18:21:14 -070072 const int32_t one_sec = 1000 * 1000 * 1000; // one second in nanoseconds.
Ian Rogersc604d732012-10-14 16:09:54 -070073 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 Rogers6f3dbba2014-10-14 17:41:57 -070086class ScopedAllMutexesLock FINAL {
Ian Rogers56edc432013-01-18 16:51:51 -080087 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070088 explicit ScopedAllMutexesLock(const BaseMutex* mutex) : mutex_(mutex) {
Ian Rogers3e5cf302014-05-20 16:40:37 -070089 while (!gAllMutexData->all_mutexes_guard.CompareExchangeWeakAcquire(0, mutex)) {
Ian Rogers56edc432013-01-18 16:51:51 -080090 NanoSleep(100);
91 }
92 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -070093
Ian Rogers56edc432013-01-18 16:51:51 -080094 ~ScopedAllMutexesLock() {
Ian Rogers6f3dbba2014-10-14 17:41:57 -070095#if !defined(__clang__)
96 // TODO: remove this workaround target GCC/libc++/bionic bug "invalid failure memory model".
97 while (!gAllMutexData->all_mutexes_guard.CompareExchangeWeakSequentiallyConsistent(mutex_, 0)) {
98#else
Ian Rogers3e5cf302014-05-20 16:40:37 -070099 while (!gAllMutexData->all_mutexes_guard.CompareExchangeWeakRelease(mutex_, 0)) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700100#endif
Ian Rogers56edc432013-01-18 16:51:51 -0800101 NanoSleep(100);
102 }
103 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700104
Ian Rogers56edc432013-01-18 16:51:51 -0800105 private:
106 const BaseMutex* const mutex_;
107};
Ian Rogers56edc432013-01-18 16:51:51 -0800108
109BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700110 if (kLogLockContentions) {
111 ScopedAllMutexesLock mu(this);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700112 std::set<BaseMutex*>** all_mutexes_ptr = &gAllMutexData->all_mutexes;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700113 if (*all_mutexes_ptr == NULL) {
114 // We leak the global set of all mutexes to avoid ordering issues in global variable
115 // construction/destruction.
116 *all_mutexes_ptr = new std::set<BaseMutex*>();
117 }
118 (*all_mutexes_ptr)->insert(this);
Ian Rogers56edc432013-01-18 16:51:51 -0800119 }
Ian Rogers56edc432013-01-18 16:51:51 -0800120}
121
122BaseMutex::~BaseMutex() {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700123 if (kLogLockContentions) {
124 ScopedAllMutexesLock mu(this);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700125 gAllMutexData->all_mutexes->erase(this);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700126 }
Ian Rogers56edc432013-01-18 16:51:51 -0800127}
128
129void BaseMutex::DumpAll(std::ostream& os) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700130 if (kLogLockContentions) {
131 os << "Mutex logging:\n";
132 ScopedAllMutexesLock mu(reinterpret_cast<const BaseMutex*>(-1));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700133 std::set<BaseMutex*>* all_mutexes = gAllMutexData->all_mutexes;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700134 if (all_mutexes == NULL) {
135 // No mutexes have been created yet during at startup.
136 return;
137 }
138 typedef std::set<BaseMutex*>::const_iterator It;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700139 os << "(Contended)\n";
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700140 for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) {
141 BaseMutex* mutex = *it;
142 if (mutex->HasEverContended()) {
143 mutex->Dump(os);
144 os << "\n";
145 }
146 }
147 os << "(Never contented)\n";
148 for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) {
149 BaseMutex* mutex = *it;
150 if (!mutex->HasEverContended()) {
151 mutex->Dump(os);
152 os << "\n";
153 }
154 }
Ian Rogers56edc432013-01-18 16:51:51 -0800155 }
Ian Rogers56edc432013-01-18 16:51:51 -0800156}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157
Ian Rogers81d425b2012-09-27 16:03:43 -0700158void BaseMutex::CheckSafeToWait(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 if (self == NULL) {
160 CheckUnattachedThread(level_);
161 return;
162 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700163 if (kDebugLocking) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700164 CHECK(self->GetHeldMutex(level_) == this || level_ == kMonitorLock)
165 << "Waiting on unacquired mutex: " << name_;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700166 bool bad_mutexes_held = false;
Elliott Hughes0f827162013-02-26 12:12:58 -0800167 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700168 if (i != level_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700169 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Ian Rogersf3d874c2014-07-17 18:52:42 -0700170 // We expect waits to happen while holding the thread list suspend thread lock.
171 if (held_mutex != NULL && i != kThreadListSuspendThreadLock) {
Elliott Hughes0f827162013-02-26 12:12:58 -0800172 LOG(ERROR) << "Holding \"" << held_mutex->name_ << "\" "
173 << "(level " << LockLevel(i) << ") while performing wait on "
174 << "\"" << name_ << "\" (level " << level_ << ")";
Ian Rogers25fd14b2012-09-05 10:56:38 -0700175 bad_mutexes_held = true;
176 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 }
178 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700179 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181}
182
Ian Rogers37f3c962014-07-17 11:25:30 -0700183void BaseMutex::ContentionLogData::AddToWaitTime(uint64_t value) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700184 if (kLogLockContentions) {
185 // Atomically add value to wait_time.
Ian Rogers37f3c962014-07-17 11:25:30 -0700186 wait_time.FetchAndAddSequentiallyConsistent(value);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700187 }
188}
189
Brian Carlstrom0de79852013-07-25 22:29:58 -0700190void BaseMutex::RecordContention(uint64_t blocked_tid,
191 uint64_t owner_tid,
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700192 uint64_t nano_time_blocked) {
193 if (kLogLockContentions) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700194 ContentionLogData* data = contention_log_data_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700195 ++(data->contention_count);
196 data->AddToWaitTime(nano_time_blocked);
197 ContentionLogEntry* log = data->contention_log;
198 // This code is intentionally racy as it is only used for diagnostics.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700199 uint32_t slot = data->cur_content_log_entry.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700200 if (log[slot].blocked_tid == blocked_tid &&
201 log[slot].owner_tid == blocked_tid) {
202 ++log[slot].count;
203 } else {
204 uint32_t new_slot;
205 do {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700206 slot = data->cur_content_log_entry.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700207 new_slot = (slot + 1) % kContentionLogSize;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700208 } while (!data->cur_content_log_entry.CompareExchangeWeakRelaxed(slot, new_slot));
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700209 log[new_slot].blocked_tid = blocked_tid;
210 log[new_slot].owner_tid = owner_tid;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700211 log[new_slot].count.StoreRelaxed(1);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700212 }
Ian Rogers56edc432013-01-18 16:51:51 -0800213 }
Ian Rogers56edc432013-01-18 16:51:51 -0800214}
215
Ian Rogers56edc432013-01-18 16:51:51 -0800216void BaseMutex::DumpContention(std::ostream& os) const {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700217 if (kLogLockContentions) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700218 const ContentionLogData* data = contention_log_data_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700219 const ContentionLogEntry* log = data->contention_log;
Ian Rogers37f3c962014-07-17 11:25:30 -0700220 uint64_t wait_time = data->wait_time.LoadRelaxed();
Ian Rogers3e5cf302014-05-20 16:40:37 -0700221 uint32_t contention_count = data->contention_count.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700222 if (contention_count == 0) {
223 os << "never contended";
224 } else {
225 os << "contended " << contention_count
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700226 << " total wait of contender " << PrettyDuration(wait_time)
227 << " average " << PrettyDuration(wait_time / contention_count);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700228 SafeMap<uint64_t, size_t> most_common_blocker;
229 SafeMap<uint64_t, size_t> most_common_blocked;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700230 for (size_t i = 0; i < kContentionLogSize; ++i) {
231 uint64_t blocked_tid = log[i].blocked_tid;
232 uint64_t owner_tid = log[i].owner_tid;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700233 uint32_t count = log[i].count.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700234 if (count > 0) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700235 auto it = most_common_blocked.find(blocked_tid);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700236 if (it != most_common_blocked.end()) {
237 most_common_blocked.Overwrite(blocked_tid, it->second + count);
238 } else {
239 most_common_blocked.Put(blocked_tid, count);
240 }
241 it = most_common_blocker.find(owner_tid);
242 if (it != most_common_blocker.end()) {
243 most_common_blocker.Overwrite(owner_tid, it->second + count);
244 } else {
245 most_common_blocker.Put(owner_tid, count);
246 }
Ian Rogers56edc432013-01-18 16:51:51 -0800247 }
248 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700249 uint64_t max_tid = 0;
250 size_t max_tid_count = 0;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700251 for (const auto& pair : most_common_blocked) {
252 if (pair.second > max_tid_count) {
253 max_tid = pair.first;
254 max_tid_count = pair.second;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700255 }
Ian Rogers56edc432013-01-18 16:51:51 -0800256 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700257 if (max_tid != 0) {
258 os << " sample shows most blocked tid=" << max_tid;
Ian Rogers56edc432013-01-18 16:51:51 -0800259 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700260 max_tid = 0;
261 max_tid_count = 0;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700262 for (const auto& pair : most_common_blocker) {
263 if (pair.second > max_tid_count) {
264 max_tid = pair.first;
265 max_tid_count = pair.second;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700266 }
267 }
268 if (max_tid != 0) {
269 os << " sample shows tid=" << max_tid << " owning during this time";
270 }
Ian Rogers56edc432013-01-18 16:51:51 -0800271 }
272 }
Ian Rogers56edc432013-01-18 16:51:51 -0800273}
274
275
Ian Rogers81d425b2012-09-27 16:03:43 -0700276Mutex::Mutex(const char* name, LockLevel level, bool recursive)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700278#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700279 DCHECK_EQ(0, state_.LoadRelaxed());
Ian Rogers3e5cf302014-05-20 16:40:37 -0700280 DCHECK_EQ(0, num_contenders_.LoadRelaxed());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700282 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, nullptr));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700284 exclusive_owner_ = 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700285}
286
287Mutex::~Mutex() {
Ian Rogersc604d732012-10-14 16:09:54 -0700288#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700289 if (state_.LoadRelaxed() != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700290 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700291 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersc604d732012-10-14 16:09:54 -0700292 LOG(shutting_down ? WARNING : FATAL) << "destroying mutex with owner: " << exclusive_owner_;
293 } else {
294 CHECK_EQ(exclusive_owner_, 0U) << "unexpectedly found an owner on unlocked mutex " << name_;
Ian Rogersc7190692014-07-08 23:50:26 -0700295 CHECK_EQ(num_contenders_.LoadSequentiallyConsistent(), 0)
Ian Rogers3e5cf302014-05-20 16:40:37 -0700296 << "unexpectedly found a contender on mutex " << name_;
Ian Rogersc604d732012-10-14 16:09:54 -0700297 }
298#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700299 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
300 // may still be using locks.
Elliott Hughes6b355752012-01-13 16:49:08 -0800301 int rc = pthread_mutex_destroy(&mutex_);
302 if (rc != 0) {
303 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800304 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Ian Rogers50b35e22012-10-04 10:09:15 -0700305 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700306 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800307 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughes6b355752012-01-13 16:49:08 -0800308 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
309 }
Ian Rogersc604d732012-10-14 16:09:54 -0700310#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -0700311}
312
Ian Rogers81d425b2012-09-27 16:03:43 -0700313void Mutex::ExclusiveLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700314 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers25fd14b2012-09-05 10:56:38 -0700315 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700316 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700317 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700318 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700319#if ART_USE_FUTEXES
320 bool done = false;
321 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700322 int32_t cur_state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700323 if (LIKELY(cur_state == 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700324 // Change state from 0 to 1 and impose load/store ordering appropriate for lock acquisition.
325 done = state_.CompareExchangeWeakAcquire(0 /* cur_state */, 1 /* new state */);
Ian Rogersc604d732012-10-14 16:09:54 -0700326 } else {
327 // Failed to acquire, hang up.
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700328 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersb122a4b2013-11-19 18:00:50 -0800329 num_contenders_++;
Ian Rogersc7190692014-07-08 23:50:26 -0700330 if (futex(state_.Address(), FUTEX_WAIT, 1, NULL, NULL, 0) != 0) {
Brian Carlstrom0de79852013-07-25 22:29:58 -0700331 // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
332 // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock.
333 if ((errno != EAGAIN) && (errno != EINTR)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700334 PLOG(FATAL) << "futex wait failed for " << name_;
335 }
336 }
Ian Rogersb122a4b2013-11-19 18:00:50 -0800337 num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700338 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700339 } while (!done);
Ian Rogersc7190692014-07-08 23:50:26 -0700340 DCHECK_EQ(state_.LoadRelaxed(), 1);
Ian Rogersc604d732012-10-14 16:09:54 -0700341#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogersc604d732012-10-14 16:09:54 -0700343#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700344 DCHECK_EQ(exclusive_owner_, 0U);
345 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700346 RegisterAsLocked(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 }
348 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700349 if (kDebugLocking) {
350 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
351 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700352 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700353 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700354}
355
Ian Rogers81d425b2012-09-27 16:03:43 -0700356bool Mutex::ExclusiveTryLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700357 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers25fd14b2012-09-05 10:56:38 -0700358 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700359 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700360 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700361 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700362#if ART_USE_FUTEXES
363 bool done = false;
364 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700365 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700366 if (cur_state == 0) {
Ian Rogersc7190692014-07-08 23:50:26 -0700367 // Change state from 0 to 1 and impose load/store ordering appropriate for lock acquisition.
368 done = state_.CompareExchangeWeakAcquire(0 /* cur_state */, 1 /* new state */);
Ian Rogersc604d732012-10-14 16:09:54 -0700369 } else {
370 return false;
371 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700372 } while (!done);
Ian Rogersc7190692014-07-08 23:50:26 -0700373 DCHECK_EQ(state_.LoadRelaxed(), 1);
Ian Rogersc604d732012-10-14 16:09:54 -0700374#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700375 int result = pthread_mutex_trylock(&mutex_);
376 if (result == EBUSY) {
377 return false;
378 }
379 if (result != 0) {
380 errno = result;
381 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
382 }
Ian Rogersc604d732012-10-14 16:09:54 -0700383#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700384 DCHECK_EQ(exclusive_owner_, 0U);
385 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700386 RegisterAsLocked(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700387 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700389 if (kDebugLocking) {
390 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
391 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700392 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700393 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700394 return true;
395}
396
Ian Rogers81d425b2012-09-27 16:03:43 -0700397void Mutex::ExclusiveUnlock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700398 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700399 AssertHeld(self);
Ian Rogersc5f17732014-06-05 20:48:42 -0700400 DCHECK_NE(exclusive_owner_, 0U);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 recursion_count_--;
402 if (!recursive_ || recursion_count_ == 0) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700403 if (kDebugLocking) {
404 CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: "
405 << name_ << " " << recursion_count_;
406 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700407 RegisterAsUnlocked(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700408#if ART_USE_FUTEXES
Ian Rogersc5f17732014-06-05 20:48:42 -0700409 bool done = false;
410 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700411 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc5f17732014-06-05 20:48:42 -0700412 if (LIKELY(cur_state == 1)) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700413 // We're no longer the owner.
414 exclusive_owner_ = 0;
Ian Rogersc7190692014-07-08 23:50:26 -0700415 // Change state to 0 and impose load/store ordering appropriate for lock release.
416 // Note, the relaxed loads below musn't reorder before the CompareExchange.
417 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
418 // a status bit into the state on contention.
419 done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, 0 /* new state */);
Ian Rogersc5f17732014-06-05 20:48:42 -0700420 if (LIKELY(done)) { // Spurious fail?
Ian Rogersc7190692014-07-08 23:50:26 -0700421 // Wake a contender.
Ian Rogersc5f17732014-06-05 20:48:42 -0700422 if (UNLIKELY(num_contenders_.LoadRelaxed() > 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700423 futex(state_.Address(), FUTEX_WAKE, 1, NULL, NULL, 0);
Ian Rogersc5f17732014-06-05 20:48:42 -0700424 }
425 }
426 } else {
427 // Logging acquires the logging lock, avoid infinite recursion in that case.
428 if (this != Locks::logging_lock_) {
429 LOG(FATAL) << "Unexpected state_ in unlock " << cur_state << " for " << name_;
430 } else {
431 LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
432 LogMessage::LogLine(data, StringPrintf("Unexpected state_ %d in unlock for %s",
433 cur_state, name_).c_str());
434 _exit(1);
Ian Rogersc604d732012-10-14 16:09:54 -0700435 }
436 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700437 } while (!done);
Ian Rogersc604d732012-10-14 16:09:54 -0700438#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700439 exclusive_owner_ = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Ian Rogersc604d732012-10-14 16:09:54 -0700441#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700442 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700443}
444
Ian Rogers56edc432013-01-18 16:51:51 -0800445void Mutex::Dump(std::ostream& os) const {
446 os << (recursive_ ? "recursive " : "non-recursive ")
447 << name_
448 << " level=" << static_cast<int>(level_)
449 << " rec=" << recursion_count_
450 << " owner=" << GetExclusiveOwnerTid() << " ";
451 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700452}
453
454std::ostream& operator<<(std::ostream& os, const Mutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800455 mu.Dump(os);
456 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700457}
458
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700459ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level)
460 : BaseMutex(name, level)
Ian Rogers81d425b2012-09-27 16:03:43 -0700461#if ART_USE_FUTEXES
Ian Rogersc5f17732014-06-05 20:48:42 -0700462 , state_(0), num_pending_readers_(0), num_pending_writers_(0)
Ian Rogers81d425b2012-09-27 16:03:43 -0700463#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700464{ // NOLINT(whitespace/braces)
Ian Rogers81d425b2012-09-27 16:03:43 -0700465#if !ART_USE_FUTEXES
Ian Rogersc5f17732014-06-05 20:48:42 -0700466 CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, nullptr));
Ian Rogers81d425b2012-09-27 16:03:43 -0700467#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700468 exclusive_owner_ = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700469}
470
471ReaderWriterMutex::~ReaderWriterMutex() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700472#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700473 CHECK_EQ(state_.LoadRelaxed(), 0);
Ian Rogers81d425b2012-09-27 16:03:43 -0700474 CHECK_EQ(exclusive_owner_, 0U);
Ian Rogersc7190692014-07-08 23:50:26 -0700475 CHECK_EQ(num_pending_readers_.LoadRelaxed(), 0);
Ian Rogers3e5cf302014-05-20 16:40:37 -0700476 CHECK_EQ(num_pending_writers_.LoadRelaxed(), 0);
Ian Rogers81d425b2012-09-27 16:03:43 -0700477#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
479 // may still be using locks.
480 int rc = pthread_rwlock_destroy(&rwlock_);
481 if (rc != 0) {
482 errno = rc;
483 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Ian Rogers50b35e22012-10-04 10:09:15 -0700484 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700485 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800486 bool shutting_down = runtime == NULL || runtime->IsShuttingDownLocked();
Ian Rogers120f1c72012-09-28 17:17:10 -0700487 PLOG(shutting_down ? WARNING : FATAL) << "pthread_rwlock_destroy failed for " << name_;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800488 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700489#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490}
491
Ian Rogers81d425b2012-09-27 16:03:43 -0700492void ReaderWriterMutex::ExclusiveLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700493 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700494 AssertNotExclusiveHeld(self);
495#if ART_USE_FUTEXES
496 bool done = false;
497 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700498 int32_t cur_state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700499 if (LIKELY(cur_state == 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700500 // Change state from 0 to -1 and impose load/store ordering appropriate for lock acquisition.
501 done = state_.CompareExchangeWeakAcquire(0 /* cur_state*/, -1 /* new state */);
Ian Rogers81d425b2012-09-27 16:03:43 -0700502 } else {
503 // Failed to acquire, hang up.
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700504 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersc7190692014-07-08 23:50:26 -0700505 ++num_pending_writers_;
506 if (futex(state_.Address(), FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
Brian Carlstrom0de79852013-07-25 22:29:58 -0700507 // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
508 // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock.
509 if ((errno != EAGAIN) && (errno != EINTR)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700510 PLOG(FATAL) << "futex wait failed for " << name_;
511 }
512 }
Ian Rogersc7190692014-07-08 23:50:26 -0700513 --num_pending_writers_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700514 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700515 } while (!done);
Ian Rogersc7190692014-07-08 23:50:26 -0700516 DCHECK_EQ(state_.LoadRelaxed(), -1);
Ian Rogers81d425b2012-09-27 16:03:43 -0700517#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700519#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700520 DCHECK_EQ(exclusive_owner_, 0U);
521 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700522 RegisterAsLocked(self);
523 AssertExclusiveHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700524}
525
Ian Rogers81d425b2012-09-27 16:03:43 -0700526void ReaderWriterMutex::ExclusiveUnlock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700527 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700528 AssertExclusiveHeld(self);
529 RegisterAsUnlocked(self);
Ian Rogersc5f17732014-06-05 20:48:42 -0700530 DCHECK_NE(exclusive_owner_, 0U);
Ian Rogers81d425b2012-09-27 16:03:43 -0700531#if ART_USE_FUTEXES
532 bool done = false;
533 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700534 int32_t cur_state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700535 if (LIKELY(cur_state == -1)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700536 // We're no longer the owner.
537 exclusive_owner_ = 0;
Ian Rogersc7190692014-07-08 23:50:26 -0700538 // Change state from -1 to 0 and impose load/store ordering appropriate for lock release.
539 // Note, the relaxed loads below musn't reorder before the CompareExchange.
540 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
541 // a status bit into the state on contention.
542 done = state_.CompareExchangeWeakSequentiallyConsistent(-1 /* cur_state*/, 0 /* new state */);
543 if (LIKELY(done)) { // Weak CAS may fail spuriously.
Ian Rogers81d425b2012-09-27 16:03:43 -0700544 // Wake any waiters.
Ian Rogersc7190692014-07-08 23:50:26 -0700545 if (UNLIKELY(num_pending_readers_.LoadRelaxed() > 0 ||
546 num_pending_writers_.LoadRelaxed() > 0)) {
547 futex(state_.Address(), FUTEX_WAKE, -1, NULL, NULL, 0);
Ian Rogers81d425b2012-09-27 16:03:43 -0700548 }
549 }
550 } else {
551 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
552 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700553 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700554#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700555 exclusive_owner_ = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700557#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558}
559
Ian Rogers66aee5c2012-08-15 17:17:47 -0700560#if HAVE_TIMED_RWLOCK
Ian Rogersc604d732012-10-14 16:09:54 -0700561bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700562 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700563#if ART_USE_FUTEXES
564 bool done = false;
Ian Rogersc604d732012-10-14 16:09:54 -0700565 timespec end_abs_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800566 InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &end_abs_ts);
Ian Rogers81d425b2012-09-27 16:03:43 -0700567 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700568 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers81d425b2012-09-27 16:03:43 -0700569 if (cur_state == 0) {
Ian Rogersc7190692014-07-08 23:50:26 -0700570 // Change state from 0 to -1 and impose load/store ordering appropriate for lock acquisition.
571 done = state_.CompareExchangeWeakAcquire(0 /* cur_state */, -1 /* new state */);
Ian Rogers81d425b2012-09-27 16:03:43 -0700572 } else {
573 // Failed to acquire, hang up.
Ian Rogersc604d732012-10-14 16:09:54 -0700574 timespec now_abs_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800575 InitTimeSpec(true, CLOCK_REALTIME, 0, 0, &now_abs_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700576 timespec rel_ts;
577 if (ComputeRelativeTimeSpec(&rel_ts, end_abs_ts, now_abs_ts)) {
578 return false; // Timed out.
579 }
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700580 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersc7190692014-07-08 23:50:26 -0700581 ++num_pending_writers_;
582 if (futex(state_.Address(), FUTEX_WAIT, cur_state, &rel_ts, NULL, 0) != 0) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700583 if (errno == ETIMEDOUT) {
Ian Rogersc7190692014-07-08 23:50:26 -0700584 --num_pending_writers_;
Ian Rogersc604d732012-10-14 16:09:54 -0700585 return false; // Timed out.
Brian Carlstrom0de79852013-07-25 22:29:58 -0700586 } else if ((errno != EAGAIN) && (errno != EINTR)) {
587 // EAGAIN and EINTR both indicate a spurious failure,
588 // recompute the relative time out from now and try again.
589 // We don't use TEMP_FAILURE_RETRY so we can recompute rel_ts;
Ian Rogers81d425b2012-09-27 16:03:43 -0700590 PLOG(FATAL) << "timed futex wait failed for " << name_;
591 }
592 }
Ian Rogersc7190692014-07-08 23:50:26 -0700593 --num_pending_writers_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700594 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700595 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700596#else
Ian Rogersc604d732012-10-14 16:09:54 -0700597 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700598 InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700599 int result = pthread_rwlock_timedwrlock(&rwlock_, &ts);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600 if (result == ETIMEDOUT) {
601 return false;
602 }
603 if (result != 0) {
604 errno = result;
Ian Rogersa5acfd32012-08-15 11:50:10 -0700605 PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700607#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700608 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700609 RegisterAsLocked(self);
610 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 return true;
612}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700613#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614
Ian Rogers81d425b2012-09-27 16:03:43 -0700615bool ReaderWriterMutex::SharedTryLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700616 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700617#if ART_USE_FUTEXES
618 bool done = false;
619 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700620 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers81d425b2012-09-27 16:03:43 -0700621 if (cur_state >= 0) {
Ian Rogersc7190692014-07-08 23:50:26 -0700622 // Add as an extra reader and impose load/store ordering appropriate for lock acquisition.
623 done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1);
Ian Rogers81d425b2012-09-27 16:03:43 -0700624 } else {
625 // Owner holds it exclusively.
626 return false;
627 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700628 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700629#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700630 int result = pthread_rwlock_tryrdlock(&rwlock_);
631 if (result == EBUSY) {
632 return false;
633 }
634 if (result != 0) {
635 errno = result;
636 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
637 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700638#endif
639 RegisterAsLocked(self);
640 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700641 return true;
642}
643
Ian Rogers81d425b2012-09-27 16:03:43 -0700644bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const {
Ian Rogers01ae5802012-09-28 16:14:01 -0700645 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 bool result;
647 if (UNLIKELY(self == NULL)) { // Handle unattached threads.
Ian Rogers01ae5802012-09-28 16:14:01 -0700648 result = IsExclusiveHeld(self); // TODO: a better best effort here.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 } else {
650 result = (self->GetHeldMutex(level_) == this);
651 }
652 return result;
653}
654
Ian Rogers56edc432013-01-18 16:51:51 -0800655void ReaderWriterMutex::Dump(std::ostream& os) const {
656 os << name_
657 << " level=" << static_cast<int>(level_)
Mathieu Chartier5869a2c2014-10-08 14:26:23 -0700658 << " owner=" << GetExclusiveOwnerTid()
659#if ART_USE_FUTEXES
660 << " state=" << state_.LoadSequentiallyConsistent()
661 << " num_pending_writers=" << num_pending_writers_.LoadSequentiallyConsistent()
662 << " num_pending_readers=" << num_pending_readers_.LoadSequentiallyConsistent()
663#endif
664 << " ";
Ian Rogers56edc432013-01-18 16:51:51 -0800665 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700666}
667
668std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800669 mu.Dump(os);
670 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700671}
672
Ian Rogers23055dc2013-04-18 16:29:16 -0700673ConditionVariable::ConditionVariable(const char* name, Mutex& guard)
Ian Rogersc604d732012-10-14 16:09:54 -0700674 : name_(name), guard_(guard) {
675#if ART_USE_FUTEXES
Ian Rogers3e5cf302014-05-20 16:40:37 -0700676 DCHECK_EQ(0, sequence_.LoadRelaxed());
Ian Rogersc604d732012-10-14 16:09:54 -0700677 num_waiters_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700678#else
Narayan Kamath51b71022014-03-04 11:57:09 +0000679 pthread_condattr_t cond_attrs;
Ian Rogersc5f17732014-06-05 20:48:42 -0700680 CHECK_MUTEX_CALL(pthread_condattr_init, (&cond_attrs));
Narayan Kamath51b71022014-03-04 11:57:09 +0000681#if !defined(__APPLE__)
682 // Apple doesn't have CLOCK_MONOTONIC or pthread_condattr_setclock.
683 CHECK_MUTEX_CALL(pthread_condattr_setclock(&cond_attrs, CLOCK_MONOTONIC));
684#endif
685 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, &cond_attrs));
Ian Rogersc604d732012-10-14 16:09:54 -0700686#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700687}
688
689ConditionVariable::~ConditionVariable() {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800690#if ART_USE_FUTEXES
691 if (num_waiters_!= 0) {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800692 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700693 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersd45f2012012-11-28 11:46:23 -0800694 LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_
695 << " called with " << num_waiters_ << " waiters.";
Ian Rogers5bd97c42012-11-27 02:38:26 -0800696 }
697#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700698 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
699 // may still be using condition variables.
700 int rc = pthread_cond_destroy(&cond_);
701 if (rc != 0) {
702 errno = rc;
Ian Rogers50b35e22012-10-04 10:09:15 -0700703 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700704 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800705 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughese62934d2012-04-09 11:24:29 -0700706 PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_;
707 }
Ian Rogersc604d732012-10-14 16:09:54 -0700708#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700709}
710
Ian Rogersc604d732012-10-14 16:09:54 -0700711void ConditionVariable::Broadcast(Thread* self) {
712 DCHECK(self == NULL || self == Thread::Current());
713 // TODO: enable below, there's a race in thread creation that causes false failures currently.
714 // guard_.AssertExclusiveHeld(self);
Mathieu Chartiere46cd752012-10-31 16:56:18 -0700715 DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self));
Ian Rogersc604d732012-10-14 16:09:54 -0700716#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800717 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800718 sequence_++; // Indicate the broadcast occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700719 bool done = false;
720 do {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700721 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersd45f2012012-11-28 11:46:23 -0800722 // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring
723 // mutex unlocks will awaken the requeued waiter thread.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800724 done = futex(sequence_.Address(), FUTEX_CMP_REQUEUE, 0,
Ian Rogers5bd97c42012-11-27 02:38:26 -0800725 reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()),
Ian Rogersc7190692014-07-08 23:50:26 -0700726 guard_.state_.Address(), cur_sequence) != -1;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800727 if (!done) {
728 if (errno != EAGAIN) {
729 PLOG(FATAL) << "futex cmp requeue failed for " << name_;
730 }
Ian Rogers5bd97c42012-11-27 02:38:26 -0800731 }
Ian Rogersc604d732012-10-14 16:09:54 -0700732 } while (!done);
733 }
734#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700735 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700736#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700737}
738
Ian Rogersc604d732012-10-14 16:09:54 -0700739void ConditionVariable::Signal(Thread* self) {
740 DCHECK(self == NULL || self == Thread::Current());
741 guard_.AssertExclusiveHeld(self);
742#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800743 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800744 sequence_++; // Indicate a signal occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700745 // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them
746 // to avoid this, however, requeueing can only move all waiters.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800747 int num_woken = futex(sequence_.Address(), FUTEX_WAKE, 1, NULL, NULL, 0);
Ian Rogersd45f2012012-11-28 11:46:23 -0800748 // Check something was woken or else we changed sequence_ before they had chance to wait.
Ian Rogers5bd97c42012-11-27 02:38:26 -0800749 CHECK((num_woken == 0) || (num_woken == 1));
Ian Rogersc604d732012-10-14 16:09:54 -0700750 }
751#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700752 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700753#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700754}
755
Ian Rogersc604d732012-10-14 16:09:54 -0700756void ConditionVariable::Wait(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700757 guard_.CheckSafeToWait(self);
758 WaitHoldingLocks(self);
759}
760
761void ConditionVariable::WaitHoldingLocks(Thread* self) {
Ian Rogersc604d732012-10-14 16:09:54 -0700762 DCHECK(self == NULL || self == Thread::Current());
763 guard_.AssertExclusiveHeld(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700764 unsigned int old_recursion_count = guard_.recursion_count_;
765#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700766 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800767 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800768 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700769 guard_.recursion_count_ = 1;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700770 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700771 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800772 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800773 // Futex failed, check it is an expected error.
774 // EAGAIN == EWOULDBLK, so we let the caller try again.
775 // EINTR implies a signal was sent to this thread.
776 if ((errno != EINTR) && (errno != EAGAIN)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700777 PLOG(FATAL) << "futex wait failed for " << name_;
778 }
779 }
780 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800781 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700782 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800783 // We awoke and so no longer require awakes from the guard_'s unlock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700784 CHECK_GE(guard_.num_contenders_.LoadRelaxed(), 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800785 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700786#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700787 uint64_t old_owner = guard_.exclusive_owner_;
788 guard_.exclusive_owner_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700789 guard_.recursion_count_ = 0;
790 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_));
Ian Rogersc5f17732014-06-05 20:48:42 -0700791 guard_.exclusive_owner_ = old_owner;
Ian Rogersc604d732012-10-14 16:09:54 -0700792#endif
793 guard_.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700794}
795
Ian Rogers7b078e82014-09-10 14:44:24 -0700796bool ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) {
Ian Rogersc604d732012-10-14 16:09:54 -0700797 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers7b078e82014-09-10 14:44:24 -0700798 bool timed_out = false;
Ian Rogersc604d732012-10-14 16:09:54 -0700799 guard_.AssertExclusiveHeld(self);
Ian Rogers1d54e732013-05-02 21:10:01 -0700800 guard_.CheckSafeToWait(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700801 unsigned int old_recursion_count = guard_.recursion_count_;
802#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700803 timespec rel_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800804 InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700805 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800806 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800807 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700808 guard_.recursion_count_ = 1;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700809 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700810 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800811 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700812 if (errno == ETIMEDOUT) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800813 // Timed out we're done.
Ian Rogers7b078e82014-09-10 14:44:24 -0700814 timed_out = true;
Brian Carlstrom0de79852013-07-25 22:29:58 -0700815 } else if ((errno == EAGAIN) || (errno == EINTR)) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800816 // A signal or ConditionVariable::Signal/Broadcast has come in.
Ian Rogersc604d732012-10-14 16:09:54 -0700817 } else {
818 PLOG(FATAL) << "timed futex wait failed for " << name_;
819 }
820 }
821 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800822 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700823 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800824 // We awoke and so no longer require awakes from the guard_'s unlock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700825 CHECK_GE(guard_.num_contenders_.LoadRelaxed(), 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800826 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700827#else
Narayan Kamath51b71022014-03-04 11:57:09 +0000828#if !defined(__APPLE__)
Ian Rogersc604d732012-10-14 16:09:54 -0700829 int clock = CLOCK_MONOTONIC;
Elliott Hughes5f791332011-09-15 17:45:30 -0700830#else
Ian Rogersc604d732012-10-14 16:09:54 -0700831 int clock = CLOCK_REALTIME;
Elliott Hughes5f791332011-09-15 17:45:30 -0700832#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700833 uint64_t old_owner = guard_.exclusive_owner_;
834 guard_.exclusive_owner_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700835 guard_.recursion_count_ = 0;
836 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700837 InitTimeSpec(true, clock, ms, ns, &ts);
Narayan Kamath51b71022014-03-04 11:57:09 +0000838 int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &guard_.mutex_, &ts));
Ian Rogers7b078e82014-09-10 14:44:24 -0700839 if (rc == ETIMEDOUT) {
840 timed_out = true;
841 } else if (rc != 0) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700842 errno = rc;
843 PLOG(FATAL) << "TimedWait failed for " << name_;
844 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700845 guard_.exclusive_owner_ = old_owner;
Ian Rogersc604d732012-10-14 16:09:54 -0700846#endif
847 guard_.recursion_count_ = old_recursion_count;
Ian Rogers7b078e82014-09-10 14:44:24 -0700848 return timed_out;
Elliott Hughes5f791332011-09-15 17:45:30 -0700849}
850
Ian Rogers719d1a32014-03-06 12:13:39 -0800851void Locks::Init() {
852 if (logging_lock_ != nullptr) {
853 // Already initialized.
Nicolas Geoffray7f0a6d62014-05-26 14:01:46 +0100854 if (kRuntimeISA == kX86 || kRuntimeISA == kX86_64) {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700855 DCHECK(modify_ldt_lock_ != nullptr);
856 } else {
857 DCHECK(modify_ldt_lock_ == nullptr);
858 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800859 DCHECK(abort_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700860 DCHECK(alloc_tracker_lock_ != nullptr);
Andreas Gampe74240812014-04-17 10:35:09 -0700861 DCHECK(allocated_monitor_ids_lock_ != nullptr);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700862 DCHECK(allocated_thread_ids_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800863 DCHECK(breakpoint_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800864 DCHECK(classlinker_classes_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700865 DCHECK(deoptimization_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800866 DCHECK(heap_bitmap_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700867 DCHECK(intern_table_lock_ != nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -0700868 DCHECK(jni_libraries_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800869 DCHECK(logging_lock_ != nullptr);
870 DCHECK(mutator_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700871 DCHECK(profiler_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800872 DCHECK(thread_list_lock_ != nullptr);
Ian Rogersf3d874c2014-07-17 18:52:42 -0700873 DCHECK(thread_list_suspend_thread_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800874 DCHECK(thread_suspend_count_lock_ != nullptr);
875 DCHECK(trace_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800876 DCHECK(unexpected_signal_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800877 } else {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700878 // Create global locks in level order from highest lock level to lowest.
Ian Rogersf3d874c2014-07-17 18:52:42 -0700879 LockLevel current_lock_level = kThreadListSuspendThreadLock;
880 DCHECK(thread_list_suspend_thread_lock_ == nullptr);
881 thread_list_suspend_thread_lock_ =
882 new Mutex("thread list suspend thread by .. lock", current_lock_level);
Ian Rogers719d1a32014-03-06 12:13:39 -0800883
Chao-ying Fu9e369312014-05-21 11:20:52 -0700884 #define UPDATE_CURRENT_LOCK_LEVEL(new_level) \
Brian Carlstrom306db812014-09-05 13:01:41 -0700885 if (new_level >= current_lock_level) { \
886 /* Do not use CHECKs or FATAL here, abort_lock_ is not setup yet. */ \
887 fprintf(stderr, "New local level %d is not less than current level %d\n", \
888 new_level, current_lock_level); \
889 exit(1); \
890 } \
Ian Rogersf3d874c2014-07-17 18:52:42 -0700891 current_lock_level = new_level;
892
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700893 UPDATE_CURRENT_LOCK_LEVEL(kInstrumentEntrypointsLock);
894 DCHECK(instrument_entrypoints_lock_ == nullptr);
895 instrument_entrypoints_lock_ = new Mutex("instrument entrypoint lock", current_lock_level);
896
Ian Rogersf3d874c2014-07-17 18:52:42 -0700897 UPDATE_CURRENT_LOCK_LEVEL(kMutatorLock);
898 DCHECK(mutator_lock_ == nullptr);
899 mutator_lock_ = new ReaderWriterMutex("mutator lock", current_lock_level);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700900
901 UPDATE_CURRENT_LOCK_LEVEL(kHeapBitmapLock);
902 DCHECK(heap_bitmap_lock_ == nullptr);
903 heap_bitmap_lock_ = new ReaderWriterMutex("heap bitmap lock", current_lock_level);
904
Jeff Hao69dbec62014-09-15 18:03:41 -0700905 UPDATE_CURRENT_LOCK_LEVEL(kTraceLock);
906 DCHECK(trace_lock_ == nullptr);
907 trace_lock_ = new Mutex("trace lock", current_lock_level);
908
Chao-ying Fu9e369312014-05-21 11:20:52 -0700909 UPDATE_CURRENT_LOCK_LEVEL(kRuntimeShutdownLock);
910 DCHECK(runtime_shutdown_lock_ == nullptr);
911 runtime_shutdown_lock_ = new Mutex("runtime shutdown lock", current_lock_level);
912
913 UPDATE_CURRENT_LOCK_LEVEL(kProfilerLock);
914 DCHECK(profiler_lock_ == nullptr);
915 profiler_lock_ = new Mutex("profiler lock", current_lock_level);
916
Brian Carlstrom306db812014-09-05 13:01:41 -0700917 UPDATE_CURRENT_LOCK_LEVEL(kDeoptimizationLock);
918 DCHECK(deoptimization_lock_ == nullptr);
919 deoptimization_lock_ = new Mutex("Deoptimization lock", current_lock_level);
920
921 UPDATE_CURRENT_LOCK_LEVEL(kAllocTrackerLock);
922 DCHECK(alloc_tracker_lock_ == nullptr);
923 alloc_tracker_lock_ = new Mutex("AllocTracker lock", current_lock_level);
924
Chao-ying Fu9e369312014-05-21 11:20:52 -0700925 UPDATE_CURRENT_LOCK_LEVEL(kThreadListLock);
926 DCHECK(thread_list_lock_ == nullptr);
927 thread_list_lock_ = new Mutex("thread list lock", current_lock_level);
928
Ian Rogers68d8b422014-07-17 11:09:10 -0700929 UPDATE_CURRENT_LOCK_LEVEL(kJniLoadLibraryLock);
930 DCHECK(jni_libraries_lock_ == nullptr);
931 jni_libraries_lock_ = new Mutex("JNI shared libraries map lock", current_lock_level);
932
Chao-ying Fu9e369312014-05-21 11:20:52 -0700933 UPDATE_CURRENT_LOCK_LEVEL(kBreakpointLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800934 DCHECK(breakpoint_lock_ == nullptr);
Sebastien Hertzed2be172014-08-19 15:33:43 +0200935 breakpoint_lock_ = new ReaderWriterMutex("breakpoint lock", current_lock_level);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700936
937 UPDATE_CURRENT_LOCK_LEVEL(kClassLinkerClassesLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800938 DCHECK(classlinker_classes_lock_ == nullptr);
939 classlinker_classes_lock_ = new ReaderWriterMutex("ClassLinker classes lock",
Chao-ying Fu9e369312014-05-21 11:20:52 -0700940 current_lock_level);
941
Andreas Gampe74240812014-04-17 10:35:09 -0700942 UPDATE_CURRENT_LOCK_LEVEL(kMonitorPoolLock);
943 DCHECK(allocated_monitor_ids_lock_ == nullptr);
944 allocated_monitor_ids_lock_ = new Mutex("allocated monitor ids lock", current_lock_level);
945
Chao-ying Fu9e369312014-05-21 11:20:52 -0700946 UPDATE_CURRENT_LOCK_LEVEL(kAllocatedThreadIdsLock);
947 DCHECK(allocated_thread_ids_lock_ == nullptr);
948 allocated_thread_ids_lock_ = new Mutex("allocated thread ids lock", current_lock_level);
949
Nicolas Geoffray7f0a6d62014-05-26 14:01:46 +0100950 if (kRuntimeISA == kX86 || kRuntimeISA == kX86_64) {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700951 UPDATE_CURRENT_LOCK_LEVEL(kModifyLdtLock);
952 DCHECK(modify_ldt_lock_ == nullptr);
953 modify_ldt_lock_ = new Mutex("modify_ldt lock", current_lock_level);
954 }
955
956 UPDATE_CURRENT_LOCK_LEVEL(kInternTableLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800957 DCHECK(intern_table_lock_ == nullptr);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700958 intern_table_lock_ = new Mutex("InternTable lock", current_lock_level);
959
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700960 UPDATE_CURRENT_LOCK_LEVEL(kReferenceProcessorLock);
961 DCHECK(reference_processor_lock_ == nullptr);
962 reference_processor_lock_ = new Mutex("ReferenceProcessor lock", current_lock_level);
963
964 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueClearedReferencesLock);
965 DCHECK(reference_queue_cleared_references_lock_ == nullptr);
966 reference_queue_cleared_references_lock_ = new Mutex("ReferenceQueue cleared references lock", current_lock_level);
967
968 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueWeakReferencesLock);
969 DCHECK(reference_queue_weak_references_lock_ == nullptr);
970 reference_queue_weak_references_lock_ = new Mutex("ReferenceQueue cleared references lock", current_lock_level);
971
972 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueFinalizerReferencesLock);
973 DCHECK(reference_queue_finalizer_references_lock_ == nullptr);
974 reference_queue_finalizer_references_lock_ = new Mutex("ReferenceQueue finalizer references lock", current_lock_level);
975
976 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueuePhantomReferencesLock);
977 DCHECK(reference_queue_phantom_references_lock_ == nullptr);
978 reference_queue_phantom_references_lock_ = new Mutex("ReferenceQueue phantom references lock", current_lock_level);
979
980 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueSoftReferencesLock);
981 DCHECK(reference_queue_soft_references_lock_ == nullptr);
982 reference_queue_soft_references_lock_ = new Mutex("ReferenceQueue soft references lock", current_lock_level);
983
Chao-ying Fu9e369312014-05-21 11:20:52 -0700984 UPDATE_CURRENT_LOCK_LEVEL(kAbortLock);
985 DCHECK(abort_lock_ == nullptr);
986 abort_lock_ = new Mutex("abort lock", current_lock_level, true);
987
988 UPDATE_CURRENT_LOCK_LEVEL(kThreadSuspendCountLock);
989 DCHECK(thread_suspend_count_lock_ == nullptr);
990 thread_suspend_count_lock_ = new Mutex("thread suspend count lock", current_lock_level);
991
992 UPDATE_CURRENT_LOCK_LEVEL(kUnexpectedSignalLock);
993 DCHECK(unexpected_signal_lock_ == nullptr);
994 unexpected_signal_lock_ = new Mutex("unexpected signal lock", current_lock_level, true);
995
Hiroshi Yamauchi3eed93d2014-06-04 11:43:59 -0700996 UPDATE_CURRENT_LOCK_LEVEL(kMemMapsLock);
997 DCHECK(mem_maps_lock_ == nullptr);
998 mem_maps_lock_ = new Mutex("mem maps lock", current_lock_level);
999
Chao-ying Fu9e369312014-05-21 11:20:52 -07001000 UPDATE_CURRENT_LOCK_LEVEL(kLoggingLock);
1001 DCHECK(logging_lock_ == nullptr);
1002 logging_lock_ = new Mutex("logging lock", current_lock_level, true);
1003
1004 #undef UPDATE_CURRENT_LOCK_LEVEL
Ian Rogers719d1a32014-03-06 12:13:39 -08001005 }
1006}
1007
1008
Elliott Hughese62934d2012-04-09 11:24:29 -07001009} // namespace art