2015-02-07 22:35:54 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-23 14:53:54 +01:00
|
|
|
#include "rtc_base/synchronization/rw_lock_win.h"
|
2015-02-07 22:35:54 +00:00
|
|
|
|
2017-10-02 13:48:55 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2015-02-07 22:35:54 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2018-11-22 20:10:11 -05:00
|
|
|
typedef void(WINAPI* PInitializeSRWLock)(PSRWLOCK);
|
2015-02-07 22:35:54 +00:00
|
|
|
|
2018-11-22 20:10:11 -05:00
|
|
|
typedef void(WINAPI* PAcquireSRWLockExclusive)(PSRWLOCK);
|
|
|
|
|
typedef void(WINAPI* PReleaseSRWLockExclusive)(PSRWLOCK);
|
2015-02-07 22:35:54 +00:00
|
|
|
|
2018-11-22 20:10:11 -05:00
|
|
|
typedef void(WINAPI* PAcquireSRWLockShared)(PSRWLOCK);
|
|
|
|
|
typedef void(WINAPI* PReleaseSRWLockShared)(PSRWLOCK);
|
2015-02-07 22:35:54 +00:00
|
|
|
|
2018-11-22 20:10:11 -05:00
|
|
|
PInitializeSRWLock initialize_srw_lock;
|
|
|
|
|
PAcquireSRWLockExclusive acquire_srw_lock_exclusive;
|
|
|
|
|
PAcquireSRWLockShared acquire_srw_lock_shared;
|
|
|
|
|
PReleaseSRWLockShared release_srw_lock_shared;
|
|
|
|
|
PReleaseSRWLockExclusive release_srw_lock_exclusive;
|
2015-02-07 22:35:54 +00:00
|
|
|
|
|
|
|
|
RWLockWin::RWLockWin() {
|
|
|
|
|
initialize_srw_lock(&lock_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RWLockWin* RWLockWin::Create() {
|
|
|
|
|
if (!LoadModule()) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return new RWLockWin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWin::AcquireLockExclusive() {
|
|
|
|
|
acquire_srw_lock_exclusive(&lock_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWin::ReleaseLockExclusive() {
|
|
|
|
|
release_srw_lock_exclusive(&lock_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWin::AcquireLockShared() {
|
|
|
|
|
acquire_srw_lock_shared(&lock_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWin::ReleaseLockShared() {
|
|
|
|
|
release_srw_lock_shared(&lock_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RWLockWin::LoadModule() {
|
2018-11-22 20:10:11 -05:00
|
|
|
static bool module_load_attempted = false;
|
|
|
|
|
static bool native_rw_locks_supported = false;
|
2015-02-07 22:35:54 +00:00
|
|
|
if (module_load_attempted) {
|
|
|
|
|
return native_rw_locks_supported;
|
|
|
|
|
}
|
|
|
|
|
module_load_attempted = true;
|
2018-11-22 20:10:11 -05:00
|
|
|
#if !defined(WINUWP)
|
2015-02-07 22:35:54 +00:00
|
|
|
// Use native implementation if supported (i.e Vista+)
|
2018-11-22 20:10:11 -05:00
|
|
|
static HMODULE library = LoadLibrary(TEXT("Kernel32.dll"));
|
2015-02-07 22:35:54 +00:00
|
|
|
if (!library) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_VERBOSE) << "Loaded Kernel.dll";
|
2015-02-07 22:35:54 +00:00
|
|
|
|
|
|
|
|
initialize_srw_lock =
|
2018-11-22 20:10:11 -05:00
|
|
|
(PInitializeSRWLock)GetProcAddress(library, "InitializeSRWLock");
|
2017-11-08 12:26:07 +01:00
|
|
|
|
2018-11-22 20:10:11 -05:00
|
|
|
acquire_srw_lock_exclusive = (PAcquireSRWLockExclusive)GetProcAddress(
|
2017-11-08 12:26:07 +01:00
|
|
|
library, "AcquireSRWLockExclusive");
|
2018-11-22 20:10:11 -05:00
|
|
|
release_srw_lock_exclusive = (PReleaseSRWLockExclusive)GetProcAddress(
|
2017-11-08 12:26:07 +01:00
|
|
|
library, "ReleaseSRWLockExclusive");
|
2015-02-07 22:35:54 +00:00
|
|
|
acquire_srw_lock_shared =
|
2018-11-22 20:10:11 -05:00
|
|
|
(PAcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared");
|
2015-02-07 22:35:54 +00:00
|
|
|
release_srw_lock_shared =
|
2018-11-22 20:10:11 -05:00
|
|
|
(PReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared");
|
2015-02-07 22:35:54 +00:00
|
|
|
|
|
|
|
|
if (initialize_srw_lock && acquire_srw_lock_exclusive &&
|
|
|
|
|
release_srw_lock_exclusive && acquire_srw_lock_shared &&
|
|
|
|
|
release_srw_lock_shared) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_VERBOSE) << "Loaded Native RW Lock";
|
2015-02-07 22:35:54 +00:00
|
|
|
native_rw_locks_supported = true;
|
|
|
|
|
}
|
2018-11-22 20:10:11 -05:00
|
|
|
#else
|
|
|
|
|
// On WinUWP the symbols loaded from this library are directly present
|
|
|
|
|
// in the headers and thus loading the library is not required (and
|
|
|
|
|
// manually loading libraries is restricted due to WinUWP sandboxing).
|
|
|
|
|
initialize_srw_lock = InitializeSRWLock;
|
|
|
|
|
acquire_srw_lock_exclusive = AcquireSRWLockExclusive;
|
|
|
|
|
release_srw_lock_exclusive = ReleaseSRWLockExclusive;
|
|
|
|
|
acquire_srw_lock_shared = AcquireSRWLockShared;
|
|
|
|
|
release_srw_lock_shared = ReleaseSRWLockShared;
|
|
|
|
|
|
|
|
|
|
native_rw_locks_supported = true;
|
|
|
|
|
#endif // !defined(WINUWP)
|
2015-02-07 22:35:54 +00:00
|
|
|
return native_rw_locks_supported;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|