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 | |||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 12 | namespace facebook { namespace thrift { namespace concurrency { |
13 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 14 | /** |
15 | * Implementation of Mutex class using POSIX mutex | ||||
16 | * | ||||
17 | * @author marc | ||||
18 | * @version $Id:$ | ||||
19 | */ | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | class Mutex::impl { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 21 | public: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 22 | impl() : initialized_(false) { |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 23 | int ret = pthread_mutex_init(&pthread_mutex_, NULL); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame^] | 24 | assert(ret == 0); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 25 | initialized_ = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | } |
27 | |||||
28 | ~impl() { | ||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 29 | if (initialized_) { |
30 | initialized_ = false; | ||||
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 31 | int ret = pthread_mutex_destroy(&pthread_mutex_); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame^] | 32 | assert(ret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 33 | } |
34 | } | ||||
35 | |||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 36 | void lock() const { pthread_mutex_lock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 38 | void unlock() const { pthread_mutex_unlock(&pthread_mutex_); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 39 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 40 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 41 | mutable pthread_mutex_t pthread_mutex_; |
42 | mutable bool initialized_; | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 43 | }; |
44 | |||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 45 | Mutex::Mutex() : impl_(new Mutex::impl()) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 46 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 47 | void Mutex::lock() const { impl_->lock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 48 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 49 | void Mutex::unlock() const { impl_->unlock(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 50 | |
51 | }}} // facebook::thrift::concurrency | ||||
52 |