[Lldb-commits] [lldb] r237402 - Don't bother dynamic loading the Windows Slim RW Lock API.

Zachary Turner zturner at google.com
Thu May 14 15:50:19 PDT 2015


Author: zturner
Date: Thu May 14 17:50:19 2015
New Revision: 237402

URL: http://llvm.org/viewvc/llvm-project?rev=237402&view=rev
Log:
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.

Modified:
    lldb/trunk/source/Host/windows/ProcessRunLock.cpp

Modified: lldb/trunk/source/Host/windows/ProcessRunLock.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/ProcessRunLock.cpp?rev=237402&r1=237401&r2=237402&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/ProcessRunLock.cpp (original)
+++ lldb/trunk/source/Host/windows/ProcessRunLock.cpp Thu May 14 17:50:19 2015
@@ -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 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 sHasSRW = false;
-
-    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 PSRWLOCK GetLock(lldb::rwlock_t lock)
+{
+    return static_cast<PSRWLOCK>(lock);
+}
 
-    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 ReadLock(lldb::rwlock_t rwlock)
+{
+    ::AcquireSRWLockShared(GetLock(rwlock));
+    return true;
+}
 
-    ProcessRunLock::~ProcessRunLock ()
-    {
-        if (!sHasSRW)
-        {
-            CloseHandle(static_cast<PWin32RWLOCK>(m_rwlock)->writable);
-            DeleteCriticalSection(&static_cast<PWin32RWLOCK>(m_rwlock)->writelock);
-        }
-        free(m_rwlock);
-    }
+static bool ReadUnlock(lldb::rwlock_t rwlock)
+{
+    ::ReleaseSRWLockShared(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;
-        }
-    }
+static bool WriteLock(lldb::rwlock_t rwlock)
+{
+    ::AcquireSRWLockExclusive(GetLock(rwlock));
+    return true;
+}
 
-    bool ProcessRunLock::ReadTryLock ()
-    {
-        ReadLock(m_rwlock);
-        if (m_running == false)
-            return true;
-        ReadUnlock();
-        return false;
-    }
+static bool WriteTryLock(lldb::rwlock_t rwlock)
+{
+    return !!::TryAcquireSRWLockExclusive(GetLock(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;
-        }
-    }
+static bool WriteUnlock(lldb::rwlock_t rwlock)
+{
+    ::ReleaseSRWLockExclusive(GetLock(rwlock));
+    return true;
+}
 
-    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;
-        }
-    }
+using namespace lldb_private;
 
-    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;
-        }
-    }
+ProcessRunLock::ProcessRunLock()
+    : m_running(false)
+{
+    m_rwlock = new SRWLOCK;
+    InitializeSRWLock(GetLock(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;
-        }
-    }
+ProcessRunLock::~ProcessRunLock()
+{
+    delete m_rwlock;
+}
 
-    bool ProcessRunLock::SetRunning ()
-    {
-        WriteLock(m_rwlock);
-        m_running = true;
-        WriteUnlock(m_rwlock);
+bool ProcessRunLock::ReadTryLock()
+{
+    ::ReadLock(m_rwlock);
+    if (m_running == false)
         return true;
-    }
+    ::ReadUnlock(m_rwlock);
+    return false;
+}
 
-    bool ProcessRunLock::TrySetRunning ()
-    {
-        if (WriteTryLock(m_rwlock))
-        {
-            bool r = !m_running;
-            m_running = true;
-            WriteUnlock(m_rwlock);
-            return r;
-        }
-        return false;
-    }
+bool ProcessRunLock::ReadUnlock()
+{
+    return ::ReadUnlock(m_rwlock);
+}
 
-    bool ProcessRunLock::SetStopped ()
+bool ProcessRunLock::SetRunning ()
+{
+    WriteLock(m_rwlock);
+    m_running = true;
+    WriteUnlock(m_rwlock);
+    return true;
+}
+
+bool ProcessRunLock::TrySetRunning ()
+{
+    if (WriteTryLock(m_rwlock))
     {
-        WriteLock(m_rwlock);
-        m_running = false;
+        bool was_running = m_running;
+        m_running = true;
         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;
+}





More information about the lldb-commits mailing list