blob: b56f1ef3b4e002ad49b8b27b25438169a69ab634 [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
Elliott Hughes8daa0922011-09-11 13:46:25 -070021#include "logging.h"
Elliott Hughes6b355752012-01-13 16:49:08 -080022#include "runtime.h"
Elliott Hughesffb465f2012-03-01 18:46:05 -080023#include "thread.h"
24#include "utils.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025
Elliott Hughesb08e8a32012-04-02 10:51:41 -070026#if defined(__APPLE__)
27#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
28#endif
29
Elliott Hughes8d768a92011-09-14 16:35:25 -070030#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
31
Elliott Hughes8daa0922011-09-11 13:46:25 -070032namespace art {
33
Elliott Hughesf1498432012-03-28 19:34:27 -070034// This works on Mac OS 10.7, but hasn't been tested on older releases.
35struct __attribute__((__may_alias__)) darwin_pthread_mutex_t {
36 uint32_t padding0[2];
37 uint32_t value;
38 uint32_t padding1[5];
39 uint64_t owner_tid;
40 // ...other stuff we don't care about.
41};
42
43struct __attribute__((__may_alias__)) glibc_pthread_mutex_t {
44 int lock;
45 unsigned int count;
46 int owner;
47 // ...other stuff we don't care about.
48};
49
Elliott Hughes76e36942012-03-16 13:44:56 -070050static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) {
Elliott Hughes67d92002012-03-26 15:08:51 -070051 if (!kIsDebugBuild) {
52 return;
53 }
Elliott Hughesffb465f2012-03-01 18:46:05 -080054 if (rank == -1) {
55 return;
56 }
57 Thread* self = Thread::Current();
58 if (self != NULL) {
Elliott Hughesa4060e52012-03-02 16:51:35 -080059 self->CheckSafeToLockOrUnlock(rank, is_locking);
60 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080061}
62
Elliott Hughes76e36942012-03-16 13:44:56 -070063static inline void CheckSafeToWait(MutexRank rank) {
Elliott Hughes67d92002012-03-26 15:08:51 -070064 if (!kIsDebugBuild) {
65 return;
66 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080067 Thread* self = Thread::Current();
68 if (self != NULL) {
69 self->CheckSafeToWait(rank);
Elliott Hughesffb465f2012-03-01 18:46:05 -080070 }
Elliott Hughesffb465f2012-03-01 18:46:05 -080071}
72
73Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080074 // Like Java, we use recursive mutexes.
75 pthread_mutexattr_t attributes;
76 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
77 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
78 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
79 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070080}
81
82Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080083 int rc = pthread_mutex_destroy(&mutex_);
84 if (rc != 0) {
85 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080086 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -080087 bool shutting_down = Runtime::Current()->IsShuttingDown();
88 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
89 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070090}
91
92void Mutex::Lock() {
Elliott Hughesa4060e52012-03-02 16:51:35 -080093 CheckSafeToLockOrUnlock(rank_, true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070094 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers105245c2012-01-27 11:42:43 -080095 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
98bool Mutex::TryLock() {
99 int result = pthread_mutex_trylock(&mutex_);
100 if (result == EBUSY) {
101 return false;
102 }
103 if (result != 0) {
104 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700105 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700106 }
Elliott Hughesa4060e52012-03-02 16:51:35 -0800107 CheckSafeToLockOrUnlock(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -0800108 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109 return true;
110}
111
112void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -0800113 AssertHeld();
Elliott Hughesa4060e52012-03-02 16:51:35 -0800114 CheckSafeToLockOrUnlock(rank_, false);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700115 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -0700116}
117
Elliott Hughesf1498432012-03-28 19:34:27 -0700118#if !defined(NDEBUG)
Elliott Hughes5d6d5dc2012-03-29 11:59:27 -0700119#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060
120// Mac OS 10.5 didn't have anything we could implement GetTid() with. One thing we could try would
121// be using pthread_t instead of the actual tid; this would be acceptable in most places, and more
122// portable. 10.5 is already obsolete, though, so doing so would probably be all pain for no gain.
123void Mutex::AssertHeld() {}
124void Mutex::AssertNotHeld() {}
125#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700126void Mutex::AssertHeld() {
127 DCHECK_EQ(GetOwner(), static_cast<uint64_t>(GetTid()));
128}
129
130void Mutex::AssertNotHeld() {
131 DCHECK_NE(GetOwner(), static_cast<uint64_t>(GetTid()));
132}
133#endif
Elliott Hughes5d6d5dc2012-03-29 11:59:27 -0700134#endif
Elliott Hughesf1498432012-03-28 19:34:27 -0700135
136uint64_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -0700137#if defined(__BIONIC__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700138 return static_cast<uint64_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -0700139#elif defined(__GLIBC__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700140 return reinterpret_cast<glibc_pthread_mutex_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800141#elif defined(__APPLE__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700142 return reinterpret_cast<darwin_pthread_mutex_t*>(&mutex_)->owner_tid;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700143#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700144#error unsupported C library
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145#endif
146}
147
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800148uint32_t Mutex::GetDepth() {
Elliott Hughesf1498432012-03-28 19:34:27 -0700149 bool held = (GetOwner() == static_cast<uint64_t>(GetTid()));
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800150 if (!held) {
151 return 0;
152 }
153 uint32_t depth;
154#if defined(__BIONIC__)
155 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
156#elif defined(__GLIBC__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700157 depth = reinterpret_cast<glibc_pthread_mutex_t*>(&mutex_)->count;
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800158#elif defined(__APPLE__)
Elliott Hughesf1498432012-03-28 19:34:27 -0700159 darwin_pthread_mutex_t* darwin_mutex = reinterpret_cast<darwin_pthread_mutex_t*>(&mutex_);
160 depth = ((darwin_mutex->value >> 16) & 0xffff);
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800161#else
Elliott Hughesf1498432012-03-28 19:34:27 -0700162#error unsupported C library
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800163#endif
Elliott Hughesf1498432012-03-28 19:34:27 -0700164 CHECK_NE(depth, 0U) << "owner=" << GetOwner() << " tid=" << GetTid();
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800165 return depth;
166}
167
Elliott Hughes5f791332011-09-15 17:45:30 -0700168ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
169 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
170}
171
172ConditionVariable::~ConditionVariable() {
173 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
174}
175
176void ConditionVariable::Broadcast() {
177 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
178}
179
180void ConditionVariable::Signal() {
181 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
182}
183
184void ConditionVariable::Wait(Mutex& mutex) {
Elliott Hughesf1498432012-03-28 19:34:27 -0700185 CheckSafeToWait(mutex.rank_);
186 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, &mutex.mutex_));
Elliott Hughes5f791332011-09-15 17:45:30 -0700187}
188
189void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
190#ifdef HAVE_TIMEDWAIT_MONOTONIC
191#define TIMEDWAIT pthread_cond_timedwait_monotonic
192#else
193#define TIMEDWAIT pthread_cond_timedwait
194#endif
Elliott Hughesf1498432012-03-28 19:34:27 -0700195 CheckSafeToWait(mutex.rank_);
196 int rc = TIMEDWAIT(&cond_, &mutex.mutex_, &ts);
Elliott Hughes5f791332011-09-15 17:45:30 -0700197 if (rc != 0 && rc != ETIMEDOUT) {
198 errno = rc;
199 PLOG(FATAL) << "TimedWait failed for " << name_;
200 }
201}
202
Elliott Hughesffb465f2012-03-01 18:46:05 -0800203std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
204 switch (rhs) {
205 case kHeapLock: os << "HeapLock"; break;
206 case kThreadListLock: os << "ThreadListLock"; break;
207 case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
208 default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
209 }
210 return os;
211}
212
Elliott Hughes8daa0922011-09-11 13:46:25 -0700213} // namespace