blob: a727992687299b9dab34f60e622ce27e7d61f740 [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 "runtime.h"
27#include "thread.h"
28
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) {
61 Runtime* runtime = Runtime::Current();
Chao-ying Fu9e369312014-05-21 11:20:52 -070062 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDownLocked() ||
63 // Used during thread creation to avoid races with runtime shutdown. Thread::Current not
64 // yet established.
65 level == kRuntimeShutdownLock ||
66 // Thread Ids are allocated/released before threads are established.
67 level == kAllocatedThreadIdsLock ||
68 // Thread LDT's are initialized without Thread::Current established.
69 level == kModifyLdtLock ||
70 // Threads are unregistered while holding the thread list lock, during this process they
71 // no longer exist and so we expect an unlock with no self.
72 level == kThreadListLock ||
73 // Ignore logging which may or may not have set up thread data structures.
74 level == kLoggingLock ||
75 // Avoid recursive death.
Ian Rogers06abcdf2014-05-23 11:39:11 -070076 level == kAbortLock) << level;
Ian Rogers693ff612013-02-01 10:56:12 -080077 }
78}
79
Ian Rogersb6c31ea2013-02-04 18:11:33 -080080inline void BaseMutex::RegisterAsLocked(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070081 if (UNLIKELY(self == nullptr)) {
Ian Rogersb6c31ea2013-02-04 18:11:33 -080082 CheckUnattachedThread(level_);
83 return;
84 }
85 if (kDebugLocking) {
86 // Check if a bad Mutex of this level or lower is held.
87 bool bad_mutexes_held = false;
88 for (int i = level_; i >= 0; --i) {
89 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 if (UNLIKELY(held_mutex != nullptr)) {
Elliott Hughes0f827162013-02-26 12:12:58 -080091 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -080092 << "(level " << LockLevel(i) << " - " << i
93 << ") while locking \"" << name_ << "\" "
94 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -080095 if (i > kAbortLock) {
96 // Only abort in the check below if this is more than abort level lock.
97 bad_mutexes_held = true;
98 }
99 }
100 }
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000101 if (gAborting == 0) { // Avoid recursive aborts.
102 CHECK(!bad_mutexes_held);
103 }
Ian Rogersb6c31ea2013-02-04 18:11:33 -0800104 }
105 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
106 // the monitor list.
107 if (level_ != kMonitorLock) {
108 self->SetHeldMutex(level_, this);
109 }
110}
111
Ian Rogers693ff612013-02-01 10:56:12 -0800112inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113 if (UNLIKELY(self == nullptr)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800114 CheckUnattachedThread(level_);
115 return;
116 }
117 if (level_ != kMonitorLock) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000118 if (kDebugLocking && gAborting == 0) { // Avoid recursive aborts.
Ian Rogers693ff612013-02-01 10:56:12 -0800119 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
120 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 self->SetHeldMutex(level_, nullptr);
Ian Rogers693ff612013-02-01 10:56:12 -0800122 }
123}
124
125inline void ReaderWriterMutex::SharedLock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 DCHECK(self == nullptr || self == Thread::Current());
Ian Rogers693ff612013-02-01 10:56:12 -0800127#if ART_USE_FUTEXES
128 bool done = false;
129 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700130 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800131 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800132 // Add as an extra reader.
Ian Rogersc7190692014-07-08 23:50:26 -0700133 done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1);
Ian Rogers693ff612013-02-01 10:56:12 -0800134 } else {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700135 HandleSharedLockContention(self, cur_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800136 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700137 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800138#else
139 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
140#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700141 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800142 RegisterAsLocked(self);
143 AssertSharedHeld(self);
144}
145
146inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700147 DCHECK(self == nullptr || self == Thread::Current());
Ian Rogersc5f17732014-06-05 20:48:42 -0700148 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800149 AssertSharedHeld(self);
150 RegisterAsUnlocked(self);
151#if ART_USE_FUTEXES
152 bool done = false;
153 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700154 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers693ff612013-02-01 10:56:12 -0800155 if (LIKELY(cur_state > 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700156 // Reduce state by 1 and impose lock release load/store ordering.
157 // Note, the relaxed loads below musn't reorder before the CompareExchange.
158 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
159 // a status bit into the state on contention.
160 done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, cur_state - 1);
161 if (done && (cur_state - 1) == 0) { // Weak CAS may fail spuriously.
162 if (num_pending_writers_.LoadRelaxed() > 0 ||
163 num_pending_readers_.LoadRelaxed() > 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800164 // Wake any exclusive waiters as there are now no readers.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700165 futex(state_.Address(), FUTEX_WAKE, -1, nullptr, nullptr, 0);
Ian Rogers693ff612013-02-01 10:56:12 -0800166 }
167 }
168 } else {
169 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
170 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700171 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800172#else
173 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
174#endif
175}
176
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700177inline bool Mutex::IsExclusiveHeld(const Thread* self) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700178 DCHECK(self == nullptr || self == Thread::Current());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700179 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
180 if (kDebugLocking) {
181 // Sanity debug check that if we think it is locked we have it in our held mutexes.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 if (result && self != nullptr && level_ != kMonitorLock && !gAborting) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700183 CHECK_EQ(self->GetHeldMutex(level_), this);
184 }
185 }
186 return result;
187}
188
189inline uint64_t Mutex::GetExclusiveOwnerTid() const {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700190 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700191}
192
193inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700194 DCHECK(self == nullptr || self == Thread::Current());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700195 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
196 if (kDebugLocking) {
197 // Sanity that if the pthread thinks we own the lock the Thread agrees.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700198 if (self != nullptr && result) {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700199 CHECK_EQ(self->GetHeldMutex(level_), this);
200 }
201 }
202 return result;
203}
204
205inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
206#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700207 int32_t state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700208 if (state == 0) {
209 return 0; // No owner.
210 } else if (state > 0) {
211 return -1; // Shared.
212 } else {
213 return exclusive_owner_;
214 }
215#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700216 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700217#endif
218}
219
Ian Rogers693ff612013-02-01 10:56:12 -0800220} // namespace art
221
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700222#endif // ART_RUNTIME_BASE_MUTEX_INL_H_