summaryrefslogtreecommitdiffstats
path: root/lldb/source/Host/windows/ProcessRunLock.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2015-05-14 22:50:19 +0000
committerZachary Turner <zturner@google.com>2015-05-14 22:50:19 +0000
commit275806f90b718affbb7612ddccbedd642ba7965f (patch)
tree16f53db34da717cfdfe5a3e074f8e83dcb0bdfbb /lldb/source/Host/windows/ProcessRunLock.cpp
parentf0f00dc33b43fb05e3ab1c8a8c370c02098951a1 (diff)
downloadbcm5719-llvm-275806f90b718affbb7612ddccbedd642ba7965f.tar.gz
bcm5719-llvm-275806f90b718affbb7612ddccbedd642ba7965f.zip
Don't bother dynamic loading the Windows Slim RW Lock API.
This API has been present since XP, and I think it's safe to drop support for XP (since other things have been introduced long ago which already don't work on XP anyway). With this patch, we can statically bind against the exports and not bother falling back to a CRITICAL_SECTION if we can't load the API. llvm-svn: 237402
Diffstat (limited to 'lldb/source/Host/windows/ProcessRunLock.cpp')
-rw-r--r--lldb/source/Host/windows/ProcessRunLock.cpp295
1 files changed, 80 insertions, 215 deletions
diff --git a/lldb/source/Host/windows/ProcessRunLock.cpp b/lldb/source/Host/windows/ProcessRunLock.cpp
index 25d034b605e..1f21552a306 100644
--- a/lldb/source/Host/windows/ProcessRunLock.cpp
+++ b/lldb/source/Host/windows/ProcessRunLock.cpp
@@ -1,241 +1,106 @@
-#ifdef _WIN32
-
#include "lldb/Host/ProcessRunLock.h"
#include "lldb/Host/windows/windows.h"
-namespace lldb_private {
-
- // 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.
+namespace
+{
#if defined(__MINGW32__)
- // Taken from WinNT.h
- typedef struct _RTL_SRWLOCK {
- PVOID Ptr;
- } RTL_SRWLOCK, *PRTL_SRWLOCK;
+// Taken from WinNT.h
+typedef struct _RTL_SRWLOCK {
+ PVOID Ptr;
+} RTL_SRWLOCK, *PRTL_SRWLOCK;
- // Taken from WinBase.h
- typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
+// Taken from WinBase.h
+typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
#endif
+}
- typedef struct Win32RWLOCK
- {
- long int readlockcount;
- HANDLE writable;
- CRITICAL_SECTION writelock;
- long int writelocked;
- } Win32RWLOCK;
-
- typedef Win32RWLOCK* PWin32RWLOCK;
+static PSRWLOCK GetLock(lldb::rwlock_t lock)
+{
+ return static_cast<PSRWLOCK>(lock);
+}
- 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 (WINAPI *fpTryAcquireSRWLockExclusive)(PSRWLOCK lock) = NULL;
- static BOOL (WINAPI *fpTryAcquireSRWLockShared)(PSRWLOCK lock) = NULL;
+static bool ReadLock(lldb::rwlock_t rwlock)
+{
+ ::AcquireSRWLockShared(GetLock(rwlock));
+ return true;
+}
- static bool sHasSRW = false;
+static bool ReadUnlock(lldb::rwlock_t rwlock)
+{
+ ::ReleaseSRWLockShared(GetLock(rwlock));
+ return true;
+}
- static bool loadSRW()
- {
- static bool sChecked = false;
- if (!sChecked)
- {
- sChecked = true;
- return false;
-
- HMODULE hLib = ::LoadLibrary(TEXT("Kernel32"));
- 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");
- fpTryAcquireSRWLockExclusive =
- (BOOL (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
- "TryAcquireSRWLockExclusive");
- fpTryAcquireSRWLockShared =
- (BOOL (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
- "TryAcquireSRWLockShared");
-
- ::FreeLibrary(hLib);
-
- if (fpInitializeSRWLock != NULL) {
- sHasSRW = true;
- }
- }
- }
- return sHasSRW;
- }
+static bool WriteLock(lldb::rwlock_t rwlock)
+{
+ ::AcquireSRWLockExclusive(GetLock(rwlock));
+ return true;
+}
- ProcessRunLock::ProcessRunLock ()
- : m_running(false)
- {
- if (loadSRW())
- {
- m_rwlock = calloc(1, sizeof(SRWLOCK));
- fpInitializeSRWLock(static_cast<PSRWLOCK>(m_rwlock));
- }
- else
- {
- m_rwlock = calloc(1, sizeof(Win32RWLOCK));
- static_cast<PWin32RWLOCK>(m_rwlock)->readlockcount = 0;
- static_cast<PWin32RWLOCK>(m_rwlock)->writable = CreateEvent(NULL, true, true, NULL);
- InitializeCriticalSection(&static_cast<PWin32RWLOCK>(m_rwlock)->writelock);
- }
- }
+static bool WriteTryLock(lldb::rwlock_t rwlock)
+{
+ return !!::TryAcquireSRWLockExclusive(GetLock(rwlock));
+}
- ProcessRunLock::~ProcessRunLock ()
- {
- if (!sHasSRW)
- {
- CloseHandle(static_cast<PWin32RWLOCK>(m_rwlock)->writable);
- DeleteCriticalSection(&static_cast<PWin32RWLOCK>(m_rwlock)->writelock);
- }
- free(m_rwlock);
- }
+static bool WriteUnlock(lldb::rwlock_t rwlock)
+{
+ ::ReleaseSRWLockExclusive(GetLock(rwlock));
+ return true;
+}
- bool ReadLock (lldb::rwlock_t rwlock)
- {
- if (sHasSRW)
- {
- fpAcquireSRWLockShared(static_cast<PSRWLOCK>(rwlock));
- return true;
- }
- else
- {
- EnterCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
- InterlockedIncrement(&static_cast<PWin32RWLOCK>(rwlock)->readlockcount);
- ResetEvent(static_cast<PWin32RWLOCK>(rwlock)->writable);
- LeaveCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
- return true;
- }
- }
+using namespace lldb_private;
- bool ProcessRunLock::ReadTryLock ()
- {
- ReadLock(m_rwlock);
- if (m_running == false)
- return true;
- ReadUnlock();
- return false;
- }
+ProcessRunLock::ProcessRunLock()
+ : m_running(false)
+{
+ m_rwlock = new SRWLOCK;
+ InitializeSRWLock(GetLock(m_rwlock));
+}
- bool ProcessRunLock::ReadUnlock ()
- {
- if (sHasSRW)
- {
- fpReleaseSRWLockShared(static_cast<PSRWLOCK>(m_rwlock));
- return true;
- }
- else
- {
- unsigned long int value = InterlockedDecrement(&static_cast<PWin32RWLOCK>(m_rwlock)->readlockcount);
- assert(((int)value) >= 0);
- if (value == 0)
- SetEvent(static_cast<PWin32RWLOCK>(m_rwlock)->writable);
- return true;
- }
- }
+ProcessRunLock::~ProcessRunLock()
+{
+ delete m_rwlock;
+}
- bool WriteLock(lldb::rwlock_t rwlock)
- {
- if (sHasSRW)
- {
- fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(rwlock));
- return true;
- }
- else
- {
- EnterCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
- WaitForSingleObject(static_cast<PWin32RWLOCK>(rwlock)->writable, INFINITE);
- int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(rwlock)->writelocked, 1);
- assert(res == 0);
- return true;
- }
- }
+bool ProcessRunLock::ReadTryLock()
+{
+ ::ReadLock(m_rwlock);
+ if (m_running == false)
+ return true;
+ ::ReadUnlock(m_rwlock);
+ return false;
+}
- bool WriteTryLock(lldb::rwlock_t rwlock)
- {
- if (sHasSRW)
- {
- return fpTryAcquireSRWLockExclusive(static_cast<PSRWLOCK>(rwlock)) != 0;
- }
- else
- {
- if (TryEnterCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock)) {
- if (WaitForSingleObject(static_cast<PWin32RWLOCK>(rwlock)->writable, 0)) {
- LeaveCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
- return false;
- }
- int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(rwlock)->writelocked, 1);
- assert(res == 0);
- return true;
- }
- return false;
- }
- }
+bool ProcessRunLock::ReadUnlock()
+{
+ return ::ReadUnlock(m_rwlock);
+}
- bool WriteUnlock(lldb::rwlock_t rwlock)
- {
- if (sHasSRW)
- {
- fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(rwlock));
- return true;
- }
- else
- {
- int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(rwlock)->writelocked, 0);
- if (res == 1) {
- LeaveCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
- return true;
- }
- return false;
- }
- }
+bool ProcessRunLock::SetRunning ()
+{
+ WriteLock(m_rwlock);
+ m_running = true;
+ WriteUnlock(m_rwlock);
+ return true;
+}
- bool ProcessRunLock::SetRunning ()
+bool ProcessRunLock::TrySetRunning ()
+{
+ if (WriteTryLock(m_rwlock))
{
- WriteLock(m_rwlock);
+ bool was_running = m_running;
m_running = true;
WriteUnlock(m_rwlock);
- return true;
- }
-
- bool ProcessRunLock::TrySetRunning ()
- {
- if (WriteTryLock(m_rwlock))
- {
- bool r = !m_running;
- m_running = true;
- WriteUnlock(m_rwlock);
- return r;
- }
- return false;
- }
-
- bool ProcessRunLock::SetStopped ()
- {
- WriteLock(m_rwlock);
- m_running = false;
- WriteUnlock(m_rwlock);
- return true;
+ return !was_running;
}
+ return false;
}
-#endif
+bool ProcessRunLock::SetStopped ()
+{
+ WriteLock(m_rwlock);
+ m_running = false;
+ WriteUnlock(m_rwlock);
+ return true;
+}
OpenPOWER on IntegriCloud