blob: 01aeff495bd87e4833e2e1e825a713cb50bd8c99 [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 Hughes1bac54f2012-03-16 12:48:31 -070030static inline void CheckSafeToLockOrUnlock(MutexRank __attribute__((unused)) rank,
31 bool __attribute__((unused)) is_locking) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080032#ifndef NDEBUG
33 if (rank == -1) {
34 return;
35 }
36 Thread* self = Thread::Current();
37 if (self != NULL) {
Elliott Hughesa4060e52012-03-02 16:51:35 -080038 self->CheckSafeToLockOrUnlock(rank, is_locking);
39 }
40#endif
41}
42
Elliott Hughes1bac54f2012-03-16 12:48:31 -070043static inline void CheckSafeToWait(MutexRank __attribute__((unused)) rank) {
Elliott Hughesa4060e52012-03-02 16:51:35 -080044#ifndef NDEBUG
45 Thread* self = Thread::Current();
46 if (self != NULL) {
47 self->CheckSafeToWait(rank);
Elliott Hughesffb465f2012-03-01 18:46:05 -080048 }
49#endif
50}
51
52Mutex::Mutex(const char* name, MutexRank rank) : name_(name), rank_(rank) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080053 // Like Java, we use recursive mutexes.
54 pthread_mutexattr_t attributes;
55 CHECK_MUTEX_CALL(pthread_mutexattr_init, (&attributes));
56 CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&attributes, PTHREAD_MUTEX_RECURSIVE));
57 CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &attributes));
58 CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&attributes));
Elliott Hughes8daa0922011-09-11 13:46:25 -070059}
60
61Mutex::~Mutex() {
Elliott Hughes6b355752012-01-13 16:49:08 -080062 int rc = pthread_mutex_destroy(&mutex_);
63 if (rc != 0) {
64 errno = rc;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080065 // TODO: should we just not log at all if shutting down? this could be the logging mutex!
Elliott Hughes6b355752012-01-13 16:49:08 -080066 bool shutting_down = Runtime::Current()->IsShuttingDown();
67 PLOG(shutting_down ? WARNING : FATAL) << "pthread_mutex_destroy failed for " << name_;
68 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070069}
70
71void Mutex::Lock() {
Elliott Hughesa4060e52012-03-02 16:51:35 -080072 CheckSafeToLockOrUnlock(rank_, true);
Elliott Hughes8d768a92011-09-14 16:35:25 -070073 CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
Ian Rogers105245c2012-01-27 11:42:43 -080074 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070075}
76
77bool Mutex::TryLock() {
78 int result = pthread_mutex_trylock(&mutex_);
79 if (result == EBUSY) {
80 return false;
81 }
82 if (result != 0) {
83 errno = result;
Elliott Hughes8d768a92011-09-14 16:35:25 -070084 PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
Elliott Hughes8daa0922011-09-11 13:46:25 -070085 }
Elliott Hughesa4060e52012-03-02 16:51:35 -080086 CheckSafeToLockOrUnlock(rank_, true);
Ian Rogers105245c2012-01-27 11:42:43 -080087 AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -070088 return true;
89}
90
91void Mutex::Unlock() {
Ian Rogers105245c2012-01-27 11:42:43 -080092 AssertHeld();
Elliott Hughesa4060e52012-03-02 16:51:35 -080093 CheckSafeToLockOrUnlock(rank_, false);
Elliott Hughes8d768a92011-09-14 16:35:25 -070094 CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
Elliott Hughes8daa0922011-09-11 13:46:25 -070095}
96
97pid_t Mutex::GetOwner() {
Elliott Hughes3147a232011-10-12 15:55:07 -070098#if defined(__BIONIC__)
Elliott Hughes8daa0922011-09-11 13:46:25 -070099 return static_cast<pid_t>((mutex_.value >> 16) & 0xffff);
Elliott Hughes3147a232011-10-12 15:55:07 -0700100#elif defined(__GLIBC__)
101 struct __attribute__((__may_alias__)) glibc_pthread_t {
102 int lock;
103 unsigned int count;
104 int owner;
105 // ...other stuff we don't care about.
106 };
107 return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner;
Elliott Hughescf044312012-01-23 18:48:51 -0800108#elif defined(__APPLE__)
109 // We don't know a way to implement this for Mac OS.
110 return 0;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111#else
112 UNIMPLEMENTED(FATAL);
113 return 0;
114#endif
115}
116
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800117uint32_t Mutex::GetDepth() {
118 bool held = (GetOwner() == GetTid());
119 if (!held) {
120 return 0;
121 }
122 uint32_t depth;
123#if defined(__BIONIC__)
124 depth = static_cast<uint32_t>((mutex_.value >> 2) & 0x7ff) + 1;
125#elif defined(__GLIBC__)
126 struct __attribute__((__may_alias__)) glibc_pthread_t {
127 int lock;
128 unsigned int count;
129 int owner;
130 // ...other stuff we don't care about.
131 };
132 depth = reinterpret_cast<glibc_pthread_t*>(&mutex_)->count;
133#elif defined(__APPLE__)
134 // We don't know a way to implement this for Mac OS.
135 return 0;
136#else
137 UNIMPLEMENTED(FATAL);
138 return 0;
139#endif
140 CHECK_NE(0U, depth) << "owner=" << GetOwner() << " tid=" << GetTid();
141 return depth;
142}
143
Elliott Hughes8daa0922011-09-11 13:46:25 -0700144pid_t Mutex::GetTid() {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800145 return ::art::GetTid();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700146}
147
Elliott Hughes5f791332011-09-15 17:45:30 -0700148ConditionVariable::ConditionVariable(const std::string& name) : name_(name) {
149 CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL));
150}
151
152ConditionVariable::~ConditionVariable() {
153 CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_));
154}
155
156void ConditionVariable::Broadcast() {
157 CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_));
158}
159
160void ConditionVariable::Signal() {
161 CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_));
162}
163
164void ConditionVariable::Wait(Mutex& mutex) {
Elliott Hughesa4060e52012-03-02 16:51:35 -0800165 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700166 CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
167}
168
169void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) {
170#ifdef HAVE_TIMEDWAIT_MONOTONIC
171#define TIMEDWAIT pthread_cond_timedwait_monotonic
172#else
173#define TIMEDWAIT pthread_cond_timedwait
174#endif
Elliott Hughesa4060e52012-03-02 16:51:35 -0800175 CheckSafeToWait(mutex.GetRank());
Elliott Hughes5f791332011-09-15 17:45:30 -0700176 int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
177 if (rc != 0 && rc != ETIMEDOUT) {
178 errno = rc;
179 PLOG(FATAL) << "TimedWait failed for " << name_;
180 }
181}
182
Elliott Hughesffb465f2012-03-01 18:46:05 -0800183std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
184 switch (rhs) {
185 case kHeapLock: os << "HeapLock"; break;
186 case kThreadListLock: os << "ThreadListLock"; break;
187 case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
188 default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
189 }
190 return os;
191}
192
Elliott Hughes8daa0922011-09-11 13:46:25 -0700193} // namespace