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: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 16 | impl() : initialized(false) { |
| 17 | assert(pthread_mutex_init(&_pthread_mutex, NULL) == 0); |
| 18 | initialized = true; |
| 19 | } |
| 20 | |
| 21 | ~impl() { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 22 | if (initialized) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 23 | initialized = false; |
| 24 | assert(pthread_mutex_destroy(&_pthread_mutex) == 0); |
| 25 | } |
| 26 | } |
| 27 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +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 | f5f2be4 | 2006-09-05 21:05:31 +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: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 33 | mutable pthread_mutex_t _pthread_mutex; |
| 34 | mutable bool initialized; |
| 35 | }; |
| 36 | |
| 37 | Mutex::Mutex() : _impl(new Mutex::impl()) {} |
| 38 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +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 | f5f2be4 | 2006-09-05 21:05:31 +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 | |