blob: 1337dff068564b88bb037ab986df9aafd97843e3 [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:
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080043#if CONTENTION_LOGGING
Ian Rogers693ff612013-02-01 10:56:12 -080044 ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid) :
45 mutex_(mutex), blocked_tid_(blocked_tid), owner_tid_(owner_tid),
46 start_milli_time_(MilliTime()) {
47 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080048#else
49 ScopedContentionRecorder(BaseMutex*, uint64_t, uint64_t) {}
50#endif
Ian Rogers693ff612013-02-01 10:56:12 -080051
52 ~ScopedContentionRecorder() {
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080053#if CONTENTION_LOGGING
Ian Rogers693ff612013-02-01 10:56:12 -080054 uint64_t end_milli_time = MilliTime();
55 mutex_->RecordContention(blocked_tid_, owner_tid_, end_milli_time - start_milli_time_);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080056#endif
Ian Rogers693ff612013-02-01 10:56:12 -080057 }
58
59 private:
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080060#if CONTENTION_LOGGING
Ian Rogers693ff612013-02-01 10:56:12 -080061 BaseMutex* const mutex_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080062 const uint64_t blocked_tid_;
63 const uint64_t owner_tid_;
Ian Rogers693ff612013-02-01 10:56:12 -080064 const uint64_t start_milli_time_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080065#endif
Ian Rogers693ff612013-02-01 10:56:12 -080066};
67
68static inline uint64_t SafeGetTid(const Thread* self) {
69 if (self != NULL) {
70 return static_cast<uint64_t>(self->GetTid());
71 } else {
72 return static_cast<uint64_t>(GetTid());
73 }
74}
75
76static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
77 // The check below enumerates the cases where we expect not to be able to sanity check locks
78 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
79 // TODO: tighten this check.
80 if (kDebugLocking) {
81 Runtime* runtime = Runtime::Current();
82 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() ||
83 level == kDefaultMutexLevel || level == kRuntimeShutdownLock ||
84 level == kThreadListLock || level == kLoggingLock || level == kAbortLock);
85 }
86}
87
Ian Rogersb6c31ea2013-02-04 18:11:33 -080088inline void BaseMutex::RegisterAsLocked(Thread* self) {
89 if (UNLIKELY(self == NULL)) {
90 CheckUnattachedThread(level_);
91 return;
92 }
93 if (kDebugLocking) {
94 // Check if a bad Mutex of this level or lower is held.
95 bool bad_mutexes_held = false;
96 for (int i = level_; i >= 0; --i) {
97 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
98 if (UNLIKELY(held_mutex != NULL)) {
Elliott Hughes0f827162013-02-26 12:12:58 -080099 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -0800100 << "(level " << LockLevel(i) << " - " << i
101 << ") while locking \"" << name_ << "\" "
102 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -0800103 if (i > kAbortLock) {
104 // Only abort in the check below if this is more than abort level lock.
105 bad_mutexes_held = true;
106 }
107 }
108 }
109 CHECK(!bad_mutexes_held);
110 }
111 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
112 // the monitor list.
113 if (level_ != kMonitorLock) {
114 self->SetHeldMutex(level_, this);
115 }
116}
117
Ian Rogers693ff612013-02-01 10:56:12 -0800118inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
119 if (UNLIKELY(self == NULL)) {
120 CheckUnattachedThread(level_);
121 return;
122 }
123 if (level_ != kMonitorLock) {
124 if (kDebugLocking && !gAborting) {
125 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
126 }
127 self->SetHeldMutex(level_, NULL);
128 }
129}
130
131inline void ReaderWriterMutex::SharedLock(Thread* self) {
132 DCHECK(self == NULL || self == Thread::Current());
133#if ART_USE_FUTEXES
134 bool done = false;
135 do {
136 int32_t cur_state = state_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800137 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800138 // Add as an extra reader.
139 done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0;
140 } else {
141 // Owner holds it exclusively, hang up.
142 ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self));
143 android_atomic_inc(&num_pending_readers_);
144 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
145 if (errno != EAGAIN) {
146 PLOG(FATAL) << "futex wait failed for " << name_;
147 }
148 }
149 android_atomic_dec(&num_pending_readers_);
150 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700151 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800152#else
153 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
154#endif
155 RegisterAsLocked(self);
156 AssertSharedHeld(self);
157}
158
159inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
160 DCHECK(self == NULL || self == Thread::Current());
161 AssertSharedHeld(self);
162 RegisterAsUnlocked(self);
163#if ART_USE_FUTEXES
164 bool done = false;
165 do {
166 int32_t cur_state = state_;
167 if (LIKELY(cur_state > 0)) {
168 // Reduce state by 1.
169 done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700170 if (done && (cur_state - 1) == 0) { // cas may fail due to noise?
Ian Rogers693ff612013-02-01 10:56:12 -0800171 if (num_pending_writers_ > 0 || num_pending_readers_ > 0) {
172 // Wake any exclusive waiters as there are now no readers.
173 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
174 }
175 }
176 } else {
177 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
178 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700179 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800180#else
181 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
182#endif
183}
184
185} // namespace art
186
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700187#endif // ART_RUNTIME_BASE_MUTEX_INL_H_