blob: a6ca5735a21fa3b5b7114f5e3433ca86e6c24964 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
18namespace rtc {
19
20// This class provides shared-exclusive lock. It can be used in cases like
21// multiple-readers/single-writer model.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000022class LOCKABLE SharedExclusiveLock {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023 public:
24 SharedExclusiveLock();
25
26 // Locking/unlocking methods. It is encouraged to use SharedScope or
27 // ExclusiveScope for protection.
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000028 void LockExclusive() EXCLUSIVE_LOCK_FUNCTION();
29 void UnlockExclusive() UNLOCK_FUNCTION();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 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
henrikg3c089d72015-09-16 05:37:44 -070039 RTC_DISALLOW_COPY_AND_ASSIGN(SharedExclusiveLock);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040};
41
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000042class SCOPED_LOCKABLE SharedScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 public:
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000044 explicit SharedScope(SharedExclusiveLock* lock) SHARED_LOCK_FUNCTION(lock)
45 : lock_(lock) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046 lock_->LockShared();
47 }
48
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000049 ~SharedScope() UNLOCK_FUNCTION() { lock_->UnlockShared(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 private:
52 SharedExclusiveLock* lock_;
53
henrikg3c089d72015-09-16 05:37:44 -070054 RTC_DISALLOW_COPY_AND_ASSIGN(SharedScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055};
56
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000057class SCOPED_LOCKABLE ExclusiveScope {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 public:
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000059 explicit ExclusiveScope(SharedExclusiveLock* lock)
60 EXCLUSIVE_LOCK_FUNCTION(lock)
61 : lock_(lock) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 lock_->LockExclusive();
63 }
64
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +000065 ~ExclusiveScope() UNLOCK_FUNCTION() { lock_->UnlockExclusive(); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
67 private:
68 SharedExclusiveLock* lock_;
69
henrikg3c089d72015-09-16 05:37:44 -070070 RTC_DISALLOW_COPY_AND_ASSIGN(ExclusiveScope);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071};
72
73} // namespace rtc
74
75#endif // WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_