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