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 | |
| 6 | namespace facebook { namespace thrift { namespace concurrency { |
| 7 | |
| 8 | class Mutex::impl { |
| 9 | public: |
| 10 | impl() : initialized(false) { |
| 11 | assert(pthread_mutex_init(&_pthread_mutex, NULL) == 0); |
| 12 | initialized = true; |
| 13 | } |
| 14 | |
| 15 | ~impl() { |
| 16 | if(initialized) { |
| 17 | initialized = false; |
| 18 | assert(pthread_mutex_destroy(&_pthread_mutex) == 0); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | void lock() const {pthread_mutex_lock(&_pthread_mutex);} |
| 23 | |
| 24 | void unlock() const {pthread_mutex_unlock(&_pthread_mutex);} |
| 25 | |
| 26 | private: |
| 27 | mutable pthread_mutex_t _pthread_mutex; |
| 28 | mutable bool initialized; |
| 29 | }; |
| 30 | |
| 31 | Mutex::Mutex() : _impl(new Mutex::impl()) {} |
| 32 | |
| 33 | void Mutex::lock() const {_impl->lock();} |
| 34 | |
| 35 | void Mutex::unlock() const {_impl->unlock();} |
| 36 | |
| 37 | }}} // facebook::thrift::concurrency |
| 38 | |