blob: 02accf90d85f930c38f22ae9d959210966b47ad7 [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();
Marc Slemko66949872006-07-15 01:52:39 +000021 virtual ~Mutex() {}
Marc Slemko66949872006-07-15 01:52:39 +000022 virtual void lock() const;
Marc Slemko66949872006-07-15 01:52:39 +000023 virtual void unlock() const;
24
25 private:
Marc Slemko66949872006-07-15 01:52:39 +000026 class impl;
Mark Slee2f6404d2006-10-10 01:37:40 +000027 impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000028};
29
bozcce81842007-07-06 22:27:52 +000030class ReadWriteMutex {
31public:
32 ReadWriteMutex();
33 virtual ~ReadWriteMutex() {}
34
35 // these get the lock and block until it is done successfully
36 virtual void acquireRead() const;
37 virtual void acquireWrite() const;
38
39 // these attempt to get the lock, returning false immediately if they fail
40 virtual bool attemptRead() const;
41 virtual bool attemptWrite() const;
42
43 // this releases both read and write locks
44 virtual void release() const;
45
46private:
47 class impl;
48 impl* impl_;
49};
50
Mark Slee85287d32007-07-09 19:50:30 +000051class Guard {
Mark Sleef5f2be42006-09-05 21:05:31 +000052 public:
bozcce81842007-07-06 22:27:52 +000053 Guard(const Mutex& value) : mutex_(value) {
Mark Slee2f6404d2006-10-10 01:37:40 +000054 mutex_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000055 }
bozcce81842007-07-06 22:27:52 +000056 ~Guard() {
Mark Slee2f6404d2006-10-10 01:37:40 +000057 mutex_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000058 }
59
60 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000061 const Mutex& mutex_;
Marc Slemko66949872006-07-15 01:52:39 +000062};
63
yunfanga36f5db2007-07-14 01:23:05 +000064class RWGuard {
65 public:
66 RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) {
67 if (write) {
68 rw_mutex_.acquireWrite();
David Reiss96d23882007-07-26 21:10:32 +000069 } else {
yunfanga36f5db2007-07-14 01:23:05 +000070 rw_mutex_.acquireRead();
David Reiss96d23882007-07-26 21:10:32 +000071 }
yunfanga36f5db2007-07-14 01:23:05 +000072 }
73 ~RWGuard() {
74 rw_mutex_.release();
75 }
76 private:
77 const ReadWriteMutex rw_mutex_;
78};
79
Marc Slemko66949872006-07-15 01:52:39 +000080
81}}} // facebook::thrift::concurrency
82
Mark Sleef5f2be42006-09-05 21:05:31 +000083#endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_