blob: a559d63f772de1f6b12c27d30e245b0c2da13bd4 [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
20#include "mutex.h"
21
Hiroshi Yamauchib3733082013-08-12 17:28:49 -070022#define ATRACE_TAG ATRACE_TAG_DALVIK
23
Ian Rogers693ff612013-02-01 10:56:12 -080024#include "cutils/atomic-inline.h"
Hiroshi Yamauchib3733082013-08-12 17:28:49 -070025#include "cutils/trace.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
44class ScopedContentionRecorder {
45 public:
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070046 ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid)
47 : mutex_(kLogLockContentions ? mutex : NULL),
48 blocked_tid_(kLogLockContentions ? blocked_tid : 0),
49 owner_tid_(kLogLockContentions ? owner_tid : 0),
50 start_nano_time_(kLogLockContentions ? NanoTime() : 0) {
Hiroshi Yamauchib3733082013-08-12 17:28:49 -070051 if (kLogLockContentions) {
52 std::string msg = StringPrintf("Lock contention on %s (owner tid: %llu)",
53 mutex->GetName(), owner_tid);
54 ATRACE_BEGIN(msg.c_str());
55 }
Ian Rogers693ff612013-02-01 10:56:12 -080056 }
57
58 ~ScopedContentionRecorder() {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070059 if (kLogLockContentions) {
Hiroshi Yamauchib3733082013-08-12 17:28:49 -070060 ATRACE_END();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070061 uint64_t end_nano_time = NanoTime();
62 mutex_->RecordContention(blocked_tid_, owner_tid_, end_nano_time - start_nano_time_);
63 }
Ian Rogers693ff612013-02-01 10:56:12 -080064 }
65
66 private:
67 BaseMutex* const mutex_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080068 const uint64_t blocked_tid_;
69 const uint64_t owner_tid_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070070 const uint64_t start_nano_time_;
Ian Rogers693ff612013-02-01 10:56:12 -080071};
72
73static inline uint64_t SafeGetTid(const Thread* self) {
74 if (self != NULL) {
75 return static_cast<uint64_t>(self->GetTid());
76 } else {
77 return static_cast<uint64_t>(GetTid());
78 }
79}
80
81static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
82 // The check below enumerates the cases where we expect not to be able to sanity check locks
83 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
84 // TODO: tighten this check.
85 if (kDebugLocking) {
86 Runtime* runtime = Runtime::Current();
87 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() ||
88 level == kDefaultMutexLevel || level == kRuntimeShutdownLock ||
89 level == kThreadListLock || level == kLoggingLock || level == kAbortLock);
90 }
91}
92
Ian Rogersb6c31ea2013-02-04 18:11:33 -080093inline void BaseMutex::RegisterAsLocked(Thread* self) {
94 if (UNLIKELY(self == NULL)) {
95 CheckUnattachedThread(level_);
96 return;
97 }
98 if (kDebugLocking) {
99 // Check if a bad Mutex of this level or lower is held.
100 bool bad_mutexes_held = false;
101 for (int i = level_; i >= 0; --i) {
102 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
103 if (UNLIKELY(held_mutex != NULL)) {
Elliott Hughes0f827162013-02-26 12:12:58 -0800104 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -0800105 << "(level " << LockLevel(i) << " - " << i
106 << ") while locking \"" << name_ << "\" "
107 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -0800108 if (i > kAbortLock) {
109 // Only abort in the check below if this is more than abort level lock.
110 bad_mutexes_held = true;
111 }
112 }
113 }
114 CHECK(!bad_mutexes_held);
115 }
116 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
117 // the monitor list.
118 if (level_ != kMonitorLock) {
119 self->SetHeldMutex(level_, this);
120 }
121}
122
Ian Rogers693ff612013-02-01 10:56:12 -0800123inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
124 if (UNLIKELY(self == NULL)) {
125 CheckUnattachedThread(level_);
126 return;
127 }
128 if (level_ != kMonitorLock) {
129 if (kDebugLocking && !gAborting) {
130 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
131 }
132 self->SetHeldMutex(level_, NULL);
133 }
134}
135
136inline void ReaderWriterMutex::SharedLock(Thread* self) {
137 DCHECK(self == NULL || self == Thread::Current());
138#if ART_USE_FUTEXES
139 bool done = false;
140 do {
141 int32_t cur_state = state_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800142 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800143 // Add as an extra reader.
144 done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0;
145 } else {
146 // Owner holds it exclusively, hang up.
147 ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self));
148 android_atomic_inc(&num_pending_readers_);
149 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
150 if (errno != EAGAIN) {
151 PLOG(FATAL) << "futex wait failed for " << name_;
152 }
153 }
154 android_atomic_dec(&num_pending_readers_);
155 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700156 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800157#else
158 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
159#endif
160 RegisterAsLocked(self);
161 AssertSharedHeld(self);
162}
163
164inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
165 DCHECK(self == NULL || self == Thread::Current());
166 AssertSharedHeld(self);
167 RegisterAsUnlocked(self);
168#if ART_USE_FUTEXES
169 bool done = false;
170 do {
171 int32_t cur_state = state_;
172 if (LIKELY(cur_state > 0)) {
173 // Reduce state by 1.
174 done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700175 if (done && (cur_state - 1) == 0) { // cas may fail due to noise?
Ian Rogers693ff612013-02-01 10:56:12 -0800176 if (num_pending_writers_ > 0 || num_pending_readers_ > 0) {
177 // Wake any exclusive waiters as there are now no readers.
178 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
179 }
180 }
181 } else {
182 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
183 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700184 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800185#else
186 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
187#endif
188}
189
190} // namespace art
191
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700192#endif // ART_RUNTIME_BASE_MUTEX_INL_H_