1 //= llvm/Support/Win32/Mutex.inc - Win32 Reader/Writer Mutual Exclusion Lock =//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Win32 specific (non-pthread) RWMutex class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Win32 code that
16 //=== is guaranteed to work on *all* Win32 variants.
17 //===----------------------------------------------------------------------===//
24 // Windows has slim read-writer lock support on Vista and higher, so we
25 // will attempt to load the APIs. If they exist, we will use them, and
26 // if not, we will fall back on critical sections. When we drop support
27 // for XP, we can stop lazy-loading these APIs and just use them directly.
28 #if defined(__MINGW32__)
30 typedef struct _RTL_SRWLOCK {
32 } RTL_SRWLOCK, *PRTL_SRWLOCK;
34 // Taken from WinBase.h
35 typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
38 static VOID (WINAPI *fpInitializeSRWLock)(PSRWLOCK lock) = NULL;
39 static VOID (WINAPI *fpAcquireSRWLockExclusive)(PSRWLOCK lock) = NULL;
40 static VOID (WINAPI *fpAcquireSRWLockShared)(PSRWLOCK lock) = NULL;
41 static VOID (WINAPI *fpReleaseSRWLockExclusive)(PSRWLOCK lock) = NULL;
42 static VOID (WINAPI *fpReleaseSRWLockShared)(PSRWLOCK lock) = NULL;
44 static bool sHasSRW = false;
46 static bool loadSRW() {
47 static bool sChecked = false;
51 HMODULE hLib = ::LoadLibraryW(L"Kernel32.dll");
54 (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
56 fpAcquireSRWLockExclusive =
57 (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
58 "AcquireSRWLockExclusive");
59 fpAcquireSRWLockShared =
60 (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
61 "AcquireSRWLockShared");
62 fpReleaseSRWLockExclusive =
63 (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
64 "ReleaseSRWLockExclusive");
65 fpReleaseSRWLockShared =
66 (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
67 "ReleaseSRWLockShared");
70 if (fpInitializeSRWLock != NULL) {
78 RWMutexImpl::RWMutexImpl() {
80 data_ = calloc(1, sizeof(SRWLOCK));
81 fpInitializeSRWLock(static_cast<PSRWLOCK>(data_));
83 data_ = calloc(1, sizeof(CRITICAL_SECTION));
84 InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
88 RWMutexImpl::~RWMutexImpl() {
90 // Nothing to do in the case of slim reader/writers
92 DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
97 bool RWMutexImpl::reader_acquire() {
99 fpAcquireSRWLockShared(static_cast<PSRWLOCK>(data_));
101 EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
106 bool RWMutexImpl::reader_release() {
108 fpReleaseSRWLockShared(static_cast<PSRWLOCK>(data_));
110 LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
115 bool RWMutexImpl::writer_acquire() {
117 fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(data_));
119 EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));
124 bool RWMutexImpl::writer_release() {
126 fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(data_));
128 LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_));