[llvm-branch-commits] [lldb] r181003 - Locking on Windows: The standard Windows "Slim Multiple Read/Single Write" implementation crashes on multiple unlocks, lldb however depends on this behavior, so revert to the critical section based version.

Carlo Kok ck at remobjects.com
Fri May 3 01:28:51 PDT 2013


Author: carlokok
Date: Fri May  3 03:28:50 2013
New Revision: 181003

URL: http://llvm.org/viewvc/llvm-project?rev=181003&view=rev
Log:
Locking on Windows: The standard Windows "Slim Multiple Read/Single Write" implementation crashes on multiple unlocks, lldb however depends on this behavior, so revert to the critical section based version.

Modified:
    lldb/branches/windows/source/Host/windows/ReadWriteLock.cpp
    lldb/branches/windows/source/Target/Process.cpp

Modified: lldb/branches/windows/source/Host/windows/ReadWriteLock.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/windows/source/Host/windows/ReadWriteLock.cpp?rev=181003&r1=181002&r2=181003&view=diff
==============================================================================
--- lldb/branches/windows/source/Host/windows/ReadWriteLock.cpp (original)
+++ lldb/branches/windows/source/Host/windows/ReadWriteLock.cpp Fri May  3 03:28:50 2013
@@ -21,6 +21,7 @@ typedef struct Win32RWLOCK {
         unsigned long int readlockcount;
         HANDLE writable;
         CRITICAL_SECTION writelock;
+        unsigned long int writelocked;
 } Win32RWLOCK;
 
 typedef Win32RWLOCK* PWin32RWLOCK; 
@@ -39,6 +40,7 @@ static bool loadSRW() {
   static bool sChecked = false;
   if (!sChecked) {
     sChecked = true;
+    return false;
 
     HMODULE hLib = ::LoadLibrary(TEXT("Kernel32"));
     if (hLib) {
@@ -123,10 +125,12 @@ bool ReadWriteLock::ReadTryLock () {
 
 bool ReadWriteLock::ReadUnlock () {
     if (sHasSRW) {
-        fpReleaseSRWLockShared(static_cast<PSRWLOCK>(m_data));
+            fpReleaseSRWLockShared(static_cast<PSRWLOCK>(m_data));
         return true;
     } else {
-        if (InterlockedDecrement(&static_cast<PWin32RWLOCK>(m_data)->readlockcount) == 0)
+        unsigned long int value = InterlockedDecrement(&static_cast<PWin32RWLOCK>(m_data)->readlockcount);
+        assert(((int)value) >= 0);
+        if (value == 0)
             SetEvent(static_cast<PWin32RWLOCK>(m_data)->writable);
         return true;
     }
@@ -139,6 +143,8 @@ bool ReadWriteLock::WriteLock () {
     } else {
         EnterCriticalSection(&static_cast<PWin32RWLOCK>(m_data)->writelock);
         WaitForSingleObject(static_cast<PWin32RWLOCK>(m_data)->writable, INFINITE);
+        int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(m_data)->writelocked, 1);
+        assert(res == 0);
         return true;
     }
 }
@@ -148,10 +154,12 @@ bool ReadWriteLock::WriteTryLock () {
         return fpTryAcquireSRWLockExclusive(static_cast<PSRWLOCK>(m_data)) != 0;
     } else {
         if (TryEnterCriticalSection(&static_cast<PWin32RWLOCK>(m_data)->writelock)) {
-            if (!WaitForSingleObject(static_cast<PWin32RWLOCK>(m_data)->writable, 0)) {
+            if (WaitForSingleObject(static_cast<PWin32RWLOCK>(m_data)->writable, 0)) {
                 LeaveCriticalSection(&static_cast<PWin32RWLOCK>(m_data)->writelock);
                 return false;
             }
+            int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(m_data)->writelocked, 1);
+            assert(res == 0);
             return true;
         }
         return false;
@@ -163,8 +171,12 @@ bool ReadWriteLock::WriteUnlock () {
         fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(m_data));
         return true;
     } else {
-        LeaveCriticalSection(&static_cast<PWin32RWLOCK>(m_data)->writelock);
-        return true;
+        int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(m_data)->writelocked, 0);
+        if (res == 1) {
+            LeaveCriticalSection(&static_cast<PWin32RWLOCK>(m_data)->writelock);
+            return true;
+        }
+        return false;
     }
 }
 

Modified: lldb/branches/windows/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/windows/source/Target/Process.cpp?rev=181003&r1=181002&r2=181003&view=diff
==============================================================================
--- lldb/branches/windows/source/Target/Process.cpp (original)
+++ lldb/branches/windows/source/Target/Process.cpp Fri May  3 03:28:50 2013
@@ -3208,8 +3208,6 @@ Process::ConnectRemote (Stream *strm, co
         
             if (state == eStateStopped || state == eStateCrashed)
             {
-                if (m_public_run_lock.WriteTryLock())
-                {
                     // If we attached and actually have a process on the other end, then 
                     // this ended up being the equivalent of an attach.
                     CompleteAttach ();
@@ -3217,7 +3215,7 @@ Process::ConnectRemote (Stream *strm, co
                     // This delays passing the stopped event to listeners till 
                     // CompleteAttach gets a chance to complete...
                     HandlePrivateEvent (event_sp);
-                }
+                
             }
         }
 
@@ -3885,6 +3883,7 @@ Process::HandlePrivateEvent (EventSP &ev
                     // FIXME: should cons up an exited event, and discard this one.
                     SetExitStatus(0, m_next_event_action_ap->GetExitString());
                     SetNextEventAction(NULL);
+                    m_currently_handling_event.SetValue(false, eBroadcastAlways);
                     return;
                 }
                 SetNextEventAction(NULL);





More information about the llvm-branch-commits mailing list