niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/rw_lock_win.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | namespace webrtc { |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 16 | |
| 17 | static bool native_rw_locks_supported = false; |
| 18 | static bool module_load_attempted = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | static HMODULE library = NULL; |
| 20 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 21 | typedef void (WINAPI* InitializeSRWLock)(PSRWLOCK); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 23 | typedef void (WINAPI* AcquireSRWLockExclusive)(PSRWLOCK); |
| 24 | typedef void (WINAPI* ReleaseSRWLockExclusive)(PSRWLOCK); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 25 | |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 26 | typedef void (WINAPI* AcquireSRWLockShared)(PSRWLOCK); |
| 27 | typedef void (WINAPI* ReleaseSRWLockShared)(PSRWLOCK); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 28 | |
| 29 | InitializeSRWLock initialize_srw_lock; |
| 30 | AcquireSRWLockExclusive acquire_srw_lock_exclusive; |
| 31 | AcquireSRWLockShared acquire_srw_lock_shared; |
| 32 | ReleaseSRWLockShared release_srw_lock_shared; |
| 33 | ReleaseSRWLockExclusive release_srw_lock_exclusive; |
| 34 | |
| 35 | RWLockWin::RWLockWin() { |
| 36 | initialize_srw_lock(&lock_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 39 | RWLockWin* RWLockWin::Create() { |
| 40 | if (!LoadModule()) { |
| 41 | return NULL; |
| 42 | } |
| 43 | return new RWLockWin(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 46 | void RWLockWin::AcquireLockExclusive() { |
| 47 | acquire_srw_lock_exclusive(&lock_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 50 | void RWLockWin::ReleaseLockExclusive() { |
| 51 | release_srw_lock_exclusive(&lock_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 54 | void RWLockWin::AcquireLockShared() { |
| 55 | acquire_srw_lock_shared(&lock_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 58 | void RWLockWin::ReleaseLockShared() { |
| 59 | release_srw_lock_shared(&lock_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 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 | } |
| 72 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Kernel.dll"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 74 | initialize_srw_lock = |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 75 | (InitializeSRWLock)GetProcAddress(library, "InitializeSRWLock"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 77 | acquire_srw_lock_exclusive = |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 78 | (AcquireSRWLockExclusive)GetProcAddress(library, |
| 79 | "AcquireSRWLockExclusive"); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 80 | release_srw_lock_exclusive = |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 81 | (ReleaseSRWLockExclusive)GetProcAddress(library, |
| 82 | "ReleaseSRWLockExclusive"); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 83 | acquire_srw_lock_shared = |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 84 | (AcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared"); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 85 | release_srw_lock_shared = |
phoglund@webrtc.org | 59ad541 | 2012-12-18 15:20:35 +0000 | [diff] [blame] | 86 | (ReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared"); |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 87 | |
| 88 | if (initialize_srw_lock && acquire_srw_lock_exclusive && |
| 89 | release_srw_lock_exclusive && acquire_srw_lock_shared && |
| 90 | release_srw_lock_shared) { |
| 91 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Native RW Lock"); |
| 92 | native_rw_locks_supported = true; |
| 93 | } |
| 94 | return native_rw_locks_supported; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
henrike@webrtc.org | 9f84723 | 2012-09-25 20:27:51 +0000 | [diff] [blame] | 96 | |
| 97 | } // namespace webrtc |