David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | #include "Mutex.h" |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 21 | #include "Util.h" |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
| 23 | #include <assert.h> |
| 24 | #include <pthread.h> |
| 25 | |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 26 | using boost::shared_ptr; |
| 27 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 28 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 29 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 30 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | * Implementation of Mutex class using POSIX mutex |
| 32 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 33 | * @version $Id:$ |
| 34 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 35 | class Mutex::impl { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 36 | public: |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 37 | impl(Initializer init) : initialized_(false) { |
| 38 | init(&pthread_mutex_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 39 | initialized_ = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ~impl() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 43 | if (initialized_) { |
| 44 | initialized_ = false; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 45 | int ret = pthread_mutex_destroy(&pthread_mutex_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 46 | assert(ret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 50 | void lock() const { pthread_mutex_lock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 51 | |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 52 | bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); } |
| 53 | |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 54 | bool timedlock(int64_t milliseconds) const { |
| 55 | struct timespec ts; |
| 56 | Util::toTimespec(ts, milliseconds); |
| 57 | return (0 == pthread_mutex_timedlock(&pthread_mutex_, &ts)); |
| 58 | } |
| 59 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 60 | void unlock() const { pthread_mutex_unlock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 61 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 62 | void* getUnderlyingImpl() const { return (void*) &pthread_mutex_; } |
| 63 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 64 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 65 | mutable pthread_mutex_t pthread_mutex_; |
| 66 | mutable bool initialized_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 69 | Mutex::Mutex(Initializer init) : impl_(new Mutex::impl(init)) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 70 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 71 | void* Mutex::getUnderlyingImpl() const { return impl_->getUnderlyingImpl(); } |
| 72 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 73 | void Mutex::lock() const { impl_->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 74 | |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 75 | bool Mutex::trylock() const { return impl_->trylock(); } |
| 76 | |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 77 | bool Mutex::timedlock(int64_t ms) const { return impl_->timedlock(ms); } |
| 78 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 79 | void Mutex::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 80 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 81 | void Mutex::DEFAULT_INITIALIZER(void* arg) { |
| 82 | pthread_mutex_t* pthread_mutex = (pthread_mutex_t*)arg; |
| 83 | int ret = pthread_mutex_init(pthread_mutex, NULL); |
| 84 | assert(ret == 0); |
| 85 | } |
| 86 | |
| 87 | static void init_with_kind(pthread_mutex_t* mutex, int kind) { |
| 88 | pthread_mutexattr_t mutexattr; |
| 89 | int ret = pthread_mutexattr_init(&mutexattr); |
| 90 | assert(ret == 0); |
| 91 | |
| 92 | // Apparently, this can fail. Should we really be aborting? |
| 93 | ret = pthread_mutexattr_settype(&mutexattr, kind); |
| 94 | assert(ret == 0); |
| 95 | |
| 96 | ret = pthread_mutex_init(mutex, &mutexattr); |
| 97 | assert(ret == 0); |
| 98 | |
| 99 | ret = pthread_mutexattr_destroy(&mutexattr); |
| 100 | assert(ret == 0); |
| 101 | } |
| 102 | |
| 103 | #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
| 104 | void Mutex::ADAPTIVE_INITIALIZER(void* arg) { |
| 105 | // From mysql source: mysys/my_thr_init.c |
| 106 | // Set mutex type to "fast" a.k.a "adaptive" |
| 107 | // |
| 108 | // In this case the thread may steal the mutex from some other thread |
| 109 | // that is waiting for the same mutex. This will save us some |
| 110 | // context switches but may cause a thread to 'starve forever' while |
| 111 | // waiting for the mutex (not likely if the code within the mutex is |
| 112 | // short). |
| 113 | init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP |
| 118 | void Mutex::RECURSIVE_INITIALIZER(void* arg) { |
| 119 | init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 124 | /** |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 125 | * Implementation of ReadWriteMutex class using POSIX rw lock |
| 126 | * |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 127 | * @version $Id:$ |
| 128 | */ |
| 129 | class ReadWriteMutex::impl { |
| 130 | public: |
| 131 | impl() : initialized_(false) { |
| 132 | int ret = pthread_rwlock_init(&rw_lock_, NULL); |
| 133 | assert(ret == 0); |
| 134 | initialized_ = true; |
| 135 | } |
| 136 | |
| 137 | ~impl() { |
| 138 | if(initialized_) { |
| 139 | initialized_ = false; |
| 140 | int ret = pthread_rwlock_destroy(&rw_lock_); |
| 141 | assert(ret == 0); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); } |
| 146 | |
| 147 | void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); } |
| 148 | |
| 149 | bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); } |
| 150 | |
| 151 | bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); } |
| 152 | |
| 153 | void release() const { pthread_rwlock_unlock(&rw_lock_); } |
| 154 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 155 | private: |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 156 | mutable pthread_rwlock_t rw_lock_; |
| 157 | mutable bool initialized_; |
| 158 | }; |
| 159 | |
| 160 | ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {} |
| 161 | |
| 162 | void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); } |
| 163 | |
| 164 | void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); } |
| 165 | |
| 166 | bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); } |
| 167 | |
| 168 | bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); } |
| 169 | |
| 170 | void ReadWriteMutex::release() const { impl_->release(); } |
| 171 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 172 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 173 | |