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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/source/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 | |
| 17 | static bool native_rw_locks_supported = false; |
| 18 | static bool module_load_attempted = false; |
| 19 | static HMODULE library = NULL; |
| 20 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 21 | typedef void(WINAPI* InitializeSRWLock)(PSRWLOCK); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 22 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 23 | typedef void(WINAPI* AcquireSRWLockExclusive)(PSRWLOCK); |
| 24 | typedef void(WINAPI* ReleaseSRWLockExclusive)(PSRWLOCK); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 25 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 26 | typedef void(WINAPI* AcquireSRWLockShared)(PSRWLOCK); |
| 27 | typedef void(WINAPI* ReleaseSRWLockShared)(PSRWLOCK); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 28 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 29 | InitializeSRWLock initialize_srw_lock; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 30 | AcquireSRWLockExclusive acquire_srw_lock_exclusive; |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 31 | AcquireSRWLockShared acquire_srw_lock_shared; |
| 32 | ReleaseSRWLockShared release_srw_lock_shared; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 33 | ReleaseSRWLockExclusive release_srw_lock_exclusive; |
| 34 | |
| 35 | RWLockWin::RWLockWin() { |
| 36 | initialize_srw_lock(&lock_); |
| 37 | } |
| 38 | |
| 39 | RWLockWin* RWLockWin::Create() { |
| 40 | if (!LoadModule()) { |
| 41 | return NULL; |
| 42 | } |
| 43 | return new RWLockWin(); |
| 44 | } |
| 45 | |
| 46 | void RWLockWin::AcquireLockExclusive() { |
| 47 | acquire_srw_lock_exclusive(&lock_); |
| 48 | } |
| 49 | |
| 50 | void RWLockWin::ReleaseLockExclusive() { |
| 51 | release_srw_lock_exclusive(&lock_); |
| 52 | } |
| 53 | |
| 54 | void RWLockWin::AcquireLockShared() { |
| 55 | acquire_srw_lock_shared(&lock_); |
| 56 | } |
| 57 | |
| 58 | void RWLockWin::ReleaseLockShared() { |
| 59 | release_srw_lock_shared(&lock_); |
| 60 | } |
| 61 | |
| 62 | bool 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öller | 5d41bde | 2017-10-02 13:48:55 +0200 | [diff] [blame] | 72 | LOG(LS_VERBOSE) << "Loaded Kernel.dll"; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 73 | |
| 74 | initialize_srw_lock = |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 75 | (InitializeSRWLock)GetProcAddress(library, "InitializeSRWLock"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 76 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 77 | acquire_srw_lock_exclusive = (AcquireSRWLockExclusive)GetProcAddress( |
| 78 | library, "AcquireSRWLockExclusive"); |
| 79 | release_srw_lock_exclusive = (ReleaseSRWLockExclusive)GetProcAddress( |
| 80 | library, "ReleaseSRWLockExclusive"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 81 | acquire_srw_lock_shared = |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 82 | (AcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 83 | release_srw_lock_shared = |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame^] | 84 | (ReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared"); |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 85 | |
| 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öller | 5d41bde | 2017-10-02 13:48:55 +0200 | [diff] [blame] | 89 | LOG(LS_VERBOSE) << "Loaded Native RW Lock"; |
tommi@webrtc.org | fe19699 | 2015-02-07 22:35:54 +0000 | [diff] [blame] | 90 | native_rw_locks_supported = true; |
| 91 | } |
| 92 | return native_rw_locks_supported; |
| 93 | } |
| 94 | |
| 95 | } // namespace webrtc |