]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/threadpsx.cpp
Fix waiting for IO on UDP sockets.
[wxWidgets.git] / src / unix / threadpsx.cpp
index 700fd19324c2ab5dc83accc6c38be86657c84666..031ade47b59acc933be89711ef0a8a22c331813e 100644 (file)
@@ -45,6 +45,7 @@
 #include <pthread.h>
 #include <errno.h>
 #include <time.h>
+#include <sys/time.h>           // needed for at least __QNX__
 #ifdef HAVE_SCHED_H
     #include <sched.h>
 #endif
 // we use wxFFile under Linux in GetCPUCount()
 #ifdef __LINUX__
     #include "wx/ffile.h"
-    // For setpriority.
-    #include <sys/time.h>
-    #include <sys/resource.h>
+    #include <sys/resource.h>   // for setpriority()
 #endif
 
-#ifdef __VMS
-    #define THR_ID(thr) ((long long)(thr)->GetId())
-#else
-    #define THR_ID(thr) ((long)(thr)->GetId())
-#endif
+#define THR_ID_CAST(id)  (reinterpret_cast<void*>(id))
+#define THR_ID(thr)      THR_ID_CAST((thr)->GetId())
 
 // ----------------------------------------------------------------------------
 // constants
@@ -594,14 +590,14 @@ wxSemaError wxSemaphoreInternal::Wait()
     {
         wxLogTrace(TRACE_SEMA,
                    wxT("Thread %p waiting for semaphore to become signalled"),
-                   wxThread::GetCurrentId());
+                   THR_ID_CAST(wxThread::GetCurrentId()));
 
         if ( m_cond.Wait() != wxCOND_NO_ERROR )
             return wxSEMA_MISC_ERROR;
 
         wxLogTrace(TRACE_SEMA,
                    wxT("Thread %p finished waiting for semaphore, count = %lu"),
-                   wxThread::GetCurrentId(), (unsigned long)m_count);
+                   THR_ID_CAST(wxThread::GetCurrentId()), (unsigned long)m_count);
     }
 
     m_count--;
@@ -668,7 +664,7 @@ wxSemaError wxSemaphoreInternal::Post()
 
     wxLogTrace(TRACE_SEMA,
                wxT("Thread %p about to signal semaphore, count = %lu"),
-               wxThread::GetCurrentId(), (unsigned long)m_count);
+               THR_ID_CAST(wxThread::GetCurrentId()), (unsigned long)m_count);
 
     return m_cond.Signal() == wxCOND_NO_ERROR ? wxSEMA_NO_ERROR
                                               : wxSEMA_MISC_ERROR;
@@ -734,7 +730,7 @@ public:
         };
 
         wxLogTrace(TRACE_THREADS, wxT("Thread %p: %s => %s."),
-                   GetId(), stateNames[m_state], stateNames[state]);
+                   THR_ID(this), stateNames[m_state], stateNames[state]);
 #endif // wxUSE_LOG_TRACE
 
         m_state = state;
@@ -1393,7 +1389,7 @@ wxThreadError wxThread::Resume()
     {
         case STATE_PAUSED:
             wxLogTrace(TRACE_THREADS, wxT("Thread %p suspended, resuming."),
-                       GetId());
+                       THR_ID(this));
 
             m_internal->Resume();
 
@@ -1401,7 +1397,7 @@ wxThreadError wxThread::Resume()
 
         case STATE_EXITED:
             wxLogTrace(TRACE_THREADS, wxT("Thread %p exited, won't resume."),
-                       GetId());
+                       THR_ID(this));
             return wxTHREAD_NO_ERROR;
 
         default:
@@ -1477,6 +1473,11 @@ wxThreadError wxThread::Delete(ExitCode *rc)
             }
             //else: can't wait for detached threads
     }
+    
+    if (state == STATE_NEW)
+        return wxTHREAD_MISC_ERROR;
+            // for coherency with the MSW implementation, signal the user that 
+            // Delete() was called on a thread which didn't start to run yet.
 
     return wxTHREAD_NO_ERROR;
 }
@@ -1617,8 +1618,8 @@ wxThread::~wxThread()
     if ( m_internal->GetState() != STATE_EXITED &&
          m_internal->GetState() != STATE_NEW )
     {
-        wxLogDebug(wxT("The thread %ld is being destroyed although it is still running! The application may crash."),
-                   (long)GetId());
+        wxLogDebug(wxT("The thread %p is being destroyed although it is still running! The application may crash."),
+                   THR_ID(this));
     }
 
     m_critsect.Leave();
@@ -1778,14 +1779,15 @@ static void ScheduleThreadForDeletion()
 
 static void DeleteThread(wxThread *This)
 {
-    // gs_mutexDeleteThread should be unlocked before signalling the condition
-    // or wxThreadModule::OnExit() would deadlock
-    wxMutexLocker locker( *gs_mutexDeleteThread );
-
-    wxLogTrace(TRACE_THREADS, wxT("Thread %p auto deletes."), This->GetId());
+    wxLogTrace(TRACE_THREADS, wxT("Thread %p auto deletes."), THR_ID(This));
 
     delete This;
 
+    // only lock gs_mutexDeleteThread after deleting the thread to avoid
+    // calling out into user code with it locked as this may result in
+    // deadlocks if the thread dtor deletes another thread (see #11501)
+    wxMutexLocker locker( *gs_mutexDeleteThread );
+
     wxCHECK_RET( gs_nThreadsBeingDeleted > 0,
                  wxT("no threads scheduled for deletion, yet we delete one?") );