#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"
+ #include "wx/utils.h"
+ #include "wx/timer.h"
+ #include "wx/stopwatch.h"
+ #include "wx/module.h"
#endif
-#include "wx/module.h"
-#include "wx/utils.h"
-#include "wx/log.h"
-#include "wx/timer.h"
-#include "wx/stopwatch.h"
-
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
// be left in memory
static wxArrayThread gs_allThreads;
+// a mutex to protect gs_allThreads
+static wxMutex *gs_mutexAllThreads = NULL;
+
// the id of the main thread
static pthread_t gs_tidMain = (pthread_t)-1;
static size_t gs_nThreadsBeingDeleted = 0;
// a mutex to protect gs_nThreadsBeingDeleted
-static wxMutex *gs_mutexDeleteThread = (wxMutex *)NULL;
+static wxMutex *gs_mutexDeleteThread = NULL;
// and a condition variable which will be signaled when all
// gs_nThreadsBeingDeleted will have been deleted
-static wxCondition *gs_condAllDeleted = (wxCondition *)NULL;
+static wxCondition *gs_condAllDeleted = NULL;
// 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
~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;
+ wxMutexType m_type;
+ unsigned long m_owningThread;
// wxConditionInternal uses our m_mutex
friend class wxConditionInternal;
wxMutexInternal::wxMutexInternal(wxMutexType mutexType)
{
+ m_type = mutexType;
+ m_owningThread = 0;
+
int err;
switch ( mutexType )
{
wxMutexError wxMutexInternal::Lock()
{
- int err = pthread_mutex_lock(&m_mutex);
+ if ((m_type == wxMUTEX_DEFAULT) && (m_owningThread != 0))
+ {
+ if (m_owningThread == wxThread::GetCurrentId())
+ return wxMUTEX_DEAD_LOCK;
+ }
+
+ 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)
+{
+ // wxPrintf( "err %d\n", err );
+
switch ( err )
{
case EDEADLK:
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:
+ if (m_type == wxMUTEX_DEFAULT)
+ m_owningThread = wxThread::GetCurrentId();
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);
break;
case 0:
+ if (m_type == wxMUTEX_DEFAULT)
+ m_owningThread = wxThread::GetCurrentId();
return wxMUTEX_NO_ERROR;
default:
wxMutexError wxMutexInternal::Unlock()
{
+ m_owningThread = 0;
+
int err = pthread_mutex_unlock(&m_mutex);
switch ( err )
{
while ( m_count == 0 )
{
wxLogTrace(TRACE_SEMA,
- _T("Thread %ld waiting for semaphore to become signalled"),
+ _T("Thread %p waiting for semaphore to become signalled"),
wxThread::GetCurrentId());
if ( m_cond.Wait() != wxCOND_NO_ERROR )
return wxSEMA_MISC_ERROR;
wxLogTrace(TRACE_SEMA,
- _T("Thread %ld finished waiting for semaphore, count = %lu"),
+ _T("Thread %p finished waiting for semaphore, count = %lu"),
wxThread::GetCurrentId(), (unsigned long)m_count);
}
m_count++;
wxLogTrace(TRACE_SEMA,
- _T("Thread %ld about to signal semaphore, count = %lu"),
+ _T("Thread %p about to signal semaphore, count = %lu"),
wxThread::GetCurrentId(), (unsigned long)m_count);
return m_cond.Signal() == wxCOND_NO_ERROR ? wxSEMA_NO_ERROR
_T("EXITED"),
};
- wxLogTrace(TRACE_THREADS, _T("Thread %ld: %s => %s."),
- (long)GetId(), stateNames[m_state], stateNames[state]);
+ wxLogTrace(TRACE_THREADS, _T("Thread %p: %s => %s."),
+ GetId(), stateNames[m_state], stateNames[state]);
#endif // __WXDEBUG__
m_state = state;
{
wxThreadInternal *pthread = thread->m_internal;
- wxLogTrace(TRACE_THREADS, _T("Thread %ld started."), THR_ID(pthread));
+ wxLogTrace(TRACE_THREADS, _T("Thread %p started."), THR_ID(pthread));
// associate the thread pointer with the newly created thread so that
// wxThread::This() will work
{
// call the main entry
wxLogTrace(TRACE_THREADS,
- _T("Thread %ld about to enter its Entry()."),
+ _T("Thread %p 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 %p Entry() returned %lu."),
+ THR_ID(pthread), wxPtrToUInt(pthread->m_exitcode));
+ }
+ wxCATCH_ALL( wxTheApp->OnUnhandledException(); )
{
wxCriticalSectionLocker lock(thread->m_critsect);
wxMutexGuiLeave();
wxLogTrace(TRACE_THREADS,
- _T("Starting to wait for thread %ld to exit."),
+ _T("Starting to wait for thread %p to exit."),
THR_ID(this));
// to avoid memory leaks we should call pthread_join(), but it must only be
wxT("thread must first be paused with wxThread::Pause().") );
wxLogTrace(TRACE_THREADS,
- _T("Thread %ld goes to sleep."), THR_ID(this));
+ _T("Thread %p goes to sleep."), THR_ID(this));
// wait until the semaphore is Post()ed from Resume()
m_semSuspend.Wait();
if ( IsReallyPaused() )
{
wxLogTrace(TRACE_THREADS,
- _T("Waking up thread %ld"), THR_ID(this));
+ _T("Waking up thread %p"), THR_ID(this));
// wake up Pause()
m_semSuspend.Post();
else
{
wxLogTrace(TRACE_THREADS,
- _T("Thread %ld is not yet really paused"), THR_ID(this));
+ _T("Thread %p is not yet really paused"), THR_ID(this));
}
SetState(STATE_RUNNING);
#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;
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
wxThread::wxThread(wxThreadKind kind)
{
// add this thread to the global list of all threads
- gs_allThreads.Add(this);
+ {
+ wxMutexLocker lock(*gs_mutexAllThreads);
+
+ gs_allThreads.Add(this);
+ }
m_internal = new wxThreadInternal();
// 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);
}
switch ( state )
{
case STATE_PAUSED:
- wxLogTrace(TRACE_THREADS, _T("Thread %ld suspended, resuming."),
+ wxLogTrace(TRACE_THREADS, _T("Thread %p suspended, resuming."),
GetId());
m_internal->Resume();
return wxTHREAD_NO_ERROR;
case STATE_EXITED:
- wxLogTrace(TRACE_THREADS, _T("Thread %ld exited, won't resume."),
+ wxLogTrace(TRACE_THREADS, _T("Thread %p exited, won't resume."),
GetId());
return wxTHREAD_NO_ERROR;
// 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
delete m_internal;
// remove this thread from the global array
- gs_allThreads.Remove(this);
+ {
+ wxMutexLocker lock(*gs_mutexAllThreads);
+
+ gs_allThreads.Remove(this);
+ }
}
// -----------------------------------------------------------------------------
gs_tidMain = pthread_self();
+ gs_mutexAllThreads = new wxMutex();
+
gs_mutexGui = new wxMutex();
gs_mutexGui->Lock();
gs_mutexDeleteThread = new wxMutex();
- gs_condAllDeleted = new wxCondition( *gs_mutexDeleteThread );
+ gs_condAllDeleted = new wxCondition(*gs_mutexDeleteThread);
return true;
}
}
}
- // terminate any threads left
- size_t count = gs_allThreads.GetCount();
- if ( count != 0u )
+ size_t count;
+
{
- wxLogDebug(wxT("%lu threads were not terminated by the application."),
- (unsigned long)count);
- }
+ wxMutexLocker lock(*gs_mutexAllThreads);
+
+ // terminate any threads left
+ count = gs_allThreads.GetCount();
+ if ( count != 0u )
+ {
+ wxLogDebug(wxT("%lu threads were not terminated by the application."),
+ (unsigned long)count);
+ }
+ } // unlock mutex before deleting the threads as they lock it in their dtor
for ( size_t n = 0u; n < count; n++ )
{
gs_allThreads[0]->Delete();
}
+ delete gs_mutexAllThreads;
+
// destroy GUI mutex
gs_mutexGui->Unlock();
delete gs_mutexGui;
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)
// or wxThreadModule::OnExit() would deadlock
wxMutexLocker locker( *gs_mutexDeleteThread );
- wxLogTrace(TRACE_THREADS, _T("Thread %ld auto deletes."), This->GetId());
+ wxLogTrace(TRACE_THREADS, _T("Thread %p auto deletes."), This->GetId());
delete This;
wxCHECK_RET( gs_nThreadsBeingDeleted > 0,
_T("no threads scheduled for deletion, yet we delete one?") );
- wxLogTrace(TRACE_THREADS, _T("%lu scheduled for deletion threads left."),
+ wxLogTrace(TRACE_THREADS, _T("%lu threads remain scheduled for deletion."),
(unsigned long)gs_nThreadsBeingDeleted - 1);
if ( !--gs_nThreadsBeingDeleted )
}
}
-void wxMutexGuiEnter()
+void wxMutexGuiEnterImpl()
{
gs_mutexGui->Lock();
}
-void wxMutexGuiLeave()
+void wxMutexGuiLeaveImpl()
{
gs_mutexGui->Unlock();
}