X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/165d3652ffb8399626c37e0f65b50560a1369bc5..96ca74cdda2a8ed485facb45de263e5b7b51d7be:/src/unix/threadpsx.cpp diff --git a/src/unix/threadpsx.cpp b/src/unix/threadpsx.cpp index ad7585062f..29288623cd 100644 --- a/src/unix/threadpsx.cpp +++ b/src/unix/threadpsx.cpp @@ -27,8 +27,10 @@ #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" @@ -132,12 +134,10 @@ static wxMutex *gs_mutexDeleteThread = (wxMutex *)NULL; // gs_nThreadsBeingDeleted will have been deleted static wxCondition *gs_condAllDeleted = (wxCondition *)NULL; -#ifndef __WXGTK20__ // this mutex must be acquired before any call to a GUI function // (it's not inside #if wxUSE_GUI because this file is compiled as part // of wxBase) static wxMutex *gs_mutexGui = NULL; -#endif // when we wait for a thread to exit, we're blocking on a condition which the // thread signals in its SignalExit() method -- but this condition can't be a @@ -165,11 +165,16 @@ public: ~wxMutexInternal(); wxMutexError Lock(); + wxMutexError Lock(unsigned long ms); 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; @@ -247,7 +252,61 @@ wxMutexInternal::~wxMutexInternal() 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: @@ -257,19 +316,23 @@ wxMutexError wxMutexInternal::Lock() return wxMUTEX_DEAD_LOCK; case EINVAL: - wxLogDebug(_T("pthread_mutex_lock(): mutex not initialized.")); + wxLogDebug(_T("pthread_mutex_[timed]lock(): mutex not initialized")); break; + case ETIMEDOUT: + return wxMUTEX_TIMEOUT; + case 0: return wxMUTEX_NO_ERROR; default: - wxLogApiError(_T("pthread_mutex_lock()"), err); + wxLogApiError(_T("pthread_mutex_[timed]lock()"), err); } return wxMUTEX_MISC_ERROR; } + wxMutexError wxMutexInternal::TryLock() { int err = pthread_mutex_trylock(&m_mutex); @@ -765,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); @@ -983,14 +1050,16 @@ void wxThread::Yield() #endif } -void wxThread::Sleep(unsigned long milliseconds) -{ - wxMilliSleep(milliseconds); -} - 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; @@ -1016,13 +1085,6 @@ int wxThread::GetCPUCount() 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 @@ -1250,7 +1312,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); } @@ -1481,7 +1543,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 @@ -1626,10 +1692,8 @@ bool wxThreadModule::OnInit() gs_mutexAllThreads = new wxMutex(); -#ifndef __WXGTK20__ gs_mutexGui = new wxMutex(); gs_mutexGui->Lock(); -#endif gs_mutexDeleteThread = new wxMutex(); gs_condAllDeleted = new wxCondition(*gs_mutexDeleteThread); @@ -1682,11 +1746,9 @@ void wxThreadModule::OnExit() delete gs_mutexAllThreads; -#ifndef __WXGTK20__ // destroy GUI mutex gs_mutexGui->Unlock(); delete gs_mutexGui; -#endif // and free TLD slot (void)pthread_key_delete(gs_keySelf); @@ -1733,17 +1795,15 @@ static void DeleteThread(wxThread *This) } } -#ifndef __WXGTK20__ -void wxMutexGuiEnter() +void wxMutexGuiEnterImpl() { gs_mutexGui->Lock(); } -void wxMutexGuiLeave() +void wxMutexGuiLeaveImpl() { gs_mutexGui->Unlock(); } -#endif // ---------------------------------------------------------------------------- // include common implementation code