blob: 5a9df17bde53a4f69b060e8614323a96a8494193 [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 -070030static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) {
Elliott Hughes67d92002012-03-26 15:08:51 -070031 if (!kIsDebugBuild) {
32 return;
33 }
Elliott Hughesffb465f2012-03-01 18:46:05 -080034 if (rank == -1) {
35 return;
36 }
37 Thread* self = Thread::Current();
38 if (self != NULL) {
Elliott Hughesa4060e52012-03-02 16:51:35 -080039 self->CheckSafeToLockOrUnlock(rank, is_locking);
40 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080041}
42
Elliott Hughes76e36942012-03-16 13:44:56 -070043static inline void CheckSafeToWait(MutexRank rank) {
Elliott Hughes67d92002012-03-26 15:08:51 -070044 if (!kIsDebugBuild) {
45 return;
46 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080047 Thread* self = Thread::Current();
48 if (self != NULL) {
49 self->CheckSafeToWait(rank);
Elliott Hughesffb465f2012-03-01 18:46:05 -080050 }
Elliott Hughesffb465f2012-03-01 18:46:05 -080051}
52
53Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080054 // Like Java, we use recursive mutexes.
55 pthread_mutexattr_t attributes;
56 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
57 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
58 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
59 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070060}
61
62Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080063 int rc = pthread_mutex_destroy(&mutex_);
64 if (rc != 0) {
65 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -080067 bool shutting_down = Runtime::Current()->IsShuttingDown();
68 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
69 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070070}
71
72void Mutex::Lock() {
Elliott Hughesa4060e52012-03-02 16:51:35 -080073 CheckSafeToLockOrUnlock(rank_, true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070074 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers105245c2012-01-27 11:42:43 -080075 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070076}
77
78bool Mutex::TryLock() {
79 int result = pthread_mutex_trylock(&mutex_);
80 if (result == EBUSY) {
81 return false;
82 }
83 if (result != 0) {
84 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -070085 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070086 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080087 CheckSafeToLockOrUnlock(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -080088 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070089 return true;
90}
91
92void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -080093 AssertHeld();
Elliott Hughesa4060e52012-03-02 16:51:35 -080094 CheckSafeToLockOrUnlock(rank_, false);
Elliott Hughes8d768a92011-09-14 16:35:25 -070095 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
98pid_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -070099#if defined(__BIONIC__)
Elliott Hughes8daa0922011-09-11 13:46:25 -0700100 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -0700101#elif defined(__GLIBC__)
102 struct __attribute__((__may_alias__)) glibc_pthread_t {
103 int lock;
104 unsigned int count;
105 int owner;
106 // ...other stuff we don't care about.
107 };
108 return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800109#elif defined(__APPLE__)
110 // We don't know a way to implement this for Mac OS.
111 return 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112#else
113 UNIMPLEMENTED(FATAL);
114 return 0;
115#endif
116}
117
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800118uint32_t Mutex::GetDepth() {
119 bool held = (GetOwner() == GetTid());
120 if (!held) {
121 return 0;
122 }
123 uint32_t depth;
124#if defined(__BIONIC__)
125 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
126#elif defined(__GLIBC__)
127 struct __attribute__((__may_alias__)) glibc_pthread_t {
128 int lock;
129 unsigned int count;
130 int owner;
131 // ...other stuff we don't care about.
132 };
133 depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count;
134#elif defined(__APPLE__)
135 // We don't know a way to implement this for Mac OS.
136 return 0;
137#else
138 UNIMPLEMENTED(FATAL);
139 return 0;
140#endif
141 CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid();
142 return depth;
143}
144
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145pid_t Mutex::GetTid() {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800146 return ::art::GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700147}
148
Elliott Hughes5f791332011-09-15 17:45:30 -0700149ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
150 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
151}
152
153ConditionVariable::~ConditionVariable() {
154 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
155}
156
157void ConditionVariable::Broadcast() {
158 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
159}
160
161void ConditionVariable::Signal() {
162 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
163}
164
165void ConditionVariable::Wait(Mutex& mutex) {
Elliott Hughesa4060e52012-03-02 16:51:35 -0800166 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700167 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
168}
169
170void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
171#ifdef HAVE_TIMEDWAIT_MONOTONIC
172#define TIMEDWAIT pthread_cond_timedwait_monotonic
173#else
174#define TIMEDWAIT pthread_cond_timedwait
175#endif
Elliott Hughesa4060e52012-03-02 16:51:35 -0800176 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700177 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
178 if (rc != 0 && rc != ETIMEDOUT) {
179 errno = rc;
180 PLOG(FATAL) << "TimedWait failed for " << name_;
181 }
182}
183
Elliott Hughesffb465f2012-03-01 18:46:05 -0800184std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
185 switch (rhs) {
186 case kHeapLock: os << "HeapLock"; break;
187 case kThreadListLock: os << "ThreadListLock"; break;
188 case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
189 default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
190 }
191 return os;
192}
193
Elliott Hughes8daa0922011-09-11 13:46:25 -0700194} // namespace