blob: 62e75b454722fad419d273d9460b871ab8d8e606 [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
kjellanderd56d68c2015-11-02 02:12:41 -080011#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_RW_LOCK_WRAPPER_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_RW_LOCK_WRAPPER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Edward Lemurc20978e2017-07-06 19:44:34 +020014#include "webrtc/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:
danilchapa37de392017-09-09 04:17:22 -070038 ReadLockScoped(RWLockWrapper& rw_lock) RTC_SHARED_LOCK_FUNCTION(rw_lock)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000039 : rw_lock_(rw_lock) {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000040 rw_lock_.AcquireLockShared();
41 }
niklase@google.com470e71d2011-07-07 08:21:25 +000042
danilchapa37de392017-09-09 04:17:22 -070043 ~ReadLockScoped() RTC_UNLOCK_FUNCTION() { rw_lock_.ReleaseLockShared(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000044
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000045 private:
46 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000047};
48
danilchapa37de392017-09-09 04:17:22 -070049class RTC_SCOPED_LOCKABLE WriteLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000050 public:
danilchapa37de392017-09-09 04:17:22 -070051 WriteLockScoped(RWLockWrapper& rw_lock) RTC_EXCLUSIVE_LOCK_FUNCTION(rw_lock)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000052 : rw_lock_(rw_lock) {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000053 rw_lock_.AcquireLockExclusive();
54 }
niklase@google.com470e71d2011-07-07 08:21:25 +000055
danilchapa37de392017-09-09 04:17:22 -070056 ~WriteLockScoped() RTC_UNLOCK_FUNCTION() { rw_lock_.ReleaseLockExclusive(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000057
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000058 private:
59 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000060};
henrike@webrtc.org9f847232012-09-25 20:27:51 +000061
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000062} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000063
kjellanderd56d68c2015-11-02 02:12:41 -080064#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_RW_LOCK_WRAPPER_H_