Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // 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 Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |
8 | #define _THRIFT_CONCURRENCY_MUTEX_H_ 1 | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 9 | |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 10 | #include <boost/shared_ptr.hpp> |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 11 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 12 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 13 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 14 | /** |
15 | * A simple mutex class | ||||
16 | * | ||||
17 | * @author marc | ||||
18 | * @version $Id:$ | ||||
19 | */ | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | class Mutex { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 21 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | Mutex(); |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 23 | virtual ~Mutex() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 24 | virtual void lock() const; |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 25 | virtual bool trylock() const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | virtual void unlock() const; |
27 | |||||
28 | private: | ||||
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 29 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 30 | class impl; |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 31 | boost::shared_ptr<impl> impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 32 | }; |
33 | |||||
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 34 | class ReadWriteMutex { |
35 | public: | ||||
36 | ReadWriteMutex(); | ||||
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 37 | virtual ~ReadWriteMutex() {} |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 38 | |
39 | // these get the lock and block until it is done successfully | ||||
40 | virtual void acquireRead() const; | ||||
41 | virtual void acquireWrite() const; | ||||
42 | |||||
43 | // these attempt to get the lock, returning false immediately if they fail | ||||
44 | virtual bool attemptRead() const; | ||||
45 | virtual bool attemptWrite() const; | ||||
46 | |||||
47 | // this releases both read and write locks | ||||
48 | virtual void release() const; | ||||
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 49 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 50 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 51 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 52 | class impl; |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 53 | boost::shared_ptr<impl> impl_; |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 54 | }; |
55 | |||||
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 56 | class Guard { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 57 | public: |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 58 | Guard(const Mutex& value) : mutex_(value) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 59 | mutex_.lock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 60 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 61 | ~Guard() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 62 | mutex_.unlock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 63 | } |
64 | |||||
65 | private: | ||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 66 | const Mutex& mutex_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | }; |
68 | |||||
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 69 | class RWGuard { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 70 | public: |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 71 | RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) { |
72 | if (write) { | ||||
73 | rw_mutex_.acquireWrite(); | ||||
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 74 | } else { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 75 | rw_mutex_.acquireRead(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 76 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 77 | } |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 78 | ~RWGuard() { |
79 | rw_mutex_.release(); | ||||
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 80 | } |
81 | private: | ||||
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 82 | const ReadWriteMutex& rw_mutex_; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 83 | }; |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 84 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 85 | |
David Reiss | eaa61e4 | 2007-12-20 21:42:05 +0000 | [diff] [blame] | 86 | // A little hack to prevent someone from trying to do "Guard(m);" |
87 | // Sorry for polluting the global namespace, but I think it's worth it. | ||||
88 | #define Guard(m) incorrect_use_of_Guard(m) | ||||
89 | #define RWGuard(m) incorrect_use_of_RWGuard(m) | ||||
90 | |||||
91 | |||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 92 | }}} // facebook::thrift::concurrency |
93 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 94 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |