blob: 695d5dc526189386c65ff340cf6ac5543706e58a [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
Marc Slemko66949872006-07-15 01:52:39 +000012namespace facebook { namespace thrift { namespace concurrency {
13
Mark Sleef5f2be42006-09-05 21:05:31 +000014/**
15 * Implementation of Mutex class using POSIX mutex
16 *
17 * @author marc
18 * @version $Id:$
19 */
Marc Slemko66949872006-07-15 01:52:39 +000020class Mutex::impl {
Mark Sleef5f2be42006-09-05 21:05:31 +000021 public:
Mark Slee2f6404d2006-10-10 01:37:40 +000022 impl() : initialized_(false) {
Aditya Agarwal9dc57402007-03-31 17:45:12 +000023 int ret = pthread_mutex_init(&pthread_mutex_, NULL);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000024 assert(ret == 0);
Mark Slee2f6404d2006-10-10 01:37:40 +000025 initialized_ = true;
Marc Slemko66949872006-07-15 01:52:39 +000026 }
27
28 ~impl() {
Mark Slee2f6404d2006-10-10 01:37:40 +000029 if (initialized_) {
30 initialized_ = false;
Aditya Agarwal9dc57402007-03-31 17:45:12 +000031 int ret = pthread_mutex_destroy(&pthread_mutex_);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000032 assert(ret == 0);
Marc Slemko66949872006-07-15 01:52:39 +000033 }
34 }
35
Mark Slee2f6404d2006-10-10 01:37:40 +000036 void lock() const { pthread_mutex_lock(&pthread_mutex_); }
Marc Slemko66949872006-07-15 01:52:39 +000037
boz5362e702007-08-15 20:55:36 +000038 bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); }
39
Mark Slee2f6404d2006-10-10 01:37:40 +000040 void unlock() const { pthread_mutex_unlock(&pthread_mutex_); }
Marc Slemko66949872006-07-15 01:52:39 +000041
Mark Sleef5f2be42006-09-05 21:05:31 +000042 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000043 mutable pthread_mutex_t pthread_mutex_;
44 mutable bool initialized_;
Marc Slemko66949872006-07-15 01:52:39 +000045};
46
Mark Slee2f6404d2006-10-10 01:37:40 +000047Mutex::Mutex() : impl_(new Mutex::impl()) {}
Marc Slemko66949872006-07-15 01:52:39 +000048
Mark Slee2f6404d2006-10-10 01:37:40 +000049void Mutex::lock() const { impl_->lock(); }
Marc Slemko66949872006-07-15 01:52:39 +000050
boz5362e702007-08-15 20:55:36 +000051bool Mutex::trylock() const { return impl_->trylock(); }
52
Mark Slee2f6404d2006-10-10 01:37:40 +000053void Mutex::unlock() const { impl_->unlock(); }
Marc Slemko66949872006-07-15 01:52:39 +000054
bozcce81842007-07-06 22:27:52 +000055/**
56 * Implementation of ReadWriteMutex class using POSIX rw lock
57 *
58 * @author boz
59 * @version $Id:$
60 */
61class ReadWriteMutex::impl {
62public:
63 impl() : initialized_(false) {
64 int ret = pthread_rwlock_init(&rw_lock_, NULL);
65 assert(ret == 0);
66 initialized_ = true;
67 }
68
69 ~impl() {
70 if(initialized_) {
71 initialized_ = false;
72 int ret = pthread_rwlock_destroy(&rw_lock_);
73 assert(ret == 0);
74 }
75 }
76
77 void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); }
78
79 void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); }
80
81 bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); }
82
83 bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); }
84
85 void release() const { pthread_rwlock_unlock(&rw_lock_); }
86
87private:
88 mutable pthread_rwlock_t rw_lock_;
89 mutable bool initialized_;
90};
91
92ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {}
93
94void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); }
95
96void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); }
97
98bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); }
99
100bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); }
101
102void ReadWriteMutex::release() const { impl_->release(); }
103
Marc Slemko66949872006-07-15 01:52:39 +0000104}}} // facebook::thrift::concurrency
105