2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
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.
11 #ifndef WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
12 #define WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
14 #include "webrtc/base/constructormagic.h"
15 #include "webrtc/base/criticalsection.h"
16 #include "webrtc/base/event.h"
20 // This class provides shared-exclusive lock. It can be used in cases like
21 // multiple-readers/single-writer model.
22 class LOCKABLE SharedExclusiveLock {
24 SharedExclusiveLock();
26 // Locking/unlocking methods. It is encouraged to use SharedScope or
27 // ExclusiveScope for protection.
28 void LockExclusive() EXCLUSIVE_LOCK_FUNCTION();
29 void UnlockExclusive() UNLOCK_FUNCTION();
34 rtc::CriticalSection cs_exclusive_;
35 rtc::CriticalSection cs_shared_;
36 rtc::Event shared_count_is_zero_;
39 DISALLOW_COPY_AND_ASSIGN(SharedExclusiveLock);
42 class SCOPED_LOCKABLE SharedScope {
44 explicit SharedScope(SharedExclusiveLock* lock) SHARED_LOCK_FUNCTION(lock)
49 ~SharedScope() UNLOCK_FUNCTION() { lock_->UnlockShared(); }
52 SharedExclusiveLock* lock_;
54 DISALLOW_COPY_AND_ASSIGN(SharedScope);
57 class SCOPED_LOCKABLE ExclusiveScope {
59 explicit ExclusiveScope(SharedExclusiveLock* lock)
60 EXCLUSIVE_LOCK_FUNCTION(lock)
62 lock_->LockExclusive();
65 ~ExclusiveScope() UNLOCK_FUNCTION() { lock_->UnlockExclusive(); }
68 SharedExclusiveLock* lock_;
70 DISALLOW_COPY_AND_ASSIGN(ExclusiveScope);
75 #endif // WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_