mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
ae65e281f3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
129 lines
4.1 KiB
C++
129 lines
4.1 KiB
C++
//= llvm/Support/Win32/Mutex.inc - Win32 Reader/Writer Mutual Exclusion Lock =//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the Win32 specific (non-pthread) RWMutex class.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
//=== WARNING: Implementation here must contain only generic Win32 code that
|
|
//=== is guaranteed to work on *all* Win32 variants.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "WindowsSupport.h"
|
|
|
|
namespace llvm {
|
|
|
|
// Windows has slim read-writer lock support on Vista and higher, so we
|
|
// will attempt to load the APIs. If they exist, we will use them, and
|
|
// if not, we will fall back on critical sections. When we drop support
|
|
// for XP, we can stop lazy-loading these APIs and just use them directly.
|
|
#if defined(__MINGW32__)
|
|
// Taken from WinNT.h
|
|
typedef struct _RTL_SRWLOCK {
|
|
PVOID Ptr;
|
|
} RTL_SRWLOCK, *PRTL_SRWLOCK;
|
|
|
|
// Taken from WinBase.h
|
|
typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
|
|
#endif
|
|
|
|
static VOID (WINAPI *fpInitializeSRWLock)(PSRWLOCK lock) = NULL;
|
|
static VOID (WINAPI *fpAcquireSRWLockExclusive)(PSRWLOCK lock) = NULL;
|
|
static VOID (WINAPI *fpAcquireSRWLockShared)(PSRWLOCK lock) = NULL;
|
|
static VOID (WINAPI *fpReleaseSRWLockExclusive)(PSRWLOCK lock) = NULL;
|
|
static VOID (WINAPI *fpReleaseSRWLockShared)(PSRWLOCK lock) = NULL;
|
|
|
|
static bool sHasSRW = false;
|
|
|
|
static bool loadSRW() {
|
|
static bool sChecked = false;
|
|
if (!sChecked) {
|
|
sChecked = true;
|
|
|
|
if (HMODULE hLib = ::GetModuleHandleW(L"Kernel32.dll")) {
|
|
fpInitializeSRWLock =
|
|
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
|
|
"InitializeSRWLock");
|
|
fpAcquireSRWLockExclusive =
|
|
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
|
|
"AcquireSRWLockExclusive");
|
|
fpAcquireSRWLockShared =
|
|
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
|
|
"AcquireSRWLockShared");
|
|
fpReleaseSRWLockExclusive =
|
|
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
|
|
"ReleaseSRWLockExclusive");
|
|
fpReleaseSRWLockShared =
|
|
(VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
|
|
"ReleaseSRWLockShared");
|
|
|
|
if (fpInitializeSRWLock != NULL) {
|
|
sHasSRW = true;
|
|
}
|
|
}
|
|
}
|
|
return sHasSRW;
|
|
}
|
|
|
|
sys::RWMutexImpl::RWMutexImpl() {
|
|
if (loadSRW()) {
|
|
data_ = safe_calloc(1, sizeof(SRWLOCK));
|
|
fpInitializeSRWLock(static_cast<PSRWLOCK>(data_));
|
|
} else {
|
|
data_ = safe_calloc(1, sizeof(CRITICAL_SECTION));
|
|
InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
}
|
|
}
|
|
|
|
sys::RWMutexImpl::~RWMutexImpl() {
|
|
if (!sHasSRW)
|
|
DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
// Nothing to do in the case of slim reader/writers except free the memory.
|
|
free(data_);
|
|
}
|
|
|
|
bool sys::RWMutexImpl::reader_acquire() {
|
|
if (sHasSRW) {
|
|
fpAcquireSRWLockShared(static_cast<PSRWLOCK>(data_));
|
|
} else {
|
|
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool sys::RWMutexImpl::reader_release() {
|
|
if (sHasSRW) {
|
|
fpReleaseSRWLockShared(static_cast<PSRWLOCK>(data_));
|
|
} else {
|
|
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool sys::RWMutexImpl::writer_acquire() {
|
|
if (sHasSRW) {
|
|
fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(data_));
|
|
} else {
|
|
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool sys::RWMutexImpl::writer_release() {
|
|
if (sHasSRW) {
|
|
fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(data_));
|
|
} else {
|
|
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|