blob: dd35edb104bd103797d8ec4964ca92f7f26f33bd [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
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_MUTEX_H_
8#define _THRIFT_CONCURRENCY_MUTEX_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00009
boz19cee902007-09-22 23:08:11 +000010#include <boost/shared_ptr.hpp>
11using boost::shared_ptr;
12
Marc Slemko66949872006-07-15 01:52:39 +000013namespace facebook { namespace thrift { namespace concurrency {
14
Mark Sleef5f2be42006-09-05 21:05:31 +000015/**
16 * A simple mutex class
17 *
18 * @author marc
19 * @version $Id:$
20 */
Marc Slemko66949872006-07-15 01:52:39 +000021class Mutex {
Marc Slemko66949872006-07-15 01:52:39 +000022 public:
Marc Slemko66949872006-07-15 01:52:39 +000023 Mutex();
boz19cee902007-09-22 23:08:11 +000024 virtual ~Mutex() {}
Marc Slemko66949872006-07-15 01:52:39 +000025 virtual void lock() const;
boz5362e702007-08-15 20:55:36 +000026 virtual bool trylock() const;
Marc Slemko66949872006-07-15 01:52:39 +000027 virtual void unlock() const;
28
29 private:
boz19cee902007-09-22 23:08:11 +000030
Marc Slemko66949872006-07-15 01:52:39 +000031 class impl;
boz19cee902007-09-22 23:08:11 +000032 shared_ptr<impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +000033};
34
bozcce81842007-07-06 22:27:52 +000035class ReadWriteMutex {
36public:
37 ReadWriteMutex();
boz19cee902007-09-22 23:08:11 +000038 virtual ~ReadWriteMutex() {}
bozcce81842007-07-06 22:27:52 +000039
40 // these get the lock and block until it is done successfully
41 virtual void acquireRead() const;
42 virtual void acquireWrite() const;
43
44 // these attempt to get the lock, returning false immediately if they fail
45 virtual bool attemptRead() const;
46 virtual bool attemptWrite() const;
47
48 // this releases both read and write locks
49 virtual void release() const;
50
51private:
boz19cee902007-09-22 23:08:11 +000052
bozcce81842007-07-06 22:27:52 +000053 class impl;
boz19cee902007-09-22 23:08:11 +000054 shared_ptr<impl> impl_;
bozcce81842007-07-06 22:27:52 +000055};
56
Mark Slee85287d32007-07-09 19:50:30 +000057class Guard {
Mark Sleef5f2be42006-09-05 21:05:31 +000058 public:
bozcce81842007-07-06 22:27:52 +000059 Guard(const Mutex& value) : mutex_(value) {
Mark Slee2f6404d2006-10-10 01:37:40 +000060 mutex_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000061 }
bozcce81842007-07-06 22:27:52 +000062 ~Guard() {
Mark Slee2f6404d2006-10-10 01:37:40 +000063 mutex_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000064 }
65
66 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000067 const Mutex& mutex_;
Marc Slemko66949872006-07-15 01:52:39 +000068};
69
yunfanga36f5db2007-07-14 01:23:05 +000070class RWGuard {
71 public:
72 RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) {
73 if (write) {
74 rw_mutex_.acquireWrite();
David Reiss96d23882007-07-26 21:10:32 +000075 } else {
yunfanga36f5db2007-07-14 01:23:05 +000076 rw_mutex_.acquireRead();
David Reiss96d23882007-07-26 21:10:32 +000077 }
yunfanga36f5db2007-07-14 01:23:05 +000078 }
79 ~RWGuard() {
80 rw_mutex_.release();
81 }
82 private:
boz19cee902007-09-22 23:08:11 +000083 const ReadWriteMutex& rw_mutex_;
yunfanga36f5db2007-07-14 01:23:05 +000084};
85
Marc Slemko66949872006-07-15 01:52:39 +000086
87}}} // facebook::thrift::concurrency
88
Mark Sleef5f2be42006-09-05 21:05:31 +000089#endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_