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