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 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace concurrency { |
| 15 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 16 | /** |
| 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: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 24 | impl() : initialized_(false) { |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 25 | int ret = pthread_mutex_init(&pthread_mutex_, NULL); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 26 | assert(ret == 0); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 27 | initialized_ = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | ~impl() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 31 | if (initialized_) { |
| 32 | initialized_ = false; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 33 | int ret = pthread_mutex_destroy(&pthread_mutex_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 34 | assert(ret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 38 | void lock() const { pthread_mutex_lock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 39 | |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 40 | bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); } |
| 41 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 42 | void unlock() const { pthread_mutex_unlock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 43 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 44 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 45 | mutable pthread_mutex_t pthread_mutex_; |
| 46 | mutable bool initialized_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 49 | Mutex::Mutex() : impl_(new Mutex::impl()) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 50 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 51 | void Mutex::lock() const { impl_->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 52 | |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 53 | bool Mutex::trylock() const { return impl_->trylock(); } |
| 54 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 55 | void Mutex::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Implementation of ReadWriteMutex class using POSIX rw lock |
| 59 | * |
| 60 | * @author boz |
| 61 | * @version $Id:$ |
| 62 | */ |
| 63 | class ReadWriteMutex::impl { |
| 64 | public: |
| 65 | impl() : initialized_(false) { |
| 66 | int ret = pthread_rwlock_init(&rw_lock_, NULL); |
| 67 | assert(ret == 0); |
| 68 | initialized_ = true; |
| 69 | } |
| 70 | |
| 71 | ~impl() { |
| 72 | if(initialized_) { |
| 73 | initialized_ = false; |
| 74 | int ret = pthread_rwlock_destroy(&rw_lock_); |
| 75 | assert(ret == 0); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); } |
| 80 | |
| 81 | void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); } |
| 82 | |
| 83 | bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); } |
| 84 | |
| 85 | bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); } |
| 86 | |
| 87 | void release() const { pthread_rwlock_unlock(&rw_lock_); } |
| 88 | |
| 89 | private: |
| 90 | mutable pthread_rwlock_t rw_lock_; |
| 91 | mutable bool initialized_; |
| 92 | }; |
| 93 | |
| 94 | ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {} |
| 95 | |
| 96 | void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); } |
| 97 | |
| 98 | void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); } |
| 99 | |
| 100 | bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); } |
| 101 | |
| 102 | bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); } |
| 103 | |
| 104 | void ReadWriteMutex::release() const { impl_->release(); } |
| 105 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 106 | }}} // facebook::thrift::concurrency |
| 107 | |