blob: 39f52fca35bd468e29dff2857fac80523fcea96e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
Karl Wiberg2b857922018-03-23 14:53:54 +010011#ifndef RTC_BASE_SYNCHRONIZATION_RW_LOCK_WRAPPER_H_
12#define RTC_BASE_SYNCHRONIZATION_RW_LOCK_WRAPPER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/thread_annotations.h"
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000015
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000016// Note, Windows pre-Vista version of RW locks are not supported natively. For
17// these OSs regular critical sections have been used to approximate RW lock
18// functionality and will therefore have worse performance.
19
niklase@google.com470e71d2011-07-07 08:21:25 +000020namespace webrtc {
henrike@webrtc.org9f847232012-09-25 20:27:51 +000021
danilchapa37de392017-09-09 04:17:22 -070022class RTC_LOCKABLE RWLockWrapper {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000023 public:
24 static RWLockWrapper* CreateRWLock();
25 virtual ~RWLockWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000026
danilchapa37de392017-09-09 04:17:22 -070027 virtual void AcquireLockExclusive() RTC_EXCLUSIVE_LOCK_FUNCTION() = 0;
28 virtual void ReleaseLockExclusive() RTC_UNLOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
danilchapa37de392017-09-09 04:17:22 -070030 virtual void AcquireLockShared() RTC_SHARED_LOCK_FUNCTION() = 0;
31 virtual void ReleaseLockShared() RTC_UNLOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000032};
33
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000034// RAII extensions of the RW lock. Prevents Acquire/Release missmatches and
35// provides more compact locking syntax.
danilchapa37de392017-09-09 04:17:22 -070036class RTC_SCOPED_LOCKABLE ReadLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000037 public:
Karl Wiberg2b857922018-03-23 14:53:54 +010038 explicit ReadLockScoped(RWLockWrapper& rw_lock)
39 RTC_SHARED_LOCK_FUNCTION(rw_lock)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000040 : rw_lock_(rw_lock) {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000041 rw_lock_.AcquireLockShared();
42 }
niklase@google.com470e71d2011-07-07 08:21:25 +000043
danilchapa37de392017-09-09 04:17:22 -070044 ~ReadLockScoped() RTC_UNLOCK_FUNCTION() { rw_lock_.ReleaseLockShared(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000045
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000046 private:
47 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000048};
49
danilchapa37de392017-09-09 04:17:22 -070050class RTC_SCOPED_LOCKABLE WriteLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000051 public:
Karl Wiberg2b857922018-03-23 14:53:54 +010052 explicit WriteLockScoped(RWLockWrapper& rw_lock)
53 RTC_EXCLUSIVE_LOCK_FUNCTION(rw_lock)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000054 : rw_lock_(rw_lock) {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000055 rw_lock_.AcquireLockExclusive();
56 }
niklase@google.com470e71d2011-07-07 08:21:25 +000057
danilchapa37de392017-09-09 04:17:22 -070058 ~WriteLockScoped() RTC_UNLOCK_FUNCTION() { rw_lock_.ReleaseLockExclusive(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000059
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000060 private:
61 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000062};
henrike@webrtc.org9f847232012-09-25 20:27:51 +000063
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000064} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000065
Karl Wiberg2b857922018-03-23 14:53:54 +010066#endif // RTC_BASE_SYNCHRONIZATION_RW_LOCK_WRAPPER_H_