blob: cbcd408b6d41ac437922c7b8e5f66bf7d9a4e1ff [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 Rogers56edc432013-01-18 16:51:51 -080086class ScopedAllMutexesLock {
87 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 }
93 ~ScopedAllMutexesLock() {
Ian Rogers3e5cf302014-05-20 16:40:37 -070094 while (!gAllMutexData->all_mutexes_guard.CompareExchangeWeakRelease(mutex_, 0)) {
Ian Rogers56edc432013-01-18 16:51:51 -080095 NanoSleep(100);
96 }
97 }
98 private:
99 const BaseMutex* const mutex_;
100};
Ian Rogers56edc432013-01-18 16:51:51 -0800101
102BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700103 if (kLogLockContentions) {
104 ScopedAllMutexesLock mu(this);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700105 std::set<BaseMutex*>** all_mutexes_ptr = &gAllMutexData->all_mutexes;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700106 if (*all_mutexes_ptr == NULL) {
107 // We leak the global set of all mutexes to avoid ordering issues in global variable
108 // construction/destruction.
109 *all_mutexes_ptr = new std::set<BaseMutex*>();
110 }
111 (*all_mutexes_ptr)->insert(this);
Ian Rogers56edc432013-01-18 16:51:51 -0800112 }
Ian Rogers56edc432013-01-18 16:51:51 -0800113}
114
115BaseMutex::~BaseMutex() {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700116 if (kLogLockContentions) {
117 ScopedAllMutexesLock mu(this);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700118 gAllMutexData->all_mutexes->erase(this);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700119 }
Ian Rogers56edc432013-01-18 16:51:51 -0800120}
121
122void BaseMutex::DumpAll(std::ostream& os) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700123 if (kLogLockContentions) {
124 os << "Mutex logging:\n";
125 ScopedAllMutexesLock mu(reinterpret_cast<const BaseMutex*>(-1));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700126 std::set<BaseMutex*>* all_mutexes = gAllMutexData->all_mutexes;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700127 if (all_mutexes == NULL) {
128 // No mutexes have been created yet during at startup.
129 return;
130 }
131 typedef std::set<BaseMutex*>::const_iterator It;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700132 os << "(Contended)\n";
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700133 for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) {
134 BaseMutex* mutex = *it;
135 if (mutex->HasEverContended()) {
136 mutex->Dump(os);
137 os << "\n";
138 }
139 }
140 os << "(Never contented)\n";
141 for (It it = all_mutexes->begin(); it != all_mutexes->end(); ++it) {
142 BaseMutex* mutex = *it;
143 if (!mutex->HasEverContended()) {
144 mutex->Dump(os);
145 os << "\n";
146 }
147 }
Ian Rogers56edc432013-01-18 16:51:51 -0800148 }
Ian Rogers56edc432013-01-18 16:51:51 -0800149}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150
Ian Rogers81d425b2012-09-27 16:03:43 -0700151void BaseMutex::CheckSafeToWait(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 if (self == NULL) {
153 CheckUnattachedThread(level_);
154 return;
155 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700156 if (kDebugLocking) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700157 CHECK(self->GetHeldMutex(level_) == this || level_ == kMonitorLock)
158 << "Waiting on unacquired mutex: " << name_;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700159 bool bad_mutexes_held = false;
Elliott Hughes0f827162013-02-26 12:12:58 -0800160 for (int i = kLockLevelCount - 1; i >= 0; --i) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700161 if (i != level_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700162 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Ian Rogersf3d874c2014-07-17 18:52:42 -0700163 // We expect waits to happen while holding the thread list suspend thread lock.
164 if (held_mutex != NULL && i != kThreadListSuspendThreadLock) {
Elliott Hughes0f827162013-02-26 12:12:58 -0800165 LOG(ERROR) << "Holding \"" << held_mutex->name_ << "\" "
166 << "(level " << LockLevel(i) << ") while performing wait on "
167 << "\"" << name_ << "\" (level " << level_ << ")";
Ian Rogers25fd14b2012-09-05 10:56:38 -0700168 bad_mutexes_held = true;
169 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 }
171 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700172 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174}
175
Ian Rogers37f3c962014-07-17 11:25:30 -0700176void BaseMutex::ContentionLogData::AddToWaitTime(uint64_t value) {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700177 if (kLogLockContentions) {
178 // Atomically add value to wait_time.
Ian Rogers37f3c962014-07-17 11:25:30 -0700179 wait_time.FetchAndAddSequentiallyConsistent(value);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700180 }
181}
182
Brian Carlstrom0de79852013-07-25 22:29:58 -0700183void BaseMutex::RecordContention(uint64_t blocked_tid,
184 uint64_t owner_tid,
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700185 uint64_t nano_time_blocked) {
186 if (kLogLockContentions) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700187 ContentionLogData* data = contention_log_data_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700188 ++(data->contention_count);
189 data->AddToWaitTime(nano_time_blocked);
190 ContentionLogEntry* log = data->contention_log;
191 // This code is intentionally racy as it is only used for diagnostics.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700192 uint32_t slot = data->cur_content_log_entry.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700193 if (log[slot].blocked_tid == blocked_tid &&
194 log[slot].owner_tid == blocked_tid) {
195 ++log[slot].count;
196 } else {
197 uint32_t new_slot;
198 do {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700199 slot = data->cur_content_log_entry.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700200 new_slot = (slot + 1) % kContentionLogSize;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700201 } while (!data->cur_content_log_entry.CompareExchangeWeakRelaxed(slot, new_slot));
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700202 log[new_slot].blocked_tid = blocked_tid;
203 log[new_slot].owner_tid = owner_tid;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700204 log[new_slot].count.StoreRelaxed(1);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700205 }
Ian Rogers56edc432013-01-18 16:51:51 -0800206 }
Ian Rogers56edc432013-01-18 16:51:51 -0800207}
208
Ian Rogers56edc432013-01-18 16:51:51 -0800209void BaseMutex::DumpContention(std::ostream& os) const {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700210 if (kLogLockContentions) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700211 const ContentionLogData* data = contention_log_data_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700212 const ContentionLogEntry* log = data->contention_log;
Ian Rogers37f3c962014-07-17 11:25:30 -0700213 uint64_t wait_time = data->wait_time.LoadRelaxed();
Ian Rogers3e5cf302014-05-20 16:40:37 -0700214 uint32_t contention_count = data->contention_count.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700215 if (contention_count == 0) {
216 os << "never contended";
217 } else {
218 os << "contended " << contention_count
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700219 << " total wait of contender " << PrettyDuration(wait_time)
220 << " average " << PrettyDuration(wait_time / contention_count);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700221 SafeMap<uint64_t, size_t> most_common_blocker;
222 SafeMap<uint64_t, size_t> most_common_blocked;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700223 for (size_t i = 0; i < kContentionLogSize; ++i) {
224 uint64_t blocked_tid = log[i].blocked_tid;
225 uint64_t owner_tid = log[i].owner_tid;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700226 uint32_t count = log[i].count.LoadRelaxed();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700227 if (count > 0) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700228 auto it = most_common_blocked.find(blocked_tid);
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700229 if (it != most_common_blocked.end()) {
230 most_common_blocked.Overwrite(blocked_tid, it->second + count);
231 } else {
232 most_common_blocked.Put(blocked_tid, count);
233 }
234 it = most_common_blocker.find(owner_tid);
235 if (it != most_common_blocker.end()) {
236 most_common_blocker.Overwrite(owner_tid, it->second + count);
237 } else {
238 most_common_blocker.Put(owner_tid, count);
239 }
Ian Rogers56edc432013-01-18 16:51:51 -0800240 }
241 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700242 uint64_t max_tid = 0;
243 size_t max_tid_count = 0;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700244 for (const auto& pair : most_common_blocked) {
245 if (pair.second > max_tid_count) {
246 max_tid = pair.first;
247 max_tid_count = pair.second;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700248 }
Ian Rogers56edc432013-01-18 16:51:51 -0800249 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700250 if (max_tid != 0) {
251 os << " sample shows most blocked tid=" << max_tid;
Ian Rogers56edc432013-01-18 16:51:51 -0800252 }
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700253 max_tid = 0;
254 max_tid_count = 0;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700255 for (const auto& pair : most_common_blocker) {
256 if (pair.second > max_tid_count) {
257 max_tid = pair.first;
258 max_tid_count = pair.second;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700259 }
260 }
261 if (max_tid != 0) {
262 os << " sample shows tid=" << max_tid << " owning during this time";
263 }
Ian Rogers56edc432013-01-18 16:51:51 -0800264 }
265 }
Ian Rogers56edc432013-01-18 16:51:51 -0800266}
267
268
Ian Rogers81d425b2012-09-27 16:03:43 -0700269Mutex::Mutex(const char* name, LockLevel level, bool recursive)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700271#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700272 DCHECK_EQ(0, state_.LoadRelaxed());
Ian Rogers3e5cf302014-05-20 16:40:37 -0700273 DCHECK_EQ(0, num_contenders_.LoadRelaxed());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700275 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, nullptr));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700276#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700277 exclusive_owner_ = 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700278}
279
280Mutex::~Mutex() {
Ian Rogersc604d732012-10-14 16:09:54 -0700281#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700282 if (state_.LoadRelaxed() != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700283 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700284 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersc604d732012-10-14 16:09:54 -0700285 LOG(shutting_down ? WARNING : FATAL) << "destroying mutex with owner: " << exclusive_owner_;
286 } else {
287 CHECK_EQ(exclusive_owner_, 0U) << "unexpectedly found an owner on unlocked mutex " << name_;
Ian Rogersc7190692014-07-08 23:50:26 -0700288 CHECK_EQ(num_contenders_.LoadSequentiallyConsistent(), 0)
Ian Rogers3e5cf302014-05-20 16:40:37 -0700289 << "unexpectedly found a contender on mutex " << name_;
Ian Rogersc604d732012-10-14 16:09:54 -0700290 }
291#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700292 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
293 // may still be using locks.
Elliott Hughes6b355752012-01-13 16:49:08 -0800294 int rc = pthread_mutex_destroy(&mutex_);
295 if (rc != 0) {
296 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800297 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Ian Rogers50b35e22012-10-04 10:09:15 -0700298 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700299 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800300 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughes6b355752012-01-13 16:49:08 -0800301 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
302 }
Ian Rogersc604d732012-10-14 16:09:54 -0700303#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -0700304}
305
Ian Rogers81d425b2012-09-27 16:03:43 -0700306void Mutex::ExclusiveLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700307 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers25fd14b2012-09-05 10:56:38 -0700308 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700309 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700310 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700311 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700312#if ART_USE_FUTEXES
313 bool done = false;
314 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700315 int32_t cur_state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700316 if (LIKELY(cur_state == 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700317 // Change state from 0 to 1 and impose load/store ordering appropriate for lock acquisition.
318 done = state_.CompareExchangeWeakAcquire(0 /* cur_state */, 1 /* new state */);
Ian Rogersc604d732012-10-14 16:09:54 -0700319 } else {
320 // Failed to acquire, hang up.
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700321 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersb122a4b2013-11-19 18:00:50 -0800322 num_contenders_++;
Ian Rogersc7190692014-07-08 23:50:26 -0700323 if (futex(state_.Address(), FUTEX_WAIT, 1, NULL, NULL, 0) != 0) {
Brian Carlstrom0de79852013-07-25 22:29:58 -0700324 // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
325 // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock.
326 if ((errno != EAGAIN) && (errno != EINTR)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700327 PLOG(FATAL) << "futex wait failed for " << name_;
328 }
329 }
Ian Rogersb122a4b2013-11-19 18:00:50 -0800330 num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700331 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700332 } while (!done);
Ian Rogersc7190692014-07-08 23:50:26 -0700333 DCHECK_EQ(state_.LoadRelaxed(), 1);
Ian Rogersc604d732012-10-14 16:09:54 -0700334#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogersc604d732012-10-14 16:09:54 -0700336#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700337 DCHECK_EQ(exclusive_owner_, 0U);
338 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700339 RegisterAsLocked(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 }
341 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700342 if (kDebugLocking) {
343 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
344 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700345 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700346 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700347}
348
Ian Rogers81d425b2012-09-27 16:03:43 -0700349bool Mutex::ExclusiveTryLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700350 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers25fd14b2012-09-05 10:56:38 -0700351 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700352 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700353 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700354 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700355#if ART_USE_FUTEXES
356 bool done = false;
357 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700358 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700359 if (cur_state == 0) {
Ian Rogersc7190692014-07-08 23:50:26 -0700360 // Change state from 0 to 1 and impose load/store ordering appropriate for lock acquisition.
361 done = state_.CompareExchangeWeakAcquire(0 /* cur_state */, 1 /* new state */);
Ian Rogersc604d732012-10-14 16:09:54 -0700362 } else {
363 return false;
364 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700365 } while (!done);
Ian Rogersc7190692014-07-08 23:50:26 -0700366 DCHECK_EQ(state_.LoadRelaxed(), 1);
Ian Rogersc604d732012-10-14 16:09:54 -0700367#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700368 int result = pthread_mutex_trylock(&mutex_);
369 if (result == EBUSY) {
370 return false;
371 }
372 if (result != 0) {
373 errno = result;
374 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
375 }
Ian Rogersc604d732012-10-14 16:09:54 -0700376#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700377 DCHECK_EQ(exclusive_owner_, 0U);
378 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700379 RegisterAsLocked(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700380 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700382 if (kDebugLocking) {
383 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
384 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700385 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700386 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700387 return true;
388}
389
Ian Rogers81d425b2012-09-27 16:03:43 -0700390void Mutex::ExclusiveUnlock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700391 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700392 AssertHeld(self);
Ian Rogersc5f17732014-06-05 20:48:42 -0700393 DCHECK_NE(exclusive_owner_, 0U);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 recursion_count_--;
395 if (!recursive_ || recursion_count_ == 0) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700396 if (kDebugLocking) {
397 CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: "
398 << name_ << " " << recursion_count_;
399 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700400 RegisterAsUnlocked(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700401#if ART_USE_FUTEXES
Ian Rogersc5f17732014-06-05 20:48:42 -0700402 bool done = false;
403 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700404 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc5f17732014-06-05 20:48:42 -0700405 if (LIKELY(cur_state == 1)) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700406 // We're no longer the owner.
407 exclusive_owner_ = 0;
Ian Rogersc7190692014-07-08 23:50:26 -0700408 // Change state to 0 and impose load/store ordering appropriate for lock release.
409 // Note, the relaxed loads below musn't reorder before the CompareExchange.
410 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
411 // a status bit into the state on contention.
412 done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, 0 /* new state */);
Ian Rogersc5f17732014-06-05 20:48:42 -0700413 if (LIKELY(done)) { // Spurious fail?
Ian Rogersc7190692014-07-08 23:50:26 -0700414 // Wake a contender.
Ian Rogersc5f17732014-06-05 20:48:42 -0700415 if (UNLIKELY(num_contenders_.LoadRelaxed() > 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700416 futex(state_.Address(), FUTEX_WAKE, 1, NULL, NULL, 0);
Ian Rogersc5f17732014-06-05 20:48:42 -0700417 }
418 }
419 } else {
420 // Logging acquires the logging lock, avoid infinite recursion in that case.
421 if (this != Locks::logging_lock_) {
422 LOG(FATAL) << "Unexpected state_ in unlock " << cur_state << " for " << name_;
423 } else {
424 LogMessageData data(__FILE__, __LINE__, INTERNAL_FATAL, -1);
425 LogMessage::LogLine(data, StringPrintf("Unexpected state_ %d in unlock for %s",
426 cur_state, name_).c_str());
427 _exit(1);
Ian Rogersc604d732012-10-14 16:09:54 -0700428 }
429 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700430 } while (!done);
Ian Rogersc604d732012-10-14 16:09:54 -0700431#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700432 exclusive_owner_ = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700433 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Ian Rogersc604d732012-10-14 16:09:54 -0700434#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700435 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700436}
437
Ian Rogers56edc432013-01-18 16:51:51 -0800438void Mutex::Dump(std::ostream& os) const {
439 os << (recursive_ ? "recursive " : "non-recursive ")
440 << name_
441 << " level=" << static_cast<int>(level_)
442 << " rec=" << recursion_count_
443 << " owner=" << GetExclusiveOwnerTid() << " ";
444 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700445}
446
447std::ostream& operator<<(std::ostream& os, const Mutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800448 mu.Dump(os);
449 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700450}
451
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700452ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level)
453 : BaseMutex(name, level)
Ian Rogers81d425b2012-09-27 16:03:43 -0700454#if ART_USE_FUTEXES
Ian Rogersc5f17732014-06-05 20:48:42 -0700455 , state_(0), num_pending_readers_(0), num_pending_writers_(0)
Ian Rogers81d425b2012-09-27 16:03:43 -0700456#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700457{ // NOLINT(whitespace/braces)
Ian Rogers81d425b2012-09-27 16:03:43 -0700458#if !ART_USE_FUTEXES
Ian Rogersc5f17732014-06-05 20:48:42 -0700459 CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, nullptr));
Ian Rogers81d425b2012-09-27 16:03:43 -0700460#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700461 exclusive_owner_ = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462}
463
464ReaderWriterMutex::~ReaderWriterMutex() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700465#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700466 CHECK_EQ(state_.LoadRelaxed(), 0);
Ian Rogers81d425b2012-09-27 16:03:43 -0700467 CHECK_EQ(exclusive_owner_, 0U);
Ian Rogersc7190692014-07-08 23:50:26 -0700468 CHECK_EQ(num_pending_readers_.LoadRelaxed(), 0);
Ian Rogers3e5cf302014-05-20 16:40:37 -0700469 CHECK_EQ(num_pending_writers_.LoadRelaxed(), 0);
Ian Rogers81d425b2012-09-27 16:03:43 -0700470#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700471 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
472 // may still be using locks.
473 int rc = pthread_rwlock_destroy(&rwlock_);
474 if (rc != 0) {
475 errno = rc;
476 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Ian Rogers50b35e22012-10-04 10:09:15 -0700477 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700478 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800479 bool shutting_down = runtime == NULL || runtime->IsShuttingDownLocked();
Ian Rogers120f1c72012-09-28 17:17:10 -0700480 PLOG(shutting_down ? WARNING : FATAL) << "pthread_rwlock_destroy failed for " << name_;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800481 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700482#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483}
484
Ian Rogers81d425b2012-09-27 16:03:43 -0700485void ReaderWriterMutex::ExclusiveLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700486 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700487 AssertNotExclusiveHeld(self);
488#if ART_USE_FUTEXES
489 bool done = false;
490 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700491 int32_t cur_state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700492 if (LIKELY(cur_state == 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700493 // Change state from 0 to -1 and impose load/store ordering appropriate for lock acquisition.
494 done = state_.CompareExchangeWeakAcquire(0 /* cur_state*/, -1 /* new state */);
Ian Rogers81d425b2012-09-27 16:03:43 -0700495 } else {
496 // Failed to acquire, hang up.
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700497 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersc7190692014-07-08 23:50:26 -0700498 ++num_pending_writers_;
499 if (futex(state_.Address(), FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
Brian Carlstrom0de79852013-07-25 22:29:58 -0700500 // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
501 // We don't use TEMP_FAILURE_RETRY so we can intentionally retry to acquire the lock.
502 if ((errno != EAGAIN) && (errno != EINTR)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700503 PLOG(FATAL) << "futex wait failed for " << name_;
504 }
505 }
Ian Rogersc7190692014-07-08 23:50:26 -0700506 --num_pending_writers_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700507 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700508 } while (!done);
Ian Rogersc7190692014-07-08 23:50:26 -0700509 DCHECK_EQ(state_.LoadRelaxed(), -1);
Ian Rogers81d425b2012-09-27 16:03:43 -0700510#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700512#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700513 DCHECK_EQ(exclusive_owner_, 0U);
514 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700515 RegisterAsLocked(self);
516 AssertExclusiveHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517}
518
Ian Rogers81d425b2012-09-27 16:03:43 -0700519void ReaderWriterMutex::ExclusiveUnlock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700520 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700521 AssertExclusiveHeld(self);
522 RegisterAsUnlocked(self);
Ian Rogersc5f17732014-06-05 20:48:42 -0700523 DCHECK_NE(exclusive_owner_, 0U);
Ian Rogers81d425b2012-09-27 16:03:43 -0700524#if ART_USE_FUTEXES
525 bool done = false;
526 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700527 int32_t cur_state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700528 if (LIKELY(cur_state == -1)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700529 // We're no longer the owner.
530 exclusive_owner_ = 0;
Ian Rogersc7190692014-07-08 23:50:26 -0700531 // Change state from -1 to 0 and impose load/store ordering appropriate for lock release.
532 // Note, the relaxed loads below musn't reorder before the CompareExchange.
533 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
534 // a status bit into the state on contention.
535 done = state_.CompareExchangeWeakSequentiallyConsistent(-1 /* cur_state*/, 0 /* new state */);
536 if (LIKELY(done)) { // Weak CAS may fail spuriously.
Ian Rogers81d425b2012-09-27 16:03:43 -0700537 // Wake any waiters.
Ian Rogersc7190692014-07-08 23:50:26 -0700538 if (UNLIKELY(num_pending_readers_.LoadRelaxed() > 0 ||
539 num_pending_writers_.LoadRelaxed() > 0)) {
540 futex(state_.Address(), FUTEX_WAKE, -1, NULL, NULL, 0);
Ian Rogers81d425b2012-09-27 16:03:43 -0700541 }
542 }
543 } else {
544 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
545 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700546 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700547#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700548 exclusive_owner_ = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700550#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700551}
552
Ian Rogers66aee5c2012-08-15 17:17:47 -0700553#if HAVE_TIMED_RWLOCK
Ian Rogersc604d732012-10-14 16:09:54 -0700554bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700555 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700556#if ART_USE_FUTEXES
557 bool done = false;
Ian Rogersc604d732012-10-14 16:09:54 -0700558 timespec end_abs_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800559 InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &end_abs_ts);
Ian Rogers81d425b2012-09-27 16:03:43 -0700560 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700561 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers81d425b2012-09-27 16:03:43 -0700562 if (cur_state == 0) {
Ian Rogersc7190692014-07-08 23:50:26 -0700563 // Change state from 0 to -1 and impose load/store ordering appropriate for lock acquisition.
564 done = state_.CompareExchangeWeakAcquire(0 /* cur_state */, -1 /* new state */);
Ian Rogers81d425b2012-09-27 16:03:43 -0700565 } else {
566 // Failed to acquire, hang up.
Ian Rogersc604d732012-10-14 16:09:54 -0700567 timespec now_abs_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800568 InitTimeSpec(true, CLOCK_REALTIME, 0, 0, &now_abs_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700569 timespec rel_ts;
570 if (ComputeRelativeTimeSpec(&rel_ts, end_abs_ts, now_abs_ts)) {
571 return false; // Timed out.
572 }
Hiroshi Yamauchib3733082013-08-12 17:28:49 -0700573 ScopedContentionRecorder scr(this, SafeGetTid(self), GetExclusiveOwnerTid());
Ian Rogersc7190692014-07-08 23:50:26 -0700574 ++num_pending_writers_;
575 if (futex(state_.Address(), FUTEX_WAIT, cur_state, &rel_ts, NULL, 0) != 0) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700576 if (errno == ETIMEDOUT) {
Ian Rogersc7190692014-07-08 23:50:26 -0700577 --num_pending_writers_;
Ian Rogersc604d732012-10-14 16:09:54 -0700578 return false; // Timed out.
Brian Carlstrom0de79852013-07-25 22:29:58 -0700579 } else if ((errno != EAGAIN) && (errno != EINTR)) {
580 // EAGAIN and EINTR both indicate a spurious failure,
581 // recompute the relative time out from now and try again.
582 // We don't use TEMP_FAILURE_RETRY so we can recompute rel_ts;
Ian Rogers81d425b2012-09-27 16:03:43 -0700583 PLOG(FATAL) << "timed futex wait failed for " << name_;
584 }
585 }
Ian Rogersc7190692014-07-08 23:50:26 -0700586 --num_pending_writers_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700587 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700588 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700589#else
Ian Rogersc604d732012-10-14 16:09:54 -0700590 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700591 InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700592 int result = pthread_rwlock_timedwrlock(&rwlock_, &ts);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700593 if (result == ETIMEDOUT) {
594 return false;
595 }
596 if (result != 0) {
597 errno = result;
Ian Rogersa5acfd32012-08-15 11:50:10 -0700598 PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700600#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700601 exclusive_owner_ = SafeGetTid(self);
Ian Rogers81d425b2012-09-27 16:03:43 -0700602 RegisterAsLocked(self);
603 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 return true;
605}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700606#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700607
Ian Rogers81d425b2012-09-27 16:03:43 -0700608bool ReaderWriterMutex::SharedTryLock(Thread* self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700609 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers81d425b2012-09-27 16:03:43 -0700610#if ART_USE_FUTEXES
611 bool done = false;
612 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700613 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers81d425b2012-09-27 16:03:43 -0700614 if (cur_state >= 0) {
Ian Rogersc7190692014-07-08 23:50:26 -0700615 // Add as an extra reader and impose load/store ordering appropriate for lock acquisition.
616 done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1);
Ian Rogers81d425b2012-09-27 16:03:43 -0700617 } else {
618 // Owner holds it exclusively.
619 return false;
620 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700621 } while (!done);
Ian Rogers81d425b2012-09-27 16:03:43 -0700622#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 int result = pthread_rwlock_tryrdlock(&rwlock_);
624 if (result == EBUSY) {
625 return false;
626 }
627 if (result != 0) {
628 errno = result;
629 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
630 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700631#endif
632 RegisterAsLocked(self);
633 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 return true;
635}
636
Ian Rogers81d425b2012-09-27 16:03:43 -0700637bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const {
Ian Rogers01ae5802012-09-28 16:14:01 -0700638 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 bool result;
640 if (UNLIKELY(self == NULL)) { // Handle unattached threads.
Ian Rogers01ae5802012-09-28 16:14:01 -0700641 result = IsExclusiveHeld(self); // TODO: a better best effort here.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 } else {
643 result = (self->GetHeldMutex(level_) == this);
644 }
645 return result;
646}
647
Ian Rogers56edc432013-01-18 16:51:51 -0800648void ReaderWriterMutex::Dump(std::ostream& os) const {
649 os << name_
650 << " level=" << static_cast<int>(level_)
Mathieu Chartier5869a2c2014-10-08 14:26:23 -0700651 << " owner=" << GetExclusiveOwnerTid()
652#if ART_USE_FUTEXES
653 << " state=" << state_.LoadSequentiallyConsistent()
654 << " num_pending_writers=" << num_pending_writers_.LoadSequentiallyConsistent()
655 << " num_pending_readers=" << num_pending_readers_.LoadSequentiallyConsistent()
656#endif
657 << " ";
Ian Rogers56edc432013-01-18 16:51:51 -0800658 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700659}
660
661std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800662 mu.Dump(os);
663 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700664}
665
Ian Rogers23055dc2013-04-18 16:29:16 -0700666ConditionVariable::ConditionVariable(const char* name, Mutex& guard)
Ian Rogersc604d732012-10-14 16:09:54 -0700667 : name_(name), guard_(guard) {
668#if ART_USE_FUTEXES
Ian Rogers3e5cf302014-05-20 16:40:37 -0700669 DCHECK_EQ(0, sequence_.LoadRelaxed());
Ian Rogersc604d732012-10-14 16:09:54 -0700670 num_waiters_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700671#else
Narayan Kamath51b71022014-03-04 11:57:09 +0000672 pthread_condattr_t cond_attrs;
Ian Rogersc5f17732014-06-05 20:48:42 -0700673 CHECK_MUTEX_CALL(pthread_condattr_init, (&cond_attrs));
Narayan Kamath51b71022014-03-04 11:57:09 +0000674#if !defined(__APPLE__)
675 // Apple doesn't have CLOCK_MONOTONIC or pthread_condattr_setclock.
676 CHECK_MUTEX_CALL(pthread_condattr_setclock(&cond_attrs, CLOCK_MONOTONIC));
677#endif
678 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, &cond_attrs));
Ian Rogersc604d732012-10-14 16:09:54 -0700679#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700680}
681
682ConditionVariable::~ConditionVariable() {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800683#if ART_USE_FUTEXES
684 if (num_waiters_!= 0) {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800685 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700686 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersd45f2012012-11-28 11:46:23 -0800687 LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_
688 << " called with " << num_waiters_ << " waiters.";
Ian Rogers5bd97c42012-11-27 02:38:26 -0800689 }
690#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700691 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
692 // may still be using condition variables.
693 int rc = pthread_cond_destroy(&cond_);
694 if (rc != 0) {
695 errno = rc;
Ian Rogers50b35e22012-10-04 10:09:15 -0700696 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700697 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800698 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughese62934d2012-04-09 11:24:29 -0700699 PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_;
700 }
Ian Rogersc604d732012-10-14 16:09:54 -0700701#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700702}
703
Ian Rogersc604d732012-10-14 16:09:54 -0700704void ConditionVariable::Broadcast(Thread* self) {
705 DCHECK(self == NULL || self == Thread::Current());
706 // TODO: enable below, there's a race in thread creation that causes false failures currently.
707 // guard_.AssertExclusiveHeld(self);
Mathieu Chartiere46cd752012-10-31 16:56:18 -0700708 DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self));
Ian Rogersc604d732012-10-14 16:09:54 -0700709#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800710 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800711 sequence_++; // Indicate the broadcast occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700712 bool done = false;
713 do {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700714 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersd45f2012012-11-28 11:46:23 -0800715 // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring
716 // mutex unlocks will awaken the requeued waiter thread.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800717 done = futex(sequence_.Address(), FUTEX_CMP_REQUEUE, 0,
Ian Rogers5bd97c42012-11-27 02:38:26 -0800718 reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()),
Ian Rogersc7190692014-07-08 23:50:26 -0700719 guard_.state_.Address(), cur_sequence) != -1;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800720 if (!done) {
721 if (errno != EAGAIN) {
722 PLOG(FATAL) << "futex cmp requeue failed for " << name_;
723 }
Ian Rogers5bd97c42012-11-27 02:38:26 -0800724 }
Ian Rogersc604d732012-10-14 16:09:54 -0700725 } while (!done);
726 }
727#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700728 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700729#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700730}
731
Ian Rogersc604d732012-10-14 16:09:54 -0700732void ConditionVariable::Signal(Thread* self) {
733 DCHECK(self == NULL || self == Thread::Current());
734 guard_.AssertExclusiveHeld(self);
735#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800736 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800737 sequence_++; // Indicate a signal occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700738 // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them
739 // to avoid this, however, requeueing can only move all waiters.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800740 int num_woken = futex(sequence_.Address(), FUTEX_WAKE, 1, NULL, NULL, 0);
Ian Rogersd45f2012012-11-28 11:46:23 -0800741 // Check something was woken or else we changed sequence_ before they had chance to wait.
Ian Rogers5bd97c42012-11-27 02:38:26 -0800742 CHECK((num_woken == 0) || (num_woken == 1));
Ian Rogersc604d732012-10-14 16:09:54 -0700743 }
744#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700745 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700746#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700747}
748
Ian Rogersc604d732012-10-14 16:09:54 -0700749void ConditionVariable::Wait(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700750 guard_.CheckSafeToWait(self);
751 WaitHoldingLocks(self);
752}
753
754void ConditionVariable::WaitHoldingLocks(Thread* self) {
Ian Rogersc604d732012-10-14 16:09:54 -0700755 DCHECK(self == NULL || self == Thread::Current());
756 guard_.AssertExclusiveHeld(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700757 unsigned int old_recursion_count = guard_.recursion_count_;
758#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700759 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800760 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800761 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700762 guard_.recursion_count_ = 1;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700763 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700764 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800765 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800766 // Futex failed, check it is an expected error.
767 // EAGAIN == EWOULDBLK, so we let the caller try again.
768 // EINTR implies a signal was sent to this thread.
769 if ((errno != EINTR) && (errno != EAGAIN)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700770 PLOG(FATAL) << "futex wait failed for " << name_;
771 }
772 }
773 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800774 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700775 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800776 // We awoke and so no longer require awakes from the guard_'s unlock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700777 CHECK_GE(guard_.num_contenders_.LoadRelaxed(), 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800778 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700779#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700780 uint64_t old_owner = guard_.exclusive_owner_;
781 guard_.exclusive_owner_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700782 guard_.recursion_count_ = 0;
783 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_));
Ian Rogersc5f17732014-06-05 20:48:42 -0700784 guard_.exclusive_owner_ = old_owner;
Ian Rogersc604d732012-10-14 16:09:54 -0700785#endif
786 guard_.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700787}
788
Ian Rogers7b078e82014-09-10 14:44:24 -0700789bool ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) {
Ian Rogersc604d732012-10-14 16:09:54 -0700790 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers7b078e82014-09-10 14:44:24 -0700791 bool timed_out = false;
Ian Rogersc604d732012-10-14 16:09:54 -0700792 guard_.AssertExclusiveHeld(self);
Ian Rogers1d54e732013-05-02 21:10:01 -0700793 guard_.CheckSafeToWait(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700794 unsigned int old_recursion_count = guard_.recursion_count_;
795#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700796 timespec rel_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800797 InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700798 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800799 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800800 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700801 guard_.recursion_count_ = 1;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700802 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700803 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800804 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700805 if (errno == ETIMEDOUT) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800806 // Timed out we're done.
Ian Rogers7b078e82014-09-10 14:44:24 -0700807 timed_out = true;
Brian Carlstrom0de79852013-07-25 22:29:58 -0700808 } else if ((errno == EAGAIN) || (errno == EINTR)) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800809 // A signal or ConditionVariable::Signal/Broadcast has come in.
Ian Rogersc604d732012-10-14 16:09:54 -0700810 } else {
811 PLOG(FATAL) << "timed futex wait failed for " << name_;
812 }
813 }
814 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800815 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700816 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800817 // We awoke and so no longer require awakes from the guard_'s unlock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700818 CHECK_GE(guard_.num_contenders_.LoadRelaxed(), 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800819 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700820#else
Narayan Kamath51b71022014-03-04 11:57:09 +0000821#if !defined(__APPLE__)
Ian Rogersc604d732012-10-14 16:09:54 -0700822 int clock = CLOCK_MONOTONIC;
Elliott Hughes5f791332011-09-15 17:45:30 -0700823#else
Ian Rogersc604d732012-10-14 16:09:54 -0700824 int clock = CLOCK_REALTIME;
Elliott Hughes5f791332011-09-15 17:45:30 -0700825#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700826 uint64_t old_owner = guard_.exclusive_owner_;
827 guard_.exclusive_owner_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700828 guard_.recursion_count_ = 0;
829 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700830 InitTimeSpec(true, clock, ms, ns, &ts);
Narayan Kamath51b71022014-03-04 11:57:09 +0000831 int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &guard_.mutex_, &ts));
Ian Rogers7b078e82014-09-10 14:44:24 -0700832 if (rc == ETIMEDOUT) {
833 timed_out = true;
834 } else if (rc != 0) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700835 errno = rc;
836 PLOG(FATAL) << "TimedWait failed for " << name_;
837 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700838 guard_.exclusive_owner_ = old_owner;
Ian Rogersc604d732012-10-14 16:09:54 -0700839#endif
840 guard_.recursion_count_ = old_recursion_count;
Ian Rogers7b078e82014-09-10 14:44:24 -0700841 return timed_out;
Elliott Hughes5f791332011-09-15 17:45:30 -0700842}
843
Ian Rogers719d1a32014-03-06 12:13:39 -0800844void Locks::Init() {
845 if (logging_lock_ != nullptr) {
846 // Already initialized.
Nicolas Geoffray7f0a6d62014-05-26 14:01:46 +0100847 if (kRuntimeISA == kX86 || kRuntimeISA == kX86_64) {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700848 DCHECK(modify_ldt_lock_ != nullptr);
849 } else {
850 DCHECK(modify_ldt_lock_ == nullptr);
851 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800852 DCHECK(abort_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700853 DCHECK(alloc_tracker_lock_ != nullptr);
Andreas Gampe74240812014-04-17 10:35:09 -0700854 DCHECK(allocated_monitor_ids_lock_ != nullptr);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700855 DCHECK(allocated_thread_ids_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800856 DCHECK(breakpoint_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800857 DCHECK(classlinker_classes_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700858 DCHECK(deoptimization_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800859 DCHECK(heap_bitmap_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700860 DCHECK(intern_table_lock_ != nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -0700861 DCHECK(jni_libraries_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800862 DCHECK(logging_lock_ != nullptr);
863 DCHECK(mutator_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700864 DCHECK(profiler_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800865 DCHECK(thread_list_lock_ != nullptr);
Ian Rogersf3d874c2014-07-17 18:52:42 -0700866 DCHECK(thread_list_suspend_thread_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800867 DCHECK(thread_suspend_count_lock_ != nullptr);
868 DCHECK(trace_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800869 DCHECK(unexpected_signal_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800870 } else {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700871 // Create global locks in level order from highest lock level to lowest.
Ian Rogersf3d874c2014-07-17 18:52:42 -0700872 LockLevel current_lock_level = kThreadListSuspendThreadLock;
873 DCHECK(thread_list_suspend_thread_lock_ == nullptr);
874 thread_list_suspend_thread_lock_ =
875 new Mutex("thread list suspend thread by .. lock", current_lock_level);
Ian Rogers719d1a32014-03-06 12:13:39 -0800876
Chao-ying Fu9e369312014-05-21 11:20:52 -0700877 #define UPDATE_CURRENT_LOCK_LEVEL(new_level) \
Brian Carlstrom306db812014-09-05 13:01:41 -0700878 if (new_level >= current_lock_level) { \
879 /* Do not use CHECKs or FATAL here, abort_lock_ is not setup yet. */ \
880 fprintf(stderr, "New local level %d is not less than current level %d\n", \
881 new_level, current_lock_level); \
882 exit(1); \
883 } \
Ian Rogersf3d874c2014-07-17 18:52:42 -0700884 current_lock_level = new_level;
885
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700886 UPDATE_CURRENT_LOCK_LEVEL(kInstrumentEntrypointsLock);
887 DCHECK(instrument_entrypoints_lock_ == nullptr);
888 instrument_entrypoints_lock_ = new Mutex("instrument entrypoint lock", current_lock_level);
889
Ian Rogersf3d874c2014-07-17 18:52:42 -0700890 UPDATE_CURRENT_LOCK_LEVEL(kMutatorLock);
891 DCHECK(mutator_lock_ == nullptr);
892 mutator_lock_ = new ReaderWriterMutex("mutator lock", current_lock_level);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700893
894 UPDATE_CURRENT_LOCK_LEVEL(kHeapBitmapLock);
895 DCHECK(heap_bitmap_lock_ == nullptr);
896 heap_bitmap_lock_ = new ReaderWriterMutex("heap bitmap lock", current_lock_level);
897
Jeff Hao69dbec62014-09-15 18:03:41 -0700898 UPDATE_CURRENT_LOCK_LEVEL(kTraceLock);
899 DCHECK(trace_lock_ == nullptr);
900 trace_lock_ = new Mutex("trace lock", current_lock_level);
901
Chao-ying Fu9e369312014-05-21 11:20:52 -0700902 UPDATE_CURRENT_LOCK_LEVEL(kRuntimeShutdownLock);
903 DCHECK(runtime_shutdown_lock_ == nullptr);
904 runtime_shutdown_lock_ = new Mutex("runtime shutdown lock", current_lock_level);
905
906 UPDATE_CURRENT_LOCK_LEVEL(kProfilerLock);
907 DCHECK(profiler_lock_ == nullptr);
908 profiler_lock_ = new Mutex("profiler lock", current_lock_level);
909
Brian Carlstrom306db812014-09-05 13:01:41 -0700910 UPDATE_CURRENT_LOCK_LEVEL(kDeoptimizationLock);
911 DCHECK(deoptimization_lock_ == nullptr);
912 deoptimization_lock_ = new Mutex("Deoptimization lock", current_lock_level);
913
914 UPDATE_CURRENT_LOCK_LEVEL(kAllocTrackerLock);
915 DCHECK(alloc_tracker_lock_ == nullptr);
916 alloc_tracker_lock_ = new Mutex("AllocTracker lock", current_lock_level);
917
Chao-ying Fu9e369312014-05-21 11:20:52 -0700918 UPDATE_CURRENT_LOCK_LEVEL(kThreadListLock);
919 DCHECK(thread_list_lock_ == nullptr);
920 thread_list_lock_ = new Mutex("thread list lock", current_lock_level);
921
Ian Rogers68d8b422014-07-17 11:09:10 -0700922 UPDATE_CURRENT_LOCK_LEVEL(kJniLoadLibraryLock);
923 DCHECK(jni_libraries_lock_ == nullptr);
924 jni_libraries_lock_ = new Mutex("JNI shared libraries map lock", current_lock_level);
925
Chao-ying Fu9e369312014-05-21 11:20:52 -0700926 UPDATE_CURRENT_LOCK_LEVEL(kBreakpointLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800927 DCHECK(breakpoint_lock_ == nullptr);
Sebastien Hertzed2be172014-08-19 15:33:43 +0200928 breakpoint_lock_ = new ReaderWriterMutex("breakpoint lock", current_lock_level);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700929
930 UPDATE_CURRENT_LOCK_LEVEL(kClassLinkerClassesLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800931 DCHECK(classlinker_classes_lock_ == nullptr);
932 classlinker_classes_lock_ = new ReaderWriterMutex("ClassLinker classes lock",
Chao-ying Fu9e369312014-05-21 11:20:52 -0700933 current_lock_level);
934
Andreas Gampe74240812014-04-17 10:35:09 -0700935 UPDATE_CURRENT_LOCK_LEVEL(kMonitorPoolLock);
936 DCHECK(allocated_monitor_ids_lock_ == nullptr);
937 allocated_monitor_ids_lock_ = new Mutex("allocated monitor ids lock", current_lock_level);
938
Chao-ying Fu9e369312014-05-21 11:20:52 -0700939 UPDATE_CURRENT_LOCK_LEVEL(kAllocatedThreadIdsLock);
940 DCHECK(allocated_thread_ids_lock_ == nullptr);
941 allocated_thread_ids_lock_ = new Mutex("allocated thread ids lock", current_lock_level);
942
Nicolas Geoffray7f0a6d62014-05-26 14:01:46 +0100943 if (kRuntimeISA == kX86 || kRuntimeISA == kX86_64) {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700944 UPDATE_CURRENT_LOCK_LEVEL(kModifyLdtLock);
945 DCHECK(modify_ldt_lock_ == nullptr);
946 modify_ldt_lock_ = new Mutex("modify_ldt lock", current_lock_level);
947 }
948
949 UPDATE_CURRENT_LOCK_LEVEL(kInternTableLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800950 DCHECK(intern_table_lock_ == nullptr);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700951 intern_table_lock_ = new Mutex("InternTable lock", current_lock_level);
952
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700953 UPDATE_CURRENT_LOCK_LEVEL(kReferenceProcessorLock);
954 DCHECK(reference_processor_lock_ == nullptr);
955 reference_processor_lock_ = new Mutex("ReferenceProcessor lock", current_lock_level);
956
957 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueClearedReferencesLock);
958 DCHECK(reference_queue_cleared_references_lock_ == nullptr);
959 reference_queue_cleared_references_lock_ = new Mutex("ReferenceQueue cleared references lock", current_lock_level);
960
961 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueWeakReferencesLock);
962 DCHECK(reference_queue_weak_references_lock_ == nullptr);
963 reference_queue_weak_references_lock_ = new Mutex("ReferenceQueue cleared references lock", current_lock_level);
964
965 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueFinalizerReferencesLock);
966 DCHECK(reference_queue_finalizer_references_lock_ == nullptr);
967 reference_queue_finalizer_references_lock_ = new Mutex("ReferenceQueue finalizer references lock", current_lock_level);
968
969 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueuePhantomReferencesLock);
970 DCHECK(reference_queue_phantom_references_lock_ == nullptr);
971 reference_queue_phantom_references_lock_ = new Mutex("ReferenceQueue phantom references lock", current_lock_level);
972
973 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueSoftReferencesLock);
974 DCHECK(reference_queue_soft_references_lock_ == nullptr);
975 reference_queue_soft_references_lock_ = new Mutex("ReferenceQueue soft references lock", current_lock_level);
976
Chao-ying Fu9e369312014-05-21 11:20:52 -0700977 UPDATE_CURRENT_LOCK_LEVEL(kAbortLock);
978 DCHECK(abort_lock_ == nullptr);
979 abort_lock_ = new Mutex("abort lock", current_lock_level, true);
980
981 UPDATE_CURRENT_LOCK_LEVEL(kThreadSuspendCountLock);
982 DCHECK(thread_suspend_count_lock_ == nullptr);
983 thread_suspend_count_lock_ = new Mutex("thread suspend count lock", current_lock_level);
984
985 UPDATE_CURRENT_LOCK_LEVEL(kUnexpectedSignalLock);
986 DCHECK(unexpected_signal_lock_ == nullptr);
987 unexpected_signal_lock_ = new Mutex("unexpected signal lock", current_lock_level, true);
988
Hiroshi Yamauchi3eed93d2014-06-04 11:43:59 -0700989 UPDATE_CURRENT_LOCK_LEVEL(kMemMapsLock);
990 DCHECK(mem_maps_lock_ == nullptr);
991 mem_maps_lock_ = new Mutex("mem maps lock", current_lock_level);
992
Chao-ying Fu9e369312014-05-21 11:20:52 -0700993 UPDATE_CURRENT_LOCK_LEVEL(kLoggingLock);
994 DCHECK(logging_lock_ == nullptr);
995 logging_lock_ = new Mutex("logging lock", current_lock_level, true);
996
997 #undef UPDATE_CURRENT_LOCK_LEVEL
Ian Rogers719d1a32014-03-06 12:13:39 -0800998 }
999}
1000
1001
Elliott Hughese62934d2012-04-09 11:24:29 -07001002} // namespace art