Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 21 | #include "heap.h" // for VERIFY_OBJECT_ENABLED |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 22 | #include "logging.h" |
| 23 | #include "utils.h" |
| 24 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 25 | #define CHECK_MUTEX_CALL(call, args) CHECK_PTHREAD_CALL(call, args, name_) |
| 26 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
| 29 | Mutex::Mutex(const char* name) : name_(name) { |
| 30 | #ifndef NDEBUG |
| 31 | pthread_mutexattr_t debug_attributes; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 32 | CHECK_MUTEX_CALL(pthread_mutexattr_init, (&debug_attributes)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 33 | #if VERIFY_OBJECT_ENABLED |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 34 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&debug_attributes, PTHREAD_MUTEX_RECURSIVE)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 35 | #else |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 36 | CHECK_MUTEX_CALL(pthread_mutexattr_settype, (&debug_attributes, PTHREAD_MUTEX_ERRORCHECK)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 37 | #endif |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 38 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, &debug_attributes)); |
| 39 | CHECK_MUTEX_CALL(pthread_mutexattr_destroy, (&debug_attributes)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | #else |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 41 | CHECK_MUTEX_CALL(pthread_mutex_init, (&mutex_, NULL)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 42 | #endif |
| 43 | } |
| 44 | |
| 45 | Mutex::~Mutex() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 46 | CHECK_MUTEX_CALL(pthread_mutex_destroy, (&mutex_)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void Mutex::Lock() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 50 | CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool Mutex::TryLock() { |
| 54 | int result = pthread_mutex_trylock(&mutex_); |
| 55 | if (result == EBUSY) { |
| 56 | return false; |
| 57 | } |
| 58 | if (result != 0) { |
| 59 | errno = result; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 60 | PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void Mutex::Unlock() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 66 | CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | pid_t Mutex::GetOwner() { |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 70 | #if defined(__BIONIC__) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 71 | return static_cast<pid_t>((mutex_.value >> 16) & 0xffff); |
Elliott Hughes | 3147a23 | 2011-10-12 15:55:07 -0700 | [diff] [blame] | 72 | #elif defined(__GLIBC__) |
| 73 | struct __attribute__((__may_alias__)) glibc_pthread_t { |
| 74 | int lock; |
| 75 | unsigned int count; |
| 76 | int owner; |
| 77 | // ...other stuff we don't care about. |
| 78 | }; |
| 79 | return reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 80 | #else |
| 81 | UNIMPLEMENTED(FATAL); |
| 82 | return 0; |
| 83 | #endif |
| 84 | } |
| 85 | |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 86 | void Mutex::ClearOwner() { |
| 87 | #if defined(__BIONIC__) |
| 88 | mutex_.value = 0; |
| 89 | #elif defined(__GLIBC__) |
| 90 | struct __attribute__((__may_alias__)) glibc_pthread_t { |
| 91 | int lock; |
| 92 | unsigned int count; |
| 93 | int owner; |
| 94 | // ...other stuff we don't care about. |
| 95 | }; |
| 96 | reinterpret_cast<glibc_pthread_t*>(&mutex_)->owner = 0; |
| 97 | #else |
| 98 | UNIMPLEMENTED(FATAL); |
| 99 | return 0; |
| 100 | #endif |
| 101 | } |
| 102 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 103 | pid_t Mutex::GetTid() { |
| 104 | return art::GetTid(); |
| 105 | } |
| 106 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 107 | ConditionVariable::ConditionVariable(const std::string& name) : name_(name) { |
| 108 | CHECK_MUTEX_CALL(pthread_cond_init, (&cond_, NULL)); |
| 109 | } |
| 110 | |
| 111 | ConditionVariable::~ConditionVariable() { |
| 112 | CHECK_MUTEX_CALL(pthread_cond_destroy, (&cond_)); |
| 113 | } |
| 114 | |
| 115 | void ConditionVariable::Broadcast() { |
| 116 | CHECK_MUTEX_CALL(pthread_cond_broadcast, (&cond_)); |
| 117 | } |
| 118 | |
| 119 | void ConditionVariable::Signal() { |
| 120 | CHECK_MUTEX_CALL(pthread_cond_signal, (&cond_)); |
| 121 | } |
| 122 | |
| 123 | void ConditionVariable::Wait(Mutex& mutex) { |
| 124 | CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl())); |
| 125 | } |
| 126 | |
| 127 | void ConditionVariable::TimedWait(Mutex& mutex, const timespec& ts) { |
| 128 | #ifdef HAVE_TIMEDWAIT_MONOTONIC |
| 129 | #define TIMEDWAIT pthread_cond_timedwait_monotonic |
| 130 | #else |
| 131 | #define TIMEDWAIT pthread_cond_timedwait |
| 132 | #endif |
| 133 | int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts); |
| 134 | if (rc != 0 && rc != ETIMEDOUT) { |
| 135 | errno = rc; |
| 136 | PLOG(FATAL) << "TimedWait failed for " << name_; |
| 137 | } |
| 138 | } |
| 139 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 140 | } // namespace |