blob: dbe6d6c7c005fabd057cf2de4df50cc302ddf9c4 [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
11#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_
13
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000014#include "webrtc/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
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000022class 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
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000027 virtual void AcquireLockExclusive() EXCLUSIVE_LOCK_FUNCTION() = 0;
28 virtual void ReleaseLockExclusive() UNLOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000030 virtual void AcquireLockShared() SHARED_LOCK_FUNCTION() = 0;
31 virtual void ReleaseLockShared() 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.
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000036class SCOPED_LOCKABLE ReadLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000037 public:
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000038 ReadLockScoped(RWLockWrapper& rw_lock) SHARED_LOCK_FUNCTION(rw_lock)
39 : 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
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000043 ~ReadLockScoped() UNLOCK_FUNCTION() {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000044 rw_lock_.ReleaseLockShared();
45 }
niklase@google.com470e71d2011-07-07 08:21:25 +000046
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000047 private:
48 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000049};
50
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000051class SCOPED_LOCKABLE WriteLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000052 public:
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000053 WriteLockScoped(RWLockWrapper& rw_lock) EXCLUSIVE_LOCK_FUNCTION(rw_lock)
54 : 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
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000058 ~WriteLockScoped() UNLOCK_FUNCTION() {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000059 rw_lock_.ReleaseLockExclusive();
60 }
niklase@google.com470e71d2011-07-07 08:21:25 +000061
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000062 private:
63 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000064};
henrike@webrtc.org9f847232012-09-25 20:27:51 +000065
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000066} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000067
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000068#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_