blob: b19e3c51078ad967d4cdd325ec179834bdb20013 [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
10namespace facebook { namespace thrift { namespace concurrency {
11
Mark Sleef5f2be42006-09-05 21:05:31 +000012/**
13 * A simple mutex class
14 *
15 * @author marc
16 * @version $Id:$
17 */
Marc Slemko66949872006-07-15 01:52:39 +000018class Mutex {
Marc Slemko66949872006-07-15 01:52:39 +000019 public:
Marc Slemko66949872006-07-15 01:52:39 +000020 Mutex();
boz62309672007-09-20 23:24:16 +000021 virtual ~Mutex();
Marc Slemko66949872006-07-15 01:52:39 +000022 virtual void lock() const;
boz5362e702007-08-15 20:55:36 +000023 virtual bool trylock() const;
Marc Slemko66949872006-07-15 01:52:39 +000024 virtual void unlock() const;
25
26 private:
Marc Slemko66949872006-07-15 01:52:39 +000027 class impl;
Mark Slee2f6404d2006-10-10 01:37:40 +000028 impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000029};
30
bozcce81842007-07-06 22:27:52 +000031class ReadWriteMutex {
32public:
33 ReadWriteMutex();
boz62309672007-09-20 23:24:16 +000034 virtual ~ReadWriteMutex();
bozcce81842007-07-06 22:27:52 +000035
36 // these get the lock and block until it is done successfully
37 virtual void acquireRead() const;
38 virtual void acquireWrite() const;
39
40 // these attempt to get the lock, returning false immediately if they fail
41 virtual bool attemptRead() const;
42 virtual bool attemptWrite() const;
43
44 // this releases both read and write locks
45 virtual void release() const;
46
47private:
48 class impl;
49 impl* impl_;
50};
51
Mark Slee85287d32007-07-09 19:50:30 +000052class Guard {
Mark Sleef5f2be42006-09-05 21:05:31 +000053 public:
bozcce81842007-07-06 22:27:52 +000054 Guard(const Mutex& value) : mutex_(value) {
Mark Slee2f6404d2006-10-10 01:37:40 +000055 mutex_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000056 }
bozcce81842007-07-06 22:27:52 +000057 ~Guard() {
Mark Slee2f6404d2006-10-10 01:37:40 +000058 mutex_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000059 }
60
61 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000062 const Mutex& mutex_;
Marc Slemko66949872006-07-15 01:52:39 +000063};
64
yunfanga36f5db2007-07-14 01:23:05 +000065class RWGuard {
66 public:
67 RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) {
68 if (write) {
69 rw_mutex_.acquireWrite();
David Reiss96d23882007-07-26 21:10:32 +000070 } else {
yunfanga36f5db2007-07-14 01:23:05 +000071 rw_mutex_.acquireRead();
David Reiss96d23882007-07-26 21:10:32 +000072 }
yunfanga36f5db2007-07-14 01:23:05 +000073 }
74 ~RWGuard() {
75 rw_mutex_.release();
76 }
77 private:
78 const ReadWriteMutex rw_mutex_;
79};
80
Marc Slemko66949872006-07-15 01:52:39 +000081
82}}} // facebook::thrift::concurrency
83
Mark Sleef5f2be42006-09-05 21:05:31 +000084#endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_