Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 7 | #include "Mutex.h" |
| 8 | |
| 9 | #include <assert.h> |
| 10 | #include <pthread.h> |
| 11 | |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 12 | using boost::shared_ptr; |
| 13 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 15 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 16 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 17 | * Implementation of Mutex class using POSIX mutex |
| 18 | * |
| 19 | * @author marc |
| 20 | * @version $Id:$ |
| 21 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | class Mutex::impl { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 23 | public: |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 24 | impl(Initializer init) : initialized_(false) { |
| 25 | init(&pthread_mutex_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 26 | initialized_ = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ~impl() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 30 | if (initialized_) { |
| 31 | initialized_ = false; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 32 | int ret = pthread_mutex_destroy(&pthread_mutex_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 33 | assert(ret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 37 | void lock() const { pthread_mutex_lock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 38 | |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 39 | bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); } |
| 40 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 41 | void unlock() const { pthread_mutex_unlock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 42 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 43 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 44 | mutable pthread_mutex_t pthread_mutex_; |
| 45 | mutable bool initialized_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 48 | Mutex::Mutex(Initializer init) : impl_(new Mutex::impl(init)) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 49 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 50 | void Mutex::lock() const { impl_->lock(); } |
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 Mutex::trylock() const { return impl_->trylock(); } |
| 53 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 54 | void Mutex::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 55 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 56 | void Mutex::DEFAULT_INITIALIZER(void* arg) { |
| 57 | pthread_mutex_t* pthread_mutex = (pthread_mutex_t*)arg; |
| 58 | int ret = pthread_mutex_init(pthread_mutex, NULL); |
| 59 | assert(ret == 0); |
| 60 | } |
| 61 | |
| 62 | static void init_with_kind(pthread_mutex_t* mutex, int kind) { |
| 63 | pthread_mutexattr_t mutexattr; |
| 64 | int ret = pthread_mutexattr_init(&mutexattr); |
| 65 | assert(ret == 0); |
| 66 | |
| 67 | // Apparently, this can fail. Should we really be aborting? |
| 68 | ret = pthread_mutexattr_settype(&mutexattr, kind); |
| 69 | assert(ret == 0); |
| 70 | |
| 71 | ret = pthread_mutex_init(mutex, &mutexattr); |
| 72 | assert(ret == 0); |
| 73 | |
| 74 | ret = pthread_mutexattr_destroy(&mutexattr); |
| 75 | assert(ret == 0); |
| 76 | } |
| 77 | |
| 78 | #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
| 79 | void Mutex::ADAPTIVE_INITIALIZER(void* arg) { |
| 80 | // From mysql source: mysys/my_thr_init.c |
| 81 | // Set mutex type to "fast" a.k.a "adaptive" |
| 82 | // |
| 83 | // In this case the thread may steal the mutex from some other thread |
| 84 | // that is waiting for the same mutex. This will save us some |
| 85 | // context switches but may cause a thread to 'starve forever' while |
| 86 | // waiting for the mutex (not likely if the code within the mutex is |
| 87 | // short). |
| 88 | init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP |
| 93 | void Mutex::RECURSIVE_INITIALIZER(void* arg) { |
| 94 | init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP); |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 99 | /** |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 100 | * Implementation of ReadWriteMutex class using POSIX rw lock |
| 101 | * |
| 102 | * @author boz |
| 103 | * @version $Id:$ |
| 104 | */ |
| 105 | class ReadWriteMutex::impl { |
| 106 | public: |
| 107 | impl() : initialized_(false) { |
| 108 | int ret = pthread_rwlock_init(&rw_lock_, NULL); |
| 109 | assert(ret == 0); |
| 110 | initialized_ = true; |
| 111 | } |
| 112 | |
| 113 | ~impl() { |
| 114 | if(initialized_) { |
| 115 | initialized_ = false; |
| 116 | int ret = pthread_rwlock_destroy(&rw_lock_); |
| 117 | assert(ret == 0); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); } |
| 122 | |
| 123 | void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); } |
| 124 | |
| 125 | bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); } |
| 126 | |
| 127 | bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); } |
| 128 | |
| 129 | void release() const { pthread_rwlock_unlock(&rw_lock_); } |
| 130 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 131 | private: |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 132 | mutable pthread_rwlock_t rw_lock_; |
| 133 | mutable bool initialized_; |
| 134 | }; |
| 135 | |
| 136 | ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {} |
| 137 | |
| 138 | void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); } |
| 139 | |
| 140 | void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); } |
| 141 | |
| 142 | bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); } |
| 143 | |
| 144 | bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); } |
| 145 | |
| 146 | void ReadWriteMutex::release() const { impl_->release(); } |
| 147 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 148 | }}} // facebook::thrift::concurrency |
| 149 | |