blob: 92b7c6537c4a5689a89711bdb9f8bd781526049b [file] [log] [blame]
Ian Rogers693ff612013-02-01 10:56:12 -08001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_MUTEX_INL_H_
18#define ART_RUNTIME_BASE_MUTEX_INL_H_
Ian Rogers693ff612013-02-01 10:56:12 -080019
Ian Rogers220228e2014-01-23 09:08:16 -080020#include <inttypes.h>
21
Ian Rogers693ff612013-02-01 10:56:12 -080022#include "mutex.h"
23
Ian Rogers576ca0c2014-06-06 15:58:22 -070024#include "base/stringprintf.h"
Ian Rogerscf7f1912014-10-22 22:06:39 -070025#include "base/value_object.h"
Ian Rogers693ff612013-02-01 10:56:12 -080026#include "thread.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "utils.h"
Ian Rogers693ff612013-02-01 10:56:12 -080028
Ian Rogers693ff612013-02-01 10:56:12 -080029#if ART_USE_FUTEXES
30#include "linux/futex.h"
31#include "sys/syscall.h"
32#ifndef SYS_futex
33#define SYS_futex __NR_futex
34#endif
Chih-Hung Hsieh729c1cf2014-11-06 10:49:16 -080035#endif // ART_USE_FUTEXES
36
Ian Rogersd6d7c3b2014-11-06 14:26:29 -080037#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
38
Chih-Hung Hsieh729c1cf2014-11-06 10:49:16 -080039namespace art {
40
41#if ART_USE_FUTEXES
Mathieu Chartier2cebb242015-04-21 16:50:40 -070042static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout,
43 volatile int *uaddr2, int val3) {
Ian Rogers693ff612013-02-01 10:56:12 -080044 return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
45}
46#endif // ART_USE_FUTEXES
47
Ian Rogers693ff612013-02-01 10:56:12 -080048static inline uint64_t SafeGetTid(const Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 if (self != nullptr) {
Ian Rogers693ff612013-02-01 10:56:12 -080050 return static_cast<uint64_t>(self->GetTid());
51 } else {
52 return static_cast<uint64_t>(GetTid());
53 }
54}
55
56static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
57 // The check below enumerates the cases where we expect not to be able to sanity check locks
58 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
59 // TODO: tighten this check.
60 if (kDebugLocking) {
David Sehrf42eb2c2016-10-19 13:20:45 -070061 CHECK(!Locks::IsSafeToCallAbortRacy() ||
Chao-ying Fu9e369312014-05-21 11:20:52 -070062 // Used during thread creation to avoid races with runtime shutdown. Thread::Current not
63 // yet established.
64 level == kRuntimeShutdownLock ||
65 // Thread Ids are allocated/released before threads are established.
66 level == kAllocatedThreadIdsLock ||
67 // Thread LDT's are initialized without Thread::Current established.
68 level == kModifyLdtLock ||
69 // Threads are unregistered while holding the thread list lock, during this process they
70 // no longer exist and so we expect an unlock with no self.
71 level == kThreadListLock ||
72 // Ignore logging which may or may not have set up thread data structures.
73 level == kLoggingLock ||
Nicolas Geoffray9f5f8ac2016-06-29 14:39:59 +010074 // When transitioning from suspended to runnable, a daemon thread might be in
75 // a situation where the runtime is shutting down. To not crash our debug locking
76 // mechanism we just pass null Thread* to the MutexLock during that transition
77 // (see Thread::TransitionFromSuspendedToRunnable).
78 level == kThreadSuspendCountLock ||
Chao-ying Fu9e369312014-05-21 11:20:52 -070079 // Avoid recursive death.
Ian Rogers06abcdf2014-05-23 11:39:11 -070080 level == kAbortLock) << level;
Ian Rogers693ff612013-02-01 10:56:12 -080081 }
82}
83
Ian Rogersb6c31ea2013-02-04 18:11:33 -080084inline void BaseMutex::RegisterAsLocked(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 if (UNLIKELY(self == nullptr)) {
Ian Rogersb6c31ea2013-02-04 18:11:33 -080086 CheckUnattachedThread(level_);
87 return;
88 }
89 if (kDebugLocking) {
90 // Check if a bad Mutex of this level or lower is held.
91 bool bad_mutexes_held = false;
92 for (int i = level_; i >= 0; --i) {
93 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 if (UNLIKELY(held_mutex != nullptr)) {
Elliott Hughes0f827162013-02-26 12:12:58 -080095 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -080096 << "(level " << LockLevel(i) << " - " << i
97 << ") while locking \"" << name_ << "\" "
98 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -080099 if (i > kAbortLock) {
100 // Only abort in the check below if this is more than abort level lock.
101 bad_mutexes_held = true;
102 }
103 }
104 }
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000105 if (gAborting == 0) { // Avoid recursive aborts.
106 CHECK(!bad_mutexes_held);
107 }
Ian Rogersb6c31ea2013-02-04 18:11:33 -0800108 }
109 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
110 // the monitor list.
111 if (level_ != kMonitorLock) {
112 self->SetHeldMutex(level_, this);
113 }
114}
115
Ian Rogers693ff612013-02-01 10:56:12 -0800116inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 if (UNLIKELY(self == nullptr)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800118 CheckUnattachedThread(level_);
119 return;
120 }
121 if (level_ != kMonitorLock) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000122 if (kDebugLocking && gAborting == 0) { // Avoid recursive aborts.
Ian Rogers693ff612013-02-01 10:56:12 -0800123 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
124 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700125 self->SetHeldMutex(level_, nullptr);
Ian Rogers693ff612013-02-01 10:56:12 -0800126 }
127}
128
129inline void ReaderWriterMutex::SharedLock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 DCHECK(self == nullptr || self == Thread::Current());
Ian Rogers693ff612013-02-01 10:56:12 -0800131#if ART_USE_FUTEXES
132 bool done = false;
133 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700134 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800135 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800136 // Add as an extra reader.
Ian Rogersc7190692014-07-08 23:50:26 -0700137 done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1);
Ian Rogers693ff612013-02-01 10:56:12 -0800138 } else {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700139 HandleSharedLockContention(self, cur_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800140 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700141 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800142#else
143 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
144#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700145 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800146 RegisterAsLocked(self);
147 AssertSharedHeld(self);
148}
149
150inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700151 DCHECK(self == nullptr || self == Thread::Current());
Ian Rogersc5f17732014-06-05 20:48:42 -0700152 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800153 AssertSharedHeld(self);
154 RegisterAsUnlocked(self);
155#if ART_USE_FUTEXES
156 bool done = false;
157 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700158 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers693ff612013-02-01 10:56:12 -0800159 if (LIKELY(cur_state > 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700160 // Reduce state by 1 and impose lock release load/store ordering.
161 // Note, the relaxed loads below musn't reorder before the CompareExchange.
162 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
163 // a status bit into the state on contention.
164 done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, cur_state - 1);
165 if (done && (cur_state - 1) == 0) { // Weak CAS may fail spuriously.
166 if (num_pending_writers_.LoadRelaxed() > 0 ||
167 num_pending_readers_.LoadRelaxed() > 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800168 // Wake any exclusive waiters as there are now no readers.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 futex(state_.Address(), FUTEX_WAKE, -1, nullptr, nullptr, 0);
Ian Rogers693ff612013-02-01 10:56:12 -0800170 }
171 }
172 } else {
173 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
174 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700175 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800176#else
177 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
178#endif
179}
180
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700181inline bool Mutex::IsExclusiveHeld(const Thread* self) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 DCHECK(self == nullptr || self == Thread::Current());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700183 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
184 if (kDebugLocking) {
185 // Sanity debug check that if we think it is locked we have it in our held mutexes.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700186 if (result && self != nullptr && level_ != kMonitorLock && !gAborting) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700187 CHECK_EQ(self->GetHeldMutex(level_), this);
188 }
189 }
190 return result;
191}
192
193inline uint64_t Mutex::GetExclusiveOwnerTid() const {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700194 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700195}
196
197inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700198 DCHECK(self == nullptr || self == Thread::Current());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700199 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
200 if (kDebugLocking) {
201 // Sanity that if the pthread thinks we own the lock the Thread agrees.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700202 if (self != nullptr && result) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700203 CHECK_EQ(self->GetHeldMutex(level_), this);
204 }
205 }
206 return result;
207}
208
209inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
210#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700211 int32_t state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700212 if (state == 0) {
213 return 0; // No owner.
214 } else if (state > 0) {
215 return -1; // Shared.
216 } else {
217 return exclusive_owner_;
218 }
219#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700220 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700221#endif
222}
223
Yu Lieac44242015-06-29 10:50:03 +0800224inline void MutatorMutex::TransitionFromRunnableToSuspended(Thread* self) {
225 AssertSharedHeld(self);
226 RegisterAsUnlocked(self);
227}
228
229inline void MutatorMutex::TransitionFromSuspendedToRunnable(Thread* self) {
230 RegisterAsLocked(self);
231 AssertSharedHeld(self);
232}
233
Ian Rogers693ff612013-02-01 10:56:12 -0800234} // namespace art
235
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700236#endif // ART_RUNTIME_BASE_MUTEX_INL_H_