blob: 7f3b459c2e5c6f75bb97e9e19dab761ee452409a [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
22#include "cutils/atomic-inline.h"
23#include "runtime.h"
24#include "thread.h"
25
26namespace art {
27
28#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
29
30#if ART_USE_FUTEXES
31#include "linux/futex.h"
32#include "sys/syscall.h"
33#ifndef SYS_futex
34#define SYS_futex __NR_futex
35#endif
36static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) {
37 return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
38}
39#endif // ART_USE_FUTEXES
40
41class ScopedContentionRecorder {
42 public:
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070043 ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid)
44 : mutex_(kLogLockContentions ? mutex : NULL),
45 blocked_tid_(kLogLockContentions ? blocked_tid : 0),
46 owner_tid_(kLogLockContentions ? owner_tid : 0),
47 start_nano_time_(kLogLockContentions ? NanoTime() : 0) {
Ian Rogers693ff612013-02-01 10:56:12 -080048 }
49
50 ~ScopedContentionRecorder() {
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070051 if (kLogLockContentions) {
52 uint64_t end_nano_time = NanoTime();
53 mutex_->RecordContention(blocked_tid_, owner_tid_, end_nano_time - start_nano_time_);
54 }
Ian Rogers693ff612013-02-01 10:56:12 -080055 }
56
57 private:
58 BaseMutex* const mutex_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080059 const uint64_t blocked_tid_;
60 const uint64_t owner_tid_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070061 const uint64_t start_nano_time_;
Ian Rogers693ff612013-02-01 10:56:12 -080062};
63
64static inline uint64_t SafeGetTid(const Thread* self) {
65 if (self != NULL) {
66 return static_cast<uint64_t>(self->GetTid());
67 } else {
68 return static_cast<uint64_t>(GetTid());
69 }
70}
71
72static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
73 // The check below enumerates the cases where we expect not to be able to sanity check locks
74 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
75 // TODO: tighten this check.
76 if (kDebugLocking) {
77 Runtime* runtime = Runtime::Current();
78 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() ||
79 level == kDefaultMutexLevel || level == kRuntimeShutdownLock ||
80 level == kThreadListLock || level == kLoggingLock || level == kAbortLock);
81 }
82}
83
Ian Rogersb6c31ea2013-02-04 18:11:33 -080084inline void BaseMutex::RegisterAsLocked(Thread* self) {
85 if (UNLIKELY(self == NULL)) {
86 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));
94 if (UNLIKELY(held_mutex != NULL)) {
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 }
105 CHECK(!bad_mutexes_held);
106 }
107 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
108 // the monitor list.
109 if (level_ != kMonitorLock) {
110 self->SetHeldMutex(level_, this);
111 }
112}
113
Ian Rogers693ff612013-02-01 10:56:12 -0800114inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
115 if (UNLIKELY(self == NULL)) {
116 CheckUnattachedThread(level_);
117 return;
118 }
119 if (level_ != kMonitorLock) {
120 if (kDebugLocking && !gAborting) {
121 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
122 }
123 self->SetHeldMutex(level_, NULL);
124 }
125}
126
127inline void ReaderWriterMutex::SharedLock(Thread* self) {
128 DCHECK(self == NULL || self == Thread::Current());
129#if ART_USE_FUTEXES
130 bool done = false;
131 do {
132 int32_t cur_state = state_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800133 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800134 // Add as an extra reader.
135 done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0;
136 } else {
137 // Owner holds it exclusively, hang up.
138 ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self));
139 android_atomic_inc(&num_pending_readers_);
140 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
141 if (errno != EAGAIN) {
142 PLOG(FATAL) << "futex wait failed for " << name_;
143 }
144 }
145 android_atomic_dec(&num_pending_readers_);
146 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700147 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800148#else
149 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
150#endif
151 RegisterAsLocked(self);
152 AssertSharedHeld(self);
153}
154
155inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
156 DCHECK(self == NULL || self == Thread::Current());
157 AssertSharedHeld(self);
158 RegisterAsUnlocked(self);
159#if ART_USE_FUTEXES
160 bool done = false;
161 do {
162 int32_t cur_state = state_;
163 if (LIKELY(cur_state > 0)) {
164 // Reduce state by 1.
165 done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700166 if (done && (cur_state - 1) == 0) { // cas may fail due to noise?
Ian Rogers693ff612013-02-01 10:56:12 -0800167 if (num_pending_writers_ > 0 || num_pending_readers_ > 0) {
168 // Wake any exclusive waiters as there are now no readers.
169 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
170 }
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
181} // namespace art
182
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700183#endif // ART_RUNTIME_BASE_MUTEX_INL_H_