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 | |
10 | namespace facebook { namespace thrift { namespace concurrency { | ||||
11 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 12 | /** |
13 | * A simple mutex class | ||||
14 | * | ||||
15 | * @author marc | ||||
16 | * @version $Id:$ | ||||
17 | */ | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 18 | class Mutex { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 19 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | Mutex(); |
boz | 6230967 | 2007-09-20 23:24:16 +0000 | [diff] [blame^] | 21 | virtual ~Mutex(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | virtual void lock() const; |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 23 | virtual bool trylock() const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 24 | virtual void unlock() const; |
25 | |||||
26 | private: | ||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 27 | class impl; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 28 | impl* impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 29 | }; |
30 | |||||
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 31 | class ReadWriteMutex { |
32 | public: | ||||
33 | ReadWriteMutex(); | ||||
boz | 6230967 | 2007-09-20 23:24:16 +0000 | [diff] [blame^] | 34 | virtual ~ReadWriteMutex(); |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 35 | |
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 | |||||
47 | private: | ||||
48 | class impl; | ||||
49 | impl* impl_; | ||||
50 | }; | ||||
51 | |||||
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 52 | class Guard { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 53 | public: |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 54 | Guard(const Mutex& value) : mutex_(value) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 55 | mutex_.lock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 56 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 57 | ~Guard() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 58 | mutex_.unlock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 59 | } |
60 | |||||
61 | private: | ||||
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 62 | const Mutex& mutex_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 63 | }; |
64 | |||||
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 65 | class RWGuard { |
66 | public: | ||||
67 | RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) { | ||||
68 | if (write) { | ||||
69 | rw_mutex_.acquireWrite(); | ||||
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 70 | } else { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 71 | rw_mutex_.acquireRead(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 72 | } |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 73 | } |
74 | ~RWGuard() { | ||||
75 | rw_mutex_.release(); | ||||
76 | } | ||||
77 | private: | ||||
78 | const ReadWriteMutex rw_mutex_; | ||||
79 | }; | ||||
80 | |||||
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 81 | |
82 | }}} // facebook::thrift::concurrency | ||||
83 | |||||
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 84 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |