blob: a055656242f655ecee7bdcd25e86af23d99b3115 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Slemko66949872006-07-15 01:52:39 +00007#include "Mutex.h"
8
9#include <assert.h>
10#include <pthread.h>
11
yunfang14542962007-10-03 22:59:41 +000012using boost::shared_ptr;
13
Marc Slemko66949872006-07-15 01:52:39 +000014namespace facebook { namespace thrift { namespace concurrency {
15
Mark Sleef5f2be42006-09-05 21:05:31 +000016/**
17 * Implementation of Mutex class using POSIX mutex
18 *
19 * @author marc
20 * @version $Id:$
21 */
Marc Slemko66949872006-07-15 01:52:39 +000022class Mutex::impl {
Mark Sleef5f2be42006-09-05 21:05:31 +000023 public:
Mark Slee2f6404d2006-10-10 01:37:40 +000024 impl() : initialized_(false) {
Aditya Agarwal9dc57402007-03-31 17:45:12 +000025 int ret = pthread_mutex_init(&pthread_mutex_, NULL);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000026 assert(ret == 0);
Mark Slee2f6404d2006-10-10 01:37:40 +000027 initialized_ = true;
Marc Slemko66949872006-07-15 01:52:39 +000028 }
29
30 ~impl() {
Mark Slee2f6404d2006-10-10 01:37:40 +000031 if (initialized_) {
32 initialized_ = false;
Aditya Agarwal9dc57402007-03-31 17:45:12 +000033 int ret = pthread_mutex_destroy(&pthread_mutex_);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000034 assert(ret == 0);
Marc Slemko66949872006-07-15 01:52:39 +000035 }
36 }
37
Mark Slee2f6404d2006-10-10 01:37:40 +000038 void lock() const { pthread_mutex_lock(&pthread_mutex_); }
Marc Slemko66949872006-07-15 01:52:39 +000039
boz5362e702007-08-15 20:55:36 +000040 bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); }
41
Mark Slee2f6404d2006-10-10 01:37:40 +000042 void unlock() const { pthread_mutex_unlock(&pthread_mutex_); }
Marc Slemko66949872006-07-15 01:52:39 +000043
Mark Sleef5f2be42006-09-05 21:05:31 +000044 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000045 mutable pthread_mutex_t pthread_mutex_;
46 mutable bool initialized_;
Marc Slemko66949872006-07-15 01:52:39 +000047};
48
Mark Slee2f6404d2006-10-10 01:37:40 +000049Mutex::Mutex() : impl_(new Mutex::impl()) {}
Marc Slemko66949872006-07-15 01:52:39 +000050
Mark Slee2f6404d2006-10-10 01:37:40 +000051void Mutex::lock() const { impl_->lock(); }
Marc Slemko66949872006-07-15 01:52:39 +000052
boz5362e702007-08-15 20:55:36 +000053bool Mutex::trylock() const { return impl_->trylock(); }
54
Mark Slee2f6404d2006-10-10 01:37:40 +000055void Mutex::unlock() const { impl_->unlock(); }
Marc Slemko66949872006-07-15 01:52:39 +000056
bozcce81842007-07-06 22:27:52 +000057/**
58 * Implementation of ReadWriteMutex class using POSIX rw lock
59 *
60 * @author boz
61 * @version $Id:$
62 */
63class ReadWriteMutex::impl {
64public:
65 impl() : initialized_(false) {
66 int ret = pthread_rwlock_init(&rw_lock_, NULL);
67 assert(ret == 0);
68 initialized_ = true;
69 }
70
71 ~impl() {
72 if(initialized_) {
73 initialized_ = false;
74 int ret = pthread_rwlock_destroy(&rw_lock_);
75 assert(ret == 0);
76 }
77 }
78
79 void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); }
80
81 void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); }
82
83 bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); }
84
85 bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); }
86
87 void release() const { pthread_rwlock_unlock(&rw_lock_); }
88
89private:
90 mutable pthread_rwlock_t rw_lock_;
91 mutable bool initialized_;
92};
93
94ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {}
95
96void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); }
97
98void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); }
99
100bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); }
101
102bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); }
103
104void ReadWriteMutex::release() const { impl_->release(); }
105
Marc Slemko66949872006-07-15 01:52:39 +0000106}}} // facebook::thrift::concurrency
107