2010-11-30 03:44:50 +08:00
|
|
|
//= llvm/Support/Win32/Mutex.inc - Win32 Reader/Writer Mutual Exclusion Lock =//
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2009-06-17 04:19:28 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2009-06-17 04:19:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "Windows.h"
|
2009-06-17 04:19:28 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
2011-10-14 07:10:56 +08:00
|
|
|
// 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;
|
|
|
|
|
2013-10-13 18:34:21 +08:00
|
|
|
HMODULE hLib;
|
|
|
|
::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"Kernel32.dll", &hLib);
|
2011-10-14 07:10:56 +08:00
|
|
|
if (hLib) {
|
|
|
|
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");
|
|
|
|
|
2012-07-19 08:06:06 +08:00
|
|
|
if (fpInitializeSRWLock != NULL) {
|
|
|
|
sHasSRW = true;
|
|
|
|
}
|
2011-10-14 07:10:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sHasSRW;
|
|
|
|
}
|
|
|
|
|
2009-06-19 02:26:15 +08:00
|
|
|
RWMutexImpl::RWMutexImpl() {
|
2011-10-14 07:10:56 +08:00
|
|
|
if (loadSRW()) {
|
|
|
|
data_ = calloc(1, sizeof(SRWLOCK));
|
|
|
|
fpInitializeSRWLock(static_cast<PSRWLOCK>(data_));
|
|
|
|
} else {
|
|
|
|
data_ = calloc(1, sizeof(CRITICAL_SECTION));
|
|
|
|
InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
|
|
}
|
2009-06-17 17:10:42 +08:00
|
|
|
}
|
2009-06-17 04:19:28 +08:00
|
|
|
|
2009-06-19 02:26:15 +08:00
|
|
|
RWMutexImpl::~RWMutexImpl() {
|
2011-10-14 07:10:56 +08:00
|
|
|
if (sHasSRW) {
|
|
|
|
// Nothing to do in the case of slim reader/writers
|
|
|
|
} else {
|
|
|
|
DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
|
|
free(data_);
|
|
|
|
}
|
2009-06-17 17:10:42 +08:00
|
|
|
}
|
2009-06-17 04:19:28 +08:00
|
|
|
|
2009-06-19 02:26:15 +08:00
|
|
|
bool RWMutexImpl::reader_acquire() {
|
2011-10-14 07:10:56 +08:00
|
|
|
if (sHasSRW) {
|
|
|
|
fpAcquireSRWLockShared(static_cast<PSRWLOCK>(data_));
|
|
|
|
} else {
|
|
|
|
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
|
|
}
|
2009-06-17 04:19:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-19 02:26:15 +08:00
|
|
|
bool RWMutexImpl::reader_release() {
|
2011-10-14 07:10:56 +08:00
|
|
|
if (sHasSRW) {
|
|
|
|
fpReleaseSRWLockShared(static_cast<PSRWLOCK>(data_));
|
|
|
|
} else {
|
|
|
|
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
|
|
}
|
2009-06-17 04:19:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-19 02:26:15 +08:00
|
|
|
bool RWMutexImpl::writer_acquire() {
|
2011-10-14 07:10:56 +08:00
|
|
|
if (sHasSRW) {
|
|
|
|
fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(data_));
|
|
|
|
} else {
|
|
|
|
EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
|
|
}
|
2009-06-17 04:19:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-19 02:26:15 +08:00
|
|
|
bool RWMutexImpl::writer_release() {
|
2011-10-14 07:10:56 +08:00
|
|
|
if (sHasSRW) {
|
|
|
|
fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(data_));
|
|
|
|
} else {
|
|
|
|
LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
|
|
|
|
}
|
2009-06-17 04:19:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|