blob: e81e9d735413cf3034d02676ff486d3879c6f1e8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/source/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
17static bool native_rw_locks_supported = false;
18static bool module_load_attempted = false;
19static HMODULE library = NULL;
20
Karl Wiberg79eb1d92017-11-08 12:26:07 +010021typedef void(WINAPI* InitializeSRWLock)(PSRWLOCK);
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000022
Karl Wiberg79eb1d92017-11-08 12:26:07 +010023typedef void(WINAPI* AcquireSRWLockExclusive)(PSRWLOCK);
24typedef void(WINAPI* ReleaseSRWLockExclusive)(PSRWLOCK);
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000025
Karl Wiberg79eb1d92017-11-08 12:26:07 +010026typedef void(WINAPI* AcquireSRWLockShared)(PSRWLOCK);
27typedef void(WINAPI* ReleaseSRWLockShared)(PSRWLOCK);
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000028
Karl Wiberg79eb1d92017-11-08 12:26:07 +010029InitializeSRWLock initialize_srw_lock;
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000030AcquireSRWLockExclusive acquire_srw_lock_exclusive;
Karl Wiberg79eb1d92017-11-08 12:26:07 +010031AcquireSRWLockShared acquire_srw_lock_shared;
32ReleaseSRWLockShared release_srw_lock_shared;
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000033ReleaseSRWLockExclusive release_srw_lock_exclusive;
34
35RWLockWin::RWLockWin() {
36 initialize_srw_lock(&lock_);
37}
38
39RWLockWin* RWLockWin::Create() {
40 if (!LoadModule()) {
41 return NULL;
42 }
43 return new RWLockWin();
44}
45
46void RWLockWin::AcquireLockExclusive() {
47 acquire_srw_lock_exclusive(&lock_);
48}
49
50void RWLockWin::ReleaseLockExclusive() {
51 release_srw_lock_exclusive(&lock_);
52}
53
54void RWLockWin::AcquireLockShared() {
55 acquire_srw_lock_shared(&lock_);
56}
57
58void RWLockWin::ReleaseLockShared() {
59 release_srw_lock_shared(&lock_);
60}
61
62bool RWLockWin::LoadModule() {
63 if (module_load_attempted) {
64 return native_rw_locks_supported;
65 }
66 module_load_attempted = true;
67 // Use native implementation if supported (i.e Vista+)
68 library = LoadLibrary(TEXT("Kernel32.dll"));
69 if (!library) {
70 return false;
71 }
Niels Möller5d41bde2017-10-02 13:48:55 +020072 LOG(LS_VERBOSE) << "Loaded Kernel.dll";
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000073
74 initialize_srw_lock =
Karl Wiberg79eb1d92017-11-08 12:26:07 +010075 (InitializeSRWLock)GetProcAddress(library, "InitializeSRWLock");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000076
Karl Wiberg79eb1d92017-11-08 12:26:07 +010077 acquire_srw_lock_exclusive = (AcquireSRWLockExclusive)GetProcAddress(
78 library, "AcquireSRWLockExclusive");
79 release_srw_lock_exclusive = (ReleaseSRWLockExclusive)GetProcAddress(
80 library, "ReleaseSRWLockExclusive");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000081 acquire_srw_lock_shared =
Karl Wiberg79eb1d92017-11-08 12:26:07 +010082 (AcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000083 release_srw_lock_shared =
Karl Wiberg79eb1d92017-11-08 12:26:07 +010084 (ReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared");
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000085
86 if (initialize_srw_lock && acquire_srw_lock_exclusive &&
87 release_srw_lock_exclusive && acquire_srw_lock_shared &&
88 release_srw_lock_shared) {
Niels Möller5d41bde2017-10-02 13:48:55 +020089 LOG(LS_VERBOSE) << "Loaded Native RW Lock";
tommi@webrtc.orgfe196992015-02-07 22:35:54 +000090 native_rw_locks_supported = true;
91 }
92 return native_rw_locks_supported;
93}
94
95} // namespace webrtc