henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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_BASE_SHAREDEXCLUSIVELOCK_H_ |
| 12 | #define WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_ |
| 13 | |
| 14 | #include "webrtc/base/constructormagic.h" |
| 15 | #include "webrtc/base/criticalsection.h" |
| 16 | #include "webrtc/base/event.h" |
| 17 | |
| 18 | namespace rtc { |
| 19 | |
| 20 | // This class provides shared-exclusive lock. It can be used in cases like |
| 21 | // multiple-readers/single-writer model. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 22 | class LOCKABLE SharedExclusiveLock { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | public: |
| 24 | SharedExclusiveLock(); |
| 25 | |
| 26 | // Locking/unlocking methods. It is encouraged to use SharedScope or |
| 27 | // ExclusiveScope for protection. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 28 | void LockExclusive() EXCLUSIVE_LOCK_FUNCTION(); |
| 29 | void UnlockExclusive() UNLOCK_FUNCTION(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | void LockShared(); |
| 31 | void UnlockShared(); |
| 32 | |
| 33 | private: |
| 34 | rtc::CriticalSection cs_exclusive_; |
| 35 | rtc::CriticalSection cs_shared_; |
| 36 | rtc::Event shared_count_is_zero_; |
| 37 | int shared_count_; |
| 38 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame^] | 39 | RTC_DISALLOW_COPY_AND_ASSIGN(SharedExclusiveLock); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 42 | class SCOPED_LOCKABLE SharedScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | public: |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 44 | explicit SharedScope(SharedExclusiveLock* lock) SHARED_LOCK_FUNCTION(lock) |
| 45 | : lock_(lock) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | lock_->LockShared(); |
| 47 | } |
| 48 | |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 49 | ~SharedScope() UNLOCK_FUNCTION() { lock_->UnlockShared(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | SharedExclusiveLock* lock_; |
| 53 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame^] | 54 | RTC_DISALLOW_COPY_AND_ASSIGN(SharedScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 57 | class SCOPED_LOCKABLE ExclusiveScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | public: |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 59 | explicit ExclusiveScope(SharedExclusiveLock* lock) |
| 60 | EXCLUSIVE_LOCK_FUNCTION(lock) |
| 61 | : lock_(lock) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | lock_->LockExclusive(); |
| 63 | } |
| 64 | |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 65 | ~ExclusiveScope() UNLOCK_FUNCTION() { lock_->UnlockExclusive(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | SharedExclusiveLock* lock_; |
| 69 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame^] | 70 | RTC_DISALLOW_COPY_AND_ASSIGN(ExclusiveScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace rtc |
| 74 | |
| 75 | #endif // WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_ |