tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 1 | /* |
| 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 Wiberg | 2b85792 | 2018-03-23 14:53:54 +0100 | [diff] [blame] | 11 | #include "rtc_base/synchronization/rw_lock_win.h" |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 12 | |
Niels Möller | 5d41bde | 2017-10-02 13:48:55 +0200 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 17 | typedef void(WINAPI* PInitializeSRWLock)(PSRWLOCK); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 18 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 19 | typedef void(WINAPI* PAcquireSRWLockExclusive)(PSRWLOCK); |
| 20 | typedef void(WINAPI* PReleaseSRWLockExclusive)(PSRWLOCK); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 21 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 22 | typedef void(WINAPI* PAcquireSRWLockShared)(PSRWLOCK); |
| 23 | typedef void(WINAPI* PReleaseSRWLockShared)(PSRWLOCK); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 24 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 25 | PInitializeSRWLock initialize_srw_lock; |
| 26 | PAcquireSRWLockExclusive acquire_srw_lock_exclusive; |
| 27 | PAcquireSRWLockShared acquire_srw_lock_shared; |
| 28 | PReleaseSRWLockShared release_srw_lock_shared; |
| 29 | PReleaseSRWLockExclusive release_srw_lock_exclusive; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 30 | |
| 31 | RWLockWin::RWLockWin() { |
| 32 | initialize_srw_lock(&lock_); |
| 33 | } |
| 34 | |
| 35 | RWLockWin* RWLockWin::Create() { |
| 36 | if (!LoadModule()) { |
| 37 | return NULL; |
| 38 | } |
| 39 | return new RWLockWin(); |
| 40 | } |
| 41 | |
| 42 | void RWLockWin::AcquireLockExclusive() { |
| 43 | acquire_srw_lock_exclusive(&lock_); |
| 44 | } |
| 45 | |
| 46 | void RWLockWin::ReleaseLockExclusive() { |
| 47 | release_srw_lock_exclusive(&lock_); |
| 48 | } |
| 49 | |
| 50 | void RWLockWin::AcquireLockShared() { |
| 51 | acquire_srw_lock_shared(&lock_); |
| 52 | } |
| 53 | |
| 54 | void RWLockWin::ReleaseLockShared() { |
| 55 | release_srw_lock_shared(&lock_); |
| 56 | } |
| 57 | |
| 58 | bool RWLockWin::LoadModule() { |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 59 | static bool module_load_attempted = false; |
| 60 | static bool native_rw_locks_supported = false; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 61 | if (module_load_attempted) { |
| 62 | return native_rw_locks_supported; |
| 63 | } |
| 64 | module_load_attempted = true; |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 65 | #if !defined(WINUWP) |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 66 | // Use native implementation if supported (i.e Vista+) |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 67 | static HMODULE library = LoadLibrary(TEXT("Kernel32.dll")); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 68 | if (!library) { |
| 69 | return false; |
| 70 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 71 | RTC_LOG(LS_VERBOSE) << "Loaded Kernel.dll"; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 72 | |
| 73 | initialize_srw_lock = |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 74 | (PInitializeSRWLock)GetProcAddress(library, "InitializeSRWLock"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 75 | |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 76 | acquire_srw_lock_exclusive = (PAcquireSRWLockExclusive)GetProcAddress( |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 77 | library, "AcquireSRWLockExclusive"); |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 78 | release_srw_lock_exclusive = (PReleaseSRWLockExclusive)GetProcAddress( |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 79 | library, "ReleaseSRWLockExclusive"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 80 | acquire_srw_lock_shared = |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 81 | (PAcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 82 | release_srw_lock_shared = |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 83 | (PReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 84 | |
| 85 | if (initialize_srw_lock && acquire_srw_lock_exclusive && |
| 86 | release_srw_lock_exclusive && acquire_srw_lock_shared && |
| 87 | release_srw_lock_shared) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 88 | RTC_LOG(LS_VERBOSE) << "Loaded Native RW Lock"; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 89 | native_rw_locks_supported = true; |
| 90 | } |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 91 | #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.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 103 | return native_rw_locks_supported; |
| 104 | } |
| 105 | |
| 106 | } // namespace webrtc |