blob: 73c73e0e98fb1d589691f41dd7bdfef3cf255d7b [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
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_MUTEX_H_
21#define _THRIFT_CONCURRENCY_MUTEX_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +000022
boz19cee902007-09-22 23:08:11 +000023#include <boost/shared_ptr.hpp>
boz19cee902007-09-22 23:08:11 +000024
T Jake Lucianib5e62212009-01-31 22:36:20 +000025namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000026
Mark Sleef5f2be42006-09-05 21:05:31 +000027/**
28 * A simple mutex class
29 *
Mark Sleef5f2be42006-09-05 21:05:31 +000030 * @version $Id:$
31 */
Marc Slemko66949872006-07-15 01:52:39 +000032class Mutex {
Marc Slemko66949872006-07-15 01:52:39 +000033 public:
David Reissc6dab612008-06-10 22:55:13 +000034 typedef void (*Initializer)(void*);
35
36 Mutex(Initializer init = DEFAULT_INITIALIZER);
boz19cee902007-09-22 23:08:11 +000037 virtual ~Mutex() {}
Marc Slemko66949872006-07-15 01:52:39 +000038 virtual void lock() const;
boz5362e702007-08-15 20:55:36 +000039 virtual bool trylock() const;
Marc Slemko66949872006-07-15 01:52:39 +000040 virtual void unlock() const;
41
David Reissc6dab612008-06-10 22:55:13 +000042 static void DEFAULT_INITIALIZER(void*);
David Reissc6dab612008-06-10 22:55:13 +000043 static void ADAPTIVE_INITIALIZER(void*);
David Reissc6dab612008-06-10 22:55:13 +000044 static void RECURSIVE_INITIALIZER(void*);
David Reissc6dab612008-06-10 22:55:13 +000045
Marc Slemko66949872006-07-15 01:52:39 +000046 private:
boz19cee902007-09-22 23:08:11 +000047
Marc Slemko66949872006-07-15 01:52:39 +000048 class impl;
yunfang14542962007-10-03 22:59:41 +000049 boost::shared_ptr<impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +000050};
51
bozcce81842007-07-06 22:27:52 +000052class ReadWriteMutex {
53public:
54 ReadWriteMutex();
boz19cee902007-09-22 23:08:11 +000055 virtual ~ReadWriteMutex() {}
bozcce81842007-07-06 22:27:52 +000056
57 // these get the lock and block until it is done successfully
58 virtual void acquireRead() const;
59 virtual void acquireWrite() const;
60
61 // these attempt to get the lock, returning false immediately if they fail
62 virtual bool attemptRead() const;
63 virtual bool attemptWrite() const;
64
65 // this releases both read and write locks
66 virtual void release() const;
David Reiss0c90f6f2008-02-06 22:18:40 +000067
bozcce81842007-07-06 22:27:52 +000068private:
boz19cee902007-09-22 23:08:11 +000069
bozcce81842007-07-06 22:27:52 +000070 class impl;
yunfang14542962007-10-03 22:59:41 +000071 boost::shared_ptr<impl> impl_;
bozcce81842007-07-06 22:27:52 +000072};
73
Mark Slee85287d32007-07-09 19:50:30 +000074class Guard {
David Reiss0c90f6f2008-02-06 22:18:40 +000075 public:
bozcce81842007-07-06 22:27:52 +000076 Guard(const Mutex& value) : mutex_(value) {
Mark Slee2f6404d2006-10-10 01:37:40 +000077 mutex_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000078 }
bozcce81842007-07-06 22:27:52 +000079 ~Guard() {
Mark Slee2f6404d2006-10-10 01:37:40 +000080 mutex_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000081 }
82
83 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000084 const Mutex& mutex_;
Marc Slemko66949872006-07-15 01:52:39 +000085};
86
yunfanga36f5db2007-07-14 01:23:05 +000087class RWGuard {
David Reiss0c90f6f2008-02-06 22:18:40 +000088 public:
yunfanga36f5db2007-07-14 01:23:05 +000089 RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) {
90 if (write) {
91 rw_mutex_.acquireWrite();
David Reiss96d23882007-07-26 21:10:32 +000092 } else {
yunfanga36f5db2007-07-14 01:23:05 +000093 rw_mutex_.acquireRead();
David Reiss96d23882007-07-26 21:10:32 +000094 }
David Reiss0c90f6f2008-02-06 22:18:40 +000095 }
yunfanga36f5db2007-07-14 01:23:05 +000096 ~RWGuard() {
97 rw_mutex_.release();
David Reiss0c90f6f2008-02-06 22:18:40 +000098 }
99 private:
boz19cee902007-09-22 23:08:11 +0000100 const ReadWriteMutex& rw_mutex_;
David Reiss0c90f6f2008-02-06 22:18:40 +0000101};
yunfanga36f5db2007-07-14 01:23:05 +0000102
Marc Slemko66949872006-07-15 01:52:39 +0000103
David Reisseaa61e42007-12-20 21:42:05 +0000104// A little hack to prevent someone from trying to do "Guard(m);"
David Reissbe378f22009-05-07 00:41:18 +0000105// Such a use is invalid because the temporary Guard object is
106// destoryed at the end of the line, releasing the lock.
David Reisseaa61e42007-12-20 21:42:05 +0000107// Sorry for polluting the global namespace, but I think it's worth it.
108#define Guard(m) incorrect_use_of_Guard(m)
109#define RWGuard(m) incorrect_use_of_RWGuard(m)
110
111
T Jake Lucianib5e62212009-01-31 22:36:20 +0000112}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000113
Mark Sleef5f2be42006-09-05 21:05:31 +0000114#endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_