blob: 4383a7c43fd940601f6e1ffb03e038b7a9dc0c8a [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_)
651 << " owner=" << GetExclusiveOwnerTid() << " ";
652 DumpContention(os);
Ian Rogers01ae5802012-09-28 16:14:01 -0700653}
654
655std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu) {
Ian Rogers56edc432013-01-18 16:51:51 -0800656 mu.Dump(os);
657 return os;
Ian Rogers01ae5802012-09-28 16:14:01 -0700658}
659
Ian Rogers23055dc2013-04-18 16:29:16 -0700660ConditionVariable::ConditionVariable(const char* name, Mutex& guard)
Ian Rogersc604d732012-10-14 16:09:54 -0700661 : name_(name), guard_(guard) {
662#if ART_USE_FUTEXES
Ian Rogers3e5cf302014-05-20 16:40:37 -0700663 DCHECK_EQ(0, sequence_.LoadRelaxed());
Ian Rogersc604d732012-10-14 16:09:54 -0700664 num_waiters_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700665#else
Narayan Kamath51b71022014-03-04 11:57:09 +0000666 pthread_condattr_t cond_attrs;
Ian Rogersc5f17732014-06-05 20:48:42 -0700667 CHECK_MUTEX_CALL(pthread_condattr_init, (&cond_attrs));
Narayan Kamath51b71022014-03-04 11:57:09 +0000668#if !defined(__APPLE__)
669 // Apple doesn't have CLOCK_MONOTONIC or pthread_condattr_setclock.
670 CHECK_MUTEX_CALL(pthread_condattr_setclock(&cond_attrs, CLOCK_MONOTONIC));
671#endif
672 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, &cond_attrs));
Ian Rogersc604d732012-10-14 16:09:54 -0700673#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700674}
675
676ConditionVariable::~ConditionVariable() {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800677#if ART_USE_FUTEXES
678 if (num_waiters_!= 0) {
Ian Rogers5bd97c42012-11-27 02:38:26 -0800679 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700680 bool shutting_down = runtime == nullptr || runtime->IsShuttingDown(Thread::Current());
Ian Rogersd45f2012012-11-28 11:46:23 -0800681 LOG(shutting_down ? WARNING : FATAL) << "ConditionVariable::~ConditionVariable for " << name_
682 << " called with " << num_waiters_ << " waiters.";
Ian Rogers5bd97c42012-11-27 02:38:26 -0800683 }
684#else
Elliott Hughese62934d2012-04-09 11:24:29 -0700685 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
686 // may still be using condition variables.
687 int rc = pthread_cond_destroy(&cond_);
688 if (rc != 0) {
689 errno = rc;
Ian Rogers50b35e22012-10-04 10:09:15 -0700690 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
Ian Rogers120f1c72012-09-28 17:17:10 -0700691 Runtime* runtime = Runtime::Current();
Mathieu Chartier34e82932013-11-12 18:22:47 -0800692 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDownLocked();
Elliott Hughese62934d2012-04-09 11:24:29 -0700693 PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_;
694 }
Ian Rogersc604d732012-10-14 16:09:54 -0700695#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700696}
697
Ian Rogersc604d732012-10-14 16:09:54 -0700698void ConditionVariable::Broadcast(Thread* self) {
699 DCHECK(self == NULL || self == Thread::Current());
700 // TODO: enable below, there's a race in thread creation that causes false failures currently.
701 // guard_.AssertExclusiveHeld(self);
Mathieu Chartiere46cd752012-10-31 16:56:18 -0700702 DCHECK_EQ(guard_.GetExclusiveOwnerTid(), SafeGetTid(self));
Ian Rogersc604d732012-10-14 16:09:54 -0700703#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800704 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800705 sequence_++; // Indicate the broadcast occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700706 bool done = false;
707 do {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700708 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersd45f2012012-11-28 11:46:23 -0800709 // Requeue waiters onto mutex. The waiter holds the contender count on the mutex high ensuring
710 // mutex unlocks will awaken the requeued waiter thread.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800711 done = futex(sequence_.Address(), FUTEX_CMP_REQUEUE, 0,
Ian Rogers5bd97c42012-11-27 02:38:26 -0800712 reinterpret_cast<const timespec*>(std::numeric_limits<int32_t>::max()),
Ian Rogersc7190692014-07-08 23:50:26 -0700713 guard_.state_.Address(), cur_sequence) != -1;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800714 if (!done) {
715 if (errno != EAGAIN) {
716 PLOG(FATAL) << "futex cmp requeue failed for " << name_;
717 }
Ian Rogers5bd97c42012-11-27 02:38:26 -0800718 }
Ian Rogersc604d732012-10-14 16:09:54 -0700719 } while (!done);
720 }
721#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700722 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700723#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700724}
725
Ian Rogersc604d732012-10-14 16:09:54 -0700726void ConditionVariable::Signal(Thread* self) {
727 DCHECK(self == NULL || self == Thread::Current());
728 guard_.AssertExclusiveHeld(self);
729#if ART_USE_FUTEXES
Ian Rogersd45f2012012-11-28 11:46:23 -0800730 if (num_waiters_ > 0) {
Ian Rogersb122a4b2013-11-19 18:00:50 -0800731 sequence_++; // Indicate a signal occurred.
Ian Rogersc604d732012-10-14 16:09:54 -0700732 // Futex wake 1 waiter who will then come and in contend on mutex. It'd be nice to requeue them
733 // to avoid this, however, requeueing can only move all waiters.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800734 int num_woken = futex(sequence_.Address(), FUTEX_WAKE, 1, NULL, NULL, 0);
Ian Rogersd45f2012012-11-28 11:46:23 -0800735 // Check something was woken or else we changed sequence_ before they had chance to wait.
Ian Rogers5bd97c42012-11-27 02:38:26 -0800736 CHECK((num_woken == 0) || (num_woken == 1));
Ian Rogersc604d732012-10-14 16:09:54 -0700737 }
738#else
Elliott Hughes5f791332011-09-15 17:45:30 -0700739 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
Ian Rogersc604d732012-10-14 16:09:54 -0700740#endif
Elliott Hughes5f791332011-09-15 17:45:30 -0700741}
742
Ian Rogersc604d732012-10-14 16:09:54 -0700743void ConditionVariable::Wait(Thread* self) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700744 guard_.CheckSafeToWait(self);
745 WaitHoldingLocks(self);
746}
747
748void ConditionVariable::WaitHoldingLocks(Thread* self) {
Ian Rogersc604d732012-10-14 16:09:54 -0700749 DCHECK(self == NULL || self == Thread::Current());
750 guard_.AssertExclusiveHeld(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700751 unsigned int old_recursion_count = guard_.recursion_count_;
752#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700753 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800754 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800755 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700756 guard_.recursion_count_ = 1;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700757 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700758 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800759 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, NULL, NULL, 0) != 0) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800760 // Futex failed, check it is an expected error.
761 // EAGAIN == EWOULDBLK, so we let the caller try again.
762 // EINTR implies a signal was sent to this thread.
763 if ((errno != EINTR) && (errno != EAGAIN)) {
Ian Rogersc604d732012-10-14 16:09:54 -0700764 PLOG(FATAL) << "futex wait failed for " << name_;
765 }
766 }
767 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800768 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700769 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800770 // We awoke and so no longer require awakes from the guard_'s unlock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700771 CHECK_GE(guard_.num_contenders_.LoadRelaxed(), 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800772 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700773#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700774 uint64_t old_owner = guard_.exclusive_owner_;
775 guard_.exclusive_owner_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700776 guard_.recursion_count_ = 0;
777 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &guard_.mutex_));
Ian Rogersc5f17732014-06-05 20:48:42 -0700778 guard_.exclusive_owner_ = old_owner;
Ian Rogersc604d732012-10-14 16:09:54 -0700779#endif
780 guard_.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700781}
782
Ian Rogers7b078e82014-09-10 14:44:24 -0700783bool ConditionVariable::TimedWait(Thread* self, int64_t ms, int32_t ns) {
Ian Rogersc604d732012-10-14 16:09:54 -0700784 DCHECK(self == NULL || self == Thread::Current());
Ian Rogers7b078e82014-09-10 14:44:24 -0700785 bool timed_out = false;
Ian Rogersc604d732012-10-14 16:09:54 -0700786 guard_.AssertExclusiveHeld(self);
Ian Rogers1d54e732013-05-02 21:10:01 -0700787 guard_.CheckSafeToWait(self);
Ian Rogersc604d732012-10-14 16:09:54 -0700788 unsigned int old_recursion_count = guard_.recursion_count_;
789#if ART_USE_FUTEXES
Ian Rogersc604d732012-10-14 16:09:54 -0700790 timespec rel_ts;
Ian Rogers5bd97c42012-11-27 02:38:26 -0800791 InitTimeSpec(false, CLOCK_REALTIME, ms, ns, &rel_ts);
Ian Rogersc604d732012-10-14 16:09:54 -0700792 num_waiters_++;
Ian Rogersd45f2012012-11-28 11:46:23 -0800793 // Ensure the Mutex is contended so that requeued threads are awoken.
Ian Rogersb122a4b2013-11-19 18:00:50 -0800794 guard_.num_contenders_++;
Ian Rogersc604d732012-10-14 16:09:54 -0700795 guard_.recursion_count_ = 1;
Ian Rogers3e5cf302014-05-20 16:40:37 -0700796 int32_t cur_sequence = sequence_.LoadRelaxed();
Ian Rogersc604d732012-10-14 16:09:54 -0700797 guard_.ExclusiveUnlock(self);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800798 if (futex(sequence_.Address(), FUTEX_WAIT, cur_sequence, &rel_ts, NULL, 0) != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700799 if (errno == ETIMEDOUT) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800800 // Timed out we're done.
Ian Rogers7b078e82014-09-10 14:44:24 -0700801 timed_out = true;
Brian Carlstrom0de79852013-07-25 22:29:58 -0700802 } else if ((errno == EAGAIN) || (errno == EINTR)) {
Ian Rogersd45f2012012-11-28 11:46:23 -0800803 // A signal or ConditionVariable::Signal/Broadcast has come in.
Ian Rogersc604d732012-10-14 16:09:54 -0700804 } else {
805 PLOG(FATAL) << "timed futex wait failed for " << name_;
806 }
807 }
808 guard_.ExclusiveLock(self);
Ian Rogersd45f2012012-11-28 11:46:23 -0800809 CHECK_GE(num_waiters_, 0);
Ian Rogersc604d732012-10-14 16:09:54 -0700810 num_waiters_--;
Ian Rogersd45f2012012-11-28 11:46:23 -0800811 // We awoke and so no longer require awakes from the guard_'s unlock.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700812 CHECK_GE(guard_.num_contenders_.LoadRelaxed(), 0);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800813 guard_.num_contenders_--;
Ian Rogersc604d732012-10-14 16:09:54 -0700814#else
Narayan Kamath51b71022014-03-04 11:57:09 +0000815#if !defined(__APPLE__)
Ian Rogersc604d732012-10-14 16:09:54 -0700816 int clock = CLOCK_MONOTONIC;
Elliott Hughes5f791332011-09-15 17:45:30 -0700817#else
Ian Rogersc604d732012-10-14 16:09:54 -0700818 int clock = CLOCK_REALTIME;
Elliott Hughes5f791332011-09-15 17:45:30 -0700819#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700820 uint64_t old_owner = guard_.exclusive_owner_;
821 guard_.exclusive_owner_ = 0;
Ian Rogersc604d732012-10-14 16:09:54 -0700822 guard_.recursion_count_ = 0;
823 timespec ts;
Brian Carlstrombcc29262012-11-02 11:36:03 -0700824 InitTimeSpec(true, clock, ms, ns, &ts);
Narayan Kamath51b71022014-03-04 11:57:09 +0000825 int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &guard_.mutex_, &ts));
Ian Rogers7b078e82014-09-10 14:44:24 -0700826 if (rc == ETIMEDOUT) {
827 timed_out = true;
828 } else if (rc != 0) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700829 errno = rc;
830 PLOG(FATAL) << "TimedWait failed for " << name_;
831 }
Ian Rogersc5f17732014-06-05 20:48:42 -0700832 guard_.exclusive_owner_ = old_owner;
Ian Rogersc604d732012-10-14 16:09:54 -0700833#endif
834 guard_.recursion_count_ = old_recursion_count;
Ian Rogers7b078e82014-09-10 14:44:24 -0700835 return timed_out;
Elliott Hughes5f791332011-09-15 17:45:30 -0700836}
837
Ian Rogers719d1a32014-03-06 12:13:39 -0800838void Locks::Init() {
839 if (logging_lock_ != nullptr) {
840 // Already initialized.
Nicolas Geoffray7f0a6d62014-05-26 14:01:46 +0100841 if (kRuntimeISA == kX86 || kRuntimeISA == kX86_64) {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700842 DCHECK(modify_ldt_lock_ != nullptr);
843 } else {
844 DCHECK(modify_ldt_lock_ == nullptr);
845 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800846 DCHECK(abort_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700847 DCHECK(alloc_tracker_lock_ != nullptr);
Andreas Gampe74240812014-04-17 10:35:09 -0700848 DCHECK(allocated_monitor_ids_lock_ != nullptr);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700849 DCHECK(allocated_thread_ids_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800850 DCHECK(breakpoint_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800851 DCHECK(classlinker_classes_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700852 DCHECK(deoptimization_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800853 DCHECK(heap_bitmap_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700854 DCHECK(intern_table_lock_ != nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -0700855 DCHECK(jni_libraries_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800856 DCHECK(logging_lock_ != nullptr);
857 DCHECK(mutator_lock_ != nullptr);
Brian Carlstrom306db812014-09-05 13:01:41 -0700858 DCHECK(profiler_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800859 DCHECK(thread_list_lock_ != nullptr);
Ian Rogersf3d874c2014-07-17 18:52:42 -0700860 DCHECK(thread_list_suspend_thread_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800861 DCHECK(thread_suspend_count_lock_ != nullptr);
862 DCHECK(trace_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800863 DCHECK(unexpected_signal_lock_ != nullptr);
Ian Rogers719d1a32014-03-06 12:13:39 -0800864 } else {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700865 // Create global locks in level order from highest lock level to lowest.
Ian Rogersf3d874c2014-07-17 18:52:42 -0700866 LockLevel current_lock_level = kThreadListSuspendThreadLock;
867 DCHECK(thread_list_suspend_thread_lock_ == nullptr);
868 thread_list_suspend_thread_lock_ =
869 new Mutex("thread list suspend thread by .. lock", current_lock_level);
Ian Rogers719d1a32014-03-06 12:13:39 -0800870
Chao-ying Fu9e369312014-05-21 11:20:52 -0700871 #define UPDATE_CURRENT_LOCK_LEVEL(new_level) \
Brian Carlstrom306db812014-09-05 13:01:41 -0700872 if (new_level >= current_lock_level) { \
873 /* Do not use CHECKs or FATAL here, abort_lock_ is not setup yet. */ \
874 fprintf(stderr, "New local level %d is not less than current level %d\n", \
875 new_level, current_lock_level); \
876 exit(1); \
877 } \
Ian Rogersf3d874c2014-07-17 18:52:42 -0700878 current_lock_level = new_level;
879
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700880 UPDATE_CURRENT_LOCK_LEVEL(kInstrumentEntrypointsLock);
881 DCHECK(instrument_entrypoints_lock_ == nullptr);
882 instrument_entrypoints_lock_ = new Mutex("instrument entrypoint lock", current_lock_level);
883
Ian Rogersf3d874c2014-07-17 18:52:42 -0700884 UPDATE_CURRENT_LOCK_LEVEL(kMutatorLock);
885 DCHECK(mutator_lock_ == nullptr);
886 mutator_lock_ = new ReaderWriterMutex("mutator lock", current_lock_level);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700887
888 UPDATE_CURRENT_LOCK_LEVEL(kHeapBitmapLock);
889 DCHECK(heap_bitmap_lock_ == nullptr);
890 heap_bitmap_lock_ = new ReaderWriterMutex("heap bitmap lock", current_lock_level);
891
Jeff Hao69dbec62014-09-15 18:03:41 -0700892 UPDATE_CURRENT_LOCK_LEVEL(kTraceLock);
893 DCHECK(trace_lock_ == nullptr);
894 trace_lock_ = new Mutex("trace lock", current_lock_level);
895
Chao-ying Fu9e369312014-05-21 11:20:52 -0700896 UPDATE_CURRENT_LOCK_LEVEL(kRuntimeShutdownLock);
897 DCHECK(runtime_shutdown_lock_ == nullptr);
898 runtime_shutdown_lock_ = new Mutex("runtime shutdown lock", current_lock_level);
899
900 UPDATE_CURRENT_LOCK_LEVEL(kProfilerLock);
901 DCHECK(profiler_lock_ == nullptr);
902 profiler_lock_ = new Mutex("profiler lock", current_lock_level);
903
Brian Carlstrom306db812014-09-05 13:01:41 -0700904 UPDATE_CURRENT_LOCK_LEVEL(kDeoptimizationLock);
905 DCHECK(deoptimization_lock_ == nullptr);
906 deoptimization_lock_ = new Mutex("Deoptimization lock", current_lock_level);
907
908 UPDATE_CURRENT_LOCK_LEVEL(kAllocTrackerLock);
909 DCHECK(alloc_tracker_lock_ == nullptr);
910 alloc_tracker_lock_ = new Mutex("AllocTracker lock", current_lock_level);
911
Chao-ying Fu9e369312014-05-21 11:20:52 -0700912 UPDATE_CURRENT_LOCK_LEVEL(kThreadListLock);
913 DCHECK(thread_list_lock_ == nullptr);
914 thread_list_lock_ = new Mutex("thread list lock", current_lock_level);
915
Ian Rogers68d8b422014-07-17 11:09:10 -0700916 UPDATE_CURRENT_LOCK_LEVEL(kJniLoadLibraryLock);
917 DCHECK(jni_libraries_lock_ == nullptr);
918 jni_libraries_lock_ = new Mutex("JNI shared libraries map lock", current_lock_level);
919
Chao-ying Fu9e369312014-05-21 11:20:52 -0700920 UPDATE_CURRENT_LOCK_LEVEL(kBreakpointLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800921 DCHECK(breakpoint_lock_ == nullptr);
Sebastien Hertzed2be172014-08-19 15:33:43 +0200922 breakpoint_lock_ = new ReaderWriterMutex("breakpoint lock", current_lock_level);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700923
924 UPDATE_CURRENT_LOCK_LEVEL(kClassLinkerClassesLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800925 DCHECK(classlinker_classes_lock_ == nullptr);
926 classlinker_classes_lock_ = new ReaderWriterMutex("ClassLinker classes lock",
Chao-ying Fu9e369312014-05-21 11:20:52 -0700927 current_lock_level);
928
Andreas Gampe74240812014-04-17 10:35:09 -0700929 UPDATE_CURRENT_LOCK_LEVEL(kMonitorPoolLock);
930 DCHECK(allocated_monitor_ids_lock_ == nullptr);
931 allocated_monitor_ids_lock_ = new Mutex("allocated monitor ids lock", current_lock_level);
932
Chao-ying Fu9e369312014-05-21 11:20:52 -0700933 UPDATE_CURRENT_LOCK_LEVEL(kAllocatedThreadIdsLock);
934 DCHECK(allocated_thread_ids_lock_ == nullptr);
935 allocated_thread_ids_lock_ = new Mutex("allocated thread ids lock", current_lock_level);
936
Nicolas Geoffray7f0a6d62014-05-26 14:01:46 +0100937 if (kRuntimeISA == kX86 || kRuntimeISA == kX86_64) {
Chao-ying Fu9e369312014-05-21 11:20:52 -0700938 UPDATE_CURRENT_LOCK_LEVEL(kModifyLdtLock);
939 DCHECK(modify_ldt_lock_ == nullptr);
940 modify_ldt_lock_ = new Mutex("modify_ldt lock", current_lock_level);
941 }
942
943 UPDATE_CURRENT_LOCK_LEVEL(kInternTableLock);
Ian Rogers719d1a32014-03-06 12:13:39 -0800944 DCHECK(intern_table_lock_ == nullptr);
Chao-ying Fu9e369312014-05-21 11:20:52 -0700945 intern_table_lock_ = new Mutex("InternTable lock", current_lock_level);
946
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700947 UPDATE_CURRENT_LOCK_LEVEL(kReferenceProcessorLock);
948 DCHECK(reference_processor_lock_ == nullptr);
949 reference_processor_lock_ = new Mutex("ReferenceProcessor lock", current_lock_level);
950
951 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueClearedReferencesLock);
952 DCHECK(reference_queue_cleared_references_lock_ == nullptr);
953 reference_queue_cleared_references_lock_ = new Mutex("ReferenceQueue cleared references lock", current_lock_level);
954
955 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueWeakReferencesLock);
956 DCHECK(reference_queue_weak_references_lock_ == nullptr);
957 reference_queue_weak_references_lock_ = new Mutex("ReferenceQueue cleared references lock", current_lock_level);
958
959 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueFinalizerReferencesLock);
960 DCHECK(reference_queue_finalizer_references_lock_ == nullptr);
961 reference_queue_finalizer_references_lock_ = new Mutex("ReferenceQueue finalizer references lock", current_lock_level);
962
963 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueuePhantomReferencesLock);
964 DCHECK(reference_queue_phantom_references_lock_ == nullptr);
965 reference_queue_phantom_references_lock_ = new Mutex("ReferenceQueue phantom references lock", current_lock_level);
966
967 UPDATE_CURRENT_LOCK_LEVEL(kReferenceQueueSoftReferencesLock);
968 DCHECK(reference_queue_soft_references_lock_ == nullptr);
969 reference_queue_soft_references_lock_ = new Mutex("ReferenceQueue soft references lock", current_lock_level);
970
Chao-ying Fu9e369312014-05-21 11:20:52 -0700971 UPDATE_CURRENT_LOCK_LEVEL(kAbortLock);
972 DCHECK(abort_lock_ == nullptr);
973 abort_lock_ = new Mutex("abort lock", current_lock_level, true);
974
975 UPDATE_CURRENT_LOCK_LEVEL(kThreadSuspendCountLock);
976 DCHECK(thread_suspend_count_lock_ == nullptr);
977 thread_suspend_count_lock_ = new Mutex("thread suspend count lock", current_lock_level);
978
979 UPDATE_CURRENT_LOCK_LEVEL(kUnexpectedSignalLock);
980 DCHECK(unexpected_signal_lock_ == nullptr);
981 unexpected_signal_lock_ = new Mutex("unexpected signal lock", current_lock_level, true);
982
Hiroshi Yamauchi3eed93d2014-06-04 11:43:59 -0700983 UPDATE_CURRENT_LOCK_LEVEL(kMemMapsLock);
984 DCHECK(mem_maps_lock_ == nullptr);
985 mem_maps_lock_ = new Mutex("mem maps lock", current_lock_level);
986
Chao-ying Fu9e369312014-05-21 11:20:52 -0700987 UPDATE_CURRENT_LOCK_LEVEL(kLoggingLock);
988 DCHECK(logging_lock_ == nullptr);
989 logging_lock_ = new Mutex("logging lock", current_lock_level, true);
990
991 #undef UPDATE_CURRENT_LOCK_LEVEL
Ian Rogers719d1a32014-03-06 12:13:39 -0800992 }
993}
994
995
Elliott Hughese62934d2012-04-09 11:24:29 -0700996} // namespace art