blob: b3cf94639f2040782d53149594fcd8fecc6109e6 [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 Hughes8d768a92011-09-14 16:35:25 -070026#define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_)
27
Elliott Hughes8daa0922011-09-11 13:46:25 -070028namespace art {
29
Elliott Hughes76e36942012-03-16 13:44:56 -070030#if !defined(NDBEUG)
31static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080032 if (rank == -1) {
33 return;
34 }
35 Thread* self = Thread::Current();
36 if (self != NULL) {
Elliott Hughesa4060e52012-03-02 16:51:35 -080037 self->CheckSafeToLockOrUnlock(rank, is_locking);
38 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080039}
Elliott Hughes76e36942012-03-16 13:44:56 -070040#else
41static inline void CheckSafeToLockOrUnlock(MutexRank, bool) {}
42#endif
Elliott Hughesa4060e52012-03-02 16:51:35 -080043
Elliott Hughes76e36942012-03-16 13:44:56 -070044#if !defined(NDEBUG)
45static inline void CheckSafeToWait(MutexRank rank) {
Elliott Hughesa4060e52012-03-02 16:51:35 -080046 Thread* self = Thread::Current();
47 if (self != NULL) {
48 self->CheckSafeToWait(rank);
Elliott Hughesffb465f2012-03-01 18:46:05 -080049 }
Elliott Hughesffb465f2012-03-01 18:46:05 -080050}
Elliott Hughes76e36942012-03-16 13:44:56 -070051#else
52static inline void CheckSafeToWait(MutexRank) {}
53#endif
Elliott Hughesffb465f2012-03-01 18:46:05 -080054
55Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080056 // Like Java, we use recursive mutexes.
57 pthread_mutexattr_t attributes;
58 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
59 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
60 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
61 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070062}
63
64Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080065 int rc = pthread_mutex_destroy(&mutex_);
66 if (rc != 0) {
67 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080068 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -080069 bool shutting_down = Runtime::Current()->IsShuttingDown();
70 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
71 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070072}
73
74void Mutex::Lock() {
Elliott Hughesa4060e52012-03-02 16:51:35 -080075 CheckSafeToLockOrUnlock(rank_, true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070076 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers105245c2012-01-27 11:42:43 -080077 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070078}
79
80bool Mutex::TryLock() {
81 int result = pthread_mutex_trylock(&mutex_);
82 if (result == EBUSY) {
83 return false;
84 }
85 if (result != 0) {
86 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -070087 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070088 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080089 CheckSafeToLockOrUnlock(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -080090 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070091 return true;
92}
93
94void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -080095 AssertHeld();
Elliott Hughesa4060e52012-03-02 16:51:35 -080096 CheckSafeToLockOrUnlock(rank_, false);
Elliott Hughes8d768a92011-09-14 16:35:25 -070097 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070098}
99
100pid_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -0700101#if defined(__BIONIC__)
Elliott Hughes8daa0922011-09-11 13:46:25 -0700102 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -0700103#elif defined(__GLIBC__)
104 struct __attribute__((__may_alias__)) glibc_pthread_t {
105 int lock;
106 unsigned int count;
107 int owner;
108 // ...other stuff we don't care about.
109 };
110 return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800111#elif defined(__APPLE__)
112 // We don't know a way to implement this for Mac OS.
113 return 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700114#else
115 UNIMPLEMENTED(FATAL);
116 return 0;
117#endif
118}
119
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800120uint32_t Mutex::GetDepth() {
121 bool held = (GetOwner() == GetTid());
122 if (!held) {
123 return 0;
124 }
125 uint32_t depth;
126#if defined(__BIONIC__)
127 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
128#elif defined(__GLIBC__)
129 struct __attribute__((__may_alias__)) glibc_pthread_t {
130 int lock;
131 unsigned int count;
132 int owner;
133 // ...other stuff we don't care about.
134 };
135 depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count;
136#elif defined(__APPLE__)
137 // We don't know a way to implement this for Mac OS.
138 return 0;
139#else
140 UNIMPLEMENTED(FATAL);
141 return 0;
142#endif
143 CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid();
144 return depth;
145}
146
Elliott Hughes8daa0922011-09-11 13:46:25 -0700147pid_t Mutex::GetTid() {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800148 return ::art::GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700149}
150
Elliott Hughes5f791332011-09-15 17:45:30 -0700151ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
152 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
153}
154
155ConditionVariable::~ConditionVariable() {
156 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
157}
158
159void ConditionVariable::Broadcast() {
160 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
161}
162
163void ConditionVariable::Signal() {
164 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
165}
166
167void ConditionVariable::Wait(Mutex& mutex) {
Elliott Hughesa4060e52012-03-02 16:51:35 -0800168 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700169 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
170}
171
172void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
173#ifdef HAVE_TIMEDWAIT_MONOTONIC
174#define TIMEDWAIT pthread_cond_timedwait_monotonic
175#else
176#define TIMEDWAIT pthread_cond_timedwait
177#endif
Elliott Hughesa4060e52012-03-02 16:51:35 -0800178 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700179 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
180 if (rc != 0 && rc != ETIMEDOUT) {
181 errno = rc;
182 PLOG(FATAL) << "TimedWait failed for " << name_;
183 }
184}
185
Elliott Hughesffb465f2012-03-01 18:46:05 -0800186std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
187 switch (rhs) {
188 case kHeapLock: os << "HeapLock"; break;
189 case kThreadListLock: os << "ThreadListLock"; break;
190 case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
191 default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
192 }
193 return os;
194}
195
Elliott Hughes8daa0922011-09-11 13:46:25 -0700196} // namespace