1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/threadpsx.cpp
3 // Purpose: wxThread (Posix) Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by: K. S. Sreeram (2002): POSIXified wxCondition, added wxSemaphore
8 // Copyright: (c) Wolfram Gloger (1996, 1997)
9 // Guilhem Lavaux (1998)
10 // Vadim Zeitlin (1999-2002)
11 // Robert Roebling (1999)
12 // K. S. Sreeram (2002)
13 // Licence: wxWindows licence
14 /////////////////////////////////////////////////////////////////////////////
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
29 #include "wx/thread.h"
30 #include "wx/except.h"
34 #include "wx/dynarray.h"
39 #include "wx/stopwatch.h"
40 #include "wx/module.h"
52 #ifdef HAVE_THR_SETCONCURRENCY
56 // we use wxFFile under Linux in GetCPUCount()
61 #include <sys/resource.h>
65 #define THR_ID(thr) ((long long)(thr)->GetId())
67 #define THR_ID(thr) ((long)(thr)->GetId())
72 // implement wxCriticalSection using mutexes
73 wxCriticalSection
::wxCriticalSection() { }
74 wxCriticalSection
::~wxCriticalSection() { }
76 void wxCriticalSection
::Enter() { (void)m_mutex
.Lock(); }
77 void wxCriticalSection
::Leave() { (void)m_mutex
.Unlock(); }
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // the possible states of the thread and transitions from them
87 STATE_NEW
, // didn't start execution yet (=> RUNNING)
88 STATE_RUNNING
, // running (=> PAUSED or EXITED)
89 STATE_PAUSED
, // suspended (=> RUNNING or EXITED)
90 STATE_EXITED
// thread doesn't exist any more
93 // the exit value of a thread which has been cancelled
94 static const wxThread
::ExitCode EXITCODE_CANCELLED
= (wxThread
::ExitCode
)-1;
96 // trace mask for wxThread operations
97 #define TRACE_THREADS _T("thread")
99 // you can get additional debugging messages for the semaphore operations
100 #define TRACE_SEMA _T("semaphore")
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 static void ScheduleThreadForDeletion();
107 static void DeleteThread(wxThread
*This
);
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 // an (non owning) array of pointers to threads
114 WX_DEFINE_ARRAY_PTR(wxThread
*, wxArrayThread
);
116 // an entry for a thread we can wait for
118 // -----------------------------------------------------------------------------
120 // -----------------------------------------------------------------------------
122 // we keep the list of all threads created by the application to be able to
123 // terminate them on exit if there are some left - otherwise the process would
125 static wxArrayThread gs_allThreads
;
127 // a mutex to protect gs_allThreads
128 static wxMutex
*gs_mutexAllThreads
= NULL
;
130 // the id of the main thread
131 static pthread_t gs_tidMain
= (pthread_t
)-1;
133 // the key for the pointer to the associated wxThread object
134 static pthread_key_t gs_keySelf
;
136 // the number of threads which are being deleted - the program won't exit
137 // until there are any left
138 static size_t gs_nThreadsBeingDeleted
= 0;
140 // a mutex to protect gs_nThreadsBeingDeleted
141 static wxMutex
*gs_mutexDeleteThread
= (wxMutex
*)NULL
;
143 // and a condition variable which will be signaled when all
144 // gs_nThreadsBeingDeleted will have been deleted
145 static wxCondition
*gs_condAllDeleted
= (wxCondition
*)NULL
;
147 // this mutex must be acquired before any call to a GUI function
148 // (it's not inside #if wxUSE_GUI because this file is compiled as part
150 static wxMutex
*gs_mutexGui
= NULL
;
152 // when we wait for a thread to exit, we're blocking on a condition which the
153 // thread signals in its SignalExit() method -- but this condition can't be a
154 // member of the thread itself as a detached thread may delete itself at any
155 // moment and accessing the condition member of the thread after this would
156 // result in a disaster
158 // so instead we maintain a global list of the structs below for the threads
159 // we're interested in waiting on
161 // ============================================================================
162 // wxMutex implementation
163 // ============================================================================
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 // this is a simple wrapper around pthread_mutex_t which provides error
171 class wxMutexInternal
174 wxMutexInternal(wxMutexType mutexType
);
178 wxMutexError
Lock(unsigned long ms
);
179 wxMutexError
TryLock();
180 wxMutexError
Unlock();
182 bool IsOk() const { return m_isOk
; }
185 // convert the result of pthread_mutex_[timed]lock() call to wx return code
186 wxMutexError
HandleLockResult(int err
);
189 pthread_mutex_t m_mutex
;
192 // wxConditionInternal uses our m_mutex
193 friend class wxConditionInternal
;
196 #if defined(HAVE_PTHREAD_MUTEXATTR_T) && \
197 wxUSE_UNIX && !defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE_DECL)
198 // on some systems pthread_mutexattr_settype() is not in the headers (but it is
199 // in the library, otherwise we wouldn't compile this code at all)
200 extern "C" int pthread_mutexattr_settype(pthread_mutexattr_t
*, int);
203 wxMutexInternal
::wxMutexInternal(wxMutexType mutexType
)
208 case wxMUTEX_RECURSIVE
:
209 // support recursive locks like Win32, i.e. a thread can lock a
210 // mutex which it had itself already locked
212 // unfortunately initialization of recursive mutexes is non
213 // portable, so try several methods
214 #ifdef HAVE_PTHREAD_MUTEXATTR_T
216 pthread_mutexattr_t attr
;
217 pthread_mutexattr_init(&attr
);
218 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
220 err
= pthread_mutex_init(&m_mutex
, &attr
);
222 #elif defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
223 // we can use this only as initializer so we have to assign it
224 // first to a temp var - assigning directly to m_mutex wouldn't
227 pthread_mutex_t mutex
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
230 #else // no recursive mutexes
232 #endif // HAVE_PTHREAD_MUTEXATTR_T/...
236 wxFAIL_MSG( _T("unknown mutex type") );
239 case wxMUTEX_DEFAULT
:
240 err
= pthread_mutex_init(&m_mutex
, NULL
);
247 wxLogApiError( wxT("pthread_mutex_init()"), err
);
251 wxMutexInternal
::~wxMutexInternal()
255 int err
= pthread_mutex_destroy(&m_mutex
);
258 wxLogApiError( wxT("pthread_mutex_destroy()"), err
);
263 wxMutexError wxMutexInternal
::Lock()
265 return HandleLockResult(pthread_mutex_lock(&m_mutex
));
268 wxMutexError wxMutexInternal
::Lock(unsigned long ms
)
270 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
271 static const long MSEC_IN_SEC
= 1000;
272 static const long NSEC_IN_MSEC
= 1000000;
273 static const long NSEC_IN_USEC
= 1000;
274 static const long NSEC_IN_SEC
= MSEC_IN_SEC
* NSEC_IN_MSEC
;
276 time_t seconds
= ms
/MSEC_IN_SEC
;
277 long nanoseconds
= (ms
% MSEC_IN_SEC
) * NSEC_IN_MSEC
;
278 timespec ts
= { 0, 0 };
280 // normally we should use clock_gettime(CLOCK_REALTIME) here but this
281 // function is in librt and we don't link with it currently, so use
282 // gettimeofday() instead -- if it turns out that this is really too
283 // imprecise, we should modify configure to check if clock_gettime() is
284 // available and whether it requires -lrt and use it instead
286 if ( clock_gettime(CLOCK_REALTIME
, &ts
) == 0 )
291 if ( wxGetTimeOfDay(&tv
) != -1 )
293 ts
.tv_sec
= tv
.tv_sec
;
294 ts
.tv_nsec
= tv
.tv_usec
*NSEC_IN_USEC
;
297 else // fall back on system timer
299 ts
.tv_sec
= time(NULL
);
302 ts
.tv_sec
+= seconds
;
303 ts
.tv_nsec
+= nanoseconds
;
304 if ( ts
.tv_nsec
> NSEC_IN_SEC
)
307 ts
.tv_nsec
-= NSEC_IN_SEC
;
310 return HandleLockResult(pthread_mutex_timedlock(&m_mutex
, &ts
));
311 #else // !HAVE_PTHREAD_MUTEX_TIMEDLOCK
314 return wxMUTEX_MISC_ERROR
;
315 #endif // HAVE_PTHREAD_MUTEX_TIMEDLOCK/!HAVE_PTHREAD_MUTEX_TIMEDLOCK
318 wxMutexError wxMutexInternal
::HandleLockResult(int err
)
323 // only error checking mutexes return this value and so it's an
324 // unexpected situation -- hence use assert, not wxLogDebug
325 wxFAIL_MSG( _T("mutex deadlock prevented") );
326 return wxMUTEX_DEAD_LOCK
;
329 wxLogDebug(_T("pthread_mutex_[timed]lock(): mutex not initialized"));
333 return wxMUTEX_TIMEOUT
;
336 return wxMUTEX_NO_ERROR
;
339 wxLogApiError(_T("pthread_mutex_[timed]lock()"), err
);
342 return wxMUTEX_MISC_ERROR
;
346 wxMutexError wxMutexInternal
::TryLock()
348 int err
= pthread_mutex_trylock(&m_mutex
);
352 // not an error: mutex is already locked, but we're prepared for
357 wxLogDebug(_T("pthread_mutex_trylock(): mutex not initialized."));
361 return wxMUTEX_NO_ERROR
;
364 wxLogApiError(_T("pthread_mutex_trylock()"), err
);
367 return wxMUTEX_MISC_ERROR
;
370 wxMutexError wxMutexInternal
::Unlock()
372 int err
= pthread_mutex_unlock(&m_mutex
);
376 // we don't own the mutex
377 return wxMUTEX_UNLOCKED
;
380 wxLogDebug(_T("pthread_mutex_unlock(): mutex not initialized."));
384 return wxMUTEX_NO_ERROR
;
387 wxLogApiError(_T("pthread_mutex_unlock()"), err
);
390 return wxMUTEX_MISC_ERROR
;
393 // ===========================================================================
394 // wxCondition implementation
395 // ===========================================================================
397 // ---------------------------------------------------------------------------
398 // wxConditionInternal
399 // ---------------------------------------------------------------------------
401 // this is a wrapper around pthread_cond_t associated with a wxMutex (and hence
402 // with a pthread_mutex_t)
403 class wxConditionInternal
406 wxConditionInternal(wxMutex
& mutex
);
407 ~wxConditionInternal();
409 bool IsOk() const { return m_isOk
&& m_mutex
.IsOk(); }
412 wxCondError
WaitTimeout(unsigned long milliseconds
);
414 wxCondError
Signal();
415 wxCondError
Broadcast();
418 // get the POSIX mutex associated with us
419 pthread_mutex_t
*GetPMutex() const { return &m_mutex
.m_internal
->m_mutex
; }
422 pthread_cond_t m_cond
;
427 wxConditionInternal
::wxConditionInternal(wxMutex
& mutex
)
430 int err
= pthread_cond_init(&m_cond
, NULL
/* default attributes */);
436 wxLogApiError(_T("pthread_cond_init()"), err
);
440 wxConditionInternal
::~wxConditionInternal()
444 int err
= pthread_cond_destroy(&m_cond
);
447 wxLogApiError(_T("pthread_cond_destroy()"), err
);
452 wxCondError wxConditionInternal
::Wait()
454 int err
= pthread_cond_wait(&m_cond
, GetPMutex());
457 wxLogApiError(_T("pthread_cond_wait()"), err
);
459 return wxCOND_MISC_ERROR
;
462 return wxCOND_NO_ERROR
;
465 wxCondError wxConditionInternal
::WaitTimeout(unsigned long milliseconds
)
467 wxLongLong curtime
= wxGetLocalTimeMillis();
468 curtime
+= milliseconds
;
469 wxLongLong temp
= curtime
/ 1000;
470 int sec
= temp
.GetLo();
472 temp
= curtime
- temp
;
473 int millis
= temp
.GetLo();
478 tspec
.tv_nsec
= millis
* 1000L * 1000L;
480 int err
= pthread_cond_timedwait( &m_cond
, GetPMutex(), &tspec
);
484 return wxCOND_TIMEOUT
;
487 return wxCOND_NO_ERROR
;
490 wxLogApiError(_T("pthread_cond_timedwait()"), err
);
493 return wxCOND_MISC_ERROR
;
496 wxCondError wxConditionInternal
::Signal()
498 int err
= pthread_cond_signal(&m_cond
);
501 wxLogApiError(_T("pthread_cond_signal()"), err
);
503 return wxCOND_MISC_ERROR
;
506 return wxCOND_NO_ERROR
;
509 wxCondError wxConditionInternal
::Broadcast()
511 int err
= pthread_cond_broadcast(&m_cond
);
514 wxLogApiError(_T("pthread_cond_broadcast()"), err
);
516 return wxCOND_MISC_ERROR
;
519 return wxCOND_NO_ERROR
;
522 // ===========================================================================
523 // wxSemaphore implementation
524 // ===========================================================================
526 // ---------------------------------------------------------------------------
527 // wxSemaphoreInternal
528 // ---------------------------------------------------------------------------
530 // we implement the semaphores using mutexes and conditions instead of using
531 // the sem_xxx() POSIX functions because they're not widely available and also
532 // because it's impossible to implement WaitTimeout() using them
533 class wxSemaphoreInternal
536 wxSemaphoreInternal(int initialcount
, int maxcount
);
538 bool IsOk() const { return m_isOk
; }
541 wxSemaError
TryWait();
542 wxSemaError
WaitTimeout(unsigned long milliseconds
);
556 wxSemaphoreInternal
::wxSemaphoreInternal(int initialcount
, int maxcount
)
560 if ( (initialcount
< 0 || maxcount
< 0) ||
561 ((maxcount
> 0) && (initialcount
> maxcount
)) )
563 wxFAIL_MSG( _T("wxSemaphore: invalid initial or maximal count") );
569 m_maxcount
= (size_t)maxcount
;
570 m_count
= (size_t)initialcount
;
573 m_isOk
= m_mutex
.IsOk() && m_cond
.IsOk();
576 wxSemaError wxSemaphoreInternal
::Wait()
578 wxMutexLocker
locker(m_mutex
);
580 while ( m_count
== 0 )
582 wxLogTrace(TRACE_SEMA
,
583 _T("Thread %ld waiting for semaphore to become signalled"),
584 wxThread
::GetCurrentId());
586 if ( m_cond
.Wait() != wxCOND_NO_ERROR
)
587 return wxSEMA_MISC_ERROR
;
589 wxLogTrace(TRACE_SEMA
,
590 _T("Thread %ld finished waiting for semaphore, count = %lu"),
591 wxThread
::GetCurrentId(), (unsigned long)m_count
);
596 return wxSEMA_NO_ERROR
;
599 wxSemaError wxSemaphoreInternal
::TryWait()
601 wxMutexLocker
locker(m_mutex
);
608 return wxSEMA_NO_ERROR
;
611 wxSemaError wxSemaphoreInternal
::WaitTimeout(unsigned long milliseconds
)
613 wxMutexLocker
locker(m_mutex
);
615 wxLongLong startTime
= wxGetLocalTimeMillis();
617 while ( m_count
== 0 )
619 wxLongLong elapsed
= wxGetLocalTimeMillis() - startTime
;
620 long remainingTime
= (long)milliseconds
- (long)elapsed
.GetLo();
621 if ( remainingTime
<= 0 )
624 return wxSEMA_TIMEOUT
;
627 switch ( m_cond
.WaitTimeout(remainingTime
) )
630 return wxSEMA_TIMEOUT
;
633 return wxSEMA_MISC_ERROR
;
635 case wxCOND_NO_ERROR
:
642 return wxSEMA_NO_ERROR
;
645 wxSemaError wxSemaphoreInternal
::Post()
647 wxMutexLocker
locker(m_mutex
);
649 if ( m_maxcount
> 0 && m_count
== m_maxcount
)
651 return wxSEMA_OVERFLOW
;
656 wxLogTrace(TRACE_SEMA
,
657 _T("Thread %ld about to signal semaphore, count = %lu"),
658 wxThread
::GetCurrentId(), (unsigned long)m_count
);
660 return m_cond
.Signal() == wxCOND_NO_ERROR ? wxSEMA_NO_ERROR
664 // ===========================================================================
665 // wxThread implementation
666 // ===========================================================================
668 // the thread callback functions must have the C linkage
672 #ifdef wxHAVE_PTHREAD_CLEANUP
673 // thread exit function
674 void wxPthreadCleanup(void *ptr
);
675 #endif // wxHAVE_PTHREAD_CLEANUP
677 void *wxPthreadStart(void *ptr
);
681 // ----------------------------------------------------------------------------
683 // ----------------------------------------------------------------------------
685 class wxThreadInternal
691 // thread entry function
692 static void *PthreadStart(wxThread
*thread
);
697 // unblock the thread allowing it to run
698 void SignalRun() { m_semRun
.Post(); }
699 // ask the thread to terminate
701 // go to sleep until Resume() is called
708 int GetPriority() const { return m_prio
; }
709 void SetPriority(int prio
) { m_prio
= prio
; }
711 wxThreadState
GetState() const { return m_state
; }
712 void SetState(wxThreadState state
)
715 static const wxChar
*stateNames
[] =
723 wxLogTrace(TRACE_THREADS
, _T("Thread %ld: %s => %s."),
724 (long)GetId(), stateNames
[m_state
], stateNames
[state
]);
725 #endif // __WXDEBUG__
730 pthread_t
GetId() const { return m_threadId
; }
731 pthread_t
*GetIdPtr() { return &m_threadId
; }
733 void SetCancelFlag() { m_cancelled
= true; }
734 bool WasCancelled() const { return m_cancelled
; }
736 void SetExitCode(wxThread
::ExitCode exitcode
) { m_exitcode
= exitcode
; }
737 wxThread
::ExitCode
GetExitCode() const { return m_exitcode
; }
740 void SetReallyPaused(bool paused
) { m_isPaused
= paused
; }
741 bool IsReallyPaused() const { return m_isPaused
; }
743 // tell the thread that it is a detached one
746 wxCriticalSectionLocker
lock(m_csJoinFlag
);
748 m_shouldBeJoined
= false;
752 #ifdef wxHAVE_PTHREAD_CLEANUP
753 // this is used by wxPthreadCleanup() only
754 static void Cleanup(wxThread
*thread
);
755 #endif // wxHAVE_PTHREAD_CLEANUP
758 pthread_t m_threadId
; // id of the thread
759 wxThreadState m_state
; // see wxThreadState enum
760 int m_prio
; // in wxWidgets units: from 0 to 100
762 // this flag is set when the thread should terminate
765 // this flag is set when the thread is blocking on m_semSuspend
768 // the thread exit code - only used for joinable (!detached) threads and
769 // is only valid after the thread termination
770 wxThread
::ExitCode m_exitcode
;
772 // many threads may call Wait(), but only one of them should call
773 // pthread_join(), so we have to keep track of this
774 wxCriticalSection m_csJoinFlag
;
775 bool m_shouldBeJoined
;
778 // this semaphore is posted by Run() and the threads Entry() is not
779 // called before it is done
780 wxSemaphore m_semRun
;
782 // this one is signaled when the thread should resume after having been
784 wxSemaphore m_semSuspend
;
787 // ----------------------------------------------------------------------------
788 // thread startup and exit functions
789 // ----------------------------------------------------------------------------
791 void *wxPthreadStart(void *ptr
)
793 return wxThreadInternal
::PthreadStart((wxThread
*)ptr
);
796 void *wxThreadInternal
::PthreadStart(wxThread
*thread
)
798 wxThreadInternal
*pthread
= thread
->m_internal
;
800 wxLogTrace(TRACE_THREADS
, _T("Thread %ld started."), THR_ID(pthread
));
802 // associate the thread pointer with the newly created thread so that
803 // wxThread::This() will work
804 int rc
= pthread_setspecific(gs_keySelf
, thread
);
807 wxLogSysError(rc
, _("Cannot start thread: error writing TLS"));
812 // have to declare this before pthread_cleanup_push() which defines a
816 #ifdef wxHAVE_PTHREAD_CLEANUP
817 // install the cleanup handler which will be called if the thread is
819 pthread_cleanup_push(wxPthreadCleanup
, thread
);
820 #endif // wxHAVE_PTHREAD_CLEANUP
822 // wait for the semaphore to be posted from Run()
823 pthread
->m_semRun
.Wait();
825 // test whether we should run the run at all - may be it was deleted
826 // before it started to Run()?
828 wxCriticalSectionLocker
lock(thread
->m_critsect
);
830 dontRunAtAll
= pthread
->GetState() == STATE_NEW
&&
831 pthread
->WasCancelled();
836 // call the main entry
837 wxLogTrace(TRACE_THREADS
,
838 _T("Thread %ld about to enter its Entry()."),
843 pthread
->m_exitcode
= thread
->Entry();
845 wxLogTrace(TRACE_THREADS
,
846 _T("Thread %ld Entry() returned %lu."),
847 THR_ID(pthread
), wxPtrToUInt(pthread
->m_exitcode
));
849 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
852 wxCriticalSectionLocker
lock(thread
->m_critsect
);
854 // change the state of the thread to "exited" so that
855 // wxPthreadCleanup handler won't do anything from now (if it's
856 // called before we do pthread_cleanup_pop below)
857 pthread
->SetState(STATE_EXITED
);
861 // NB: pthread_cleanup_push/pop() are macros and pop contains the matching
862 // '}' for the '{' in push, so they must be used in the same block!
863 #ifdef wxHAVE_PTHREAD_CLEANUP
865 // under Tru64 we get a warning from macro expansion
867 #pragma message disable(declbutnotref)
870 // remove the cleanup handler without executing it
871 pthread_cleanup_pop(FALSE
);
874 #pragma message restore
876 #endif // wxHAVE_PTHREAD_CLEANUP
880 // FIXME: deleting a possibly joinable thread here???
883 return EXITCODE_CANCELLED
;
887 // terminate the thread
888 thread
->Exit(pthread
->m_exitcode
);
890 wxFAIL_MSG(wxT("wxThread::Exit() can't return."));
896 #ifdef wxHAVE_PTHREAD_CLEANUP
898 // this handler is called when the thread is cancelled
899 extern "C" void wxPthreadCleanup(void *ptr
)
901 wxThreadInternal
::Cleanup((wxThread
*)ptr
);
904 void wxThreadInternal
::Cleanup(wxThread
*thread
)
906 if (pthread_getspecific(gs_keySelf
) == 0) return;
908 wxCriticalSectionLocker
lock(thread
->m_critsect
);
909 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
911 // thread is already considered as finished.
916 // exit the thread gracefully
917 thread
->Exit(EXITCODE_CANCELLED
);
920 #endif // wxHAVE_PTHREAD_CLEANUP
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
926 wxThreadInternal
::wxThreadInternal()
930 m_prio
= WXTHREAD_DEFAULT_PRIORITY
;
934 // set to true only when the thread starts waiting on m_semSuspend
937 // defaults for joinable threads
938 m_shouldBeJoined
= true;
939 m_isDetached
= false;
942 wxThreadInternal
::~wxThreadInternal()
946 wxThreadError wxThreadInternal
::Run()
948 wxCHECK_MSG( GetState() == STATE_NEW
, wxTHREAD_RUNNING
,
949 wxT("thread may only be started once after Create()") );
951 SetState(STATE_RUNNING
);
953 // wake up threads waiting for our start
956 return wxTHREAD_NO_ERROR
;
959 void wxThreadInternal
::Wait()
961 wxCHECK_RET( !m_isDetached
, _T("can't wait for a detached thread") );
963 // if the thread we're waiting for is waiting for the GUI mutex, we will
964 // deadlock so make sure we release it temporarily
965 if ( wxThread
::IsMain() )
968 wxLogTrace(TRACE_THREADS
,
969 _T("Starting to wait for thread %ld to exit."),
972 // to avoid memory leaks we should call pthread_join(), but it must only be
973 // done once so use a critical section to serialize the code below
975 wxCriticalSectionLocker
lock(m_csJoinFlag
);
977 if ( m_shouldBeJoined
)
979 // FIXME shouldn't we set cancellation type to DISABLED here? If
980 // we're cancelled inside pthread_join(), things will almost
981 // certainly break - but if we disable the cancellation, we
983 if ( pthread_join(GetId(), &m_exitcode
) != 0 )
985 // this is a serious problem, so use wxLogError and not
986 // wxLogDebug: it is possible to bring the system to its knees
987 // by creating too many threads and not joining them quite
989 wxLogError(_("Failed to join a thread, potential memory leak detected - please restart the program"));
992 m_shouldBeJoined
= false;
996 // reacquire GUI mutex
997 if ( wxThread
::IsMain() )
1001 void wxThreadInternal
::Pause()
1003 // the state is set from the thread which pauses us first, this function
1004 // is called later so the state should have been already set
1005 wxCHECK_RET( m_state
== STATE_PAUSED
,
1006 wxT("thread must first be paused with wxThread::Pause().") );
1008 wxLogTrace(TRACE_THREADS
,
1009 _T("Thread %ld goes to sleep."), THR_ID(this));
1011 // wait until the semaphore is Post()ed from Resume()
1012 m_semSuspend
.Wait();
1015 void wxThreadInternal
::Resume()
1017 wxCHECK_RET( m_state
== STATE_PAUSED
,
1018 wxT("can't resume thread which is not suspended.") );
1020 // the thread might be not actually paused yet - if there were no call to
1021 // TestDestroy() since the last call to Pause() for example
1022 if ( IsReallyPaused() )
1024 wxLogTrace(TRACE_THREADS
,
1025 _T("Waking up thread %ld"), THR_ID(this));
1028 m_semSuspend
.Post();
1031 SetReallyPaused(false);
1035 wxLogTrace(TRACE_THREADS
,
1036 _T("Thread %ld is not yet really paused"), THR_ID(this));
1039 SetState(STATE_RUNNING
);
1042 // -----------------------------------------------------------------------------
1043 // wxThread static functions
1044 // -----------------------------------------------------------------------------
1046 wxThread
*wxThread
::This()
1048 return (wxThread
*)pthread_getspecific(gs_keySelf
);
1051 bool wxThread
::IsMain()
1053 return (bool)pthread_equal(pthread_self(), gs_tidMain
) || gs_tidMain
== (pthread_t
)-1;
1056 void wxThread
::Yield()
1058 #ifdef HAVE_SCHED_YIELD
1063 int wxThread
::GetCPUCount()
1065 #if defined(_SC_NPROCESSORS_ONLN)
1066 // this works for Solaris and Linux 2.6
1067 int rc
= sysconf(_SC_NPROCESSORS_ONLN
);
1072 #elif defined(__LINUX__) && wxUSE_FFILE
1073 // read from proc (can't use wxTextFile here because it's a special file:
1074 // it has 0 size but still can be read from)
1077 wxFFile
file(_T("/proc/cpuinfo"));
1078 if ( file
.IsOpened() )
1080 // slurp the whole file
1082 if ( file
.ReadAll(&s
) )
1084 // (ab)use Replace() to find the number of "processor: num" strings
1085 size_t count
= s
.Replace(_T("processor\t:"), _T(""));
1091 wxLogDebug(_T("failed to parse /proc/cpuinfo"));
1095 wxLogDebug(_T("failed to read /proc/cpuinfo"));
1098 #endif // different ways to get number of CPUs
1104 // VMS is a 64 bit system and threads have 64 bit pointers.
1105 // FIXME: also needed for other systems????
1107 unsigned long long wxThread
::GetCurrentId()
1109 return (unsigned long long)pthread_self();
1114 unsigned long wxThread
::GetCurrentId()
1116 return (unsigned long)pthread_self();
1119 #endif // __VMS/!__VMS
1122 bool wxThread
::SetConcurrency(size_t level
)
1124 #ifdef HAVE_THR_SETCONCURRENCY
1125 int rc
= thr_setconcurrency(level
);
1128 wxLogSysError(rc
, _T("thr_setconcurrency() failed"));
1132 #else // !HAVE_THR_SETCONCURRENCY
1133 // ok only for the default value
1135 #endif // HAVE_THR_SETCONCURRENCY/!HAVE_THR_SETCONCURRENCY
1138 // -----------------------------------------------------------------------------
1140 // -----------------------------------------------------------------------------
1142 wxThread
::wxThread(wxThreadKind kind
)
1144 // add this thread to the global list of all threads
1146 wxMutexLocker
lock(*gs_mutexAllThreads
);
1148 gs_allThreads
.Add(this);
1151 m_internal
= new wxThreadInternal();
1153 m_isDetached
= kind
== wxTHREAD_DETACHED
;
1156 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1157 #define WXUNUSED_STACKSIZE(identifier) identifier
1159 #define WXUNUSED_STACKSIZE(identifier) WXUNUSED(identifier)
1162 wxThreadError wxThread
::Create(unsigned int WXUNUSED_STACKSIZE(stackSize
))
1164 if ( m_internal
->GetState() != STATE_NEW
)
1166 // don't recreate thread
1167 return wxTHREAD_RUNNING
;
1170 // set up the thread attribute: right now, we only set thread priority
1171 pthread_attr_t attr
;
1172 pthread_attr_init(&attr
);
1174 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1176 pthread_attr_setstacksize(&attr
, stackSize
);
1179 #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
1181 if ( pthread_attr_getschedpolicy(&attr
, &policy
) != 0 )
1183 wxLogError(_("Cannot retrieve thread scheduling policy."));
1187 /* the pthread.h contains too many spaces. This is a work-around */
1188 # undef sched_get_priority_max
1189 #undef sched_get_priority_min
1190 #define sched_get_priority_max(_pol_) \
1191 (_pol_ == SCHED_OTHER ? PRI_FG_MAX_NP : PRI_FIFO_MAX)
1192 #define sched_get_priority_min(_pol_) \
1193 (_pol_ == SCHED_OTHER ? PRI_FG_MIN_NP : PRI_FIFO_MIN)
1196 int max_prio
= sched_get_priority_max(policy
),
1197 min_prio
= sched_get_priority_min(policy
),
1198 prio
= m_internal
->GetPriority();
1200 if ( min_prio
== -1 || max_prio
== -1 )
1202 wxLogError(_("Cannot get priority range for scheduling policy %d."),
1205 else if ( max_prio
== min_prio
)
1207 if ( prio
!= WXTHREAD_DEFAULT_PRIORITY
)
1209 // notify the programmer that this doesn't work here
1210 wxLogWarning(_("Thread priority setting is ignored."));
1212 //else: we have default priority, so don't complain
1214 // anyhow, don't do anything because priority is just ignored
1218 struct sched_param sp
;
1219 if ( pthread_attr_getschedparam(&attr
, &sp
) != 0 )
1221 wxFAIL_MSG(_T("pthread_attr_getschedparam() failed"));
1224 sp
.sched_priority
= min_prio
+ (prio
*(max_prio
- min_prio
))/100;
1226 if ( pthread_attr_setschedparam(&attr
, &sp
) != 0 )
1228 wxFAIL_MSG(_T("pthread_attr_setschedparam(priority) failed"));
1231 #endif // HAVE_THREAD_PRIORITY_FUNCTIONS
1233 #ifdef HAVE_PTHREAD_ATTR_SETSCOPE
1234 // this will make the threads created by this process really concurrent
1235 if ( pthread_attr_setscope(&attr
, PTHREAD_SCOPE_SYSTEM
) != 0 )
1237 wxFAIL_MSG(_T("pthread_attr_setscope(PTHREAD_SCOPE_SYSTEM) failed"));
1239 #endif // HAVE_PTHREAD_ATTR_SETSCOPE
1241 // VZ: assume that this one is always available (it's rather fundamental),
1242 // if this function is ever missing we should try to use
1243 // pthread_detach() instead (after thread creation)
1246 if ( pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
) != 0 )
1248 wxFAIL_MSG(_T("pthread_attr_setdetachstate(DETACHED) failed"));
1251 // never try to join detached threads
1252 m_internal
->Detach();
1254 //else: threads are created joinable by default, it's ok
1256 // create the new OS thread object
1257 int rc
= pthread_create
1259 m_internal
->GetIdPtr(),
1265 if ( pthread_attr_destroy(&attr
) != 0 )
1267 wxFAIL_MSG(_T("pthread_attr_destroy() failed"));
1272 m_internal
->SetState(STATE_EXITED
);
1274 return wxTHREAD_NO_RESOURCE
;
1277 return wxTHREAD_NO_ERROR
;
1280 wxThreadError wxThread
::Run()
1282 wxCriticalSectionLocker
lock(m_critsect
);
1284 wxCHECK_MSG( m_internal
->GetId(), wxTHREAD_MISC_ERROR
,
1285 wxT("must call wxThread::Create() first") );
1287 return m_internal
->Run();
1290 // -----------------------------------------------------------------------------
1292 // -----------------------------------------------------------------------------
1294 void wxThread
::SetPriority(unsigned int prio
)
1296 wxCHECK_RET( ((int)WXTHREAD_MIN_PRIORITY
<= (int)prio
) &&
1297 ((int)prio
<= (int)WXTHREAD_MAX_PRIORITY
),
1298 wxT("invalid thread priority") );
1300 wxCriticalSectionLocker
lock(m_critsect
);
1302 switch ( m_internal
->GetState() )
1305 // thread not yet started, priority will be set when it is
1306 m_internal
->SetPriority(prio
);
1311 #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
1312 #if defined(__LINUX__)
1313 // On Linux, pthread_setschedparam with SCHED_OTHER does not allow
1314 // a priority other than 0. Instead, we use the BSD setpriority
1315 // which alllows us to set a 'nice' value between 20 to -20. Only
1316 // super user can set a value less than zero (more negative yields
1317 // higher priority). setpriority set the static priority of a
1318 // process, but this is OK since Linux is configured as a thread
1321 // FIXME this is not true for 2.6!!
1323 // map wx priorites WXTHREAD_MIN_PRIORITY..WXTHREAD_MAX_PRIORITY
1324 // to Unix priorities 20..-20
1325 if ( setpriority(PRIO_PROCESS
, 0, -(2*(int)prio
)/5 + 20) == -1 )
1327 wxLogError(_("Failed to set thread priority %d."), prio
);
1331 struct sched_param sparam
;
1332 sparam
.sched_priority
= prio
;
1334 if ( pthread_setschedparam(m_internal
->GetId(),
1335 SCHED_OTHER
, &sparam
) != 0 )
1337 wxLogError(_("Failed to set thread priority %d."), prio
);
1341 #endif // HAVE_THREAD_PRIORITY_FUNCTIONS
1346 wxFAIL_MSG(wxT("impossible to set thread priority in this state"));
1350 unsigned int wxThread
::GetPriority() const
1352 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1354 return m_internal
->GetPriority();
1357 wxThreadIdType wxThread
::GetId() const
1359 return (wxThreadIdType
) m_internal
->GetId();
1362 // -----------------------------------------------------------------------------
1364 // -----------------------------------------------------------------------------
1366 wxThreadError wxThread
::Pause()
1368 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1369 _T("a thread can't pause itself") );
1371 wxCriticalSectionLocker
lock(m_critsect
);
1373 if ( m_internal
->GetState() != STATE_RUNNING
)
1375 wxLogDebug(wxT("Can't pause thread which is not running."));
1377 return wxTHREAD_NOT_RUNNING
;
1380 // just set a flag, the thread will be really paused only during the next
1381 // call to TestDestroy()
1382 m_internal
->SetState(STATE_PAUSED
);
1384 return wxTHREAD_NO_ERROR
;
1387 wxThreadError wxThread
::Resume()
1389 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1390 _T("a thread can't resume itself") );
1392 wxCriticalSectionLocker
lock(m_critsect
);
1394 wxThreadState state
= m_internal
->GetState();
1399 wxLogTrace(TRACE_THREADS
, _T("Thread %ld suspended, resuming."),
1402 m_internal
->Resume();
1404 return wxTHREAD_NO_ERROR
;
1407 wxLogTrace(TRACE_THREADS
, _T("Thread %ld exited, won't resume."),
1409 return wxTHREAD_NO_ERROR
;
1412 wxLogDebug(_T("Attempt to resume a thread which is not paused."));
1414 return wxTHREAD_MISC_ERROR
;
1418 // -----------------------------------------------------------------------------
1420 // -----------------------------------------------------------------------------
1422 wxThread
::ExitCode wxThread
::Wait()
1424 wxCHECK_MSG( This() != this, (ExitCode
)-1,
1425 _T("a thread can't wait for itself") );
1427 wxCHECK_MSG( !m_isDetached
, (ExitCode
)-1,
1428 _T("can't wait for detached thread") );
1432 return m_internal
->GetExitCode();
1435 wxThreadError wxThread
::Delete(ExitCode
*rc
)
1437 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1438 _T("a thread can't delete itself") );
1440 bool isDetached
= m_isDetached
;
1443 wxThreadState state
= m_internal
->GetState();
1445 // ask the thread to stop
1446 m_internal
->SetCancelFlag();
1453 // we need to wake up the thread so that PthreadStart() will
1454 // terminate - right now it's blocking on run semaphore in
1456 m_internal
->SignalRun();
1465 // resume the thread first
1466 m_internal
->Resume();
1473 // wait until the thread stops
1478 // return the exit code of the thread
1479 *rc
= m_internal
->GetExitCode();
1482 //else: can't wait for detached threads
1485 return wxTHREAD_NO_ERROR
;
1488 wxThreadError wxThread
::Kill()
1490 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1491 _T("a thread can't kill itself") );
1493 switch ( m_internal
->GetState() )
1497 return wxTHREAD_NOT_RUNNING
;
1500 // resume the thread first
1506 #ifdef HAVE_PTHREAD_CANCEL
1507 if ( pthread_cancel(m_internal
->GetId()) != 0 )
1508 #endif // HAVE_PTHREAD_CANCEL
1510 wxLogError(_("Failed to terminate a thread."));
1512 return wxTHREAD_MISC_ERROR
;
1515 #ifdef HAVE_PTHREAD_CANCEL
1518 // if we use cleanup function, this will be done from
1519 // wxPthreadCleanup()
1520 #ifndef wxHAVE_PTHREAD_CLEANUP
1521 ScheduleThreadForDeletion();
1523 // don't call OnExit() here, it can only be called in the
1524 // threads context and we're in the context of another thread
1527 #endif // wxHAVE_PTHREAD_CLEANUP
1531 m_internal
->SetExitCode(EXITCODE_CANCELLED
);
1534 return wxTHREAD_NO_ERROR
;
1535 #endif // HAVE_PTHREAD_CANCEL
1539 void wxThread
::Exit(ExitCode status
)
1541 wxASSERT_MSG( This() == this,
1542 _T("wxThread::Exit() can only be called in the context of the same thread") );
1546 // from the moment we call OnExit(), the main program may terminate at
1547 // any moment, so mark this thread as being already in process of being
1548 // deleted or wxThreadModule::OnExit() will try to delete it again
1549 ScheduleThreadForDeletion();
1552 // don't enter m_critsect before calling OnExit() because the user code
1553 // might deadlock if, for example, it signals a condition in OnExit() (a
1554 // common case) while the main thread calls any of functions entering
1555 // m_critsect on us (almost all of them do)
1560 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
1562 // delete C++ thread object if this is a detached thread - user is
1563 // responsible for doing this for joinable ones
1566 // FIXME I'm feeling bad about it - what if another thread function is
1567 // called (in another thread context) now? It will try to access
1568 // half destroyed object which will probably result in something
1569 // very bad - but we can't protect this by a crit section unless
1570 // we make it a global object, but this would mean that we can
1571 // only call one thread function at a time :-(
1573 pthread_setspecific(gs_keySelf
, 0);
1578 m_internal
->SetState(STATE_EXITED
);
1582 // terminate the thread (pthread_exit() never returns)
1583 pthread_exit(status
);
1585 wxFAIL_MSG(_T("pthread_exit() failed"));
1588 // also test whether we were paused
1589 bool wxThread
::TestDestroy()
1591 wxASSERT_MSG( This() == this,
1592 _T("wxThread::TestDestroy() can only be called in the context of the same thread") );
1596 if ( m_internal
->GetState() == STATE_PAUSED
)
1598 m_internal
->SetReallyPaused(true);
1600 // leave the crit section or the other threads will stop too if they
1601 // try to call any of (seemingly harmless) IsXXX() functions while we
1605 m_internal
->Pause();
1609 // thread wasn't requested to pause, nothing to do
1613 return m_internal
->WasCancelled();
1616 wxThread
::~wxThread()
1621 // check that the thread either exited or couldn't be created
1622 if ( m_internal
->GetState() != STATE_EXITED
&&
1623 m_internal
->GetState() != STATE_NEW
)
1625 wxLogDebug(_T("The thread %ld is being destroyed although it is still running! The application may crash."),
1630 #endif // __WXDEBUG__
1634 // remove this thread from the global array
1636 wxMutexLocker
lock(*gs_mutexAllThreads
);
1638 gs_allThreads
.Remove(this);
1642 // -----------------------------------------------------------------------------
1644 // -----------------------------------------------------------------------------
1646 bool wxThread
::IsRunning() const
1648 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1650 return m_internal
->GetState() == STATE_RUNNING
;
1653 bool wxThread
::IsAlive() const
1655 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1657 switch ( m_internal
->GetState() )
1668 bool wxThread
::IsPaused() const
1670 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1672 return (m_internal
->GetState() == STATE_PAUSED
);
1675 //--------------------------------------------------------------------
1677 //--------------------------------------------------------------------
1679 class wxThreadModule
: public wxModule
1682 virtual bool OnInit();
1683 virtual void OnExit();
1686 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1689 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1691 bool wxThreadModule
::OnInit()
1693 int rc
= pthread_key_create(&gs_keySelf
, NULL
/* dtor function */);
1696 wxLogSysError(rc
, _("Thread module initialization failed: failed to create thread key"));
1701 gs_tidMain
= pthread_self();
1703 gs_mutexAllThreads
= new wxMutex();
1705 gs_mutexGui
= new wxMutex();
1706 gs_mutexGui
->Lock();
1708 gs_mutexDeleteThread
= new wxMutex();
1709 gs_condAllDeleted
= new wxCondition(*gs_mutexDeleteThread
);
1714 void wxThreadModule
::OnExit()
1716 wxASSERT_MSG( wxThread
::IsMain(), wxT("only main thread can be here") );
1718 // are there any threads left which are being deleted right now?
1719 size_t nThreadsBeingDeleted
;
1722 wxMutexLocker
lock( *gs_mutexDeleteThread
);
1723 nThreadsBeingDeleted
= gs_nThreadsBeingDeleted
;
1725 if ( nThreadsBeingDeleted
> 0 )
1727 wxLogTrace(TRACE_THREADS
,
1728 _T("Waiting for %lu threads to disappear"),
1729 (unsigned long)nThreadsBeingDeleted
);
1731 // have to wait until all of them disappear
1732 gs_condAllDeleted
->Wait();
1739 wxMutexLocker
lock(*gs_mutexAllThreads
);
1741 // terminate any threads left
1742 count
= gs_allThreads
.GetCount();
1745 wxLogDebug(wxT("%lu threads were not terminated by the application."),
1746 (unsigned long)count
);
1748 } // unlock mutex before deleting the threads as they lock it in their dtor
1750 for ( size_t n
= 0u; n
< count
; n
++ )
1752 // Delete calls the destructor which removes the current entry. We
1753 // should only delete the first one each time.
1754 gs_allThreads
[0]->Delete();
1757 delete gs_mutexAllThreads
;
1759 // destroy GUI mutex
1760 gs_mutexGui
->Unlock();
1763 // and free TLD slot
1764 (void)pthread_key_delete(gs_keySelf
);
1766 delete gs_condAllDeleted
;
1767 delete gs_mutexDeleteThread
;
1770 // ----------------------------------------------------------------------------
1772 // ----------------------------------------------------------------------------
1774 static void ScheduleThreadForDeletion()
1776 wxMutexLocker
lock( *gs_mutexDeleteThread
);
1778 gs_nThreadsBeingDeleted
++;
1780 wxLogTrace(TRACE_THREADS
, _T("%lu thread%s waiting to be deleted"),
1781 (unsigned long)gs_nThreadsBeingDeleted
,
1782 gs_nThreadsBeingDeleted
== 1 ?
_T("") : _T("s"));
1785 static void DeleteThread(wxThread
*This
)
1787 // gs_mutexDeleteThread should be unlocked before signalling the condition
1788 // or wxThreadModule::OnExit() would deadlock
1789 wxMutexLocker
locker( *gs_mutexDeleteThread
);
1791 wxLogTrace(TRACE_THREADS
, _T("Thread %ld auto deletes."), This
->GetId());
1795 wxCHECK_RET( gs_nThreadsBeingDeleted
> 0,
1796 _T("no threads scheduled for deletion, yet we delete one?") );
1798 wxLogTrace(TRACE_THREADS
, _T("%lu scheduled for deletion threads left."),
1799 (unsigned long)gs_nThreadsBeingDeleted
- 1);
1801 if ( !--gs_nThreadsBeingDeleted
)
1803 // no more threads left, signal it
1804 gs_condAllDeleted
->Signal();
1808 void wxMutexGuiEnterImpl()
1810 gs_mutexGui
->Lock();
1813 void wxMutexGuiLeaveImpl()
1815 gs_mutexGui
->Unlock();
1818 // ----------------------------------------------------------------------------
1819 // include common implementation code
1820 // ----------------------------------------------------------------------------
1822 #include "wx/thrimpl.cpp"
1824 #endif // wxUSE_THREADS