blob: 340a075a73c817035f462230c5abcb1a3bd73d2b [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 Hughesa4060e52012-03-02 16:51:35 -080030static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080031#ifndef NDEBUG
32 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 }
39#endif
40}
41
42static inline void CheckSafeToWait(MutexRank rank) {
43#ifndef NDEBUG
44 Thread* self = Thread::Current();
45 if (self != NULL) {
46 self->CheckSafeToWait(rank);
Elliott Hughesffb465f2012-03-01 18:46:05 -080047 }
48#endif
49}
50
51Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080052 // Like Java, we use recursive mutexes.
53 pthread_mutexattr_t attributes;
54 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
55 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
56 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
57 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070058}
59
60Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080061 int rc = pthread_mutex_destroy(&mutex_);
62 if (rc != 0) {
63 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080064 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -080065 bool shutting_down = Runtime::Current()->IsShuttingDown();
66 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
67 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070068}
69
70void Mutex::Lock() {
Elliott Hughesa4060e52012-03-02 16:51:35 -080071 CheckSafeToLockOrUnlock(rank_, true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070072 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers105245c2012-01-27 11:42:43 -080073 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070074}
75
76bool Mutex::TryLock() {
77 int result = pthread_mutex_trylock(&mutex_);
78 if (result == EBUSY) {
79 return false;
80 }
81 if (result != 0) {
82 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -070083 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070084 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080085 CheckSafeToLockOrUnlock(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -080086 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070087 return true;
88}
89
90void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -080091 AssertHeld();
Elliott Hughesa4060e52012-03-02 16:51:35 -080092 CheckSafeToLockOrUnlock(rank_, false);
Elliott Hughes8d768a92011-09-14 16:35:25 -070093 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070094}
95
96pid_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -070097#if defined(__BIONIC__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070098 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -070099#elif defined(__GLIBC__)
100 struct __attribute__((__may_alias__)) glibc_pthread_t {
101 int lock;
102 unsigned int count;
103 int owner;
104 // ...other stuff we don't care about.
105 };
106 return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800107#elif defined(__APPLE__)
108 // We don't know a way to implement this for Mac OS.
109 return 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700110#else
111 UNIMPLEMENTED(FATAL);
112 return 0;
113#endif
114}
115
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800116uint32_t Mutex::GetDepth() {
117 bool held = (GetOwner() == GetTid());
118 if (!held) {
119 return 0;
120 }
121 uint32_t depth;
122#if defined(__BIONIC__)
123 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
124#elif defined(__GLIBC__)
125 struct __attribute__((__may_alias__)) glibc_pthread_t {
126 int lock;
127 unsigned int count;
128 int owner;
129 // ...other stuff we don't care about.
130 };
131 depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count;
132#elif defined(__APPLE__)
133 // We don't know a way to implement this for Mac OS.
134 return 0;
135#else
136 UNIMPLEMENTED(FATAL);
137 return 0;
138#endif
139 CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid();
140 return depth;
141}
142
Elliott Hughes8daa0922011-09-11 13:46:25 -0700143pid_t Mutex::GetTid() {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800144 return ::art::GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145}
146
Elliott Hughes5f791332011-09-15 17:45:30 -0700147ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
148 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
149}
150
151ConditionVariable::~ConditionVariable() {
152 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
153}
154
155void ConditionVariable::Broadcast() {
156 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
157}
158
159void ConditionVariable::Signal() {
160 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
161}
162
163void ConditionVariable::Wait(Mutex& mutex) {
Elliott Hughesa4060e52012-03-02 16:51:35 -0800164 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700165 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
166}
167
168void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
169#ifdef HAVE_TIMEDWAIT_MONOTONIC
170#define TIMEDWAIT pthread_cond_timedwait_monotonic
171#else
172#define TIMEDWAIT pthread_cond_timedwait
173#endif
Elliott Hughesa4060e52012-03-02 16:51:35 -0800174 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700175 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
176 if (rc != 0 && rc != ETIMEDOUT) {
177 errno = rc;
178 PLOG(FATAL) << "TimedWait failed for " << name_;
179 }
180}
181
Elliott Hughesffb465f2012-03-01 18:46:05 -0800182std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
183 switch (rhs) {
184 case kHeapLock: os << "HeapLock"; break;
185 case kThreadListLock: os << "ThreadListLock"; break;
186 case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
187 default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
188 }
189 return os;
190}
191
Elliott Hughes8daa0922011-09-11 13:46:25 -0700192} // namespace