| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 1 | #include "Mutex.h" |
| 2 | |||||
| 3 | #include <assert.h> | ||||
| 4 | #include <pthread.h> | ||||
| 5 | |||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 6 | namespace facebook { namespace thrift { namespace concurrency { |
| 7 | |||||
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 8 | /** |
| 9 | * Implementation of Mutex class using POSIX mutex | ||||
| 10 | * | ||||
| 11 | * @author marc | ||||
| 12 | * @version $Id:$ | ||||
| 13 | */ | ||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 14 | class Mutex::impl { |
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 15 | public: |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 16 | impl() : initialized_(false) { |
| 17 | assert(pthread_mutex_init(&pthread_mutex_, NULL) == 0); | ||||
| 18 | initialized_ = true; | ||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 19 | } |
| 20 | |||||
| 21 | ~impl() { | ||||
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 22 | if (initialized_) { |
| 23 | initialized_ = false; | ||||
| 24 | assert(pthread_mutex_destroy(&pthread_mutex_) == 0); | ||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 25 | } |
| 26 | } | ||||
| 27 | |||||
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 28 | void lock() const { pthread_mutex_lock(&pthread_mutex_); } |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 29 | |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 30 | void unlock() const { pthread_mutex_unlock(&pthread_mutex_); } |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | |
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 32 | private: |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 33 | mutable pthread_mutex_t pthread_mutex_; |
| 34 | mutable bool initialized_; | ||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 35 | }; |
| 36 | |||||
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 37 | Mutex::Mutex() : impl_(new Mutex::impl()) {} |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 38 | |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 39 | void Mutex::lock() const { impl_->lock(); } |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 41 | void Mutex::unlock() const { impl_->unlock(); } |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 42 | |
| 43 | }}} // facebook::thrift::concurrency | ||||
| 44 | |||||