blob: 94c2c9774bc67bed058641f4b76cc66705953d01 [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
niklase@google.com470e71d2011-07-07 08:21:25 +000016namespace webrtc {
henrike@webrtc.org9f847232012-09-25 20:27:51 +000017
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000018class LOCKABLE RWLockWrapper {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000019 public:
20 static RWLockWrapper* CreateRWLock();
21 virtual ~RWLockWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000022
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000023 virtual void AcquireLockExclusive() EXCLUSIVE_LOCK_FUNCTION() = 0;
24 virtual void ReleaseLockExclusive() UNLOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000025
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000026 virtual void AcquireLockShared() SHARED_LOCK_FUNCTION() = 0;
27 virtual void ReleaseLockShared() UNLOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000028};
29
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000030class SCOPED_LOCKABLE ReadLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000031 public:
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000032 ReadLockScoped(RWLockWrapper& rw_lock) SHARED_LOCK_FUNCTION(rw_lock)
33 : rw_lock_(rw_lock) {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000034 rw_lock_.AcquireLockShared();
35 }
niklase@google.com470e71d2011-07-07 08:21:25 +000036
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000037 ~ReadLockScoped() UNLOCK_FUNCTION() {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000038 rw_lock_.ReleaseLockShared();
39 }
niklase@google.com470e71d2011-07-07 08:21:25 +000040
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000041 private:
42 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000043};
44
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000045class SCOPED_LOCKABLE WriteLockScoped {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000046 public:
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000047 WriteLockScoped(RWLockWrapper& rw_lock) EXCLUSIVE_LOCK_FUNCTION(rw_lock)
48 : rw_lock_(rw_lock) {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000049 rw_lock_.AcquireLockExclusive();
50 }
niklase@google.com470e71d2011-07-07 08:21:25 +000051
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000052 ~WriteLockScoped() UNLOCK_FUNCTION() {
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000053 rw_lock_.ReleaseLockExclusive();
54 }
niklase@google.com470e71d2011-07-07 08:21:25 +000055
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000056 private:
57 RWLockWrapper& rw_lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000058};
henrike@webrtc.org9f847232012-09-25 20:27:51 +000059
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000060} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000061
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000062#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_