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
7 // Copyright: (c) Wolfram Gloger (1996, 1997)
8 // Guilhem Lavaux (1998)
9 // Vadim Zeitlin (1999-2002)
10 // Robert Roebling (1999)
11 // K. S. Sreeram (2002)
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // for compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
28 #include "wx/thread.h"
29 #include "wx/except.h"
33 #include "wx/dynarray.h"
38 #include "wx/stopwatch.h"
39 #include "wx/module.h"
47 #include <sys/time.h> // needed for at least __QNX__
52 #ifdef HAVE_THR_SETCONCURRENCY
56 #ifdef HAVE_ABI_FORCEDUNWIND
60 #ifdef HAVE_SETPRIORITY
61 #include <sys/resource.h> // for setpriority()
64 // we use wxFFile under Linux in GetCPUCount()
69 #define THR_ID_CAST(id) (reinterpret_cast<void*>(id))
70 #define THR_ID(thr) THR_ID_CAST((thr)->GetId())
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // the possible states of the thread and transitions from them
79 STATE_NEW
, // didn't start execution yet (=> RUNNING)
80 STATE_RUNNING
, // running (=> PAUSED or EXITED)
81 STATE_PAUSED
, // suspended (=> RUNNING or EXITED)
82 STATE_EXITED
// thread doesn't exist any more
85 // the exit value of a thread which has been cancelled
86 static const wxThread::ExitCode EXITCODE_CANCELLED
= (wxThread::ExitCode
)-1;
88 // trace mask for wxThread operations
89 #define TRACE_THREADS wxT("thread")
91 // you can get additional debugging messages for the semaphore operations
92 #define TRACE_SEMA wxT("semaphore")
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 static void ScheduleThreadForDeletion();
99 static void DeleteThread(wxThread
*This
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // an (non owning) array of pointers to threads
106 WX_DEFINE_ARRAY_PTR(wxThread
*, wxArrayThread
);
108 // an entry for a thread we can wait for
110 // -----------------------------------------------------------------------------
112 // -----------------------------------------------------------------------------
114 // we keep the list of all threads created by the application to be able to
115 // terminate them on exit if there are some left - otherwise the process would
117 static wxArrayThread gs_allThreads
;
119 // a mutex to protect gs_allThreads
120 static wxMutex
*gs_mutexAllThreads
= NULL
;
122 // the id of the main thread
124 // we suppose that 0 is not a valid pthread_t value but in principle this might
125 // be false (e.g. if it's a selector-like value), wxThread::IsMain() would need
126 // to be updated in such case
127 wxThreadIdType
wxThread::ms_idMainThread
= 0;
129 // the key for the pointer to the associated wxThread object
130 static pthread_key_t gs_keySelf
;
132 // the number of threads which are being deleted - the program won't exit
133 // until there are any left
134 static size_t gs_nThreadsBeingDeleted
= 0;
136 // a mutex to protect gs_nThreadsBeingDeleted
137 static wxMutex
*gs_mutexDeleteThread
= NULL
;
139 // and a condition variable which will be signaled when all
140 // gs_nThreadsBeingDeleted will have been deleted
141 static wxCondition
*gs_condAllDeleted
= NULL
;
144 // this mutex must be acquired before any call to a GUI function
145 // (it's not inside #if wxUSE_GUI because this file is compiled as part
147 static wxMutex
*gs_mutexGui
= NULL
;
150 // when we wait for a thread to exit, we're blocking on a condition which the
151 // thread signals in its SignalExit() method -- but this condition can't be a
152 // member of the thread itself as a detached thread may delete itself at any
153 // moment and accessing the condition member of the thread after this would
154 // result in a disaster
156 // so instead we maintain a global list of the structs below for the threads
157 // we're interested in waiting on
159 // ============================================================================
160 // wxMutex implementation
161 // ============================================================================
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 // this is a simple wrapper around pthread_mutex_t which provides error
169 class wxMutexInternal
172 wxMutexInternal(wxMutexType mutexType
);
176 wxMutexError
Lock(unsigned long ms
);
177 wxMutexError
TryLock();
178 wxMutexError
Unlock();
180 bool IsOk() const { return m_isOk
; }
183 // convert the result of pthread_mutex_[timed]lock() call to wx return code
184 wxMutexError
HandleLockResult(int err
);
187 pthread_mutex_t m_mutex
;
190 unsigned long m_owningThread
;
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
)
211 case wxMUTEX_RECURSIVE
:
212 // support recursive locks like Win32, i.e. a thread can lock a
213 // mutex which it had itself already locked
215 // unfortunately initialization of recursive mutexes is non
216 // portable, so try several methods
217 #ifdef HAVE_PTHREAD_MUTEXATTR_T
219 pthread_mutexattr_t attr
;
220 pthread_mutexattr_init(&attr
);
221 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
223 err
= pthread_mutex_init(&m_mutex
, &attr
);
225 #elif defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
226 // we can use this only as initializer so we have to assign it
227 // first to a temp var - assigning directly to m_mutex wouldn't
230 pthread_mutex_t mutex
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
233 #else // no recursive mutexes
235 #endif // HAVE_PTHREAD_MUTEXATTR_T/...
239 wxFAIL_MSG( wxT("unknown mutex type") );
242 case wxMUTEX_DEFAULT
:
243 err
= pthread_mutex_init(&m_mutex
, NULL
);
250 wxLogApiError( wxT("pthread_mutex_init()"), err
);
254 wxMutexInternal::~wxMutexInternal()
258 int err
= pthread_mutex_destroy(&m_mutex
);
261 wxLogApiError( wxT("pthread_mutex_destroy()"), err
);
266 wxMutexError
wxMutexInternal::Lock()
268 if ((m_type
== wxMUTEX_DEFAULT
) && (m_owningThread
!= 0))
270 if (m_owningThread
== wxThread::GetCurrentId())
271 return wxMUTEX_DEAD_LOCK
;
274 return HandleLockResult(pthread_mutex_lock(&m_mutex
));
277 wxMutexError
wxMutexInternal::Lock(unsigned long ms
)
279 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
280 static const long MSEC_IN_SEC
= 1000;
281 static const long NSEC_IN_MSEC
= 1000000;
282 static const long NSEC_IN_USEC
= 1000;
283 static const long NSEC_IN_SEC
= MSEC_IN_SEC
* NSEC_IN_MSEC
;
285 time_t seconds
= ms
/MSEC_IN_SEC
;
286 long nanoseconds
= (ms
% MSEC_IN_SEC
) * NSEC_IN_MSEC
;
287 timespec ts
= { 0, 0 };
289 // normally we should use clock_gettime(CLOCK_REALTIME) here but this
290 // function is in librt and we don't link with it currently, so use
291 // gettimeofday() instead -- if it turns out that this is really too
292 // imprecise, we should modify configure to check if clock_gettime() is
293 // available and whether it requires -lrt and use it instead
295 if ( clock_gettime(CLOCK_REALTIME
, &ts
) == 0 )
300 if ( wxGetTimeOfDay(&tv
) != -1 )
302 ts
.tv_sec
= tv
.tv_sec
;
303 ts
.tv_nsec
= tv
.tv_usec
*NSEC_IN_USEC
;
306 else // fall back on system timer
308 ts
.tv_sec
= time(NULL
);
311 ts
.tv_sec
+= seconds
;
312 ts
.tv_nsec
+= nanoseconds
;
313 if ( ts
.tv_nsec
> NSEC_IN_SEC
)
316 ts
.tv_nsec
-= NSEC_IN_SEC
;
319 return HandleLockResult(pthread_mutex_timedlock(&m_mutex
, &ts
));
320 #else // !HAVE_PTHREAD_MUTEX_TIMEDLOCK
323 return wxMUTEX_MISC_ERROR
;
324 #endif // HAVE_PTHREAD_MUTEX_TIMEDLOCK/!HAVE_PTHREAD_MUTEX_TIMEDLOCK
327 wxMutexError
wxMutexInternal::HandleLockResult(int err
)
329 // wxPrintf( "err %d\n", err );
334 // only error checking mutexes return this value and so it's an
335 // unexpected situation -- hence use assert, not wxLogDebug
336 wxFAIL_MSG( wxT("mutex deadlock prevented") );
337 return wxMUTEX_DEAD_LOCK
;
340 wxLogDebug(wxT("pthread_mutex_[timed]lock(): mutex not initialized"));
344 return wxMUTEX_TIMEOUT
;
347 if (m_type
== wxMUTEX_DEFAULT
)
348 m_owningThread
= wxThread::GetCurrentId();
349 return wxMUTEX_NO_ERROR
;
352 wxLogApiError(wxT("pthread_mutex_[timed]lock()"), err
);
355 return wxMUTEX_MISC_ERROR
;
359 wxMutexError
wxMutexInternal::TryLock()
361 int err
= pthread_mutex_trylock(&m_mutex
);
365 // not an error: mutex is already locked, but we're prepared for
370 wxLogDebug(wxT("pthread_mutex_trylock(): mutex not initialized."));
374 if (m_type
== wxMUTEX_DEFAULT
)
375 m_owningThread
= wxThread::GetCurrentId();
376 return wxMUTEX_NO_ERROR
;
379 wxLogApiError(wxT("pthread_mutex_trylock()"), err
);
382 return wxMUTEX_MISC_ERROR
;
385 wxMutexError
wxMutexInternal::Unlock()
389 int err
= pthread_mutex_unlock(&m_mutex
);
393 // we don't own the mutex
394 return wxMUTEX_UNLOCKED
;
397 wxLogDebug(wxT("pthread_mutex_unlock(): mutex not initialized."));
401 return wxMUTEX_NO_ERROR
;
404 wxLogApiError(wxT("pthread_mutex_unlock()"), err
);
407 return wxMUTEX_MISC_ERROR
;
410 // ===========================================================================
411 // wxCondition implementation
412 // ===========================================================================
414 // ---------------------------------------------------------------------------
415 // wxConditionInternal
416 // ---------------------------------------------------------------------------
418 // this is a wrapper around pthread_cond_t associated with a wxMutex (and hence
419 // with a pthread_mutex_t)
420 class wxConditionInternal
423 wxConditionInternal(wxMutex
& mutex
);
424 ~wxConditionInternal();
426 bool IsOk() const { return m_isOk
&& m_mutex
.IsOk(); }
429 wxCondError
WaitTimeout(unsigned long milliseconds
);
431 wxCondError
Signal();
432 wxCondError
Broadcast();
435 // get the POSIX mutex associated with us
436 pthread_mutex_t
*GetPMutex() const { return &m_mutex
.m_internal
->m_mutex
; }
439 pthread_cond_t m_cond
;
444 wxConditionInternal::wxConditionInternal(wxMutex
& mutex
)
447 int err
= pthread_cond_init(&m_cond
, NULL
/* default attributes */);
453 wxLogApiError(wxT("pthread_cond_init()"), err
);
457 wxConditionInternal::~wxConditionInternal()
461 int err
= pthread_cond_destroy(&m_cond
);
464 wxLogApiError(wxT("pthread_cond_destroy()"), err
);
469 wxCondError
wxConditionInternal::Wait()
471 int err
= pthread_cond_wait(&m_cond
, GetPMutex());
474 wxLogApiError(wxT("pthread_cond_wait()"), err
);
476 return wxCOND_MISC_ERROR
;
479 return wxCOND_NO_ERROR
;
482 wxCondError
wxConditionInternal::WaitTimeout(unsigned long milliseconds
)
484 wxLongLong curtime
= wxGetUTCTimeMillis();
485 curtime
+= milliseconds
;
486 wxLongLong temp
= curtime
/ 1000;
487 int sec
= temp
.GetLo();
489 temp
= curtime
- temp
;
490 int millis
= temp
.GetLo();
495 tspec
.tv_nsec
= millis
* 1000L * 1000L;
497 int err
= pthread_cond_timedwait( &m_cond
, GetPMutex(), &tspec
);
501 return wxCOND_TIMEOUT
;
504 return wxCOND_NO_ERROR
;
507 wxLogApiError(wxT("pthread_cond_timedwait()"), err
);
510 return wxCOND_MISC_ERROR
;
513 wxCondError
wxConditionInternal::Signal()
515 int err
= pthread_cond_signal(&m_cond
);
518 wxLogApiError(wxT("pthread_cond_signal()"), err
);
520 return wxCOND_MISC_ERROR
;
523 return wxCOND_NO_ERROR
;
526 wxCondError
wxConditionInternal::Broadcast()
528 int err
= pthread_cond_broadcast(&m_cond
);
531 wxLogApiError(wxT("pthread_cond_broadcast()"), err
);
533 return wxCOND_MISC_ERROR
;
536 return wxCOND_NO_ERROR
;
539 // ===========================================================================
540 // wxSemaphore implementation
541 // ===========================================================================
543 // ---------------------------------------------------------------------------
544 // wxSemaphoreInternal
545 // ---------------------------------------------------------------------------
547 // we implement the semaphores using mutexes and conditions instead of using
548 // the sem_xxx() POSIX functions because they're not widely available and also
549 // because it's impossible to implement WaitTimeout() using them
550 class wxSemaphoreInternal
553 wxSemaphoreInternal(int initialcount
, int maxcount
);
555 bool IsOk() const { return m_isOk
; }
558 wxSemaError
TryWait();
559 wxSemaError
WaitTimeout(unsigned long milliseconds
);
573 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
577 if ( (initialcount
< 0 || maxcount
< 0) ||
578 ((maxcount
> 0) && (initialcount
> maxcount
)) )
580 wxFAIL_MSG( wxT("wxSemaphore: invalid initial or maximal count") );
586 m_maxcount
= (size_t)maxcount
;
587 m_count
= (size_t)initialcount
;
590 m_isOk
= m_mutex
.IsOk() && m_cond
.IsOk();
593 wxSemaError
wxSemaphoreInternal::Wait()
595 wxMutexLocker
locker(m_mutex
);
597 while ( m_count
== 0 )
599 wxLogTrace(TRACE_SEMA
,
600 wxT("Thread %p waiting for semaphore to become signalled"),
601 THR_ID_CAST(wxThread::GetCurrentId()));
603 if ( m_cond
.Wait() != wxCOND_NO_ERROR
)
604 return wxSEMA_MISC_ERROR
;
606 wxLogTrace(TRACE_SEMA
,
607 wxT("Thread %p finished waiting for semaphore, count = %lu"),
608 THR_ID_CAST(wxThread::GetCurrentId()), (unsigned long)m_count
);
613 return wxSEMA_NO_ERROR
;
616 wxSemaError
wxSemaphoreInternal::TryWait()
618 wxMutexLocker
locker(m_mutex
);
625 return wxSEMA_NO_ERROR
;
628 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds
)
630 wxMutexLocker
locker(m_mutex
);
632 wxLongLong startTime
= wxGetLocalTimeMillis();
634 while ( m_count
== 0 )
636 wxLongLong elapsed
= wxGetLocalTimeMillis() - startTime
;
637 long remainingTime
= (long)milliseconds
- (long)elapsed
.GetLo();
638 if ( remainingTime
<= 0 )
641 return wxSEMA_TIMEOUT
;
644 switch ( m_cond
.WaitTimeout(remainingTime
) )
647 return wxSEMA_TIMEOUT
;
650 return wxSEMA_MISC_ERROR
;
652 case wxCOND_NO_ERROR
:
659 return wxSEMA_NO_ERROR
;
662 wxSemaError
wxSemaphoreInternal::Post()
664 wxMutexLocker
locker(m_mutex
);
666 if ( m_maxcount
> 0 && m_count
== m_maxcount
)
668 return wxSEMA_OVERFLOW
;
673 wxLogTrace(TRACE_SEMA
,
674 wxT("Thread %p about to signal semaphore, count = %lu"),
675 THR_ID_CAST(wxThread::GetCurrentId()), (unsigned long)m_count
);
677 return m_cond
.Signal() == wxCOND_NO_ERROR
? wxSEMA_NO_ERROR
681 // ===========================================================================
682 // wxThread implementation
683 // ===========================================================================
685 // the thread callback functions must have the C linkage
689 #ifdef wxHAVE_PTHREAD_CLEANUP
690 // thread exit function
691 void wxPthreadCleanup(void *ptr
);
692 #endif // wxHAVE_PTHREAD_CLEANUP
694 void *wxPthreadStart(void *ptr
);
698 // ----------------------------------------------------------------------------
700 // ----------------------------------------------------------------------------
702 class wxThreadInternal
708 // thread entry function
709 static void *PthreadStart(wxThread
*thread
);
713 wxThreadError
Create(wxThread
*thread
, unsigned int stackSize
);
716 // unblock the thread allowing it to run
717 void SignalRun() { m_semRun
.Post(); }
718 // ask the thread to terminate
720 // go to sleep until Resume() is called
727 int GetPriority() const { return m_prio
; }
728 void SetPriority(int prio
) { m_prio
= prio
; }
730 wxThreadState
GetState() const { return m_state
; }
731 void SetState(wxThreadState state
)
734 static const wxChar
*const stateNames
[] =
742 wxLogTrace(TRACE_THREADS
, wxT("Thread %p: %s => %s."),
743 THR_ID(this), stateNames
[m_state
], stateNames
[state
]);
744 #endif // wxUSE_LOG_TRACE
749 pthread_t
GetId() const { return m_threadId
; }
750 pthread_t
*GetIdPtr() { return &m_threadId
; }
752 bool WasCreated() const { return m_created
; }
754 void SetCancelFlag() { m_cancelled
= true; }
755 bool WasCancelled() const { return m_cancelled
; }
757 void SetExitCode(wxThread::ExitCode exitcode
) { m_exitcode
= exitcode
; }
758 wxThread::ExitCode
GetExitCode() const { return m_exitcode
; }
761 void SetReallyPaused(bool paused
) { m_isPaused
= paused
; }
762 bool IsReallyPaused() const { return m_isPaused
; }
764 // tell the thread that it is a detached one
767 wxCriticalSectionLocker
lock(m_csJoinFlag
);
769 m_shouldBeJoined
= false;
773 #ifdef wxHAVE_PTHREAD_CLEANUP
774 // this is used by wxPthreadCleanup() only
775 static void Cleanup(wxThread
*thread
);
776 #endif // wxHAVE_PTHREAD_CLEANUP
779 pthread_t m_threadId
; // id of the thread
780 wxThreadState m_state
; // see wxThreadState enum
781 int m_prio
; // in wxWidgets units: from 0 to 100
783 // this flag is set when the thread was successfully created
786 // this flag is set when the thread should terminate
789 // this flag is set when the thread is blocking on m_semSuspend
792 // the thread exit code - only used for joinable (!detached) threads and
793 // is only valid after the thread termination
794 wxThread::ExitCode m_exitcode
;
796 // many threads may call Wait(), but only one of them should call
797 // pthread_join(), so we have to keep track of this
798 wxCriticalSection m_csJoinFlag
;
799 bool m_shouldBeJoined
;
802 // this semaphore is posted by Run() and the threads Entry() is not
803 // called before it is done
804 wxSemaphore m_semRun
;
806 // this one is signaled when the thread should resume after having been
808 wxSemaphore m_semSuspend
;
811 // ----------------------------------------------------------------------------
812 // thread startup and exit functions
813 // ----------------------------------------------------------------------------
815 void *wxPthreadStart(void *ptr
)
817 return wxThreadInternal::PthreadStart((wxThread
*)ptr
);
820 void *wxThreadInternal::PthreadStart(wxThread
*thread
)
822 wxThreadInternal
*pthread
= thread
->m_internal
;
824 wxLogTrace(TRACE_THREADS
, wxT("Thread %p started."), THR_ID(pthread
));
826 // associate the thread pointer with the newly created thread so that
827 // wxThread::This() will work
828 int rc
= pthread_setspecific(gs_keySelf
, thread
);
831 wxLogSysError(rc
, _("Cannot start thread: error writing TLS."));
836 // have to declare this before pthread_cleanup_push() which defines a
840 #ifdef wxHAVE_PTHREAD_CLEANUP
841 // install the cleanup handler which will be called if the thread is
843 pthread_cleanup_push(wxPthreadCleanup
, thread
);
844 #endif // wxHAVE_PTHREAD_CLEANUP
846 // wait for the semaphore to be posted from Run()
847 pthread
->m_semRun
.Wait();
849 // test whether we should run the run at all - may be it was deleted
850 // before it started to Run()?
852 wxCriticalSectionLocker
lock(thread
->m_critsect
);
854 dontRunAtAll
= pthread
->GetState() == STATE_NEW
&&
855 pthread
->WasCancelled();
860 // call the main entry
861 wxLogTrace(TRACE_THREADS
,
862 wxT("Thread %p about to enter its Entry()."),
867 pthread
->m_exitcode
= thread
->CallEntry();
869 wxLogTrace(TRACE_THREADS
,
870 wxT("Thread %p Entry() returned %lu."),
871 THR_ID(pthread
), wxPtrToUInt(pthread
->m_exitcode
));
873 #ifdef HAVE_ABI_FORCEDUNWIND
874 // When using common C++ ABI under Linux we must always rethrow this
875 // special exception used to unwind the stack when the thread was
876 // cancelled, otherwise the thread library would simply terminate the
877 // program, see http://udrepper.livejournal.com/21541.html
878 catch ( abi::__forced_unwind
& )
880 wxCriticalSectionLocker
lock(thread
->m_critsect
);
881 pthread
->SetState(STATE_EXITED
);
884 #endif // HAVE_ABI_FORCEDUNWIND
885 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
888 wxCriticalSectionLocker
lock(thread
->m_critsect
);
890 // change the state of the thread to "exited" so that
891 // wxPthreadCleanup handler won't do anything from now (if it's
892 // called before we do pthread_cleanup_pop below)
893 pthread
->SetState(STATE_EXITED
);
897 // NB: pthread_cleanup_push/pop() are macros and pop contains the matching
898 // '}' for the '{' in push, so they must be used in the same block!
899 #ifdef wxHAVE_PTHREAD_CLEANUP
901 // under Tru64 we get a warning from macro expansion
903 #pragma message disable(declbutnotref)
906 // remove the cleanup handler without executing it
907 pthread_cleanup_pop(FALSE
);
910 #pragma message restore
912 #endif // wxHAVE_PTHREAD_CLEANUP
916 // FIXME: deleting a possibly joinable thread here???
919 return EXITCODE_CANCELLED
;
923 // terminate the thread
924 thread
->Exit(pthread
->m_exitcode
);
926 wxFAIL_MSG(wxT("wxThread::Exit() can't return."));
932 #ifdef wxHAVE_PTHREAD_CLEANUP
934 // this handler is called when the thread is cancelled
935 extern "C" void wxPthreadCleanup(void *ptr
)
937 wxThreadInternal::Cleanup((wxThread
*)ptr
);
940 void wxThreadInternal::Cleanup(wxThread
*thread
)
942 if (pthread_getspecific(gs_keySelf
) == 0) return;
944 wxCriticalSectionLocker
lock(thread
->m_critsect
);
945 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
947 // thread is already considered as finished.
952 // exit the thread gracefully
953 thread
->Exit(EXITCODE_CANCELLED
);
956 #endif // wxHAVE_PTHREAD_CLEANUP
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
962 wxThreadInternal::wxThreadInternal()
967 m_prio
= wxPRIORITY_DEFAULT
;
971 // set to true only when the thread starts waiting on m_semSuspend
974 // defaults for joinable threads
975 m_shouldBeJoined
= true;
976 m_isDetached
= false;
979 wxThreadInternal::~wxThreadInternal()
983 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
984 #define WXUNUSED_STACKSIZE(identifier) identifier
986 #define WXUNUSED_STACKSIZE(identifier) WXUNUSED(identifier)
989 wxThreadError
wxThreadInternal::Create(wxThread
*thread
,
990 unsigned int WXUNUSED_STACKSIZE(stackSize
))
992 if ( GetState() != STATE_NEW
)
994 // don't recreate thread
995 return wxTHREAD_RUNNING
;
998 // set up the thread attribute: right now, we only set thread priority
1000 pthread_attr_init(&attr
);
1002 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1004 pthread_attr_setstacksize(&attr
, stackSize
);
1007 #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
1009 if ( pthread_attr_getschedpolicy(&attr
, &policy
) != 0 )
1011 wxLogError(_("Cannot retrieve thread scheduling policy."));
1015 /* the pthread.h contains too many spaces. This is a work-around */
1016 # undef sched_get_priority_max
1017 #undef sched_get_priority_min
1018 #define sched_get_priority_max(_pol_) \
1019 (_pol_ == SCHED_OTHER ? PRI_FG_MAX_NP : PRI_FIFO_MAX)
1020 #define sched_get_priority_min(_pol_) \
1021 (_pol_ == SCHED_OTHER ? PRI_FG_MIN_NP : PRI_FIFO_MIN)
1024 int max_prio
= sched_get_priority_max(policy
),
1025 min_prio
= sched_get_priority_min(policy
),
1026 prio
= GetPriority();
1028 if ( min_prio
== -1 || max_prio
== -1 )
1030 wxLogError(_("Cannot get priority range for scheduling policy %d."),
1033 else if ( max_prio
== min_prio
)
1035 if ( prio
!= wxPRIORITY_DEFAULT
)
1037 // notify the programmer that this doesn't work here
1038 wxLogWarning(_("Thread priority setting is ignored."));
1040 //else: we have default priority, so don't complain
1042 // anyhow, don't do anything because priority is just ignored
1046 struct sched_param sp
;
1047 if ( pthread_attr_getschedparam(&attr
, &sp
) != 0 )
1049 wxFAIL_MSG(wxT("pthread_attr_getschedparam() failed"));
1052 sp
.sched_priority
= min_prio
+ (prio
*(max_prio
- min_prio
))/100;
1054 if ( pthread_attr_setschedparam(&attr
, &sp
) != 0 )
1056 wxFAIL_MSG(wxT("pthread_attr_setschedparam(priority) failed"));
1059 #endif // HAVE_THREAD_PRIORITY_FUNCTIONS
1061 #ifdef HAVE_PTHREAD_ATTR_SETSCOPE
1062 // this will make the threads created by this process really concurrent
1063 if ( pthread_attr_setscope(&attr
, PTHREAD_SCOPE_SYSTEM
) != 0 )
1065 wxFAIL_MSG(wxT("pthread_attr_setscope(PTHREAD_SCOPE_SYSTEM) failed"));
1067 #endif // HAVE_PTHREAD_ATTR_SETSCOPE
1069 // VZ: assume that this one is always available (it's rather fundamental),
1070 // if this function is ever missing we should try to use
1071 // pthread_detach() instead (after thread creation)
1072 if ( thread
->IsDetached() )
1074 if ( pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
) != 0 )
1076 wxFAIL_MSG(wxT("pthread_attr_setdetachstate(DETACHED) failed"));
1079 // never try to join detached threads
1082 //else: threads are created joinable by default, it's ok
1084 // create the new OS thread object
1085 int rc
= pthread_create
1093 if ( pthread_attr_destroy(&attr
) != 0 )
1095 wxFAIL_MSG(wxT("pthread_attr_destroy() failed"));
1100 SetState(STATE_EXITED
);
1102 return wxTHREAD_NO_RESOURCE
;
1106 return wxTHREAD_NO_ERROR
;
1109 wxThreadError
wxThreadInternal::Run()
1111 wxCHECK_MSG( GetState() == STATE_NEW
, wxTHREAD_RUNNING
,
1112 wxT("thread may only be started once after Create()") );
1114 SetState(STATE_RUNNING
);
1116 // wake up threads waiting for our start
1119 return wxTHREAD_NO_ERROR
;
1122 void wxThreadInternal::Wait()
1124 wxCHECK_RET( !m_isDetached
, wxT("can't wait for a detached thread") );
1126 // if the thread we're waiting for is waiting for the GUI mutex, we will
1127 // deadlock so make sure we release it temporarily
1128 if ( wxThread::IsMain() )
1131 // give the thread we're waiting for chance to do the GUI call
1132 // it might be in, we don't do this conditionally as the to be waited on
1133 // thread might have to acquire the mutex later but before terminating
1134 if ( wxGuiOwnedByMainThread() )
1141 wxLogTrace(TRACE_THREADS
,
1142 wxT("Starting to wait for thread %p to exit."),
1145 // to avoid memory leaks we should call pthread_join(), but it must only be
1146 // done once so use a critical section to serialize the code below
1148 wxCriticalSectionLocker
lock(m_csJoinFlag
);
1150 if ( m_shouldBeJoined
)
1152 // FIXME shouldn't we set cancellation type to DISABLED here? If
1153 // we're cancelled inside pthread_join(), things will almost
1154 // certainly break - but if we disable the cancellation, we
1156 if ( pthread_join(GetId(), &m_exitcode
) != 0 )
1158 // this is a serious problem, so use wxLogError and not
1159 // wxLogDebug: it is possible to bring the system to its knees
1160 // by creating too many threads and not joining them quite
1162 wxLogError(_("Failed to join a thread, potential memory leak detected - please restart the program"));
1165 m_shouldBeJoined
= false;
1170 // reacquire GUI mutex
1171 if ( wxThread::IsMain() )
1176 void wxThreadInternal::Pause()
1178 // the state is set from the thread which pauses us first, this function
1179 // is called later so the state should have been already set
1180 wxCHECK_RET( m_state
== STATE_PAUSED
,
1181 wxT("thread must first be paused with wxThread::Pause().") );
1183 wxLogTrace(TRACE_THREADS
,
1184 wxT("Thread %p goes to sleep."), THR_ID(this));
1186 // wait until the semaphore is Post()ed from Resume()
1187 m_semSuspend
.Wait();
1190 void wxThreadInternal::Resume()
1192 wxCHECK_RET( m_state
== STATE_PAUSED
,
1193 wxT("can't resume thread which is not suspended.") );
1195 // the thread might be not actually paused yet - if there were no call to
1196 // TestDestroy() since the last call to Pause() for example
1197 if ( IsReallyPaused() )
1199 wxLogTrace(TRACE_THREADS
,
1200 wxT("Waking up thread %p"), THR_ID(this));
1203 m_semSuspend
.Post();
1206 SetReallyPaused(false);
1210 wxLogTrace(TRACE_THREADS
,
1211 wxT("Thread %p is not yet really paused"), THR_ID(this));
1214 SetState(STATE_RUNNING
);
1217 // -----------------------------------------------------------------------------
1218 // wxThread static functions
1219 // -----------------------------------------------------------------------------
1221 wxThread
*wxThread::This()
1223 return (wxThread
*)pthread_getspecific(gs_keySelf
);
1226 void wxThread::Yield()
1228 #ifdef HAVE_SCHED_YIELD
1233 int wxThread::GetCPUCount()
1235 #if defined(_SC_NPROCESSORS_ONLN)
1236 // this works for Solaris and Linux 2.6
1237 int rc
= sysconf(_SC_NPROCESSORS_ONLN
);
1242 #elif defined(__LINUX__) && wxUSE_FFILE
1243 // read from proc (can't use wxTextFile here because it's a special file:
1244 // it has 0 size but still can be read from)
1247 wxFFile
file(wxT("/proc/cpuinfo"));
1248 if ( file
.IsOpened() )
1250 // slurp the whole file
1252 if ( file
.ReadAll(&s
) )
1254 // (ab)use Replace() to find the number of "processor: num" strings
1255 size_t count
= s
.Replace(wxT("processor\t:"), wxT(""));
1261 wxLogDebug(wxT("failed to parse /proc/cpuinfo"));
1265 wxLogDebug(wxT("failed to read /proc/cpuinfo"));
1268 #endif // different ways to get number of CPUs
1274 wxThreadIdType
wxThread::GetCurrentId()
1276 return (wxThreadIdType
)pthread_self();
1280 bool wxThread::SetConcurrency(size_t level
)
1282 #ifdef HAVE_PTHREAD_SET_CONCURRENCY
1283 int rc
= pthread_setconcurrency( level
);
1284 #elif defined(HAVE_THR_SETCONCURRENCY)
1285 int rc
= thr_setconcurrency(level
);
1286 #else // !HAVE_THR_SETCONCURRENCY
1287 // ok only for the default value
1288 int rc
= level
== 0 ? 0 : -1;
1289 #endif // HAVE_THR_SETCONCURRENCY/!HAVE_THR_SETCONCURRENCY
1293 wxLogSysError(rc
, _("Failed to set thread concurrency level to %lu"),
1294 static_cast<unsigned long>(level
));
1301 // -----------------------------------------------------------------------------
1303 // -----------------------------------------------------------------------------
1305 wxThread::wxThread(wxThreadKind kind
)
1307 // add this thread to the global list of all threads
1309 wxMutexLocker
lock(*gs_mutexAllThreads
);
1311 gs_allThreads
.Add(this);
1314 m_internal
= new wxThreadInternal();
1316 m_isDetached
= kind
== wxTHREAD_DETACHED
;
1319 wxThreadError
wxThread::Create(unsigned int stackSize
)
1321 wxCriticalSectionLocker
lock(m_critsect
);
1323 return m_internal
->Create(this, stackSize
);
1326 wxThreadError
wxThread::Run()
1328 wxCriticalSectionLocker
lock(m_critsect
);
1330 // Create the thread if it wasn't created yet with an explicit
1332 if ( !m_internal
->WasCreated() )
1334 wxThreadError rv
= m_internal
->Create(this, 0);
1335 if ( rv
!= wxTHREAD_NO_ERROR
)
1339 return m_internal
->Run();
1342 // -----------------------------------------------------------------------------
1344 // -----------------------------------------------------------------------------
1346 void wxThread::SetPriority(unsigned int prio
)
1348 wxCHECK_RET( wxPRIORITY_MIN
<= prio
&& prio
<= wxPRIORITY_MAX
,
1349 wxT("invalid thread priority") );
1351 wxCriticalSectionLocker
lock(m_critsect
);
1353 switch ( m_internal
->GetState() )
1356 // thread not yet started, priority will be set when it is
1357 m_internal
->SetPriority(prio
);
1362 #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
1363 #if defined(__LINUX__)
1364 // On Linux, pthread_setschedparam with SCHED_OTHER does not allow
1365 // a priority other than 0. Instead, we use the BSD setpriority
1366 // which alllows us to set a 'nice' value between 20 to -20. Only
1367 // super user can set a value less than zero (more negative yields
1368 // higher priority). setpriority set the static priority of a
1369 // process, but this is OK since Linux is configured as a thread
1372 // FIXME this is not true for 2.6!!
1374 // map wx priorites 0..100 to Unix priorities 20..-20
1375 if ( setpriority(PRIO_PROCESS
, 0, -(2*(int)prio
)/5 + 20) == -1 )
1377 wxLogError(_("Failed to set thread priority %d."), prio
);
1381 struct sched_param sparam
;
1382 sparam
.sched_priority
= prio
;
1384 if ( pthread_setschedparam(m_internal
->GetId(),
1385 SCHED_OTHER
, &sparam
) != 0 )
1387 wxLogError(_("Failed to set thread priority %d."), prio
);
1391 #endif // HAVE_THREAD_PRIORITY_FUNCTIONS
1396 wxFAIL_MSG(wxT("impossible to set thread priority in this state"));
1400 unsigned int wxThread::GetPriority() const
1402 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1404 return m_internal
->GetPriority();
1407 wxThreadIdType
wxThread::GetId() const
1409 return (wxThreadIdType
) m_internal
->GetId();
1412 // -----------------------------------------------------------------------------
1414 // -----------------------------------------------------------------------------
1416 wxThreadError
wxThread::Pause()
1418 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1419 wxT("a thread can't pause itself") );
1421 wxCriticalSectionLocker
lock(m_critsect
);
1423 if ( m_internal
->GetState() != STATE_RUNNING
)
1425 wxLogDebug(wxT("Can't pause thread which is not running."));
1427 return wxTHREAD_NOT_RUNNING
;
1430 // just set a flag, the thread will be really paused only during the next
1431 // call to TestDestroy()
1432 m_internal
->SetState(STATE_PAUSED
);
1434 return wxTHREAD_NO_ERROR
;
1437 wxThreadError
wxThread::Resume()
1439 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1440 wxT("a thread can't resume itself") );
1442 wxCriticalSectionLocker
lock(m_critsect
);
1444 wxThreadState state
= m_internal
->GetState();
1449 wxLogTrace(TRACE_THREADS
, wxT("Thread %p suspended, resuming."),
1452 m_internal
->Resume();
1454 return wxTHREAD_NO_ERROR
;
1457 wxLogTrace(TRACE_THREADS
, wxT("Thread %p exited, won't resume."),
1459 return wxTHREAD_NO_ERROR
;
1462 wxLogDebug(wxT("Attempt to resume a thread which is not paused."));
1464 return wxTHREAD_MISC_ERROR
;
1468 // -----------------------------------------------------------------------------
1470 // -----------------------------------------------------------------------------
1472 wxThread::ExitCode
wxThread::Wait(wxThreadWait
WXUNUSED(waitMode
))
1474 wxCHECK_MSG( This() != this, (ExitCode
)-1,
1475 wxT("a thread can't wait for itself") );
1477 wxCHECK_MSG( !m_isDetached
, (ExitCode
)-1,
1478 wxT("can't wait for detached thread") );
1482 return m_internal
->GetExitCode();
1485 wxThreadError
wxThread::Delete(ExitCode
*rc
, wxThreadWait
WXUNUSED(waitMode
))
1487 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1488 wxT("a thread can't delete itself") );
1490 bool isDetached
= m_isDetached
;
1493 wxThreadState state
= m_internal
->GetState();
1495 // ask the thread to stop
1496 m_internal
->SetCancelFlag();
1505 // we need to wake up the thread so that PthreadStart() will
1506 // terminate - right now it's blocking on run semaphore in
1508 m_internal
->SignalRun();
1517 // resume the thread first
1518 m_internal
->Resume();
1525 // wait until the thread stops
1530 // return the exit code of the thread
1531 *rc
= m_internal
->GetExitCode();
1534 //else: can't wait for detached threads
1537 if (state
== STATE_NEW
)
1538 return wxTHREAD_MISC_ERROR
;
1539 // for coherency with the MSW implementation, signal the user that
1540 // Delete() was called on a thread which didn't start to run yet.
1542 return wxTHREAD_NO_ERROR
;
1545 wxThreadError
wxThread::Kill()
1547 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR
,
1548 wxT("a thread can't kill itself") );
1552 switch ( m_internal
->GetState() )
1556 return wxTHREAD_NOT_RUNNING
;
1559 // resume the thread first
1565 #ifdef HAVE_PTHREAD_CANCEL
1566 if ( pthread_cancel(m_internal
->GetId()) != 0 )
1567 #endif // HAVE_PTHREAD_CANCEL
1569 wxLogError(_("Failed to terminate a thread."));
1571 return wxTHREAD_MISC_ERROR
;
1574 #ifdef HAVE_PTHREAD_CANCEL
1577 // if we use cleanup function, this will be done from
1578 // wxPthreadCleanup()
1579 #ifndef wxHAVE_PTHREAD_CLEANUP
1580 ScheduleThreadForDeletion();
1582 // don't call OnExit() here, it can only be called in the
1583 // threads context and we're in the context of another thread
1586 #endif // wxHAVE_PTHREAD_CLEANUP
1590 m_internal
->SetExitCode(EXITCODE_CANCELLED
);
1593 return wxTHREAD_NO_ERROR
;
1594 #endif // HAVE_PTHREAD_CANCEL
1598 void wxThread::Exit(ExitCode status
)
1600 wxASSERT_MSG( This() == this,
1601 wxT("wxThread::Exit() can only be called in the context of the same thread") );
1605 // from the moment we call OnExit(), the main program may terminate at
1606 // any moment, so mark this thread as being already in process of being
1607 // deleted or wxThreadModule::OnExit() will try to delete it again
1608 ScheduleThreadForDeletion();
1611 // don't enter m_critsect before calling OnExit() because the user code
1612 // might deadlock if, for example, it signals a condition in OnExit() (a
1613 // common case) while the main thread calls any of functions entering
1614 // m_critsect on us (almost all of them do)
1619 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
1621 // delete C++ thread object if this is a detached thread - user is
1622 // responsible for doing this for joinable ones
1625 // FIXME I'm feeling bad about it - what if another thread function is
1626 // called (in another thread context) now? It will try to access
1627 // half destroyed object which will probably result in something
1628 // very bad - but we can't protect this by a crit section unless
1629 // we make it a global object, but this would mean that we can
1630 // only call one thread function at a time :-(
1632 pthread_setspecific(gs_keySelf
, 0);
1637 m_internal
->SetState(STATE_EXITED
);
1641 // terminate the thread (pthread_exit() never returns)
1642 pthread_exit(status
);
1644 wxFAIL_MSG(wxT("pthread_exit() failed"));
1647 // also test whether we were paused
1648 bool wxThread::TestDestroy()
1650 wxASSERT_MSG( This() == this,
1651 wxT("wxThread::TestDestroy() can only be called in the context of the same thread") );
1655 if ( m_internal
->GetState() == STATE_PAUSED
)
1657 m_internal
->SetReallyPaused(true);
1659 // leave the crit section or the other threads will stop too if they
1660 // try to call any of (seemingly harmless) IsXXX() functions while we
1664 m_internal
->Pause();
1668 // thread wasn't requested to pause, nothing to do
1672 return m_internal
->WasCancelled();
1675 wxThread::~wxThread()
1679 // check that the thread either exited or couldn't be created
1680 if ( m_internal
->GetState() != STATE_EXITED
&&
1681 m_internal
->GetState() != STATE_NEW
)
1683 wxLogDebug(wxT("The thread %p is being destroyed although it is still running! The application may crash."),
1691 // remove this thread from the global array
1693 wxMutexLocker
lock(*gs_mutexAllThreads
);
1695 gs_allThreads
.Remove(this);
1699 // -----------------------------------------------------------------------------
1701 // -----------------------------------------------------------------------------
1703 bool wxThread::IsRunning() const
1705 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1707 return m_internal
->GetState() == STATE_RUNNING
;
1710 bool wxThread::IsAlive() const
1712 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1714 switch ( m_internal
->GetState() )
1725 bool wxThread::IsPaused() const
1727 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
1729 return (m_internal
->GetState() == STATE_PAUSED
);
1732 //--------------------------------------------------------------------
1734 //--------------------------------------------------------------------
1737 void wxOSXThreadModuleOnInit();
1738 void wxOSXThreadModuleOnExit();
1741 class wxThreadModule
: public wxModule
1744 virtual bool OnInit();
1745 virtual void OnExit();
1748 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1751 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1753 bool wxThreadModule::OnInit()
1755 int rc
= pthread_key_create(&gs_keySelf
, NULL
/* dtor function */);
1758 wxLogSysError(rc
, _("Thread module initialization failed: failed to create thread key"));
1763 wxThread::ms_idMainThread
= wxThread::GetCurrentId();
1765 gs_mutexAllThreads
= new wxMutex();
1768 wxOSXThreadModuleOnInit();
1770 gs_mutexGui
= new wxMutex();
1771 gs_mutexGui
->Lock();
1774 gs_mutexDeleteThread
= new wxMutex();
1775 gs_condAllDeleted
= new wxCondition(*gs_mutexDeleteThread
);
1780 void wxThreadModule::OnExit()
1782 wxASSERT_MSG( wxThread::IsMain(), wxT("only main thread can be here") );
1784 // are there any threads left which are being deleted right now?
1785 size_t nThreadsBeingDeleted
;
1788 wxMutexLocker
lock( *gs_mutexDeleteThread
);
1789 nThreadsBeingDeleted
= gs_nThreadsBeingDeleted
;
1791 if ( nThreadsBeingDeleted
> 0 )
1793 wxLogTrace(TRACE_THREADS
,
1794 wxT("Waiting for %lu threads to disappear"),
1795 (unsigned long)nThreadsBeingDeleted
);
1797 // have to wait until all of them disappear
1798 gs_condAllDeleted
->Wait();
1805 wxMutexLocker
lock(*gs_mutexAllThreads
);
1807 // terminate any threads left
1808 count
= gs_allThreads
.GetCount();
1811 wxLogDebug(wxT("%lu threads were not terminated by the application."),
1812 (unsigned long)count
);
1814 } // unlock mutex before deleting the threads as they lock it in their dtor
1816 for ( size_t n
= 0u; n
< count
; n
++ )
1818 // Delete calls the destructor which removes the current entry. We
1819 // should only delete the first one each time.
1820 gs_allThreads
[0]->Delete();
1823 delete gs_mutexAllThreads
;
1826 wxOSXThreadModuleOnExit();
1828 // destroy GUI mutex
1829 gs_mutexGui
->Unlock();
1833 // and free TLD slot
1834 (void)pthread_key_delete(gs_keySelf
);
1836 delete gs_condAllDeleted
;
1837 delete gs_mutexDeleteThread
;
1840 // ----------------------------------------------------------------------------
1842 // ----------------------------------------------------------------------------
1844 static void ScheduleThreadForDeletion()
1846 wxMutexLocker
lock( *gs_mutexDeleteThread
);
1848 gs_nThreadsBeingDeleted
++;
1850 wxLogTrace(TRACE_THREADS
, wxT("%lu thread%s waiting to be deleted"),
1851 (unsigned long)gs_nThreadsBeingDeleted
,
1852 gs_nThreadsBeingDeleted
== 1 ? wxT("") : wxT("s"));
1855 static void DeleteThread(wxThread
*This
)
1857 wxLogTrace(TRACE_THREADS
, wxT("Thread %p auto deletes."), THR_ID(This
));
1861 // only lock gs_mutexDeleteThread after deleting the thread to avoid
1862 // calling out into user code with it locked as this may result in
1863 // deadlocks if the thread dtor deletes another thread (see #11501)
1864 wxMutexLocker
locker( *gs_mutexDeleteThread
);
1866 wxCHECK_RET( gs_nThreadsBeingDeleted
> 0,
1867 wxT("no threads scheduled for deletion, yet we delete one?") );
1869 wxLogTrace(TRACE_THREADS
, wxT("%lu threads remain scheduled for deletion."),
1870 (unsigned long)gs_nThreadsBeingDeleted
- 1);
1872 if ( !--gs_nThreadsBeingDeleted
)
1874 // no more threads left, signal it
1875 gs_condAllDeleted
->Signal();
1881 void wxMutexGuiEnterImpl()
1883 gs_mutexGui
->Lock();
1886 void wxMutexGuiLeaveImpl()
1888 gs_mutexGui
->Unlock();
1893 // ----------------------------------------------------------------------------
1894 // include common implementation code
1895 // ----------------------------------------------------------------------------
1897 #include "wx/thrimpl.cpp"
1899 #endif // wxUSE_THREADS