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 | |
| 14 | // Note, Windows pre-Vista version of RW locks are not supported nativly. For |
| 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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | class RWLockWrapper |
| 21 | { |
| 22 | public: |
| 23 | static RWLockWrapper* CreateRWLock(); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 24 | virtual ~RWLockWrapper() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
| 26 | virtual void AcquireLockExclusive() = 0; |
| 27 | virtual void ReleaseLockExclusive() = 0; |
| 28 | |
| 29 | virtual void AcquireLockShared() = 0; |
| 30 | virtual void ReleaseLockShared() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | // RAII extensions of the RW lock. Prevents Acquire/Release missmatches and |
| 34 | // provides more compact locking syntax. |
| 35 | class ReadLockScoped |
| 36 | { |
| 37 | public: |
| 38 | ReadLockScoped(RWLockWrapper& rwLock) |
| 39 | : |
| 40 | _rwLock(rwLock) |
| 41 | { |
| 42 | _rwLock.AcquireLockShared(); |
| 43 | } |
| 44 | |
| 45 | ~ReadLockScoped() |
| 46 | { |
| 47 | _rwLock.ReleaseLockShared(); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | RWLockWrapper& _rwLock; |
| 52 | }; |
| 53 | |
| 54 | class WriteLockScoped |
| 55 | { |
| 56 | public: |
| 57 | WriteLockScoped(RWLockWrapper& rwLock) |
| 58 | : |
| 59 | _rwLock(rwLock) |
| 60 | { |
| 61 | _rwLock.AcquireLockExclusive(); |
| 62 | } |
| 63 | |
| 64 | ~WriteLockScoped() |
| 65 | { |
| 66 | _rwLock.ReleaseLockExclusive(); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | RWLockWrapper& _rwLock; |
| 71 | }; |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 72 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | } // namespace webrtc |
| 74 | |
| 75 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ |