blob: ccb913f3f2702253eea2db5670c0e80cfe8550c8 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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#include "mutex.h"
18
19#include <errno.h>
20
Ian Rogers81d425b2012-09-27 16:03:43 -070021#include "cutils/atomic.h"
22#include "cutils/atomic-inline.h"
23#include "linux/futex.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070024#include "logging.h"
Elliott Hughes6b355752012-01-13 16:49:08 -080025#include "runtime.h"
Elliott Hughesffb465f2012-03-01 18:46:05 -080026#include "thread.h"
27#include "utils.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070028
Elliott Hughesb08e8a32012-04-02 10:51:41 -070029#if defined(__APPLE__)
30#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
31#endif
32
Elliott Hughes8d768a92011-09-14 16:35:25 -070033#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
34
Elliott Hughesf8349362012-06-18 15:00:06 -070035extern int pthread_mutex_lock(pthread_mutex_t* mutex) EXCLUSIVE_LOCK_FUNCTION(mutex);
36extern int pthread_mutex_unlock(pthread_mutex_t* mutex) UNLOCK_FUNCTION(1);
37extern int pthread_mutex_trylock(pthread_mutex_t* mutex) EXCLUSIVE_TRYLOCK_FUNCTION(0, mutex);
38
Ian Rogers81d425b2012-09-27 16:03:43 -070039#if ART_USE_FUTEXES
40#include "sys/syscall.h"
41#ifndef SYS_futex
42#define SYS_futex __NR_futex
43#endif
44int futex(volatile int *uaddr, int op, int val, const struct timespec *timeout, int *, int ) {
45 return syscall(SYS_futex, uaddr, op, val, timeout, NULL, NULL);
46}
47#endif // ART_USE_FUTEXES
48
Elliott Hughes8daa0922011-09-11 13:46:25 -070049namespace art {
50
Brian Carlstromf3a26412012-08-24 11:06:02 -070051// This works on Mac OS 10.6 but hasn't been tested on older releases.
Elliott Hughesf1498432012-03-28 19:34:27 -070052struct __attribute__((__may_alias__)) darwin_pthread_mutex_t {
Brian Carlstromf3a26412012-08-24 11:06:02 -070053 long padding0;
54 int padding1;
55 uint32_t padding2;
56 int16_t padding3;
57 int16_t padding4;
58 uint32_t padding5;
59 pthread_t darwin_pthread_mutex_owner;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 // ...other stuff we don't care about.
61};
62
63struct __attribute__((__may_alias__)) darwin_pthread_rwlock_t {
Brian Carlstromf3a26412012-08-24 11:06:02 -070064 long padding0;
65 pthread_mutex_t padding1;
66 int padding2;
67 pthread_cond_t padding3;
68 pthread_cond_t padding4;
69 int padding5;
70 int padding6;
71 pthread_t darwin_pthread_rwlock_owner;
Elliott Hughesf1498432012-03-28 19:34:27 -070072 // ...other stuff we don't care about.
73};
74
75struct __attribute__((__may_alias__)) glibc_pthread_mutex_t {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 int32_t padding0[2];
Elliott Hughesf1498432012-03-28 19:34:27 -070077 int owner;
78 // ...other stuff we don't care about.
79};
80
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081struct __attribute__((__may_alias__)) glibc_pthread_rwlock_t {
82#ifdef __LP64__
83 int32_t padding0[6];
84#else
85 int32_t padding0[7];
86#endif
87 int writer;
88 // ...other stuff we don't care about.
89};
90
Ian Rogers81d425b2012-09-27 16:03:43 -070091BaseMutex::BaseMutex(const char* name, LockLevel level) : level_(level), name_(name) {}
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092
Ian Rogers81d425b2012-09-27 16:03:43 -070093static void CheckUnattachedThread(LockLevel level) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 // The check below enumerates the cases where we expect not to be able to sanity check locks
95 // on a thread. TODO: tighten this check.
Ian Rogers25fd14b2012-09-05 10:56:38 -070096 if (kDebugLocking) {
97 Runtime* runtime = Runtime::Current();
98 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown() ||
99 level == kDefaultMutexLevel || level == kThreadListLock ||
100 level == kLoggingLock || level == kAbortLock);
101 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800102}
103
Ian Rogers81d425b2012-09-27 16:03:43 -0700104void BaseMutex::RegisterAsLocked(Thread* self) {
105 if (UNLIKELY(self == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 CheckUnattachedThread(level_);
107 return;
108 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700109 if (kDebugLocking) {
110 // Check if a bad Mutex of this level or lower is held.
111 bool bad_mutexes_held = false;
112 for (int i = level_; i >= 0; --i) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700113 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Ian Rogers25fd14b2012-09-05 10:56:38 -0700114 if (UNLIKELY(held_mutex != NULL)) {
115 LOG(ERROR) << "Lock level violation: holding \"" << held_mutex->name_ << "\" (level " << i
116 << ") while locking \"" << name_ << "\" (level " << static_cast<int>(level_) << ")";
117 if (i > kAbortLock) {
118 // Only abort in the check below if this is more than abort level lock.
119 bad_mutexes_held = true;
120 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 }
122 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700123 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 // Don't record monitors as they are outside the scope of analysis. They may be inspected off of
126 // the monitor list.
127 if (level_ != kMonitorLock) {
128 self->SetHeldMutex(level_, this);
129 }
130}
131
Ian Rogers81d425b2012-09-27 16:03:43 -0700132void BaseMutex::RegisterAsUnlocked(Thread* self) {
133 if (UNLIKELY(self == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 CheckUnattachedThread(level_);
135 return;
136 }
137 if (level_ != kMonitorLock) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700138 if (kDebugLocking) {
139 CHECK(self->GetHeldMutex(level_) == this) << "Unlocking on unacquired mutex: " << name_;
140 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 self->SetHeldMutex(level_, NULL);
142 }
143}
144
Ian Rogers81d425b2012-09-27 16:03:43 -0700145void BaseMutex::CheckSafeToWait(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 if (self == NULL) {
147 CheckUnattachedThread(level_);
148 return;
149 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700150 if (kDebugLocking) {
151 CHECK(self->GetHeldMutex(level_) == this) << "Waiting on unacquired mutex: " << name_;
152 bool bad_mutexes_held = false;
153 for (int i = kMaxMutexLevel; i >= 0; --i) {
154 if (i != level_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700155 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
Ian Rogers25fd14b2012-09-05 10:56:38 -0700156 if (held_mutex != NULL) {
157 LOG(ERROR) << "Holding " << held_mutex->name_ << " (level " << i
158 << ") while performing wait on: "
159 << name_ << " (level " << static_cast<int>(level_) << ")";
160 bad_mutexes_held = true;
161 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 }
163 }
Ian Rogers25fd14b2012-09-05 10:56:38 -0700164 CHECK(!bad_mutexes_held);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166}
167
Ian Rogers81d425b2012-09-27 16:03:43 -0700168Mutex::Mutex(const char* name, LockLevel level, bool recursive)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 : BaseMutex(name, level), recursive_(recursive), recursion_count_(0) {
Brian Carlstromf3a26412012-08-24 11:06:02 -0700170#if defined(__BIONIC__) || defined(__APPLE__)
171 // Use recursive mutexes for bionic and Apple otherwise the
172 // non-recursive mutexes don't have TIDs to check lock ownership of.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800173 pthread_mutexattr_t attributes;
174 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
175 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
176 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
177 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178#else
179 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL));
180#endif
Elliott Hughes8daa0922011-09-11 13:46:25 -0700181}
182
183Mutex::~Mutex() {
Elliott Hughese62934d2012-04-09 11:24:29 -0700184 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
185 // may still be using locks.
Elliott Hughes6b355752012-01-13 16:49:08 -0800186 int rc = pthread_mutex_destroy(&mutex_);
187 if (rc != 0) {
188 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800189 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -0800190 bool shutting_down = Runtime::Current()->IsShuttingDown();
191 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
192 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700193}
194
Ian Rogers81d425b2012-09-27 16:03:43 -0700195void Mutex::ExclusiveLock(Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700196 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700197 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700198 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700199 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700201 RegisterAsLocked(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 }
203 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700204 if (kDebugLocking) {
205 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
206 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700207 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700208 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700209}
210
Ian Rogers81d425b2012-09-27 16:03:43 -0700211bool Mutex::ExclusiveTryLock(Thread* self) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700212 if (kDebugLocking && !recursive_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700213 AssertNotHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700214 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700215 if (!recursive_ || !IsExclusiveHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700216 int result = pthread_mutex_trylock(&mutex_);
217 if (result == EBUSY) {
218 return false;
219 }
220 if (result != 0) {
221 errno = result;
222 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
223 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700224 RegisterAsLocked(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700225 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 recursion_count_++;
Ian Rogers25fd14b2012-09-05 10:56:38 -0700227 if (kDebugLocking) {
228 CHECK(recursion_count_ == 1 || recursive_) << "Unexpected recursion count on mutex: "
229 << name_ << " " << recursion_count_;
Ian Rogers81d425b2012-09-27 16:03:43 -0700230 AssertHeld(self);
Ian Rogers25fd14b2012-09-05 10:56:38 -0700231 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700232 return true;
233}
234
Ian Rogers81d425b2012-09-27 16:03:43 -0700235void Mutex::ExclusiveUnlock(Thread* self) {
236 AssertHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 recursion_count_--;
238 if (!recursive_ || recursion_count_ == 0) {
Ian Rogers25fd14b2012-09-05 10:56:38 -0700239 if (kDebugLocking) {
240 CHECK(recursion_count_ == 0 || recursive_) << "Unexpected recursion count on mutex: "
241 << name_ << " " << recursion_count_;
242 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700243 RegisterAsUnlocked(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
245 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700246}
247
Ian Rogers81d425b2012-09-27 16:03:43 -0700248bool Mutex::IsExclusiveHeld(const Thread* self) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 bool result;
250 if (self == NULL || level_ == kMonitorLock) { // Handle unattached threads and monitors.
251 result = (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid()));
252 } else {
253 result = (self->GetHeldMutex(level_) == this);
254 // Sanity debug check that if we think it is locked, so does the pthread.
Ian Rogers25fd14b2012-09-05 10:56:38 -0700255 if (kDebugLocking) {
256 CHECK(result == (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid())));
257 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 }
259 return result;
Elliott Hughesf1498432012-03-28 19:34:27 -0700260}
261
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262uint64_t Mutex::GetExclusiveOwnerTid() const {
Elliott Hughes3147a232011-10-12 15:55:07 -0700263#if defined(__BIONIC__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700264 return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -0700265#elif defined(__GLIBC__)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 return reinterpret_cast<const glibc_pthread_mutex_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800267#elif defined(__APPLE__)
Brian Carlstromf3a26412012-08-24 11:06:02 -0700268 const darwin_pthread_mutex_t* dpmutex = reinterpret_cast<const darwin_pthread_mutex_t*>(&mutex_);
269 pthread_t owner = dpmutex->darwin_pthread_mutex_owner;
Brian Carlstrombd93c302012-08-27 16:58:28 -0700270 // 0 for unowned, -1 for PTHREAD_MTX_TID_SWITCHING
271 // TODO: should we make darwin_pthread_mutex_owner volatile and recheck until not -1?
Brian Carlstromf3a26412012-08-24 11:06:02 -0700272 if ((owner == (pthread_t)0) || (owner == (pthread_t)-1)) {
273 return 0;
274 }
275 uint64_t tid;
276 CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6
277 return tid;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700278#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700279#error unsupported C library
Elliott Hughes8daa0922011-09-11 13:46:25 -0700280#endif
281}
282
Ian Rogers81d425b2012-09-27 16:03:43 -0700283ReaderWriterMutex::ReaderWriterMutex(const char* name, LockLevel level) :
284 BaseMutex(name, level)
285#if ART_USE_FUTEXES
286 , state_(0), exclusive_owner_(0), num_pending_readers_(0), num_pending_writers_(0)
287#endif
288{
289#if !ART_USE_FUTEXES
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 CHECK_MUTEX_CALL(pthread_rwlock_init, (&rwlock_, NULL));
Ian Rogers81d425b2012-09-27 16:03:43 -0700291#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292}
293
294ReaderWriterMutex::~ReaderWriterMutex() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700295#if ART_USE_FUTEXES
296 CHECK_EQ(state_, 0);
297 CHECK_EQ(exclusive_owner_, 0U);
298 CHECK_EQ(num_pending_readers_, 0);
299 CHECK_EQ(num_pending_writers_, 0);
300#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
302 // may still be using locks.
303 int rc = pthread_rwlock_destroy(&rwlock_);
304 if (rc != 0) {
305 errno = rc;
306 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
307 bool shutting_down = Runtime::Current()->IsShuttingDown();
308 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800309 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700310#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311}
312
Ian Rogers81d425b2012-09-27 16:03:43 -0700313void ReaderWriterMutex::ExclusiveLock(Thread* self) {
314 AssertNotExclusiveHeld(self);
315#if ART_USE_FUTEXES
316 bool done = false;
317 do {
318 int32_t cur_state = state_;
319 if (cur_state == 0) {
320 // Change state from 0 to -1.
321 done = android_atomic_cmpxchg(0, -1, &state_) == 0;
322 } else {
323 // Failed to acquire, hang up.
324 android_atomic_inc(&num_pending_writers_);
325 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
326 if (errno != EAGAIN) {
327 PLOG(FATAL) << "futex wait failed for " << name_;
328 }
329 }
330 android_atomic_dec(&num_pending_writers_);
331 }
332 } while(!done);
333 exclusive_owner_ = static_cast<uint64_t>(GetTid());
334#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 CHECK_MUTEX_CALL(pthread_rwlock_wrlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700336#endif
337 RegisterAsLocked(self);
338 AssertExclusiveHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339}
340
Ian Rogers81d425b2012-09-27 16:03:43 -0700341void ReaderWriterMutex::ExclusiveUnlock(Thread* self) {
342 AssertExclusiveHeld(self);
343 RegisterAsUnlocked(self);
344#if ART_USE_FUTEXES
345 bool done = false;
346 do {
347 int32_t cur_state = state_;
348 if (cur_state == -1) {
349 // We're no longer the owner.
350 exclusive_owner_ = 0;
351 // Change state from -1 to 0.
352 done = android_atomic_cmpxchg(-1, 0, &state_) == 0;
353 if (done) { // cmpxchg may fail due to noise?
354 // Wake any waiters.
355 if (num_pending_readers_ > 0 || num_pending_writers_ > 0) {
356 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
357 }
358 }
359 } else {
360 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
361 }
362 } while(!done);
363#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700365#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366}
367
Ian Rogers66aee5c2012-08-15 17:17:47 -0700368#if HAVE_TIMED_RWLOCK
Ian Rogers81d425b2012-09-27 16:03:43 -0700369bool ReaderWriterMutex::ExclusiveLockWithTimeout(Thread* self, const timespec& abs_timeout) {
370#if ART_USE_FUTEXES
371 bool done = false;
372 do {
373 int32_t cur_state = state_;
374 if (cur_state == 0) {
375 // Change state from 0 to -1.
376 done = android_atomic_cmpxchg(0, -1, &state_) == 0;
377 } else {
378 // Failed to acquire, hang up.
379 android_atomic_inc(&num_pending_writers_);
380 if (futex(&state_, FUTEX_WAIT, cur_state, &abs_timeout, NULL, 0) != 0) {
381 if (errno == ETIMEDOUT) {
382 android_atomic_dec(&num_pending_writers_);
383 return false;
384 } else if (errno != EAGAIN) {
385 PLOG(FATAL) << "timed futex wait failed for " << name_;
386 }
387 }
388 android_atomic_dec(&num_pending_writers_);
389 }
390 } while(!done);
391 exclusive_owner_ = static_cast<uint64_t>(GetTid());
392#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 int result = pthread_rwlock_timedwrlock(&rwlock_, &abs_timeout);
394 if (result == ETIMEDOUT) {
395 return false;
396 }
397 if (result != 0) {
398 errno = result;
Ian Rogersa5acfd32012-08-15 11:50:10 -0700399 PLOG(FATAL) << "pthread_rwlock_timedwrlock failed for " << name_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700401#endif
402 RegisterAsLocked(self);
403 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 return true;
405}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700406#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407
Ian Rogers81d425b2012-09-27 16:03:43 -0700408void ReaderWriterMutex::SharedLock(Thread* self) {
409#if ART_USE_FUTEXES
410 bool done = false;
411 do {
412 int32_t cur_state = state_;
413 if (cur_state >= 0) {
414 // Add as an extra reader.
415 done = android_atomic_cmpxchg(cur_state, cur_state + 1, &state_) == 0;
416 } else {
417 // Owner holds it exclusively, hang up.
418 android_atomic_inc(&num_pending_readers_);
419 if (futex(&state_, FUTEX_WAIT, cur_state, NULL, NULL, 0) != 0) {
420 if (errno != EAGAIN) {
421 PLOG(FATAL) << "futex wait failed for " << name_;
422 }
423 }
424 android_atomic_dec(&num_pending_readers_);
425 }
426 } while(!done);
427#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428 CHECK_MUTEX_CALL(pthread_rwlock_rdlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700429#endif
430 RegisterAsLocked(self);
431 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700432}
433
Ian Rogers81d425b2012-09-27 16:03:43 -0700434bool ReaderWriterMutex::SharedTryLock(Thread* self) {
435#if ART_USE_FUTEXES
436 bool done = false;
437 do {
438 int32_t cur_state = state_;
439 if (cur_state >= 0) {
440 // Add as an extra reader.
441 done = android_atomic_cmpxchg(cur_state, cur_state + 1, &state_) == 0;
442 } else {
443 // Owner holds it exclusively.
444 return false;
445 }
446 } while(!done);
447#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 int result = pthread_rwlock_tryrdlock(&rwlock_);
449 if (result == EBUSY) {
450 return false;
451 }
452 if (result != 0) {
453 errno = result;
454 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
455 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700456#endif
457 RegisterAsLocked(self);
458 AssertSharedHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 return true;
460}
461
Ian Rogers81d425b2012-09-27 16:03:43 -0700462void ReaderWriterMutex::SharedUnlock(Thread* self) {
463 AssertSharedHeld(self);
464 RegisterAsUnlocked(self);
465#if ART_USE_FUTEXES
466 bool done = false;
467 do {
468 int32_t cur_state = state_;
469 if (LIKELY(cur_state > 0)) {
470 // Reduce state by 1.
471 done = android_atomic_cmpxchg(cur_state, cur_state - 1, &state_) == 0;
472 if (done && (cur_state - 1) == 0) { // cmpxchg may fail due to noise?
473 if (num_pending_writers_ > 0 || num_pending_readers_ > 0) {
474 // Wake any exclusive waiters as there are now no readers.
475 futex(&state_, FUTEX_WAKE, -1, NULL, NULL, 0);
476 }
477 }
478 } else {
479 LOG(FATAL) << "Unexpected state_:" << cur_state << " for " << name_;
480 }
481 } while(!done);
482#else
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483 CHECK_MUTEX_CALL(pthread_rwlock_unlock, (&rwlock_));
Ian Rogers81d425b2012-09-27 16:03:43 -0700484#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700485}
486
Ian Rogers81d425b2012-09-27 16:03:43 -0700487bool ReaderWriterMutex::IsExclusiveHeld(const Thread* self) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 bool result = (GetExclusiveOwnerTid() == static_cast<uint64_t>(GetTid()));
Ian Rogers25fd14b2012-09-05 10:56:38 -0700489 if (kDebugLocking) {
490 // Sanity that if the pthread thinks we own the lock the Thread agrees.
Ian Rogers25fd14b2012-09-05 10:56:38 -0700491 CHECK((self == NULL) || !result || (self->GetHeldMutex(level_) == this));
492 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 return result;
494}
495
Ian Rogers81d425b2012-09-27 16:03:43 -0700496bool ReaderWriterMutex::IsSharedHeld(const Thread* self) const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700497 bool result;
498 if (UNLIKELY(self == NULL)) { // Handle unattached threads.
Ian Rogers81d425b2012-09-27 16:03:43 -0700499 result = IsExclusiveHeld(self); // TODO: a better best effort here.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700500 } else {
501 result = (self->GetHeldMutex(level_) == this);
502 }
503 return result;
504}
505
506uint64_t ReaderWriterMutex::GetExclusiveOwnerTid() const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700507#if ART_USE_FUTEXES
508 return exclusive_owner_;
509#else
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800510#if defined(__BIONIC__)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700511 return rwlock_.writerThreadId;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800512#elif defined(__GLIBC__)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 return reinterpret_cast<const glibc_pthread_rwlock_t*>(&rwlock_)->writer;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800514#elif defined(__APPLE__)
Ian Rogers81d425b2012-09-27 16:03:43 -0700515 const darwin_pthread_rwlock_t*
516 dprwlock = reinterpret_cast<const darwin_pthread_rwlock_t*>(&rwlock_);
Brian Carlstromf3a26412012-08-24 11:06:02 -0700517 pthread_t owner = dprwlock->darwin_pthread_rwlock_owner;
518 if (owner == (pthread_t)0) {
519 return 0;
520 }
521 uint64_t tid;
522 CHECK_PTHREAD_CALL(pthread_threadid_np, (owner, &tid), __FUNCTION__); // Requires Mac OS 10.6
523 return tid;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800524#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700525#error unsupported C library
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800526#endif
Ian Rogers81d425b2012-09-27 16:03:43 -0700527#endif
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800528}
529
Elliott Hughes5f791332011-09-15 17:45:30 -0700530ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
531 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
532}
533
534ConditionVariable::~ConditionVariable() {
Elliott Hughese62934d2012-04-09 11:24:29 -0700535 // We can't use CHECK_MUTEX_CALL here because on shutdown a suspended daemon thread
536 // may still be using condition variables.
537 int rc = pthread_cond_destroy(&cond_);
538 if (rc != 0) {
539 errno = rc;
540 bool shutting_down = Runtime::Current()->IsShuttingDown();
541 PLOG(shutting_down ? WARNING : FATAL) << "pthread_cond_destroy failed for " << name_;
542 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700543}
544
545void ConditionVariable::Broadcast() {
546 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
547}
548
549void ConditionVariable::Signal() {
550 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
551}
552
Ian Rogers81d425b2012-09-27 16:03:43 -0700553void ConditionVariable::Wait(Thread* self, Mutex& mutex) {
554 mutex.CheckSafeToWait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555 unsigned int old_recursion_count = mutex.recursion_count_;
556 mutex.recursion_count_ = 0;
Elliott Hughesf1498432012-03-28 19:34:27 -0700557 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &mutex.mutex_));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 mutex.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700559}
560
Ian Rogers81d425b2012-09-27 16:03:43 -0700561void ConditionVariable::TimedWait(Thread* self, Mutex& mutex, const timespec& ts) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700562#ifdef HAVE_TIMEDWAIT_MONOTONIC
563#define TIMEDWAIT pthread_cond_timedwait_monotonic
564#else
565#define TIMEDWAIT pthread_cond_timedwait
566#endif
Ian Rogers81d425b2012-09-27 16:03:43 -0700567 mutex.CheckSafeToWait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700568 unsigned int old_recursion_count = mutex.recursion_count_;
569 mutex.recursion_count_ = 0;
Elliott Hughesf1498432012-03-28 19:34:27 -0700570 int rc = TIMEDWAIT(&cond_, &mutex.mutex_, &ts);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700571 mutex.recursion_count_ = old_recursion_count;
Elliott Hughes5f791332011-09-15 17:45:30 -0700572 if (rc != 0 && rc != ETIMEDOUT) {
573 errno = rc;
574 PLOG(FATAL) << "TimedWait failed for " << name_;
575 }
576}
577
Elliott Hughese62934d2012-04-09 11:24:29 -0700578} // namespace art