blob: e066787bfd4353c79e476c44e7fe97bc80fadfa9 [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
29namespace art {
30
31#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
32
33#if ART_USE_FUTEXES
34#include "linux/futex.h"
35#include "sys/syscall.h"
36#ifndef SYS_futex
37#define SYS_futex __NR_futex
38#endif
39static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) {
40 return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
41}
42#endif // ART_USE_FUTEXES
43
Ian Rogers693ff612013-02-01 10:56:12 -080044static inline uint64_t SafeGetTid(const Thread* self) {
45 if (self != NULL) {
46 return static_cast<uint64_t>(self->GetTid());
47 } else {
48 return static_cast<uint64_t>(GetTid());
49 }
50}
51
52static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
53 // The check below enumerates the cases where we expect not to be able to sanity check locks
54 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
55 // TODO: tighten this check.
56 if (kDebugLocking) {
57 Runtime* runtime = Runtime::Current();
Chao-ying Fu9e369312014-05-21 11:20:52 -070058 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDownLocked() ||
59 // Used during thread creation to avoid races with runtime shutdown. Thread::Current not
60 // yet established.
61 level == kRuntimeShutdownLock ||
62 // Thread Ids are allocated/released before threads are established.
63 level == kAllocatedThreadIdsLock ||
64 // Thread LDT's are initialized without Thread::Current established.
65 level == kModifyLdtLock ||
66 // Threads are unregistered while holding the thread list lock, during this process they
67 // no longer exist and so we expect an unlock with no self.
68 level == kThreadListLock ||
69 // Ignore logging which may or may not have set up thread data structures.
70 level == kLoggingLock ||
71 // Avoid recursive death.
Ian Rogers06abcdf2014-05-23 11:39:11 -070072 level == kAbortLock) << level;
Ian Rogers693ff612013-02-01 10:56:12 -080073 }
74}
75
Ian Rogersb6c31ea2013-02-04 18:11:33 -080076inline void BaseMutex::RegisterAsLocked(Thread* self) {
77 if (UNLIKELY(self == NULL)) {
78 CheckUnattachedThread(level_);
79 return;
80 }
81 if (kDebugLocking) {
82 // Check if a bad Mutex of this level or lower is held.
83 bool bad_mutexes_held = false;
84 for (int i = level_; i >= 0; --i) {
85 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
86 if (UNLIKELY(held_mutex != NULL)) {
Elliott Hughes0f827162013-02-26 12:12:58 -080087 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -080088 << "(level " << LockLevel(i) << " - " << i
89 << ") while locking \"" << name_ << "\" "
90 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -080091 if (i > kAbortLock) {
92 // Only abort in the check below if this is more than abort level lock.
93 bad_mutexes_held = true;
94 }
95 }
96 }
97 CHECK(!bad_mutexes_held);
98 }
99 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
100 // the monitor list.
101 if (level_ != kMonitorLock) {
102 self->SetHeldMutex(level_, this);
103 }
104}
105
Ian Rogers693ff612013-02-01 10:56:12 -0800106inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
107 if (UNLIKELY(self == NULL)) {
108 CheckUnattachedThread(level_);
109 return;
110 }
111 if (level_ != kMonitorLock) {
112 if (kDebugLocking && !gAborting) {
113 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
114 }
115 self->SetHeldMutex(level_, NULL);
116 }
117}
118
119inline void ReaderWriterMutex::SharedLock(Thread* self) {
120 DCHECK(self == NULL || self == Thread::Current());
121#if ART_USE_FUTEXES
122 bool done = false;
123 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700124 int32_t cur_state = state_.LoadRelaxed();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800125 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800126 // Add as an extra reader.
Ian Rogersc7190692014-07-08 23:50:26 -0700127 done = state_.CompareExchangeWeakAcquire(cur_state, cur_state + 1);
Ian Rogers693ff612013-02-01 10:56:12 -0800128 } else {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700129 HandleSharedLockContention(self, cur_state);
Ian Rogers693ff612013-02-01 10:56:12 -0800130 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700131 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800132#else
133 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
134#endif
Ian Rogersc5f17732014-06-05 20:48:42 -0700135 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800136 RegisterAsLocked(self);
137 AssertSharedHeld(self);
138}
139
140inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
141 DCHECK(self == NULL || self == Thread::Current());
Ian Rogersc5f17732014-06-05 20:48:42 -0700142 DCHECK(exclusive_owner_ == 0U || exclusive_owner_ == -1U);
Ian Rogers693ff612013-02-01 10:56:12 -0800143 AssertSharedHeld(self);
144 RegisterAsUnlocked(self);
145#if ART_USE_FUTEXES
146 bool done = false;
147 do {
Ian Rogersc7190692014-07-08 23:50:26 -0700148 int32_t cur_state = state_.LoadRelaxed();
Ian Rogers693ff612013-02-01 10:56:12 -0800149 if (LIKELY(cur_state > 0)) {
Ian Rogersc7190692014-07-08 23:50:26 -0700150 // Reduce state by 1 and impose lock release load/store ordering.
151 // Note, the relaxed loads below musn't reorder before the CompareExchange.
152 // TODO: the ordering here is non-trivial as state is split across 3 fields, fix by placing
153 // a status bit into the state on contention.
154 done = state_.CompareExchangeWeakSequentiallyConsistent(cur_state, cur_state - 1);
155 if (done && (cur_state - 1) == 0) { // Weak CAS may fail spuriously.
156 if (num_pending_writers_.LoadRelaxed() > 0 ||
157 num_pending_readers_.LoadRelaxed() > 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800158 // Wake any exclusive waiters as there are now no readers.
Ian Rogersc7190692014-07-08 23:50:26 -0700159 futex(state_.Address(), FUTEX_WAKE, -1, NULL, NULL, 0);
Ian Rogers693ff612013-02-01 10:56:12 -0800160 }
161 }
162 } else {
163 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
164 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700165 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800166#else
167 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
168#endif
169}
170
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700171inline bool Mutex::IsExclusiveHeld(const Thread* self) const {
172 DCHECK(self == NULL || self == Thread::Current());
173 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
174 if (kDebugLocking) {
175 // Sanity debug check that if we think it is locked we have it in our held mutexes.
176 if (result && self != NULL && level_ != kMonitorLock && !gAborting) {
177 CHECK_EQ(self->GetHeldMutex(level_), this);
178 }
179 }
180 return result;
181}
182
183inline uint64_t Mutex::GetExclusiveOwnerTid() const {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700184 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700185}
186
187inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const {
188 DCHECK(self == NULL || self == Thread::Current());
189 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
190 if (kDebugLocking) {
191 // Sanity that if the pthread thinks we own the lock the Thread agrees.
192 if (self != NULL && result) {
193 CHECK_EQ(self->GetHeldMutex(level_), this);
194 }
195 }
196 return result;
197}
198
199inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
200#if ART_USE_FUTEXES
Ian Rogersc7190692014-07-08 23:50:26 -0700201 int32_t state = state_.LoadRelaxed();
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700202 if (state == 0) {
203 return 0; // No owner.
204 } else if (state > 0) {
205 return -1; // Shared.
206 } else {
207 return exclusive_owner_;
208 }
209#else
Ian Rogersc5f17732014-06-05 20:48:42 -0700210 return exclusive_owner_;
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700211#endif
212}
213
Ian Rogers693ff612013-02-01 10:56:12 -0800214} // namespace art
215
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700216#endif // ART_RUNTIME_BASE_MUTEX_INL_H_