]> git.saurik.com Git - wxWidgets.git/commitdiff
Test for reentrance into non-recursive wxMutex. If it happens, yield dead lock error...
authorRobert Roebling <robert@roebling.de>
Mon, 8 Sep 2008 08:00:03 +0000 (08:00 +0000)
committerRobert Roebling <robert@roebling.de>
Mon, 8 Sep 2008 08:00:03 +0000 (08:00 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55521 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/msw/thread.cpp
src/unix/threadpsx.cpp

index f6d9939eb4cf84b33813a57504e47467bca54982..cc4d13a2e6f37cfb926c3196a5dcd31de2313910 100644 (file)
@@ -191,7 +191,6 @@ private:
     HANDLE m_mutex;
     
     unsigned long m_owningThread;
-    bool m_isLocked;
     wxMutexType m_type;
 
     DECLARE_NO_COPY_CLASS(wxMutexInternal)
@@ -210,7 +209,6 @@ wxMutexInternal::wxMutexInternal(wxMutexType mutexType)
 
     m_type = mutexType;
     m_owningThread = 0;
-    m_isLocked = false;
 
     if ( !m_mutex )
     {
@@ -243,7 +241,7 @@ wxMutexError wxMutexInternal::LockTimeout(DWORD milliseconds)
     if (m_type == wxMUTEX_DEFAULT)
     {
         // Don't allow recursive
-        if (m_isLocked)
+        if (m_owningThread != 0)
         {
             if (m_owningThread == wxThread::GetCurrentId())
                 return wxMUTEX_DEAD_LOCK;
@@ -283,7 +281,6 @@ wxMutexError wxMutexInternal::LockTimeout(DWORD milliseconds)
     if (m_type == wxMUTEX_DEFAULT)
     { 
         // required for checking recursiveness
-        m_isLocked = true;
         m_owningThread = wxThread::GetCurrentId();
     }
     
@@ -292,15 +289,15 @@ wxMutexError wxMutexInternal::LockTimeout(DWORD milliseconds)
 
 wxMutexError wxMutexInternal::Unlock()
 {
+    // required for checking recursiveness
+    m_owningThread = 0;
+
     if ( !::ReleaseMutex(m_mutex) )
     {
         wxLogLastError(_T("ReleaseMutex()"));
 
         return wxMUTEX_MISC_ERROR;
     }
-    
-    // required for checking recursiveness
-    m_isLocked = false;
 
     return wxMUTEX_NO_ERROR;
 }
index a133ba9fa0d117dc7d662d414a69b81e98428856..99f497e9f8243a0b7fb38e5167423da4b68c531f 100644 (file)
@@ -189,6 +189,8 @@ private:
 private:
     pthread_mutex_t m_mutex;
     bool m_isOk;
+    wxMutexType m_type;
+    unsigned long m_owningThread;
 
     // wxConditionInternal uses our m_mutex
     friend class wxConditionInternal;
@@ -203,6 +205,9 @@ extern "C" int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
 
 wxMutexInternal::wxMutexInternal(wxMutexType mutexType)
 {
+    m_type = mutexType;
+    m_owningThread = 0;
+
     int err;
     switch ( mutexType )
     {
@@ -263,6 +268,12 @@ wxMutexInternal::~wxMutexInternal()
 
 wxMutexError wxMutexInternal::Lock()
 {
+    if ((m_type == wxMUTEX_DEFAULT) && (m_owningThread != 0))
+    {
+        if (m_owningThread == wxThread::GetCurrentId())
+           return wxMUTEX_DEAD_LOCK;
+    }
+        
     return HandleLockResult(pthread_mutex_lock(&m_mutex));
 }
 
@@ -318,6 +329,8 @@ wxMutexError wxMutexInternal::Lock(unsigned long ms)
 
 wxMutexError wxMutexInternal::HandleLockResult(int err)
 {
+    // wxPrintf( "err %d\n", err );
+
     switch ( err )
     {
         case EDEADLK:
@@ -334,6 +347,8 @@ wxMutexError wxMutexInternal::HandleLockResult(int err)
             return wxMUTEX_TIMEOUT;
 
         case 0:
+            if (m_type == wxMUTEX_DEFAULT)
+                m_owningThread = wxThread::GetCurrentId();
             return wxMUTEX_NO_ERROR;
 
         default:
@@ -359,6 +374,8 @@ wxMutexError wxMutexInternal::TryLock()
             break;
 
         case 0:
+            if (m_type == wxMUTEX_DEFAULT)
+                m_owningThread = wxThread::GetCurrentId();
             return wxMUTEX_NO_ERROR;
 
         default:
@@ -370,6 +387,8 @@ wxMutexError wxMutexInternal::TryLock()
 
 wxMutexError wxMutexInternal::Unlock()
 {
+    m_owningThread = 0;
+                
     int err = pthread_mutex_unlock(&m_mutex);
     switch ( err )
     {