niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ |
| 13 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 14 | // Note, Windows pre-Vista version of RW locks are not supported natively. For |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | // these OSs regular critical sections have been used to approximate RW lock |
| 16 | // functionality and will therefore have worse performance. |
| 17 | |
| 18 | namespace webrtc { |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 19 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 20 | class RWLockWrapper { |
| 21 | public: |
| 22 | static RWLockWrapper* CreateRWLock(); |
| 23 | virtual ~RWLockWrapper() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 25 | virtual void AcquireLockExclusive() = 0; |
| 26 | virtual void ReleaseLockExclusive() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 28 | virtual void AcquireLockShared() = 0; |
| 29 | virtual void ReleaseLockShared() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | // RAII extensions of the RW lock. Prevents Acquire/Release missmatches and |
| 33 | // provides more compact locking syntax. |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 34 | class ReadLockScoped { |
| 35 | public: |
| 36 | ReadLockScoped(RWLockWrapper& rw_lock) |
| 37 | : |
| 38 | rw_lock_(rw_lock) { |
| 39 | rw_lock_.AcquireLockShared(); |
| 40 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 42 | ~ReadLockScoped() { |
| 43 | rw_lock_.ReleaseLockShared(); |
| 44 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 46 | private: |
| 47 | RWLockWrapper& rw_lock_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 50 | class WriteLockScoped { |
| 51 | public: |
| 52 | WriteLockScoped(RWLockWrapper& rw_lock) |
| 53 | : |
| 54 | rw_lock_(rw_lock) { |
| 55 | rw_lock_.AcquireLockExclusive(); |
| 56 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 58 | ~WriteLockScoped() { |
| 59 | rw_lock_.ReleaseLockExclusive(); |
| 60 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 62 | private: |
| 63 | RWLockWrapper& rw_lock_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | }; |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 65 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | } // namespace webrtc |
| 67 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame^] | 68 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ |