]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/threadpsx.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[wxWidgets.git] / src / unix / threadpsx.cpp
index 36943023ced8ed393db9cf76f22a68092056571c..0384805c1ad09e9f98354e583ef09a43b0d2ba5f 100644 (file)
 #if wxUSE_THREADS
 
 #include "wx/thread.h"
+#include "wx/except.h"
 
 #ifndef WX_PRECOMP
+    #include "wx/app.h"
     #include "wx/dynarray.h"
     #include "wx/intl.h"
     #include "wx/log.h"
@@ -255,32 +257,52 @@ wxMutexError wxMutexInternal::Lock()
 
 wxMutexError wxMutexInternal::Lock(unsigned long ms)
 {
+#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
     static const long MSEC_IN_SEC   = 1000;
     static const long NSEC_IN_MSEC  = 1000000;
+    static const long NSEC_IN_USEC  = 1000;
     static const long NSEC_IN_SEC   = MSEC_IN_SEC * NSEC_IN_MSEC;
 
     time_t seconds = ms/MSEC_IN_SEC;
     long nanoseconds = (ms % MSEC_IN_SEC) * NSEC_IN_MSEC;
     timespec ts = { 0, 0 };
 
+    // normally we should use clock_gettime(CLOCK_REALTIME) here but this
+    // function is in librt and we don't link with it currently, so use
+    // gettimeofday() instead -- if it turns out that this is really too
+    // imprecise, we should modify configure to check if clock_gettime() is
+    // available and whether it requires -lrt and use it instead
+#if 0
     if ( clock_gettime(CLOCK_REALTIME, &ts) == 0 )
     {
-        ts.tv_sec += seconds;
-        ts.tv_nsec += nanoseconds;
-        if ( ts.tv_nsec > NSEC_IN_SEC )
-        {
-            ts.tv_sec += 1;
-            ts.tv_nsec -= NSEC_IN_SEC;
-        }
     }
+#else
+    struct timeval tv;
+    if ( wxGetTimeOfDay(&tv) != -1 )
+    {
+        ts.tv_sec = tv.tv_sec;
+        ts.tv_nsec = tv.tv_usec*NSEC_IN_USEC;
+    }
+#endif
     else // fall back on system timer
     {
-        wxLogDebug(_T("clock_gettime(CLOCK_REALTIME) failed"));
-        ts.tv_sec = time(NULL) + seconds;
-        ts.tv_nsec = nanoseconds;
+        ts.tv_sec = time(NULL);
+    }
+
+    ts.tv_sec += seconds;
+    ts.tv_nsec += nanoseconds;
+    if ( ts.tv_nsec > NSEC_IN_SEC )
+    {
+        ts.tv_sec += 1;
+        ts.tv_nsec -= NSEC_IN_SEC;
     }
 
     return HandleLockResult(pthread_mutex_timedlock(&m_mutex, &ts));
+#else // !HAVE_PTHREAD_MUTEX_TIMEDLOCK
+    wxUnusedVar(ms);
+
+    return wxMUTEX_MISC_ERROR;
+#endif // HAVE_PTHREAD_MUTEX_TIMEDLOCK/!HAVE_PTHREAD_MUTEX_TIMEDLOCK
 }
 
 wxMutexError wxMutexInternal::HandleLockResult(int err)
@@ -806,11 +828,15 @@ void *wxThreadInternal::PthreadStart(wxThread *thread)
                    _T("Thread %ld about to enter its Entry()."),
                    THR_ID(pthread));
 
-        pthread->m_exitcode = thread->Entry();
+        wxTRY
+        {
+            pthread->m_exitcode = thread->Entry();
 
-        wxLogTrace(TRACE_THREADS,
-                   _T("Thread %ld Entry() returned %lu."),
-                   THR_ID(pthread), wxPtrToUInt(pthread->m_exitcode));
+            wxLogTrace(TRACE_THREADS,
+                       _T("Thread %ld Entry() returned %lu."),
+                       THR_ID(pthread), wxPtrToUInt(pthread->m_exitcode));
+        }
+        wxCATCH_ALL( wxTheApp->OnUnhandledException(); )
 
         {
             wxCriticalSectionLocker lock(thread->m_critsect);
@@ -1291,7 +1317,7 @@ void wxThread::SetPriority(unsigned int prio)
 
             // map wx priorites WXTHREAD_MIN_PRIORITY..WXTHREAD_MAX_PRIORITY
             // to Unix priorities 20..-20
-            if ( setpriority(PRIO_PROCESS, 0, -(2*prio)/5 + 20) == -1 )
+            if ( setpriority(PRIO_PROCESS, 0, -(2*(int)prio)/5 + 20) == -1 )
             {
                 wxLogError(_("Failed to set thread priority %d."), prio);
             }
@@ -1522,7 +1548,11 @@ void wxThread::Exit(ExitCode status)
     // might deadlock if, for example, it signals a condition in OnExit() (a
     // common case) while the main thread calls any of functions entering
     // m_critsect on us (almost all of them do)
-    OnExit();
+    wxTRY
+    {
+        OnExit();
+    }
+    wxCATCH_ALL( wxTheApp->OnUnhandledException(); )
 
     // delete C++ thread object if this is a detached thread - user is
     // responsible for doing this for joinable ones