blob: 0da0e184a3461bb8eece06d8044dfec5579093a3 [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
17#ifndef ART_SRC_BASE_MUTEX_INL_H_
18#define ART_SRC_BASE_MUTEX_INL_H_
19
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)) {
99 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" (level " << i
100 << ") while locking \"" << name_ << "\" (level " << static_cast<int>(level_) << ")";
101 if (i > kAbortLock) {
102 // Only abort in the check below if this is more than abort level lock.
103 bad_mutexes_held = true;
104 }
105 }
106 }
107 CHECK(!bad_mutexes_held);
108 }
109 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
110 // the monitor list.
111 if (level_ != kMonitorLock) {
112 self->SetHeldMutex(level_, this);
113 }
114}
115
Ian Rogers693ff612013-02-01 10:56:12 -0800116inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
117 if (UNLIKELY(self == NULL)) {
118 CheckUnattachedThread(level_);
119 return;
120 }
121 if (level_ != kMonitorLock) {
122 if (kDebugLocking && !gAborting) {
123 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
124 }
125 self->SetHeldMutex(level_, NULL);
126 }
127}
128
129inline void ReaderWriterMutex::SharedLock(Thread* self) {
130 DCHECK(self == NULL || self == Thread::Current());
131#if ART_USE_FUTEXES
132 bool done = false;
133 do {
134 int32_t cur_state = state_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800135 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800136 // Add as an extra reader.
137 done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0;
138 } else {
139 // Owner holds it exclusively, hang up.
140 ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self));
141 android_atomic_inc(&num_pending_readers_);
142 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
143 if (errno != EAGAIN) {
144 PLOG(FATAL) << "futex wait failed for " << name_;
145 }
146 }
147 android_atomic_dec(&num_pending_readers_);
148 }
149 } while(!done);
150#else
151 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
152#endif
153 RegisterAsLocked(self);
154 AssertSharedHeld(self);
155}
156
157inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
158 DCHECK(self == NULL || self == Thread::Current());
159 AssertSharedHeld(self);
160 RegisterAsUnlocked(self);
161#if ART_USE_FUTEXES
162 bool done = false;
163 do {
164 int32_t cur_state = state_;
165 if (LIKELY(cur_state > 0)) {
166 // Reduce state by 1.
167 done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0;
168 if (done && (cur_state - 1) == 0) { // cas may fail due to noise?
169 if (num_pending_writers_ > 0 || num_pending_readers_ > 0) {
170 // Wake any exclusive waiters as there are now no readers.
171 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
172 }
173 }
174 } else {
175 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
176 }
177 } while(!done);
178#else
179 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
180#endif
181}
182
183} // namespace art
184
185#endif // ART_SRC_BASE_MUTEX_INL_H_