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 | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 6 | /** Implementation of Mutex class using POSIX mutex |
| 7 | |
| 8 | @author marc |
| 9 | @version $Id:$ */ |
| 10 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 11 | namespace facebook { namespace thrift { namespace concurrency { |
| 12 | |
| 13 | class Mutex::impl { |
| 14 | public: |
| 15 | impl() : initialized(false) { |
| 16 | assert(pthread_mutex_init(&_pthread_mutex, NULL) == 0); |
| 17 | initialized = true; |
| 18 | } |
| 19 | |
| 20 | ~impl() { |
| 21 | if(initialized) { |
| 22 | initialized = false; |
| 23 | assert(pthread_mutex_destroy(&_pthread_mutex) == 0); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void lock() const {pthread_mutex_lock(&_pthread_mutex);} |
| 28 | |
| 29 | void unlock() const {pthread_mutex_unlock(&_pthread_mutex);} |
| 30 | |
| 31 | private: |
| 32 | mutable pthread_mutex_t _pthread_mutex; |
| 33 | mutable bool initialized; |
| 34 | }; |
| 35 | |
| 36 | Mutex::Mutex() : _impl(new Mutex::impl()) {} |
| 37 | |
| 38 | void Mutex::lock() const {_impl->lock();} |
| 39 | |
| 40 | void Mutex::unlock() const {_impl->unlock();} |
| 41 | |
| 42 | }}} // facebook::thrift::concurrency |
| 43 | |