blob: 5728202737278a738783172d9ed0dc50570a58ad [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
14// Note, Windows pre-Vista version of RW locks are not supported nativly. For
15// these OSs regular critical sections have been used to approximate RW lock
16// functionality and will therefore have worse performance.
17
18namespace webrtc {
henrike@webrtc.org9f847232012-09-25 20:27:51 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020class RWLockWrapper
21{
22public:
23 static RWLockWrapper* CreateRWLock();
henrike@webrtc.org9f847232012-09-25 20:27:51 +000024 virtual ~RWLockWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26 virtual void AcquireLockExclusive() = 0;
27 virtual void ReleaseLockExclusive() = 0;
28
29 virtual void AcquireLockShared() = 0;
30 virtual void ReleaseLockShared() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000031};
32
33// RAII extensions of the RW lock. Prevents Acquire/Release missmatches and
34// provides more compact locking syntax.
35class ReadLockScoped
36{
37public:
38 ReadLockScoped(RWLockWrapper& rwLock)
39 :
40 _rwLock(rwLock)
41 {
42 _rwLock.AcquireLockShared();
43 }
44
45 ~ReadLockScoped()
46 {
47 _rwLock.ReleaseLockShared();
48 }
49
50private:
51 RWLockWrapper& _rwLock;
52};
53
54class WriteLockScoped
55{
56public:
57 WriteLockScoped(RWLockWrapper& rwLock)
58 :
59 _rwLock(rwLock)
60 {
61 _rwLock.AcquireLockExclusive();
62 }
63
64 ~WriteLockScoped()
65 {
66 _rwLock.ReleaseLockExclusive();
67 }
68
69private:
70 RWLockWrapper& _rwLock;
71};
henrike@webrtc.org9f847232012-09-25 20:27:51 +000072
niklase@google.com470e71d2011-07-07 08:21:25 +000073} // namespace webrtc
74
75#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_