blob: 8282e73f8564819b81e3deab5c5efa89a1c41c2e [file] [log] [blame]
Marc Slemko66949872006-07-15 01:52:39 +00001#include "Mutex.h"
2
3#include <assert.h>
4#include <pthread.h>
5
Marc Slemko0e53ccd2006-07-17 23:51:05 +00006/** Implementation of Mutex class using POSIX mutex
7
8 @author marc
9 @version $Id:$ */
10
Marc Slemko66949872006-07-15 01:52:39 +000011namespace facebook { namespace thrift { namespace concurrency {
12
13class Mutex::impl {
14public:
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
31private:
32 mutable pthread_mutex_t _pthread_mutex;
33 mutable bool initialized;
34};
35
36Mutex::Mutex() : _impl(new Mutex::impl()) {}
37
38void Mutex::lock() const {_impl->lock();}
39
40void Mutex::unlock() const {_impl->unlock();}
41
42}}} // facebook::thrift::concurrency
43