Charles Davis | 54c9eb6 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //= llvm/Support/Win32/Mutex.inc - Win32 Reader/Writer Mutual Exclusion Lock =// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Win32 specific (non-pthread) RWMutex class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic Win32 code that |
| 16 | //=== is guaranteed to work on *all* Win32 variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 19 | #include "WindowsSupport.h" |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | using namespace sys; |
| 23 | |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 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__) |
| 29 | // Taken from WinNT.h |
| 30 | typedef struct _RTL_SRWLOCK { |
| 31 | PVOID Ptr; |
| 32 | } RTL_SRWLOCK, *PRTL_SRWLOCK; |
| 33 | |
| 34 | // Taken from WinBase.h |
| 35 | typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK; |
| 36 | #endif |
| 37 | |
| 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; |
| 43 | |
| 44 | static bool sHasSRW = false; |
| 45 | |
| 46 | static bool loadSRW() { |
| 47 | static bool sChecked = false; |
| 48 | if (!sChecked) { |
| 49 | sChecked = true; |
| 50 | |
David Majnemer | 7af1857 | 2013-10-14 00:06:58 +0000 | [diff] [blame] | 51 | if (HMODULE hLib = ::GetModuleHandleW(L"Kernel32.dll")) { |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 52 | fpInitializeSRWLock = |
| 53 | (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib, |
| 54 | "InitializeSRWLock"); |
| 55 | fpAcquireSRWLockExclusive = |
| 56 | (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib, |
| 57 | "AcquireSRWLockExclusive"); |
| 58 | fpAcquireSRWLockShared = |
| 59 | (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib, |
| 60 | "AcquireSRWLockShared"); |
| 61 | fpReleaseSRWLockExclusive = |
| 62 | (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib, |
| 63 | "ReleaseSRWLockExclusive"); |
| 64 | fpReleaseSRWLockShared = |
| 65 | (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib, |
| 66 | "ReleaseSRWLockShared"); |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 67 | |
Bill Wendling | 2b07965 | 2012-07-19 00:06:06 +0000 | [diff] [blame] | 68 | if (fpInitializeSRWLock != NULL) { |
| 69 | sHasSRW = true; |
| 70 | } |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return sHasSRW; |
| 74 | } |
| 75 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 76 | RWMutexImpl::RWMutexImpl() { |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 77 | if (loadSRW()) { |
| 78 | data_ = calloc(1, sizeof(SRWLOCK)); |
| 79 | fpInitializeSRWLock(static_cast<PSRWLOCK>(data_)); |
| 80 | } else { |
| 81 | data_ = calloc(1, sizeof(CRITICAL_SECTION)); |
| 82 | InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); |
| 83 | } |
Owen Anderson | 74af4ee | 2009-06-17 09:10:42 +0000 | [diff] [blame] | 84 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 85 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 86 | RWMutexImpl::~RWMutexImpl() { |
David Blaikie | df95153 | 2014-10-21 00:34:39 +0000 | [diff] [blame] | 87 | if (!sHasSRW) |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 88 | DeleteCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); |
David Blaikie | df95153 | 2014-10-21 00:34:39 +0000 | [diff] [blame] | 89 | // Nothing to do in the case of slim reader/writers except free the memory. |
| 90 | free(data_); |
Owen Anderson | 74af4ee | 2009-06-17 09:10:42 +0000 | [diff] [blame] | 91 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 92 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 93 | bool RWMutexImpl::reader_acquire() { |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 94 | if (sHasSRW) { |
| 95 | fpAcquireSRWLockShared(static_cast<PSRWLOCK>(data_)); |
| 96 | } else { |
| 97 | EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); |
| 98 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 102 | bool RWMutexImpl::reader_release() { |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 103 | if (sHasSRW) { |
| 104 | fpReleaseSRWLockShared(static_cast<PSRWLOCK>(data_)); |
| 105 | } else { |
| 106 | LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); |
| 107 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 111 | bool RWMutexImpl::writer_acquire() { |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 112 | if (sHasSRW) { |
| 113 | fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(data_)); |
| 114 | } else { |
| 115 | EnterCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); |
| 116 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 120 | bool RWMutexImpl::writer_release() { |
Michael J. Spencer | 0084615 | 2011-10-13 23:10:56 +0000 | [diff] [blame] | 121 | if (sHasSRW) { |
| 122 | fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(data_)); |
| 123 | } else { |
| 124 | LeaveCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); |
| 125 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | } |