blob: a0d24a350e673d8c2e882fdc4737579ca325c356 [file] [log] [blame]
tommi@webrtc.orgfe196992015-02-07 22:35:54 +00001/*
2 * Copyright (c) 2012 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
Karl Wiberg2b857922018-03-23 14:53:54 +010011#include "rtc_base/synchronization/rw_lock_win.h"
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000012
Niels Möller5d41bde2017-10-02 13:48:55 +020013#include "rtc_base/logging.h"
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000014
15namespace webrtc {
16
Robin Raymondce1b1402018-11-22 20:10:11 -050017typedef void(WINAPI* PInitializeSRWLock)(PSRWLOCK);
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000018
Robin Raymondce1b1402018-11-22 20:10:11 -050019typedef void(WINAPI* PAcquireSRWLockExclusive)(PSRWLOCK);
20typedef void(WINAPI* PReleaseSRWLockExclusive)(PSRWLOCK);
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000021
Robin Raymondce1b1402018-11-22 20:10:11 -050022typedef void(WINAPI* PAcquireSRWLockShared)(PSRWLOCK);
23typedef void(WINAPI* PReleaseSRWLockShared)(PSRWLOCK);
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000024
Robin Raymondce1b1402018-11-22 20:10:11 -050025PInitializeSRWLock initialize_srw_lock;
26PAcquireSRWLockExclusive acquire_srw_lock_exclusive;
27PAcquireSRWLockShared acquire_srw_lock_shared;
28PReleaseSRWLockShared release_srw_lock_shared;
29PReleaseSRWLockExclusive release_srw_lock_exclusive;
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000030
31RWLockWin::RWLockWin() {
32 initialize_srw_lock(&lock_);
33}
34
35RWLockWin* RWLockWin::Create() {
36 if (!LoadModule()) {
37 return NULL;
38 }
39 return new RWLockWin();
40}
41
42void RWLockWin::AcquireLockExclusive() {
43 acquire_srw_lock_exclusive(&lock_);
44}
45
46void RWLockWin::ReleaseLockExclusive() {
47 release_srw_lock_exclusive(&lock_);
48}
49
50void RWLockWin::AcquireLockShared() {
51 acquire_srw_lock_shared(&lock_);
52}
53
54void RWLockWin::ReleaseLockShared() {
55 release_srw_lock_shared(&lock_);
56}
57
58bool RWLockWin::LoadModule() {
Robin Raymondce1b1402018-11-22 20:10:11 -050059 static bool module_load_attempted = false;
60 static bool native_rw_locks_supported = false;
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000061 if (module_load_attempted) {
62 return native_rw_locks_supported;
63 }
64 module_load_attempted = true;
Robin Raymondce1b1402018-11-22 20:10:11 -050065#if !defined(WINUWP)
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000066 // Use native implementation if supported (i.e Vista+)
Robin Raymondce1b1402018-11-22 20:10:11 -050067 static HMODULE library = LoadLibrary(TEXT("Kernel32.dll"));
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000068 if (!library) {
69 return false;
70 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_VERBOSE) << "Loaded Kernel.dll";
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000072
73 initialize_srw_lock =
Robin Raymondce1b1402018-11-22 20:10:11 -050074 (PInitializeSRWLock)GetProcAddress(library, "InitializeSRWLock");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000075
Robin Raymondce1b1402018-11-22 20:10:11 -050076 acquire_srw_lock_exclusive = (PAcquireSRWLockExclusive)GetProcAddress(
Karl Wiberg79eb1d92017-11-08 12:26:07 +010077 library, "AcquireSRWLockExclusive");
Robin Raymondce1b1402018-11-22 20:10:11 -050078 release_srw_lock_exclusive = (PReleaseSRWLockExclusive)GetProcAddress(
Karl Wiberg79eb1d92017-11-08 12:26:07 +010079 library, "ReleaseSRWLockExclusive");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000080 acquire_srw_lock_shared =
Robin Raymondce1b1402018-11-22 20:10:11 -050081 (PAcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000082 release_srw_lock_shared =
Robin Raymondce1b1402018-11-22 20:10:11 -050083 (PReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000084
85 if (initialize_srw_lock && acquire_srw_lock_exclusive &&
86 release_srw_lock_exclusive && acquire_srw_lock_shared &&
87 release_srw_lock_shared) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG(LS_VERBOSE) << "Loaded Native RW Lock";
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000089 native_rw_locks_supported = true;
90 }
Robin Raymondce1b1402018-11-22 20:10:11 -050091#else
92 // On WinUWP the symbols loaded from this library are directly present
93 // in the headers and thus loading the library is not required (and
94 // manually loading libraries is restricted due to WinUWP sandboxing).
95 initialize_srw_lock = InitializeSRWLock;
96 acquire_srw_lock_exclusive = AcquireSRWLockExclusive;
97 release_srw_lock_exclusive = ReleaseSRWLockExclusive;
98 acquire_srw_lock_shared = AcquireSRWLockShared;
99 release_srw_lock_shared = ReleaseSRWLockShared;
100
101 native_rw_locks_supported = true;
102#endif // !defined(WINUWP)
tommi@webrtc.orgfe196992015-02-07 22:35:54 +0000103 return native_rw_locks_supported;
104}
105
106} // namespace webrtc