]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/threadpsx.cpp
blind OS/2 compilation fix in wxSetWorkingDirectory()
[wxWidgets.git] / src / unix / threadpsx.cpp
index 1104048d4e6d09383331449c3ff80d22b691b63b..5d7c3bcd2aba900fe340779a8f24c01d63907ba9 100644 (file)
     #include "wx/utils.h"
     #include "wx/timer.h"
     #include "wx/stopwatch.h"
     #include "wx/utils.h"
     #include "wx/timer.h"
     #include "wx/stopwatch.h"
+    #include "wx/module.h"
 #endif
 
 #endif
 
-#include "wx/module.h"
-
 #include <stdio.h>
 #include <unistd.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <pthread.h>
@@ -164,11 +163,16 @@ public:
     ~wxMutexInternal();
 
     wxMutexError Lock();
     ~wxMutexInternal();
 
     wxMutexError Lock();
+    wxMutexError Lock(unsigned long ms);
     wxMutexError TryLock();
     wxMutexError Unlock();
 
     bool IsOk() const { return m_isOk; }
 
     wxMutexError TryLock();
     wxMutexError Unlock();
 
     bool IsOk() const { return m_isOk; }
 
+private:
+    // convert the result of pthread_mutex_[timed]lock() call to wx return code
+    wxMutexError HandleLockResult(int err);
+
 private:
     pthread_mutex_t m_mutex;
     bool m_isOk;
 private:
     pthread_mutex_t m_mutex;
     bool m_isOk;
@@ -246,7 +250,61 @@ wxMutexInternal::~wxMutexInternal()
 
 wxMutexError wxMutexInternal::Lock()
 {
 
 wxMutexError wxMutexInternal::Lock()
 {
-    int err = pthread_mutex_lock(&m_mutex);
+    return HandleLockResult(pthread_mutex_lock(&m_mutex));
+}
+
+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 )
+    {
+    }
+#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
+    {
+        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)
+{
     switch ( err )
     {
         case EDEADLK:
     switch ( err )
     {
         case EDEADLK:
@@ -256,19 +314,23 @@ wxMutexError wxMutexInternal::Lock()
             return wxMUTEX_DEAD_LOCK;
 
         case EINVAL:
             return wxMUTEX_DEAD_LOCK;
 
         case EINVAL:
-            wxLogDebug(_T("pthread_mutex_lock(): mutex not initialized."));
+            wxLogDebug(_T("pthread_mutex_[timed]lock(): mutex not initialized"));
             break;
 
             break;
 
+        case ETIMEDOUT:
+            return wxMUTEX_TIMEOUT;
+
         case 0:
             return wxMUTEX_NO_ERROR;
 
         default:
         case 0:
             return wxMUTEX_NO_ERROR;
 
         default:
-            wxLogApiError(_T("pthread_mutex_lock()"), err);
+            wxLogApiError(_T("pthread_mutex_[timed]lock()"), err);
     }
 
     return wxMUTEX_MISC_ERROR;
 }
 
     }
 
     return wxMUTEX_MISC_ERROR;
 }
 
+
 wxMutexError wxMutexInternal::TryLock()
 {
     int err = pthread_mutex_trylock(&m_mutex);
 wxMutexError wxMutexInternal::TryLock()
 {
     int err = pthread_mutex_trylock(&m_mutex);
@@ -989,7 +1051,14 @@ void wxThread::Sleep(unsigned long milliseconds)
 
 int wxThread::GetCPUCount()
 {
 
 int wxThread::GetCPUCount()
 {
-#if defined(__LINUX__) && wxUSE_FFILE
+#if defined(_SC_NPROCESSORS_ONLN)
+    // this works for Solaris and Linux 2.6
+    int rc = sysconf(_SC_NPROCESSORS_ONLN);
+    if ( rc != -1 )
+    {
+        return rc;
+    }
+#elif defined(__LINUX__) && wxUSE_FFILE
     // read from proc (can't use wxTextFile here because it's a special file:
     // it has 0 size but still can be read from)
     wxLogNull nolog;
     // read from proc (can't use wxTextFile here because it's a special file:
     // it has 0 size but still can be read from)
     wxLogNull nolog;
@@ -1015,13 +1084,6 @@ int wxThread::GetCPUCount()
             wxLogDebug(_T("failed to read /proc/cpuinfo"));
         }
     }
             wxLogDebug(_T("failed to read /proc/cpuinfo"));
         }
     }
-#elif defined(_SC_NPROCESSORS_ONLN)
-    // this works for Solaris
-    int rc = sysconf(_SC_NPROCESSORS_ONLN);
-    if ( rc != -1 )
-    {
-        return rc;
-    }
 #endif // different ways to get number of CPUs
 
     // unknown
 #endif // different ways to get number of CPUs
 
     // unknown
@@ -1702,7 +1764,7 @@ static void ScheduleThreadForDeletion()
 
     wxLogTrace(TRACE_THREADS, _T("%lu thread%s waiting to be deleted"),
                (unsigned long)gs_nThreadsBeingDeleted,
 
     wxLogTrace(TRACE_THREADS, _T("%lu thread%s waiting to be deleted"),
                (unsigned long)gs_nThreadsBeingDeleted,
-               gs_nThreadsBeingDeleted == 1 ? "" : "s");
+               gs_nThreadsBeingDeleted == 1 ? _T("") : _T("s"));
 }
 
 static void DeleteThread(wxThread *This)
 }
 
 static void DeleteThread(wxThread *This)