blob: 5d33c114f9bf01cf0de8a0439fed7ae68949f847 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Marc Slemko66949872006-07-15 01:52:39 +000020#include "Mutex.h"
21
22#include <assert.h>
23#include <pthread.h>
24
yunfang14542962007-10-03 22:59:41 +000025using boost::shared_ptr;
26
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000028
David Reiss0c90f6f2008-02-06 22:18:40 +000029/**
Mark Sleef5f2be42006-09-05 21:05:31 +000030 * Implementation of Mutex class using POSIX mutex
31 *
Mark Sleef5f2be42006-09-05 21:05:31 +000032 * @version $Id:$
33 */
Marc Slemko66949872006-07-15 01:52:39 +000034class Mutex::impl {
Mark Sleef5f2be42006-09-05 21:05:31 +000035 public:
David Reissc6dab612008-06-10 22:55:13 +000036 impl(Initializer init) : initialized_(false) {
37 init(&pthread_mutex_);
Mark Slee2f6404d2006-10-10 01:37:40 +000038 initialized_ = true;
Marc Slemko66949872006-07-15 01:52:39 +000039 }
40
41 ~impl() {
Mark Slee2f6404d2006-10-10 01:37:40 +000042 if (initialized_) {
43 initialized_ = false;
Aditya Agarwal9dc57402007-03-31 17:45:12 +000044 int ret = pthread_mutex_destroy(&pthread_mutex_);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000045 assert(ret == 0);
Marc Slemko66949872006-07-15 01:52:39 +000046 }
47 }
48
Mark Slee2f6404d2006-10-10 01:37:40 +000049 void lock() const { pthread_mutex_lock(&pthread_mutex_); }
Marc Slemko66949872006-07-15 01:52:39 +000050
boz5362e702007-08-15 20:55:36 +000051 bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); }
52
Mark Slee2f6404d2006-10-10 01:37:40 +000053 void unlock() const { pthread_mutex_unlock(&pthread_mutex_); }
Marc Slemko66949872006-07-15 01:52:39 +000054
David Reissb9db49c2010-03-09 05:19:30 +000055 void* getUnderlyingImpl() const { return (void*) &pthread_mutex_; }
56
Mark Sleef5f2be42006-09-05 21:05:31 +000057 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000058 mutable pthread_mutex_t pthread_mutex_;
59 mutable bool initialized_;
Marc Slemko66949872006-07-15 01:52:39 +000060};
61
David Reissc6dab612008-06-10 22:55:13 +000062Mutex::Mutex(Initializer init) : impl_(new Mutex::impl(init)) {}
Marc Slemko66949872006-07-15 01:52:39 +000063
David Reissb9db49c2010-03-09 05:19:30 +000064void* Mutex::getUnderlyingImpl() const { return impl_->getUnderlyingImpl(); }
65
Mark Slee2f6404d2006-10-10 01:37:40 +000066void Mutex::lock() const { impl_->lock(); }
Marc Slemko66949872006-07-15 01:52:39 +000067
boz5362e702007-08-15 20:55:36 +000068bool Mutex::trylock() const { return impl_->trylock(); }
69
Mark Slee2f6404d2006-10-10 01:37:40 +000070void Mutex::unlock() const { impl_->unlock(); }
Marc Slemko66949872006-07-15 01:52:39 +000071
David Reissc6dab612008-06-10 22:55:13 +000072void Mutex::DEFAULT_INITIALIZER(void* arg) {
73 pthread_mutex_t* pthread_mutex = (pthread_mutex_t*)arg;
74 int ret = pthread_mutex_init(pthread_mutex, NULL);
75 assert(ret == 0);
76}
77
78static void init_with_kind(pthread_mutex_t* mutex, int kind) {
79 pthread_mutexattr_t mutexattr;
80 int ret = pthread_mutexattr_init(&mutexattr);
81 assert(ret == 0);
82
83 // Apparently, this can fail. Should we really be aborting?
84 ret = pthread_mutexattr_settype(&mutexattr, kind);
85 assert(ret == 0);
86
87 ret = pthread_mutex_init(mutex, &mutexattr);
88 assert(ret == 0);
89
90 ret = pthread_mutexattr_destroy(&mutexattr);
91 assert(ret == 0);
92}
93
94#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
95void Mutex::ADAPTIVE_INITIALIZER(void* arg) {
96 // From mysql source: mysys/my_thr_init.c
97 // Set mutex type to "fast" a.k.a "adaptive"
98 //
99 // In this case the thread may steal the mutex from some other thread
100 // that is waiting for the same mutex. This will save us some
101 // context switches but may cause a thread to 'starve forever' while
102 // waiting for the mutex (not likely if the code within the mutex is
103 // short).
104 init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP);
105}
106#endif
107
108#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
109void Mutex::RECURSIVE_INITIALIZER(void* arg) {
110 init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
111}
112#endif
113
114
David Reiss0c90f6f2008-02-06 22:18:40 +0000115/**
bozcce81842007-07-06 22:27:52 +0000116 * Implementation of ReadWriteMutex class using POSIX rw lock
117 *
bozcce81842007-07-06 22:27:52 +0000118 * @version $Id:$
119 */
120class ReadWriteMutex::impl {
121public:
122 impl() : initialized_(false) {
123 int ret = pthread_rwlock_init(&rw_lock_, NULL);
124 assert(ret == 0);
125 initialized_ = true;
126 }
127
128 ~impl() {
129 if(initialized_) {
130 initialized_ = false;
131 int ret = pthread_rwlock_destroy(&rw_lock_);
132 assert(ret == 0);
133 }
134 }
135
136 void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); }
137
138 void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); }
139
140 bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); }
141
142 bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); }
143
144 void release() const { pthread_rwlock_unlock(&rw_lock_); }
145
David Reiss0c90f6f2008-02-06 22:18:40 +0000146private:
bozcce81842007-07-06 22:27:52 +0000147 mutable pthread_rwlock_t rw_lock_;
148 mutable bool initialized_;
149};
150
151ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {}
152
153void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); }
154
155void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); }
156
157bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); }
158
159bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); }
160
161void ReadWriteMutex::release() const { impl_->release(); }
162
T Jake Lucianib5e62212009-01-31 22:36:20 +0000163}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000164