blob: 045dbdfe2d62d7dd62b034b8caaba29056438cd6 [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
Mark Sleef5f2be42006-09-05 21:05:31 +000055 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000056 mutable pthread_mutex_t pthread_mutex_;
57 mutable bool initialized_;
Marc Slemko66949872006-07-15 01:52:39 +000058};
59
David Reissc6dab612008-06-10 22:55:13 +000060Mutex::Mutex(Initializer init) : impl_(new Mutex::impl(init)) {}
Marc Slemko66949872006-07-15 01:52:39 +000061
Mark Slee2f6404d2006-10-10 01:37:40 +000062void Mutex::lock() const { impl_->lock(); }
Marc Slemko66949872006-07-15 01:52:39 +000063
boz5362e702007-08-15 20:55:36 +000064bool Mutex::trylock() const { return impl_->trylock(); }
65
Mark Slee2f6404d2006-10-10 01:37:40 +000066void Mutex::unlock() const { impl_->unlock(); }
Marc Slemko66949872006-07-15 01:52:39 +000067
David Reissc6dab612008-06-10 22:55:13 +000068void Mutex::DEFAULT_INITIALIZER(void* arg) {
69 pthread_mutex_t* pthread_mutex = (pthread_mutex_t*)arg;
70 int ret = pthread_mutex_init(pthread_mutex, NULL);
71 assert(ret == 0);
72}
73
74static void init_with_kind(pthread_mutex_t* mutex, int kind) {
75 pthread_mutexattr_t mutexattr;
76 int ret = pthread_mutexattr_init(&mutexattr);
77 assert(ret == 0);
78
79 // Apparently, this can fail. Should we really be aborting?
80 ret = pthread_mutexattr_settype(&mutexattr, kind);
81 assert(ret == 0);
82
83 ret = pthread_mutex_init(mutex, &mutexattr);
84 assert(ret == 0);
85
86 ret = pthread_mutexattr_destroy(&mutexattr);
87 assert(ret == 0);
88}
89
90#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
91void Mutex::ADAPTIVE_INITIALIZER(void* arg) {
92 // From mysql source: mysys/my_thr_init.c
93 // Set mutex type to "fast" a.k.a "adaptive"
94 //
95 // In this case the thread may steal the mutex from some other thread
96 // that is waiting for the same mutex. This will save us some
97 // context switches but may cause a thread to 'starve forever' while
98 // waiting for the mutex (not likely if the code within the mutex is
99 // short).
100 init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP);
101}
102#endif
103
104#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
105void Mutex::RECURSIVE_INITIALIZER(void* arg) {
106 init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
107}
108#endif
109
110
David Reiss0c90f6f2008-02-06 22:18:40 +0000111/**
bozcce81842007-07-06 22:27:52 +0000112 * Implementation of ReadWriteMutex class using POSIX rw lock
113 *
bozcce81842007-07-06 22:27:52 +0000114 * @version $Id:$
115 */
116class ReadWriteMutex::impl {
117public:
118 impl() : initialized_(false) {
119 int ret = pthread_rwlock_init(&rw_lock_, NULL);
120 assert(ret == 0);
121 initialized_ = true;
122 }
123
124 ~impl() {
125 if(initialized_) {
126 initialized_ = false;
127 int ret = pthread_rwlock_destroy(&rw_lock_);
128 assert(ret == 0);
129 }
130 }
131
132 void acquireRead() const { pthread_rwlock_rdlock(&rw_lock_); }
133
134 void acquireWrite() const { pthread_rwlock_wrlock(&rw_lock_); }
135
136 bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); }
137
138 bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); }
139
140 void release() const { pthread_rwlock_unlock(&rw_lock_); }
141
David Reiss0c90f6f2008-02-06 22:18:40 +0000142private:
bozcce81842007-07-06 22:27:52 +0000143 mutable pthread_rwlock_t rw_lock_;
144 mutable bool initialized_;
145};
146
147ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {}
148
149void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); }
150
151void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); }
152
153bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); }
154
155bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); }
156
157void ReadWriteMutex::release() const { impl_->release(); }
158
T Jake Lucianib5e62212009-01-31 22:36:20 +0000159}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000160