1 /*
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_SOURCE_RW_LOCK_WINDOWS_H_
12 #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WINDOWS_H_
13 
14 #include "rw_lock_wrapper.h"
15 
16 #include <Windows.h>
17 
18 #if !defined(RTL_SRWLOCK_INIT)
19     typedef struct _RTL_SRWLOCK
20     {
21         void* Ptr;
22     } RTL_SRWLOCK, *PRTL_SRWLOCK;
23     typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
24 #endif
25 
26 namespace webrtc {
27 class CriticalSectionWrapper;
28 class ConditionVariableWrapper;
29 
30 typedef void (WINAPI *PInitializeSRWLock)(PSRWLOCK);
31 
32 typedef void (WINAPI *PAcquireSRWLockExclusive)(PSRWLOCK);
33 typedef void (WINAPI *PReleaseSRWLockExclusive)(PSRWLOCK);
34 
35 typedef void (WINAPI *PAcquireSRWLockShared)(PSRWLOCK);
36 typedef void (WINAPI *PReleaseSRWLockShared)(PSRWLOCK);
37 
38 
39 class RWLockWindows :public RWLockWrapper
40 {
41 public:
42     RWLockWindows();
43     virtual ~RWLockWindows();
44 
45     virtual void AcquireLockExclusive();
46     virtual void ReleaseLockExclusive();
47 
48     virtual void AcquireLockShared();
49     virtual void ReleaseLockShared();
50 
51 protected:
52     virtual int Init();
53 
54 private:
55     // For native implementation.
56     static bool _winSupportRWLockPrimitive;
57     SRWLOCK     _lock;
58 
59     // Genric implementation, fallback if native is not supported.
60     CriticalSectionWrapper*   _critSectPtr;
61     ConditionVariableWrapper* _readCondPtr;
62     ConditionVariableWrapper* _writeCondPtr;
63 
64     int  _readersActive;
65     bool _writerActive;
66     int  _readersWaiting;
67     int  _writersWaiting;
68 };
69 } // namespace webrtc
70 
71 #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WINDOWS_H_
72