blob: 6c415e77e300788daa28956b35798e55515be11e [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
Hiroshi Yamauchib3733082013-08-12 17:28:49 -070024#define ATRACE_TAG ATRACE_TAG_DALVIK
25
Ian Rogers693ff612013-02-01 10:56:12 -080026#include "cutils/atomic-inline.h"
Hiroshi Yamauchib3733082013-08-12 17:28:49 -070027#include "cutils/trace.h"
Ian Rogers693ff612013-02-01 10:56:12 -080028#include "runtime.h"
29#include "thread.h"
30
31namespace art {
32
33#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
34
35#if ART_USE_FUTEXES
36#include "linux/futex.h"
37#include "sys/syscall.h"
38#ifndef SYS_futex
39#define SYS_futex __NR_futex
40#endif
41static inline int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, volatile int *uaddr2, int val3) {
42 return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
43}
44#endif // ART_USE_FUTEXES
45
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070046#if defined(__APPLE__)
47
48// This works on Mac OS 10.6 but hasn't been tested on older releases.
49struct __attribute__((__may_alias__)) darwin_pthread_mutex_t {
50 long padding0; // NOLINT(runtime/int) exact match to darwin type
51 int padding1;
52 uint32_t padding2;
53 int16_t padding3;
54 int16_t padding4;
55 uint32_t padding5;
56 pthread_t darwin_pthread_mutex_owner;
57 // ...other stuff we don't care about.
58};
59
60struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t {
61 long padding0; // NOLINT(runtime/int) exact match to darwin type
62 pthread_mutex_t padding1;
63 int padding2;
64 pthread_cond_t padding3;
65 pthread_cond_t padding4;
66 int padding5;
67 int padding6;
68 pthread_t darwin_pthread_rwlock_owner;
69 // ...other stuff we don't care about.
70};
71
72#endif // __APPLE__
73
74#if defined(__GLIBC__)
75
76struct __attribute__((__may_alias__)) glibc_pthread_mutex_t {
77 int32_t padding0[2];
78 int owner;
79 // ...other stuff we don't care about.
80};
81
82struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t {
83#ifdef __LP64__
84 int32_t padding0[6];
85#else
86 int32_t padding0[7];
87#endif
88 int writer;
89 // ...other stuff we don't care about.
90};
91
92#endif // __GLIBC__
93
Ian Rogers693ff612013-02-01 10:56:12 -080094class ScopedContentionRecorder {
95 public:
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -070096 ScopedContentionRecorder(BaseMutex* mutex, uint64_t blocked_tid, uint64_t owner_tid)
97 : mutex_(kLogLockContentions ? mutex : NULL),
98 blocked_tid_(kLogLockContentions ? blocked_tid : 0),
99 owner_tid_(kLogLockContentions ? owner_tid : 0),
100 start_nano_time_(kLogLockContentions ? NanoTime() : 0) {
Ian Rogers220228e2014-01-23 09:08:16 -0800101 std::string msg = StringPrintf("Lock contention on %s (owner tid: %" PRIu64 ")",
Jeff Hao08f2e7b2013-09-09 16:44:02 -0700102 mutex->GetName(), owner_tid);
103 ATRACE_BEGIN(msg.c_str());
Ian Rogers693ff612013-02-01 10:56:12 -0800104 }
105
106 ~ScopedContentionRecorder() {
Jeff Hao08f2e7b2013-09-09 16:44:02 -0700107 ATRACE_END();
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700108 if (kLogLockContentions) {
109 uint64_t end_nano_time = NanoTime();
110 mutex_->RecordContention(blocked_tid_, owner_tid_, end_nano_time - start_nano_time_);
111 }
Ian Rogers693ff612013-02-01 10:56:12 -0800112 }
113
114 private:
115 BaseMutex* const mutex_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800116 const uint64_t blocked_tid_;
117 const uint64_t owner_tid_;
Hiroshi Yamauchi1afde132013-08-06 17:09:30 -0700118 const uint64_t start_nano_time_;
Ian Rogers693ff612013-02-01 10:56:12 -0800119};
120
121static inline uint64_t SafeGetTid(const Thread* self) {
122 if (self != NULL) {
123 return static_cast<uint64_t>(self->GetTid());
124 } else {
125 return static_cast<uint64_t>(GetTid());
126 }
127}
128
129static inline void CheckUnattachedThread(LockLevel level) NO_THREAD_SAFETY_ANALYSIS {
130 // The check below enumerates the cases where we expect not to be able to sanity check locks
131 // on a thread. Lock checking is disabled to avoid deadlock when checking shutdown lock.
132 // TODO: tighten this check.
133 if (kDebugLocking) {
134 Runtime* runtime = Runtime::Current();
Chao-ying Fu9e369312014-05-21 11:20:52 -0700135 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDownLocked() ||
136 // Used during thread creation to avoid races with runtime shutdown. Thread::Current not
137 // yet established.
138 level == kRuntimeShutdownLock ||
139 // Thread Ids are allocated/released before threads are established.
140 level == kAllocatedThreadIdsLock ||
141 // Thread LDT's are initialized without Thread::Current established.
142 level == kModifyLdtLock ||
143 // Threads are unregistered while holding the thread list lock, during this process they
144 // no longer exist and so we expect an unlock with no self.
145 level == kThreadListLock ||
146 // Ignore logging which may or may not have set up thread data structures.
147 level == kLoggingLock ||
148 // Avoid recursive death.
149 level == kAbortLock);
Ian Rogers693ff612013-02-01 10:56:12 -0800150 }
151}
152
Ian Rogersb6c31ea2013-02-04 18:11:33 -0800153inline void BaseMutex::RegisterAsLocked(Thread* self) {
154 if (UNLIKELY(self == NULL)) {
155 CheckUnattachedThread(level_);
156 return;
157 }
158 if (kDebugLocking) {
159 // Check if a bad Mutex of this level or lower is held.
160 bool bad_mutexes_held = false;
161 for (int i = level_; i >= 0; --i) {
162 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
163 if (UNLIKELY(held_mutex != NULL)) {
Elliott Hughes0f827162013-02-26 12:12:58 -0800164 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" "
Ian Rogers62d6c772013-02-27 08:32:07 -0800165 << "(level " << LockLevel(i) << " - " << i
166 << ") while locking \"" << name_ << "\" "
167 << "(level " << level_ << " - " << static_cast<int>(level_) << ")";
Ian Rogersb6c31ea2013-02-04 18:11:33 -0800168 if (i > kAbortLock) {
169 // Only abort in the check below if this is more than abort level lock.
170 bad_mutexes_held = true;
171 }
172 }
173 }
174 CHECK(!bad_mutexes_held);
175 }
176 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
177 // the monitor list.
178 if (level_ != kMonitorLock) {
179 self->SetHeldMutex(level_, this);
180 }
181}
182
Ian Rogers693ff612013-02-01 10:56:12 -0800183inline void BaseMutex::RegisterAsUnlocked(Thread* self) {
184 if (UNLIKELY(self == NULL)) {
185 CheckUnattachedThread(level_);
186 return;
187 }
188 if (level_ != kMonitorLock) {
189 if (kDebugLocking && !gAborting) {
190 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
191 }
192 self->SetHeldMutex(level_, NULL);
193 }
194}
195
196inline void ReaderWriterMutex::SharedLock(Thread* self) {
197 DCHECK(self == NULL || self == Thread::Current());
198#if ART_USE_FUTEXES
199 bool done = false;
200 do {
201 int32_t cur_state = state_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800202 if (LIKELY(cur_state >= 0)) {
Ian Rogers693ff612013-02-01 10:56:12 -0800203 // Add as an extra reader.
204 done = android_atomic_acquire_cas(cur_state, cur_state + 1, &state_) == 0;
205 } else {
206 // Owner holds it exclusively, hang up.
207 ScopedContentionRecorder scr(this, GetExclusiveOwnerTid(), SafeGetTid(self));
208 android_atomic_inc(&num_pending_readers_);
209 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
210 if (errno != EAGAIN) {
211 PLOG(FATAL) << "futex wait failed for " << name_;
212 }
213 }
214 android_atomic_dec(&num_pending_readers_);
215 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700216 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800217#else
218 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
219#endif
220 RegisterAsLocked(self);
221 AssertSharedHeld(self);
222}
223
224inline void ReaderWriterMutex::SharedUnlock(Thread* self) {
225 DCHECK(self == NULL || self == Thread::Current());
226 AssertSharedHeld(self);
227 RegisterAsUnlocked(self);
228#if ART_USE_FUTEXES
229 bool done = false;
230 do {
231 int32_t cur_state = state_;
232 if (LIKELY(cur_state > 0)) {
233 // Reduce state by 1.
234 done = android_atomic_release_cas(cur_state, cur_state - 1, &state_) == 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700235 if (done && (cur_state - 1) == 0) { // cas may fail due to noise?
Ian Rogers3e5cf302014-05-20 16:40:37 -0700236 if (num_pending_writers_.LoadRelaxed() > 0 || num_pending_readers_ > 0) {
Ian Rogers693ff612013-02-01 10:56:12 -0800237 // Wake any exclusive waiters as there are now no readers.
238 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
239 }
240 }
241 } else {
242 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
243 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700244 } while (!done);
Ian Rogers693ff612013-02-01 10:56:12 -0800245#else
246 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
247#endif
248}
249
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700250inline bool Mutex::IsExclusiveHeld(const Thread* self) const {
251 DCHECK(self == NULL || self == Thread::Current());
252 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
253 if (kDebugLocking) {
254 // Sanity debug check that if we think it is locked we have it in our held mutexes.
255 if (result && self != NULL && level_ != kMonitorLock && !gAborting) {
256 CHECK_EQ(self->GetHeldMutex(level_), this);
257 }
258 }
259 return result;
260}
261
262inline uint64_t Mutex::GetExclusiveOwnerTid() const {
263#if ART_USE_FUTEXES
264 return exclusive_owner_;
265#elif defined(__BIONIC__)
266 return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff);
267#elif defined(__GLIBC__)
268 return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner;
269#elif defined(__APPLE__)
270 const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_);
271 pthread_t owner = dpmutex->darwin_pthread_mutex_owner;
272 // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING
273 // TODO: should we make darwin_pthread_mutex_owner volatile and recheck until not -1?
274 if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) {
275 return 0;
276 }
277 uint64_t tid;
278 CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6
279 return tid;
280#else
281#error unsupported C library
282#endif
283}
284
285inline bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const {
286 DCHECK(self == NULL || self == Thread::Current());
287 bool result = (GetExclusiveOwnerTid() == SafeGetTid(self));
288 if (kDebugLocking) {
289 // Sanity that if the pthread thinks we own the lock the Thread agrees.
290 if (self != NULL && result) {
291 CHECK_EQ(self->GetHeldMutex(level_), this);
292 }
293 }
294 return result;
295}
296
297inline uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
298#if ART_USE_FUTEXES
299 int32_t state = state_;
300 if (state == 0) {
301 return 0; // No owner.
302 } else if (state > 0) {
303 return -1; // Shared.
304 } else {
305 return exclusive_owner_;
306 }
307#else
308#if defined(__BIONIC__)
309 return rwlock_.writerThreadId;
310#elif defined(__GLIBC__)
311 return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer;
312#elif defined(__APPLE__)
313 const darwin_pthread_rwlock_t*
314 dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_);
315 pthread_t owner = dprwlock->darwin_pthread_rwlock_owner;
316 if (owner == (pthread_t)0) {
317 return 0;
318 }
319 uint64_t tid;
320 CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6
321 return tid;
322#else
323#error unsupported C library
324#endif
325#endif
326}
327
Ian Rogers693ff612013-02-01 10:56:12 -0800328} // namespace art
329
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700330#endif // ART_RUNTIME_BASE_MUTEX_INL_H_