]> git.saurik.com Git - wxWidgets.git/blame - src/unix/threadpsx.cpp
allow passing literal strings and results of c_str() to SetXXXLabels() methods too
[wxWidgets.git] / src / unix / threadpsx.cpp
CommitLineData
518b5d2f 1/////////////////////////////////////////////////////////////////////////////
ad9835c9 2// Name: src/unix/threadpsx.cpp
518b5d2f
VZ
3// Purpose: wxThread (Posix) Implementation
4// Author: Original from Wolfram Gloger/Guilhem Lavaux
be809868 5// Modified by: K. S. Sreeram (2002): POSIXified wxCondition, added wxSemaphore
518b5d2f
VZ
6// Created: 04/22/98
7// RCS-ID: $Id$
8// Copyright: (c) Wolfram Gloger (1996, 1997)
9// Guilhem Lavaux (1998)
8d5eff60 10// Vadim Zeitlin (1999-2002)
518b5d2f 11// Robert Roebling (1999)
be809868 12// K. S. Sreeram (2002)
65571936 13// Licence: wxWindows licence
518b5d2f
VZ
14/////////////////////////////////////////////////////////////////////////////
15
16// ============================================================================
17// declaration
18// ============================================================================
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
14f355c2
VS
24// for compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
518b5d2f 26
7bcb11d3 27#if wxUSE_THREADS
518b5d2f 28
88a7a4e1 29#include "wx/thread.h"
79b7b95a 30#include "wx/except.h"
88a7a4e1 31
ad9835c9 32#ifndef WX_PRECOMP
79b7b95a 33 #include "wx/app.h"
ad9835c9 34 #include "wx/dynarray.h"
88a7a4e1 35 #include "wx/intl.h"
e4db172a 36 #include "wx/log.h"
de6185e2 37 #include "wx/utils.h"
c0badb70 38 #include "wx/timer.h"
bb90a3e6 39 #include "wx/stopwatch.h"
02761f6c 40 #include "wx/module.h"
ad9835c9
WS
41#endif
42
518b5d2f
VZ
43#include <stdio.h>
44#include <unistd.h>
45#include <pthread.h>
46#include <errno.h>
47#include <time.h>
4f66d445 48#ifdef HAVE_SCHED_H
518b5d2f
VZ
49 #include <sched.h>
50#endif
51
ef8d96c2
VZ
52#ifdef HAVE_THR_SETCONCURRENCY
53 #include <thread.h>
54#endif
55
56// we use wxFFile under Linux in GetCPUCount()
57#ifdef __LINUX__
58 #include "wx/ffile.h"
7bfe6a32
JS
59 // For setpriority.
60 #include <sys/time.h>
61 #include <sys/resource.h>
ef8d96c2
VZ
62#endif
63
ef4f69ec
VZ
64#ifdef __VMS
65 #define THR_ID(thr) ((long long)(thr)->GetId())
66#else
67 #define THR_ID(thr) ((long)(thr)->GetId())
68#endif
69
ca9e2173
SC
70#ifdef __WXMAC__
71
72 // implement wxCriticalSection using mutexes
0eb949e1
RR
73wxCriticalSection::wxCriticalSection( wxCriticalSectionType critSecType )
74 : m_mutex( critSecType == wxCRITSEC_DEFAULT ? wxMUTEX_RECURSIVE : wxMUTEX_DEFAULT ) { }
ca9e2173
SC
75wxCriticalSection::~wxCriticalSection() { }
76
77void wxCriticalSection::Enter() { (void)m_mutex.Lock(); }
78void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); }
79
80#endif
518b5d2f
VZ
81// ----------------------------------------------------------------------------
82// constants
83// ----------------------------------------------------------------------------
84
882eefb1
VZ
85// the possible states of the thread and transitions from them
86enum wxThreadState
518b5d2f
VZ
87{
88 STATE_NEW, // didn't start execution yet (=> RUNNING)
882eefb1
VZ
89 STATE_RUNNING, // running (=> PAUSED or EXITED)
90 STATE_PAUSED, // suspended (=> RUNNING or EXITED)
91 STATE_EXITED // thread doesn't exist any more
518b5d2f
VZ
92};
93
9fc3ad34
VZ
94// the exit value of a thread which has been cancelled
95static const wxThread::ExitCode EXITCODE_CANCELLED = (wxThread::ExitCode)-1;
96
9e84b847 97// trace mask for wxThread operations
9fc3ad34
VZ
98#define TRACE_THREADS _T("thread")
99
9e84b847
VZ
100// you can get additional debugging messages for the semaphore operations
101#define TRACE_SEMA _T("semaphore")
102
9fc3ad34
VZ
103// ----------------------------------------------------------------------------
104// private functions
105// ----------------------------------------------------------------------------
106
107static void ScheduleThreadForDeletion();
108static void DeleteThread(wxThread *This);
109
882eefb1 110// ----------------------------------------------------------------------------
9e84b847 111// private classes
882eefb1
VZ
112// ----------------------------------------------------------------------------
113
9e84b847 114// an (non owning) array of pointers to threads
0d2c74c6 115WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);
518b5d2f 116
9e84b847
VZ
117// an entry for a thread we can wait for
118
518b5d2f
VZ
119// -----------------------------------------------------------------------------
120// global data
121// -----------------------------------------------------------------------------
122
123// we keep the list of all threads created by the application to be able to
124// terminate them on exit if there are some left - otherwise the process would
125// be left in memory
126static wxArrayThread gs_allThreads;
127
25b34b26
VZ
128// a mutex to protect gs_allThreads
129static wxMutex *gs_mutexAllThreads = NULL;
130
518b5d2f 131// the id of the main thread
5b65f136 132static pthread_t gs_tidMain = (pthread_t)-1;
518b5d2f
VZ
133
134// the key for the pointer to the associated wxThread object
135static pthread_key_t gs_keySelf;
136
9fc3ad34
VZ
137// the number of threads which are being deleted - the program won't exit
138// until there are any left
139static size_t gs_nThreadsBeingDeleted = 0;
140
141// a mutex to protect gs_nThreadsBeingDeleted
be809868 142static wxMutex *gs_mutexDeleteThread = (wxMutex *)NULL;
9fc3ad34
VZ
143
144// and a condition variable which will be signaled when all
145// gs_nThreadsBeingDeleted will have been deleted
146static wxCondition *gs_condAllDeleted = (wxCondition *)NULL;
147
c80217d1
VS
148// this mutex must be acquired before any call to a GUI function
149// (it's not inside #if wxUSE_GUI because this file is compiled as part
150// of wxBase)
151static wxMutex *gs_mutexGui = NULL;
518b5d2f 152
9e84b847
VZ
153// when we wait for a thread to exit, we're blocking on a condition which the
154// thread signals in its SignalExit() method -- but this condition can't be a
155// member of the thread itself as a detached thread may delete itself at any
156// moment and accessing the condition member of the thread after this would
157// result in a disaster
158//
159// so instead we maintain a global list of the structs below for the threads
160// we're interested in waiting on
161
518b5d2f 162// ============================================================================
8d5eff60 163// wxMutex implementation
518b5d2f
VZ
164// ============================================================================
165
8d5eff60
VZ
166// ----------------------------------------------------------------------------
167// wxMutexInternal
168// ----------------------------------------------------------------------------
518b5d2f 169
9e84b847
VZ
170// this is a simple wrapper around pthread_mutex_t which provides error
171// checking
518b5d2f
VZ
172class wxMutexInternal
173{
174public:
9e84b847 175 wxMutexInternal(wxMutexType mutexType);
8d5eff60
VZ
176 ~wxMutexInternal();
177
178 wxMutexError Lock();
696d13ee 179 wxMutexError Lock(unsigned long ms);
8d5eff60
VZ
180 wxMutexError TryLock();
181 wxMutexError Unlock();
182
9e84b847
VZ
183 bool IsOk() const { return m_isOk; }
184
696d13ee
VZ
185private:
186 // convert the result of pthread_mutex_[timed]lock() call to wx return code
187 wxMutexError HandleLockResult(int err);
188
8d5eff60 189private:
9fc3ad34 190 pthread_mutex_t m_mutex;
9e84b847 191 bool m_isOk;
be809868 192
9e84b847 193 // wxConditionInternal uses our m_mutex
be809868 194 friend class wxConditionInternal;
518b5d2f
VZ
195};
196
123dca7d
VZ
197#if defined(HAVE_PTHREAD_MUTEXATTR_T) && \
198 wxUSE_UNIX && !defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE_DECL)
707a3782
VS
199// on some systems pthread_mutexattr_settype() is not in the headers (but it is
200// in the library, otherwise we wouldn't compile this code at all)
201extern "C" int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
202#endif
203
9e84b847 204wxMutexInternal::wxMutexInternal(wxMutexType mutexType)
518b5d2f 205{
9e84b847
VZ
206 int err;
207 switch ( mutexType )
208 {
209 case wxMUTEX_RECURSIVE:
210 // support recursive locks like Win32, i.e. a thread can lock a
211 // mutex which it had itself already locked
212 //
213 // unfortunately initialization of recursive mutexes is non
214 // portable, so try several methods
d9b9876f 215#ifdef HAVE_PTHREAD_MUTEXATTR_T
9e84b847
VZ
216 {
217 pthread_mutexattr_t attr;
218 pthread_mutexattr_init(&attr);
219 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
d9b9876f 220
9e84b847
VZ
221 err = pthread_mutex_init(&m_mutex, &attr);
222 }
d9b9876f 223#elif defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
9e84b847
VZ
224 // we can use this only as initializer so we have to assign it
225 // first to a temp var - assigning directly to m_mutex wouldn't
226 // even compile
227 {
228 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
229 m_mutex = mutex;
230 }
d9b9876f 231#else // no recursive mutexes
9e84b847 232 err = EINVAL;
d9b9876f 233#endif // HAVE_PTHREAD_MUTEXATTR_T/...
9e84b847
VZ
234 break;
235
236 default:
237 wxFAIL_MSG( _T("unknown mutex type") );
238 // fall through
239
240 case wxMUTEX_DEFAULT:
241 err = pthread_mutex_init(&m_mutex, NULL);
242 break;
243 }
244
245 m_isOk = err == 0;
246 if ( !m_isOk )
247 {
401eb3de 248 wxLogApiError( wxT("pthread_mutex_init()"), err);
9e84b847 249 }
8d5eff60
VZ
250}
251
252wxMutexInternal::~wxMutexInternal()
253{
9e84b847
VZ
254 if ( m_isOk )
255 {
256 int err = pthread_mutex_destroy(&m_mutex);
257 if ( err != 0 )
258 {
401eb3de 259 wxLogApiError( wxT("pthread_mutex_destroy()"), err);
9e84b847
VZ
260 }
261 }
8d5eff60
VZ
262}
263
264wxMutexError wxMutexInternal::Lock()
265{
696d13ee
VZ
266 return HandleLockResult(pthread_mutex_lock(&m_mutex));
267}
268
269wxMutexError wxMutexInternal::Lock(unsigned long ms)
270{
e55a667c 271#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
696d13ee
VZ
272 static const long MSEC_IN_SEC = 1000;
273 static const long NSEC_IN_MSEC = 1000000;
fd91cec1 274 static const long NSEC_IN_USEC = 1000;
696d13ee
VZ
275 static const long NSEC_IN_SEC = MSEC_IN_SEC * NSEC_IN_MSEC;
276
277 time_t seconds = ms/MSEC_IN_SEC;
278 long nanoseconds = (ms % MSEC_IN_SEC) * NSEC_IN_MSEC;
279 timespec ts = { 0, 0 };
280
fd91cec1
VZ
281 // normally we should use clock_gettime(CLOCK_REALTIME) here but this
282 // function is in librt and we don't link with it currently, so use
283 // gettimeofday() instead -- if it turns out that this is really too
284 // imprecise, we should modify configure to check if clock_gettime() is
285 // available and whether it requires -lrt and use it instead
286#if 0
696d13ee
VZ
287 if ( clock_gettime(CLOCK_REALTIME, &ts) == 0 )
288 {
696d13ee 289 }
fd91cec1
VZ
290#else
291 struct timeval tv;
292 if ( wxGetTimeOfDay(&tv) != -1 )
293 {
294 ts.tv_sec = tv.tv_sec;
295 ts.tv_nsec = tv.tv_usec*NSEC_IN_USEC;
296 }
297#endif
696d13ee
VZ
298 else // fall back on system timer
299 {
fd91cec1
VZ
300 ts.tv_sec = time(NULL);
301 }
302
303 ts.tv_sec += seconds;
304 ts.tv_nsec += nanoseconds;
305 if ( ts.tv_nsec > NSEC_IN_SEC )
306 {
307 ts.tv_sec += 1;
308 ts.tv_nsec -= NSEC_IN_SEC;
696d13ee
VZ
309 }
310
311 return HandleLockResult(pthread_mutex_timedlock(&m_mutex, &ts));
e55a667c
VZ
312#else // !HAVE_PTHREAD_MUTEX_TIMEDLOCK
313 wxUnusedVar(ms);
314
315 return wxMUTEX_MISC_ERROR;
316#endif // HAVE_PTHREAD_MUTEX_TIMEDLOCK/!HAVE_PTHREAD_MUTEX_TIMEDLOCK
696d13ee
VZ
317}
318
319wxMutexError wxMutexInternal::HandleLockResult(int err)
320{
8d5eff60
VZ
321 switch ( err )
322 {
323 case EDEADLK:
9e84b847
VZ
324 // only error checking mutexes return this value and so it's an
325 // unexpected situation -- hence use assert, not wxLogDebug
326 wxFAIL_MSG( _T("mutex deadlock prevented") );
8d5eff60
VZ
327 return wxMUTEX_DEAD_LOCK;
328
8d5eff60 329 case EINVAL:
696d13ee 330 wxLogDebug(_T("pthread_mutex_[timed]lock(): mutex not initialized"));
9e84b847 331 break;
8d5eff60 332
696d13ee
VZ
333 case ETIMEDOUT:
334 return wxMUTEX_TIMEOUT;
335
8d5eff60
VZ
336 case 0:
337 return wxMUTEX_NO_ERROR;
9e84b847
VZ
338
339 default:
696d13ee 340 wxLogApiError(_T("pthread_mutex_[timed]lock()"), err);
8d5eff60 341 }
9e84b847
VZ
342
343 return wxMUTEX_MISC_ERROR;
8d5eff60
VZ
344}
345
696d13ee 346
8d5eff60
VZ
347wxMutexError wxMutexInternal::TryLock()
348{
349 int err = pthread_mutex_trylock(&m_mutex);
350 switch ( err )
351 {
352 case EBUSY:
9e84b847
VZ
353 // not an error: mutex is already locked, but we're prepared for
354 // this
8d5eff60
VZ
355 return wxMUTEX_BUSY;
356
8d5eff60 357 case EINVAL:
9e84b847
VZ
358 wxLogDebug(_T("pthread_mutex_trylock(): mutex not initialized."));
359 break;
8d5eff60
VZ
360
361 case 0:
362 return wxMUTEX_NO_ERROR;
9e84b847
VZ
363
364 default:
365 wxLogApiError(_T("pthread_mutex_trylock()"), err);
8d5eff60 366 }
9e84b847
VZ
367
368 return wxMUTEX_MISC_ERROR;
8d5eff60
VZ
369}
370
371wxMutexError wxMutexInternal::Unlock()
372{
373 int err = pthread_mutex_unlock(&m_mutex);
374 switch ( err )
375 {
376 case EPERM:
377 // we don't own the mutex
378 return wxMUTEX_UNLOCKED;
379
8d5eff60 380 case EINVAL:
9e84b847
VZ
381 wxLogDebug(_T("pthread_mutex_unlock(): mutex not initialized."));
382 break;
8d5eff60
VZ
383
384 case 0:
385 return wxMUTEX_NO_ERROR;
518b5d2f 386
9e84b847
VZ
387 default:
388 wxLogApiError(_T("pthread_mutex_unlock()"), err);
518b5d2f
VZ
389 }
390
9e84b847 391 return wxMUTEX_MISC_ERROR;
518b5d2f
VZ
392}
393
be809868 394// ===========================================================================
8d5eff60 395// wxCondition implementation
be809868 396// ===========================================================================
8d5eff60 397
be809868 398// ---------------------------------------------------------------------------
8d5eff60 399// wxConditionInternal
be809868 400// ---------------------------------------------------------------------------
518b5d2f 401
9e84b847
VZ
402// this is a wrapper around pthread_cond_t associated with a wxMutex (and hence
403// with a pthread_mutex_t)
518b5d2f
VZ
404class wxConditionInternal
405{
406public:
c112e100 407 wxConditionInternal(wxMutex& mutex);
9fc3ad34
VZ
408 ~wxConditionInternal();
409
9e84b847 410 bool IsOk() const { return m_isOk && m_mutex.IsOk(); }
4c460b34 411
9e84b847
VZ
412 wxCondError Wait();
413 wxCondError WaitTimeout(unsigned long milliseconds);
547b93ab 414
9e84b847
VZ
415 wxCondError Signal();
416 wxCondError Broadcast();
4c460b34 417
be809868 418private:
c112e100 419 // get the POSIX mutex associated with us
9e84b847 420 pthread_mutex_t *GetPMutex() const { return &m_mutex.m_internal->m_mutex; }
518b5d2f 421
c112e100 422 wxMutex& m_mutex;
be809868 423 pthread_cond_t m_cond;
9e84b847
VZ
424
425 bool m_isOk;
8d5eff60 426};
547b93ab 427
c112e100
VZ
428wxConditionInternal::wxConditionInternal(wxMutex& mutex)
429 : m_mutex(mutex)
9fc3ad34 430{
9e84b847 431 int err = pthread_cond_init(&m_cond, NULL /* default attributes */);
9fc3ad34 432
9e84b847 433 m_isOk = err == 0;
9fc3ad34 434
9e84b847 435 if ( !m_isOk )
9fc3ad34 436 {
9e84b847 437 wxLogApiError(_T("pthread_cond_init()"), err);
9fc3ad34
VZ
438 }
439}
440
9e84b847 441wxConditionInternal::~wxConditionInternal()
4c460b34 442{
9e84b847 443 if ( m_isOk )
c112e100 444 {
9e84b847
VZ
445 int err = pthread_cond_destroy(&m_cond);
446 if ( err != 0 )
447 {
448 wxLogApiError(_T("pthread_cond_destroy()"), err);
449 }
c112e100 450 }
be809868 451}
4c460b34 452
9e84b847 453wxCondError wxConditionInternal::Wait()
be809868 454{
9e84b847
VZ
455 int err = pthread_cond_wait(&m_cond, GetPMutex());
456 if ( err != 0 )
457 {
458 wxLogApiError(_T("pthread_cond_wait()"), err);
4c460b34 459
9e84b847 460 return wxCOND_MISC_ERROR;
be809868 461 }
4c460b34 462
9e84b847 463 return wxCOND_NO_ERROR;
be809868 464}
60ce696e 465
9e84b847 466wxCondError wxConditionInternal::WaitTimeout(unsigned long milliseconds)
be809868 467{
be809868 468 wxLongLong curtime = wxGetLocalTimeMillis();
9e84b847 469 curtime += milliseconds;
be809868
VZ
470 wxLongLong temp = curtime / 1000;
471 int sec = temp.GetLo();
9e84b847 472 temp *= 1000;
be809868
VZ
473 temp = curtime - temp;
474 int millis = temp.GetLo();
475
476 timespec tspec;
477
478 tspec.tv_sec = sec;
479 tspec.tv_nsec = millis * 1000L * 1000L;
4c460b34 480
9e84b847
VZ
481 int err = pthread_cond_timedwait( &m_cond, GetPMutex(), &tspec );
482 switch ( err )
483 {
484 case ETIMEDOUT:
485 return wxCOND_TIMEOUT;
486
487 case 0:
488 return wxCOND_NO_ERROR;
489
490 default:
491 wxLogApiError(_T("pthread_cond_timedwait()"), err);
492 }
493
494 return wxCOND_MISC_ERROR;
9fc3ad34
VZ
495}
496
9e84b847 497wxCondError wxConditionInternal::Signal()
9fc3ad34 498{
9e84b847
VZ
499 int err = pthread_cond_signal(&m_cond);
500 if ( err != 0 )
501 {
502 wxLogApiError(_T("pthread_cond_signal()"), err);
503
504 return wxCOND_MISC_ERROR;
505 }
506
507 return wxCOND_NO_ERROR;
be809868 508}
547b93ab 509
9e84b847 510wxCondError wxConditionInternal::Broadcast()
be809868 511{
9e84b847
VZ
512 int err = pthread_cond_broadcast(&m_cond);
513 if ( err != 0 )
514 {
515 wxLogApiError(_T("pthread_cond_broadcast()"), err);
516
517 return wxCOND_MISC_ERROR;
518 }
519
520 return wxCOND_NO_ERROR;
be809868
VZ
521}
522
523// ===========================================================================
524// wxSemaphore implementation
525// ===========================================================================
9fc3ad34 526
be809868
VZ
527// ---------------------------------------------------------------------------
528// wxSemaphoreInternal
529// ---------------------------------------------------------------------------
530
9e84b847
VZ
531// we implement the semaphores using mutexes and conditions instead of using
532// the sem_xxx() POSIX functions because they're not widely available and also
533// because it's impossible to implement WaitTimeout() using them
be809868
VZ
534class wxSemaphoreInternal
535{
536public:
9e84b847 537 wxSemaphoreInternal(int initialcount, int maxcount);
be809868 538
9e84b847 539 bool IsOk() const { return m_isOk; }
be809868 540
9e84b847
VZ
541 wxSemaError Wait();
542 wxSemaError TryWait();
543 wxSemaError WaitTimeout(unsigned long milliseconds);
be809868 544
9e84b847 545 wxSemaError Post();
be809868
VZ
546
547private:
548 wxMutex m_mutex;
549 wxCondition m_cond;
550
9e84b847
VZ
551 size_t m_count,
552 m_maxcount;
553
554 bool m_isOk;
be809868
VZ
555};
556
9e84b847 557wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount)
c112e100 558 : m_cond(m_mutex)
be809868
VZ
559{
560
9e84b847
VZ
561 if ( (initialcount < 0 || maxcount < 0) ||
562 ((maxcount > 0) && (initialcount > maxcount)) )
563 {
564 wxFAIL_MSG( _T("wxSemaphore: invalid initial or maximal count") );
565
ad9835c9 566 m_isOk = false;
9e84b847
VZ
567 }
568 else
be809868 569 {
9e84b847
VZ
570 m_maxcount = (size_t)maxcount;
571 m_count = (size_t)initialcount;
547b93ab 572 }
547b93ab 573
9e84b847 574 m_isOk = m_mutex.IsOk() && m_cond.IsOk();
be809868 575}
547b93ab 576
9e84b847 577wxSemaError wxSemaphoreInternal::Wait()
be809868 578{
cd10339a 579 wxMutexLocker locker(m_mutex);
60ce696e 580
9e84b847 581 while ( m_count == 0 )
9fc3ad34 582 {
9e84b847 583 wxLogTrace(TRACE_SEMA,
5031200a 584 _T("Thread %ld waiting for semaphore to become signalled"),
9e84b847
VZ
585 wxThread::GetCurrentId());
586
587 if ( m_cond.Wait() != wxCOND_NO_ERROR )
588 return wxSEMA_MISC_ERROR;
589
590 wxLogTrace(TRACE_SEMA,
5031200a 591 _T("Thread %ld finished waiting for semaphore, count = %lu"),
2b5f62a0 592 wxThread::GetCurrentId(), (unsigned long)m_count);
9fc3ad34 593 }
be809868 594
9e84b847
VZ
595 m_count--;
596
597 return wxSEMA_NO_ERROR;
be809868
VZ
598}
599
9e84b847 600wxSemaError wxSemaphoreInternal::TryWait()
be809868 601{
cd10339a 602 wxMutexLocker locker(m_mutex);
be809868 603
9e84b847
VZ
604 if ( m_count == 0 )
605 return wxSEMA_BUSY;
be809868 606
9e84b847 607 m_count--;
be809868 608
9e84b847 609 return wxSEMA_NO_ERROR;
be809868
VZ
610}
611
9e84b847 612wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds)
be809868 613{
cd10339a 614 wxMutexLocker locker(m_mutex);
be809868
VZ
615
616 wxLongLong startTime = wxGetLocalTimeMillis();
617
9e84b847 618 while ( m_count == 0 )
9fc3ad34 619 {
be809868 620 wxLongLong elapsed = wxGetLocalTimeMillis() - startTime;
9e84b847 621 long remainingTime = (long)milliseconds - (long)elapsed.GetLo();
be809868 622 if ( remainingTime <= 0 )
9e84b847
VZ
623 {
624 // timeout
625 return wxSEMA_TIMEOUT;
626 }
be809868 627
918dc519
VZ
628 switch ( m_cond.WaitTimeout(remainingTime) )
629 {
630 case wxCOND_TIMEOUT:
631 return wxSEMA_TIMEOUT;
632
633 default:
634 return wxSEMA_MISC_ERROR;
635
636 case wxCOND_NO_ERROR:
637 ;
638 }
9fc3ad34 639 }
8d5eff60 640
9e84b847 641 m_count--;
be809868 642
9e84b847 643 return wxSEMA_NO_ERROR;
be809868
VZ
644}
645
9e84b847 646wxSemaError wxSemaphoreInternal::Post()
be809868 647{
cd10339a 648 wxMutexLocker locker(m_mutex);
be809868 649
9e84b847 650 if ( m_maxcount > 0 && m_count == m_maxcount )
60ce696e 651 {
9e84b847 652 return wxSEMA_OVERFLOW;
60ce696e 653 }
be809868 654
9e84b847 655 m_count++;
518b5d2f 656
9e84b847 657 wxLogTrace(TRACE_SEMA,
5031200a 658 _T("Thread %ld about to signal semaphore, count = %lu"),
2b5f62a0
VZ
659 wxThread::GetCurrentId(), (unsigned long)m_count);
660
9e84b847
VZ
661 return m_cond.Signal() == wxCOND_NO_ERROR ? wxSEMA_NO_ERROR
662 : wxSEMA_MISC_ERROR;
518b5d2f
VZ
663}
664
be809868 665// ===========================================================================
8d5eff60 666// wxThread implementation
be809868 667// ===========================================================================
518b5d2f 668
295272bd
VZ
669// the thread callback functions must have the C linkage
670extern "C"
671{
672
816a7358 673#ifdef wxHAVE_PTHREAD_CLEANUP
295272bd
VZ
674 // thread exit function
675 void wxPthreadCleanup(void *ptr);
816a7358 676#endif // wxHAVE_PTHREAD_CLEANUP
90350682 677
295272bd 678void *wxPthreadStart(void *ptr);
90350682 679
295272bd 680} // extern "C"
90350682 681
8d5eff60
VZ
682// ----------------------------------------------------------------------------
683// wxThreadInternal
684// ----------------------------------------------------------------------------
685
518b5d2f
VZ
686class wxThreadInternal
687{
688public:
689 wxThreadInternal();
690 ~wxThreadInternal();
691
692 // thread entry function
295272bd 693 static void *PthreadStart(wxThread *thread);
518b5d2f
VZ
694
695 // thread actions
696 // start the thread
697 wxThreadError Run();
9e84b847
VZ
698 // unblock the thread allowing it to run
699 void SignalRun() { m_semRun.Post(); }
518b5d2f 700 // ask the thread to terminate
882eefb1 701 void Wait();
518b5d2f
VZ
702 // go to sleep until Resume() is called
703 void Pause();
704 // resume the thread
705 void Resume();
706
707 // accessors
708 // priority
709 int GetPriority() const { return m_prio; }
710 void SetPriority(int prio) { m_prio = prio; }
711 // state
882eefb1 712 wxThreadState GetState() const { return m_state; }
9e84b847
VZ
713 void SetState(wxThreadState state)
714 {
715#ifdef __WXDEBUG__
716 static const wxChar *stateNames[] =
717 {
718 _T("NEW"),
719 _T("RUNNING"),
720 _T("PAUSED"),
721 _T("EXITED"),
722 };
723
724 wxLogTrace(TRACE_THREADS, _T("Thread %ld: %s => %s."),
2b5f62a0 725 (long)GetId(), stateNames[m_state], stateNames[state]);
9e84b847
VZ
726#endif // __WXDEBUG__
727
728 m_state = state;
729 }
518b5d2f 730 // id
882eefb1
VZ
731 pthread_t GetId() const { return m_threadId; }
732 pthread_t *GetIdPtr() { return &m_threadId; }
518b5d2f 733 // "cancelled" flag
ad9835c9 734 void SetCancelFlag() { m_cancelled = true; }
518b5d2f 735 bool WasCancelled() const { return m_cancelled; }
9fc3ad34
VZ
736 // exit code
737 void SetExitCode(wxThread::ExitCode exitcode) { m_exitcode = exitcode; }
738 wxThread::ExitCode GetExitCode() const { return m_exitcode; }
739
4c460b34
VZ
740 // the pause flag
741 void SetReallyPaused(bool paused) { m_isPaused = paused; }
742 bool IsReallyPaused() const { return m_isPaused; }
743
9fc3ad34 744 // tell the thread that it is a detached one
4c460b34
VZ
745 void Detach()
746 {
9e84b847
VZ
747 wxCriticalSectionLocker lock(m_csJoinFlag);
748
ad9835c9
WS
749 m_shouldBeJoined = false;
750 m_isDetached = true;
4c460b34 751 }
518b5d2f 752
816a7358 753#ifdef wxHAVE_PTHREAD_CLEANUP
90350682
VZ
754 // this is used by wxPthreadCleanup() only
755 static void Cleanup(wxThread *thread);
816a7358 756#endif // wxHAVE_PTHREAD_CLEANUP
90350682 757
518b5d2f 758private:
882eefb1
VZ
759 pthread_t m_threadId; // id of the thread
760 wxThreadState m_state; // see wxThreadState enum
77ffb593 761 int m_prio; // in wxWidgets units: from 0 to 100
518b5d2f 762
9fc3ad34 763 // this flag is set when the thread should terminate
518b5d2f
VZ
764 bool m_cancelled;
765
be809868 766 // this flag is set when the thread is blocking on m_semSuspend
4c460b34
VZ
767 bool m_isPaused;
768
9fc3ad34
VZ
769 // the thread exit code - only used for joinable (!detached) threads and
770 // is only valid after the thread termination
771 wxThread::ExitCode m_exitcode;
772
773 // many threads may call Wait(), but only one of them should call
774 // pthread_join(), so we have to keep track of this
775 wxCriticalSection m_csJoinFlag;
776 bool m_shouldBeJoined;
4c460b34 777 bool m_isDetached;
9fc3ad34 778
be809868 779 // this semaphore is posted by Run() and the threads Entry() is not
9fc3ad34 780 // called before it is done
be809868 781 wxSemaphore m_semRun;
9fc3ad34
VZ
782
783 // this one is signaled when the thread should resume after having been
784 // Pause()d
be809868 785 wxSemaphore m_semSuspend;
518b5d2f
VZ
786};
787
9fc3ad34
VZ
788// ----------------------------------------------------------------------------
789// thread startup and exit functions
790// ----------------------------------------------------------------------------
791
295272bd
VZ
792void *wxPthreadStart(void *ptr)
793{
794 return wxThreadInternal::PthreadStart((wxThread *)ptr);
795}
796
797void *wxThreadInternal::PthreadStart(wxThread *thread)
518b5d2f 798{
9fc3ad34 799 wxThreadInternal *pthread = thread->m_internal;
518b5d2f 800
ef4f69ec
VZ
801 wxLogTrace(TRACE_THREADS, _T("Thread %ld started."), THR_ID(pthread));
802
9fc3ad34
VZ
803 // associate the thread pointer with the newly created thread so that
804 // wxThread::This() will work
862cc6f9
VZ
805 int rc = pthread_setspecific(gs_keySelf, thread);
806 if ( rc != 0 )
518b5d2f 807 {
58230fb1 808 wxLogSysError(rc, _("Cannot start thread: error writing TLS"));
518b5d2f
VZ
809
810 return (void *)-1;
811 }
9fc3ad34 812
4c460b34
VZ
813 // have to declare this before pthread_cleanup_push() which defines a
814 // block!
815 bool dontRunAtAll;
816
816a7358 817#ifdef wxHAVE_PTHREAD_CLEANUP
9fc3ad34
VZ
818 // install the cleanup handler which will be called if the thread is
819 // cancelled
295272bd 820 pthread_cleanup_push(wxPthreadCleanup, thread);
816a7358 821#endif // wxHAVE_PTHREAD_CLEANUP
518b5d2f 822
be809868
VZ
823 // wait for the semaphore to be posted from Run()
824 pthread->m_semRun.Wait();
518b5d2f 825
4c460b34
VZ
826 // test whether we should run the run at all - may be it was deleted
827 // before it started to Run()?
828 {
829 wxCriticalSectionLocker lock(thread->m_critsect);
9fc3ad34 830
4c460b34
VZ
831 dontRunAtAll = pthread->GetState() == STATE_NEW &&
832 pthread->WasCancelled();
833 }
9fc3ad34 834
4c460b34 835 if ( !dontRunAtAll )
9fc3ad34 836 {
4c460b34 837 // call the main entry
ef4f69ec
VZ
838 wxLogTrace(TRACE_THREADS,
839 _T("Thread %ld about to enter its Entry()."),
840 THR_ID(pthread));
841
79b7b95a
VS
842 wxTRY
843 {
844 pthread->m_exitcode = thread->Entry();
5092b3ad 845
79b7b95a
VS
846 wxLogTrace(TRACE_THREADS,
847 _T("Thread %ld Entry() returned %lu."),
848 THR_ID(pthread), wxPtrToUInt(pthread->m_exitcode));
849 }
850 wxCATCH_ALL( wxTheApp->OnUnhandledException(); )
ef4f69ec 851
4c460b34
VZ
852 {
853 wxCriticalSectionLocker lock(thread->m_critsect);
854
4c460b34 855 // change the state of the thread to "exited" so that
90350682 856 // wxPthreadCleanup handler won't do anything from now (if it's
4c460b34
VZ
857 // called before we do pthread_cleanup_pop below)
858 pthread->SetState(STATE_EXITED);
859 }
9fc3ad34
VZ
860 }
861
741d91c5
VZ
862 // NB: pthread_cleanup_push/pop() are macros and pop contains the matching
863 // '}' for the '{' in push, so they must be used in the same block!
816a7358 864#ifdef wxHAVE_PTHREAD_CLEANUP
741d91c5
VZ
865 #ifdef __DECCXX
866 // under Tru64 we get a warning from macro expansion
867 #pragma message save
868 #pragma message disable(declbutnotref)
869 #endif
870
9fc3ad34 871 // remove the cleanup handler without executing it
062c4861 872 pthread_cleanup_pop(FALSE);
741d91c5
VZ
873
874 #ifdef __DECCXX
875 #pragma message restore
876 #endif
816a7358 877#endif // wxHAVE_PTHREAD_CLEANUP
518b5d2f 878
4c460b34
VZ
879 if ( dontRunAtAll )
880 {
9e84b847 881 // FIXME: deleting a possibly joinable thread here???
4c460b34 882 delete thread;
518b5d2f 883
4c460b34
VZ
884 return EXITCODE_CANCELLED;
885 }
886 else
887 {
888 // terminate the thread
889 thread->Exit(pthread->m_exitcode);
890
891 wxFAIL_MSG(wxT("wxThread::Exit() can't return."));
518b5d2f 892
4c460b34
VZ
893 return NULL;
894 }
518b5d2f
VZ
895}
896
816a7358 897#ifdef wxHAVE_PTHREAD_CLEANUP
5092b3ad 898
9fc3ad34 899// this handler is called when the thread is cancelled
90350682 900extern "C" void wxPthreadCleanup(void *ptr)
5092b3ad 901{
90350682
VZ
902 wxThreadInternal::Cleanup((wxThread *)ptr);
903}
5092b3ad 904
90350682
VZ
905void wxThreadInternal::Cleanup(wxThread *thread)
906{
b4866582 907 if (pthread_getspecific(gs_keySelf) == 0) return;
9fc3ad34
VZ
908 {
909 wxCriticalSectionLocker lock(thread->m_critsect);
910 if ( thread->m_internal->GetState() == STATE_EXITED )
911 {
912 // thread is already considered as finished.
913 return;
914 }
915 }
5092b3ad 916
9fc3ad34
VZ
917 // exit the thread gracefully
918 thread->Exit(EXITCODE_CANCELLED);
919}
5092b3ad 920
816a7358 921#endif // wxHAVE_PTHREAD_CLEANUP
5092b3ad 922
9fc3ad34
VZ
923// ----------------------------------------------------------------------------
924// wxThreadInternal
925// ----------------------------------------------------------------------------
5092b3ad 926
518b5d2f
VZ
927wxThreadInternal::wxThreadInternal()
928{
929 m_state = STATE_NEW;
ad9835c9 930 m_cancelled = false;
bbfa0322
VZ
931 m_prio = WXTHREAD_DEFAULT_PRIORITY;
932 m_threadId = 0;
9fc3ad34 933 m_exitcode = 0;
518b5d2f 934
ad9835c9
WS
935 // set to true only when the thread starts waiting on m_semSuspend
936 m_isPaused = false;
4c460b34 937
9fc3ad34 938 // defaults for joinable threads
ad9835c9
WS
939 m_shouldBeJoined = true;
940 m_isDetached = false;
518b5d2f
VZ
941}
942
943wxThreadInternal::~wxThreadInternal()
944{
518b5d2f
VZ
945}
946
947wxThreadError wxThreadInternal::Run()
948{
949 wxCHECK_MSG( GetState() == STATE_NEW, wxTHREAD_RUNNING,
9fc3ad34 950 wxT("thread may only be started once after Create()") );
518b5d2f 951
9fc3ad34 952 SetState(STATE_RUNNING);
518b5d2f 953
9e84b847
VZ
954 // wake up threads waiting for our start
955 SignalRun();
956
518b5d2f 957 return wxTHREAD_NO_ERROR;
518b5d2f
VZ
958}
959
882eefb1 960void wxThreadInternal::Wait()
518b5d2f 961{
9e84b847
VZ
962 wxCHECK_RET( !m_isDetached, _T("can't wait for a detached thread") );
963
518b5d2f
VZ
964 // if the thread we're waiting for is waiting for the GUI mutex, we will
965 // deadlock so make sure we release it temporarily
966 if ( wxThread::IsMain() )
967 wxMutexGuiLeave();
968
547b93ab 969 wxLogTrace(TRACE_THREADS,
ef4f69ec
VZ
970 _T("Starting to wait for thread %ld to exit."),
971 THR_ID(this));
972
9e84b847
VZ
973 // to avoid memory leaks we should call pthread_join(), but it must only be
974 // done once so use a critical section to serialize the code below
9fc3ad34 975 {
4c460b34
VZ
976 wxCriticalSectionLocker lock(m_csJoinFlag);
977
978 if ( m_shouldBeJoined )
9fc3ad34 979 {
4c460b34
VZ
980 // FIXME shouldn't we set cancellation type to DISABLED here? If
981 // we're cancelled inside pthread_join(), things will almost
982 // certainly break - but if we disable the cancellation, we
983 // might deadlock
2b5f62a0 984 if ( pthread_join(GetId(), &m_exitcode) != 0 )
4c460b34 985 {
9e84b847
VZ
986 // this is a serious problem, so use wxLogError and not
987 // wxLogDebug: it is possible to bring the system to its knees
988 // by creating too many threads and not joining them quite
989 // easily
76a6e803 990 wxLogError(_("Failed to join a thread, potential memory leak detected - please restart the program"));
4c460b34 991 }
9fc3ad34 992
ad9835c9 993 m_shouldBeJoined = false;
4c460b34 994 }
9fc3ad34 995 }
9111db68 996
518b5d2f
VZ
997 // reacquire GUI mutex
998 if ( wxThread::IsMain() )
999 wxMutexGuiEnter();
1000}
1001
518b5d2f
VZ
1002void wxThreadInternal::Pause()
1003{
882eefb1
VZ
1004 // the state is set from the thread which pauses us first, this function
1005 // is called later so the state should have been already set
518b5d2f 1006 wxCHECK_RET( m_state == STATE_PAUSED,
223d09f6 1007 wxT("thread must first be paused with wxThread::Pause().") );
518b5d2f 1008
ef4f69ec
VZ
1009 wxLogTrace(TRACE_THREADS,
1010 _T("Thread %ld goes to sleep."), THR_ID(this));
1011
be809868
VZ
1012 // wait until the semaphore is Post()ed from Resume()
1013 m_semSuspend.Wait();
518b5d2f
VZ
1014}
1015
1016void wxThreadInternal::Resume()
1017{
1018 wxCHECK_RET( m_state == STATE_PAUSED,
223d09f6 1019 wxT("can't resume thread which is not suspended.") );
518b5d2f 1020
4c460b34
VZ
1021 // the thread might be not actually paused yet - if there were no call to
1022 // TestDestroy() since the last call to Pause() for example
1023 if ( IsReallyPaused() )
1024 {
ef4f69ec
VZ
1025 wxLogTrace(TRACE_THREADS,
1026 _T("Waking up thread %ld"), THR_ID(this));
1027
4c460b34 1028 // wake up Pause()
be809868 1029 m_semSuspend.Post();
4c460b34
VZ
1030
1031 // reset the flag
ad9835c9 1032 SetReallyPaused(false);
4c460b34
VZ
1033 }
1034 else
1035 {
ef4f69ec
VZ
1036 wxLogTrace(TRACE_THREADS,
1037 _T("Thread %ld is not yet really paused"), THR_ID(this));
4c460b34 1038 }
518b5d2f
VZ
1039
1040 SetState(STATE_RUNNING);
1041}
1042
1043// -----------------------------------------------------------------------------
9fc3ad34 1044// wxThread static functions
518b5d2f
VZ
1045// -----------------------------------------------------------------------------
1046
1047wxThread *wxThread::This()
1048{
1049 return (wxThread *)pthread_getspecific(gs_keySelf);
1050}
1051
1052bool wxThread::IsMain()
1053{
5b65f136 1054 return (bool)pthread_equal(pthread_self(), gs_tidMain) || gs_tidMain == (pthread_t)-1;
518b5d2f
VZ
1055}
1056
1057void wxThread::Yield()
1058{
6f837e5c 1059#ifdef HAVE_SCHED_YIELD
518b5d2f 1060 sched_yield();
6f837e5c 1061#endif
518b5d2f
VZ
1062}
1063
ef8d96c2
VZ
1064int wxThread::GetCPUCount()
1065{
85bf679a
VZ
1066#if defined(_SC_NPROCESSORS_ONLN)
1067 // this works for Solaris and Linux 2.6
1068 int rc = sysconf(_SC_NPROCESSORS_ONLN);
1069 if ( rc != -1 )
1070 {
1071 return rc;
1072 }
1073#elif defined(__LINUX__) && wxUSE_FFILE
ef8d96c2
VZ
1074 // read from proc (can't use wxTextFile here because it's a special file:
1075 // it has 0 size but still can be read from)
1076 wxLogNull nolog;
1077
1078 wxFFile file(_T("/proc/cpuinfo"));
1079 if ( file.IsOpened() )
1080 {
1081 // slurp the whole file
1082 wxString s;
1083 if ( file.ReadAll(&s) )
1084 {
9ec1ed2a
VZ
1085 // (ab)use Replace() to find the number of "processor: num" strings
1086 size_t count = s.Replace(_T("processor\t:"), _T(""));
ef8d96c2
VZ
1087 if ( count > 0 )
1088 {
1089 return count;
1090 }
1091
1092 wxLogDebug(_T("failed to parse /proc/cpuinfo"));
1093 }
1094 else
1095 {
1096 wxLogDebug(_T("failed to read /proc/cpuinfo"));
1097 }
1098 }
ef8d96c2
VZ
1099#endif // different ways to get number of CPUs
1100
1101 // unknown
1102 return -1;
1103}
1104
ef4f69ec
VZ
1105// VMS is a 64 bit system and threads have 64 bit pointers.
1106// FIXME: also needed for other systems????
a7aef4a9 1107#ifdef __VMS
a7aef4a9
JJ
1108unsigned long long wxThread::GetCurrentId()
1109{
1110 return (unsigned long long)pthread_self();
ef4f69ec
VZ
1111}
1112
1113#else // !__VMS
1114
1fcfb40d
RD
1115unsigned long wxThread::GetCurrentId()
1116{
1117 return (unsigned long)pthread_self();
1118}
1119
ef4f69ec
VZ
1120#endif // __VMS/!__VMS
1121
1122
ef8d96c2
VZ
1123bool wxThread::SetConcurrency(size_t level)
1124{
1125#ifdef HAVE_THR_SETCONCURRENCY
1126 int rc = thr_setconcurrency(level);
1127 if ( rc != 0 )
1128 {
1129 wxLogSysError(rc, _T("thr_setconcurrency() failed"));
1130 }
1131
1132 return rc == 0;
1133#else // !HAVE_THR_SETCONCURRENCY
1134 // ok only for the default value
1135 return level == 0;
1136#endif // HAVE_THR_SETCONCURRENCY/!HAVE_THR_SETCONCURRENCY
1137}
1138
518b5d2f
VZ
1139// -----------------------------------------------------------------------------
1140// creating thread
1141// -----------------------------------------------------------------------------
1142
acb8423c 1143wxThread::wxThread(wxThreadKind kind)
518b5d2f
VZ
1144{
1145 // add this thread to the global list of all threads
25b34b26
VZ
1146 {
1147 wxMutexLocker lock(*gs_mutexAllThreads);
1148
1149 gs_allThreads.Add(this);
1150 }
518b5d2f 1151
9fc3ad34 1152 m_internal = new wxThreadInternal();
acb8423c
VZ
1153
1154 m_isDetached = kind == wxTHREAD_DETACHED;
518b5d2f
VZ
1155}
1156
8a077f28
SN
1157#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1158 #define WXUNUSED_STACKSIZE(identifier) identifier
1159#else
1160 #define WXUNUSED_STACKSIZE(identifier) WXUNUSED(identifier)
1161#endif
1162
1163wxThreadError wxThread::Create(unsigned int WXUNUSED_STACKSIZE(stackSize))
518b5d2f 1164{
9fc3ad34
VZ
1165 if ( m_internal->GetState() != STATE_NEW )
1166 {
1167 // don't recreate thread
518b5d2f 1168 return wxTHREAD_RUNNING;
9fc3ad34 1169 }
518b5d2f
VZ
1170
1171 // set up the thread attribute: right now, we only set thread priority
1172 pthread_attr_t attr;
1173 pthread_attr_init(&attr);
1174
8a077f28
SN
1175#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1176 if (stackSize)
1177 pthread_attr_setstacksize(&attr, stackSize);
1178#endif
1179
fc9ef629 1180#ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
9fc3ad34
VZ
1181 int policy;
1182 if ( pthread_attr_getschedpolicy(&attr, &policy) != 0 )
518b5d2f 1183 {
58230fb1 1184 wxLogError(_("Cannot retrieve thread scheduling policy."));
518b5d2f
VZ
1185 }
1186
fb10f04c
JJ
1187#ifdef __VMS__
1188 /* the pthread.h contains too many spaces. This is a work-around */
1189# undef sched_get_priority_max
1190#undef sched_get_priority_min
1191#define sched_get_priority_max(_pol_) \
1192 (_pol_ == SCHED_OTHER ? PRI_FG_MAX_NP : PRI_FIFO_MAX)
1193#define sched_get_priority_min(_pol_) \
1194 (_pol_ == SCHED_OTHER ? PRI_FG_MIN_NP : PRI_FIFO_MIN)
1195#endif
1fcfb40d 1196
fb10f04c
JJ
1197 int max_prio = sched_get_priority_max(policy),
1198 min_prio = sched_get_priority_min(policy),
9fc3ad34 1199 prio = m_internal->GetPriority();
518b5d2f
VZ
1200
1201 if ( min_prio == -1 || max_prio == -1 )
1202 {
58230fb1 1203 wxLogError(_("Cannot get priority range for scheduling policy %d."),
9fc3ad34 1204 policy);
518b5d2f 1205 }
bbfa0322
VZ
1206 else if ( max_prio == min_prio )
1207 {
9fc3ad34 1208 if ( prio != WXTHREAD_DEFAULT_PRIORITY )
bbfa0322
VZ
1209 {
1210 // notify the programmer that this doesn't work here
1211 wxLogWarning(_("Thread priority setting is ignored."));
1212 }
1213 //else: we have default priority, so don't complain
1214
1215 // anyhow, don't do anything because priority is just ignored
1216 }
518b5d2f
VZ
1217 else
1218 {
1219 struct sched_param sp;
9fc3ad34
VZ
1220 if ( pthread_attr_getschedparam(&attr, &sp) != 0 )
1221 {
1222 wxFAIL_MSG(_T("pthread_attr_getschedparam() failed"));
1223 }
1224
1225 sp.sched_priority = min_prio + (prio*(max_prio - min_prio))/100;
1226
1227 if ( pthread_attr_setschedparam(&attr, &sp) != 0 )
1228 {
1229 wxFAIL_MSG(_T("pthread_attr_setschedparam(priority) failed"));
1230 }
518b5d2f 1231 }
34f8c26e 1232#endif // HAVE_THREAD_PRIORITY_FUNCTIONS
518b5d2f 1233
58230fb1
VZ
1234#ifdef HAVE_PTHREAD_ATTR_SETSCOPE
1235 // this will make the threads created by this process really concurrent
9fc3ad34
VZ
1236 if ( pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) != 0 )
1237 {
1238 wxFAIL_MSG(_T("pthread_attr_setscope(PTHREAD_SCOPE_SYSTEM) failed"));
1239 }
58230fb1
VZ
1240#endif // HAVE_PTHREAD_ATTR_SETSCOPE
1241
9fc3ad34
VZ
1242 // VZ: assume that this one is always available (it's rather fundamental),
1243 // if this function is ever missing we should try to use
1244 // pthread_detach() instead (after thread creation)
1245 if ( m_isDetached )
1246 {
1247 if ( pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0 )
1248 {
1249 wxFAIL_MSG(_T("pthread_attr_setdetachstate(DETACHED) failed"));
1250 }
1251
1252 // never try to join detached threads
1253 m_internal->Detach();
1254 }
1255 //else: threads are created joinable by default, it's ok
1256
518b5d2f 1257 // create the new OS thread object
9fc3ad34
VZ
1258 int rc = pthread_create
1259 (
1260 m_internal->GetIdPtr(),
1261 &attr,
295272bd 1262 wxPthreadStart,
9fc3ad34
VZ
1263 (void *)this
1264 );
1265
1266 if ( pthread_attr_destroy(&attr) != 0 )
1267 {
1268 wxFAIL_MSG(_T("pthread_attr_destroy() failed"));
1269 }
518b5d2f
VZ
1270
1271 if ( rc != 0 )
1272 {
9fc3ad34
VZ
1273 m_internal->SetState(STATE_EXITED);
1274
518b5d2f
VZ
1275 return wxTHREAD_NO_RESOURCE;
1276 }
1277
1278 return wxTHREAD_NO_ERROR;
1279}
1280
1281wxThreadError wxThread::Run()
1282{
9fc3ad34
VZ
1283 wxCriticalSectionLocker lock(m_critsect);
1284
1285 wxCHECK_MSG( m_internal->GetId(), wxTHREAD_MISC_ERROR,
223d09f6 1286 wxT("must call wxThread::Create() first") );
bbfa0322 1287
9fc3ad34 1288 return m_internal->Run();
518b5d2f
VZ
1289}
1290
1291// -----------------------------------------------------------------------------
1292// misc accessors
1293// -----------------------------------------------------------------------------
1294
1295void wxThread::SetPriority(unsigned int prio)
1296{
34f8c26e
VZ
1297 wxCHECK_RET( ((int)WXTHREAD_MIN_PRIORITY <= (int)prio) &&
1298 ((int)prio <= (int)WXTHREAD_MAX_PRIORITY),
223d09f6 1299 wxT("invalid thread priority") );
518b5d2f
VZ
1300
1301 wxCriticalSectionLocker lock(m_critsect);
1302
9fc3ad34 1303 switch ( m_internal->GetState() )
518b5d2f
VZ
1304 {
1305 case STATE_NEW:
1306 // thread not yet started, priority will be set when it is
9fc3ad34 1307 m_internal->SetPriority(prio);
518b5d2f
VZ
1308 break;
1309
1310 case STATE_RUNNING:
1311 case STATE_PAUSED:
34f8c26e 1312#ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
7bfe6a32 1313#if defined(__LINUX__)
17a1ebd1
VZ
1314 // On Linux, pthread_setschedparam with SCHED_OTHER does not allow
1315 // a priority other than 0. Instead, we use the BSD setpriority
1316 // which alllows us to set a 'nice' value between 20 to -20. Only
1317 // super user can set a value less than zero (more negative yields
1318 // higher priority). setpriority set the static priority of a
1319 // process, but this is OK since Linux is configured as a thread
1320 // per process.
1321 //
1322 // FIXME this is not true for 2.6!!
7bfe6a32 1323
17a1ebd1
VZ
1324 // map wx priorites WXTHREAD_MIN_PRIORITY..WXTHREAD_MAX_PRIORITY
1325 // to Unix priorities 20..-20
a07bf2d0 1326 if ( setpriority(PRIO_PROCESS, 0, -(2*(int)prio)/5 + 20) == -1 )
17a1ebd1
VZ
1327 {
1328 wxLogError(_("Failed to set thread priority %d."), prio);
7bfe6a32
JS
1329 }
1330#else // __LINUX__
518b5d2f
VZ
1331 {
1332 struct sched_param sparam;
1333 sparam.sched_priority = prio;
1334
9fc3ad34 1335 if ( pthread_setschedparam(m_internal->GetId(),
518b5d2f
VZ
1336 SCHED_OTHER, &sparam) != 0 )
1337 {
1338 wxLogError(_("Failed to set thread priority %d."), prio);
1339 }
1340 }
7bfe6a32 1341#endif // __LINUX__
34f8c26e 1342#endif // HAVE_THREAD_PRIORITY_FUNCTIONS
518b5d2f
VZ
1343 break;
1344
1345 case STATE_EXITED:
1346 default:
223d09f6 1347 wxFAIL_MSG(wxT("impossible to set thread priority in this state"));
518b5d2f
VZ
1348 }
1349}
1350
1351unsigned int wxThread::GetPriority() const
1352{
1353 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
1354
9fc3ad34 1355 return m_internal->GetPriority();
518b5d2f
VZ
1356}
1357
547b93ab 1358wxThreadIdType wxThread::GetId() const
13d068d9 1359{
df744f4d 1360 return (wxThreadIdType) m_internal->GetId();
518b5d2f
VZ
1361}
1362
1363// -----------------------------------------------------------------------------
1364// pause/resume
1365// -----------------------------------------------------------------------------
1366
1367wxThreadError wxThread::Pause()
1368{
4c460b34
VZ
1369 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR,
1370 _T("a thread can't pause itself") );
1371
518b5d2f
VZ
1372 wxCriticalSectionLocker lock(m_critsect);
1373
9fc3ad34 1374 if ( m_internal->GetState() != STATE_RUNNING )
518b5d2f 1375 {
223d09f6 1376 wxLogDebug(wxT("Can't pause thread which is not running."));
518b5d2f
VZ
1377
1378 return wxTHREAD_NOT_RUNNING;
1379 }
1380
9fc3ad34
VZ
1381 // just set a flag, the thread will be really paused only during the next
1382 // call to TestDestroy()
1383 m_internal->SetState(STATE_PAUSED);
518b5d2f
VZ
1384
1385 return wxTHREAD_NO_ERROR;
1386}
1387
1388wxThreadError wxThread::Resume()
1389{
4c460b34
VZ
1390 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR,
1391 _T("a thread can't resume itself") );
518b5d2f 1392
4c460b34 1393 wxCriticalSectionLocker lock(m_critsect);
518b5d2f 1394
4c460b34 1395 wxThreadState state = m_internal->GetState();
9fc3ad34
VZ
1396
1397 switch ( state )
518b5d2f 1398 {
9fc3ad34 1399 case STATE_PAUSED:
4c460b34 1400 wxLogTrace(TRACE_THREADS, _T("Thread %ld suspended, resuming."),
9fc3ad34
VZ
1401 GetId());
1402
1403 m_internal->Resume();
1404
1405 return wxTHREAD_NO_ERROR;
1406
1407 case STATE_EXITED:
1408 wxLogTrace(TRACE_THREADS, _T("Thread %ld exited, won't resume."),
1409 GetId());
1410 return wxTHREAD_NO_ERROR;
518b5d2f 1411
9fc3ad34
VZ
1412 default:
1413 wxLogDebug(_T("Attempt to resume a thread which is not paused."));
1414
1415 return wxTHREAD_MISC_ERROR;
518b5d2f
VZ
1416 }
1417}
1418
1419// -----------------------------------------------------------------------------
1420// exiting thread
1421// -----------------------------------------------------------------------------
1422
9fc3ad34 1423wxThread::ExitCode wxThread::Wait()
b568d04f 1424{
9fc3ad34
VZ
1425 wxCHECK_MSG( This() != this, (ExitCode)-1,
1426 _T("a thread can't wait for itself") );
1427
1428 wxCHECK_MSG( !m_isDetached, (ExitCode)-1,
1429 _T("can't wait for detached thread") );
1430
1431 m_internal->Wait();
b568d04f 1432
9fc3ad34 1433 return m_internal->GetExitCode();
b568d04f
VZ
1434}
1435
1436wxThreadError wxThread::Delete(ExitCode *rc)
518b5d2f 1437{
4c460b34
VZ
1438 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR,
1439 _T("a thread can't delete itself") );
1440
9e84b847
VZ
1441 bool isDetached = m_isDetached;
1442
518b5d2f 1443 m_critsect.Enter();
9fc3ad34 1444 wxThreadState state = m_internal->GetState();
518b5d2f 1445
882eefb1 1446 // ask the thread to stop
9fc3ad34
VZ
1447 m_internal->SetCancelFlag();
1448
9111db68
GL
1449 m_critsect.Leave();
1450
518b5d2f
VZ
1451 switch ( state )
1452 {
1453 case STATE_NEW:
4c460b34 1454 // we need to wake up the thread so that PthreadStart() will
9e84b847
VZ
1455 // terminate - right now it's blocking on run semaphore in
1456 // PthreadStart()
4c460b34
VZ
1457 m_internal->SignalRun();
1458
1459 // fall through
1460
518b5d2f
VZ
1461 case STATE_EXITED:
1462 // nothing to do
1463 break;
1464
1465 case STATE_PAUSED:
9e84b847 1466 // resume the thread first
9fc3ad34 1467 m_internal->Resume();
518b5d2f
VZ
1468
1469 // fall through
1470
1471 default:
9e84b847 1472 if ( !isDetached )
9fc3ad34 1473 {
9e84b847
VZ
1474 // wait until the thread stops
1475 m_internal->Wait();
9fc3ad34 1476
9e84b847
VZ
1477 if ( rc )
1478 {
1479 // return the exit code of the thread
1480 *rc = m_internal->GetExitCode();
1481 }
9fc3ad34 1482 }
9e84b847 1483 //else: can't wait for detached threads
518b5d2f
VZ
1484 }
1485
acb8423c 1486 return wxTHREAD_NO_ERROR;
518b5d2f
VZ
1487}
1488
1489wxThreadError wxThread::Kill()
1490{
9fc3ad34
VZ
1491 wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR,
1492 _T("a thread can't kill itself") );
1493
1494 switch ( m_internal->GetState() )
518b5d2f
VZ
1495 {
1496 case STATE_NEW:
1497 case STATE_EXITED:
1498 return wxTHREAD_NOT_RUNNING;
1499
9fc3ad34
VZ
1500 case STATE_PAUSED:
1501 // resume the thread first
1502 Resume();
1503
1504 // fall through
1505
518b5d2f 1506 default:
9fc3ad34
VZ
1507#ifdef HAVE_PTHREAD_CANCEL
1508 if ( pthread_cancel(m_internal->GetId()) != 0 )
741d91c5 1509#endif // HAVE_PTHREAD_CANCEL
518b5d2f
VZ
1510 {
1511 wxLogError(_("Failed to terminate a thread."));
1512
1513 return wxTHREAD_MISC_ERROR;
1514 }
9fc3ad34 1515
741d91c5 1516#ifdef HAVE_PTHREAD_CANCEL
9fc3ad34
VZ
1517 if ( m_isDetached )
1518 {
1519 // if we use cleanup function, this will be done from
90350682 1520 // wxPthreadCleanup()
816a7358 1521#ifndef wxHAVE_PTHREAD_CLEANUP
9fc3ad34
VZ
1522 ScheduleThreadForDeletion();
1523
b18cfdd9
VZ
1524 // don't call OnExit() here, it can only be called in the
1525 // threads context and we're in the context of another thread
9fc3ad34
VZ
1526
1527 DeleteThread(this);
816a7358 1528#endif // wxHAVE_PTHREAD_CLEANUP
9fc3ad34
VZ
1529 }
1530 else
1531 {
1532 m_internal->SetExitCode(EXITCODE_CANCELLED);
1533 }
518b5d2f
VZ
1534
1535 return wxTHREAD_NO_ERROR;
741d91c5 1536#endif // HAVE_PTHREAD_CANCEL
518b5d2f
VZ
1537 }
1538}
1539
b568d04f 1540void wxThread::Exit(ExitCode status)
518b5d2f 1541{
4c460b34 1542 wxASSERT_MSG( This() == this,
76a6e803 1543 _T("wxThread::Exit() can only be called in the context of the same thread") );
4c460b34 1544
9e84b847
VZ
1545 if ( m_isDetached )
1546 {
1547 // from the moment we call OnExit(), the main program may terminate at
1548 // any moment, so mark this thread as being already in process of being
1549 // deleted or wxThreadModule::OnExit() will try to delete it again
1550 ScheduleThreadForDeletion();
1551 }
9fc3ad34
VZ
1552
1553 // don't enter m_critsect before calling OnExit() because the user code
1554 // might deadlock if, for example, it signals a condition in OnExit() (a
1555 // common case) while the main thread calls any of functions entering
1556 // m_critsect on us (almost all of them do)
79b7b95a
VS
1557 wxTRY
1558 {
1559 OnExit();
1560 }
1561 wxCATCH_ALL( wxTheApp->OnUnhandledException(); )
518b5d2f 1562
9fc3ad34
VZ
1563 // delete C++ thread object if this is a detached thread - user is
1564 // responsible for doing this for joinable ones
1565 if ( m_isDetached )
1566 {
1567 // FIXME I'm feeling bad about it - what if another thread function is
1568 // called (in another thread context) now? It will try to access
1569 // half destroyed object which will probably result in something
1570 // very bad - but we can't protect this by a crit section unless
1571 // we make it a global object, but this would mean that we can
1572 // only call one thread function at a time :-(
1573 DeleteThread(this);
b4866582 1574 pthread_setspecific(gs_keySelf, 0);
9fc3ad34 1575 }
343b1669
RR
1576 else
1577 {
1578 m_critsect.Enter();
1579 m_internal->SetState(STATE_EXITED);
1580 m_critsect.Leave();
1581 }
9fc3ad34
VZ
1582
1583 // terminate the thread (pthread_exit() never returns)
518b5d2f 1584 pthread_exit(status);
9fc3ad34
VZ
1585
1586 wxFAIL_MSG(_T("pthread_exit() failed"));
518b5d2f
VZ
1587}
1588
1589// also test whether we were paused
1590bool wxThread::TestDestroy()
1591{
4c460b34 1592 wxASSERT_MSG( This() == this,
76a6e803 1593 _T("wxThread::TestDestroy() can only be called in the context of the same thread") );
4c460b34 1594
9fc3ad34 1595 m_critsect.Enter();
518b5d2f 1596
9fc3ad34 1597 if ( m_internal->GetState() == STATE_PAUSED )
518b5d2f 1598 {
ad9835c9 1599 m_internal->SetReallyPaused(true);
4c460b34 1600
9fc3ad34
VZ
1601 // leave the crit section or the other threads will stop too if they
1602 // try to call any of (seemingly harmless) IsXXX() functions while we
1603 // sleep
518b5d2f
VZ
1604 m_critsect.Leave();
1605
9fc3ad34
VZ
1606 m_internal->Pause();
1607 }
1608 else
1609 {
1610 // thread wasn't requested to pause, nothing to do
1611 m_critsect.Leave();
518b5d2f
VZ
1612 }
1613
9fc3ad34 1614 return m_internal->WasCancelled();
518b5d2f
VZ
1615}
1616
1617wxThread::~wxThread()
1618{
9fc3ad34 1619#ifdef __WXDEBUG__
062c4861 1620 m_critsect.Enter();
9fc3ad34
VZ
1621
1622 // check that the thread either exited or couldn't be created
1623 if ( m_internal->GetState() != STATE_EXITED &&
1624 m_internal->GetState() != STATE_NEW )
bbfa0322 1625 {
17a1ebd1
VZ
1626 wxLogDebug(_T("The thread %ld is being destroyed although it is still running! The application may crash."),
1627 (long)GetId());
bbfa0322 1628 }
062c4861
GL
1629
1630 m_critsect.Leave();
9fc3ad34 1631#endif // __WXDEBUG__
062c4861 1632
9fc3ad34 1633 delete m_internal;
bbfa0322 1634
518b5d2f 1635 // remove this thread from the global array
25b34b26
VZ
1636 {
1637 wxMutexLocker lock(*gs_mutexAllThreads);
1638
1639 gs_allThreads.Remove(this);
1640 }
518b5d2f
VZ
1641}
1642
1643// -----------------------------------------------------------------------------
1644// state tests
1645// -----------------------------------------------------------------------------
1646
1647bool wxThread::IsRunning() const
1648{
1649 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
1650
9fc3ad34 1651 return m_internal->GetState() == STATE_RUNNING;
518b5d2f
VZ
1652}
1653
1654bool wxThread::IsAlive() const
1655{
1656 wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect);
1657
9fc3ad34 1658 switch ( m_internal->GetState() )
518b5d2f
VZ
1659 {
1660 case STATE_RUNNING:
1661 case STATE_PAUSED:
ad9835c9 1662 return true;
518b5d2f
VZ
1663
1664 default:
ad9835c9 1665 return false;
518b5d2f
VZ
1666 }
1667}
1668
a737331d
GL
1669bool wxThread::IsPaused() const
1670{
1671 wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect);
1672
9fc3ad34 1673 return (m_internal->GetState() == STATE_PAUSED);
a737331d
GL
1674}
1675
518b5d2f
VZ
1676//--------------------------------------------------------------------
1677// wxThreadModule
1678//--------------------------------------------------------------------
1679
1680class wxThreadModule : public wxModule
1681{
1682public:
1683 virtual bool OnInit();
1684 virtual void OnExit();
1685
1686private:
1687 DECLARE_DYNAMIC_CLASS(wxThreadModule)
1688};
1689
1690IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
1691
1692bool wxThreadModule::OnInit()
1693{
58230fb1
VZ
1694 int rc = pthread_key_create(&gs_keySelf, NULL /* dtor function */);
1695 if ( rc != 0 )
518b5d2f 1696 {
76a6e803 1697 wxLogSysError(rc, _("Thread module initialization failed: failed to create thread key"));
518b5d2f 1698
ad9835c9 1699 return false;
518b5d2f
VZ
1700 }
1701
518b5d2f 1702 gs_tidMain = pthread_self();
19da4326 1703
25b34b26
VZ
1704 gs_mutexAllThreads = new wxMutex();
1705
9fc3ad34 1706 gs_mutexGui = new wxMutex();
518b5d2f
VZ
1707 gs_mutexGui->Lock();
1708
be809868 1709 gs_mutexDeleteThread = new wxMutex();
25b34b26 1710 gs_condAllDeleted = new wxCondition(*gs_mutexDeleteThread);
88e243b2 1711
ad9835c9 1712 return true;
518b5d2f
VZ
1713}
1714
1715void wxThreadModule::OnExit()
1716{
223d09f6 1717 wxASSERT_MSG( wxThread::IsMain(), wxT("only main thread can be here") );
518b5d2f 1718
9fc3ad34
VZ
1719 // are there any threads left which are being deleted right now?
1720 size_t nThreadsBeingDeleted;
be809868 1721
9fc3ad34 1722 {
be809868 1723 wxMutexLocker lock( *gs_mutexDeleteThread );
9fc3ad34 1724 nThreadsBeingDeleted = gs_nThreadsBeingDeleted;
9fc3ad34 1725
be809868 1726 if ( nThreadsBeingDeleted > 0 )
9e84b847 1727 {
2b5f62a0
VZ
1728 wxLogTrace(TRACE_THREADS,
1729 _T("Waiting for %lu threads to disappear"),
1730 (unsigned long)nThreadsBeingDeleted);
9fc3ad34 1731
be809868
VZ
1732 // have to wait until all of them disappear
1733 gs_condAllDeleted->Wait();
1734 }
9fc3ad34
VZ
1735 }
1736
25b34b26
VZ
1737 size_t count;
1738
4c460b34 1739 {
25b34b26
VZ
1740 wxMutexLocker lock(*gs_mutexAllThreads);
1741
1742 // terminate any threads left
1743 count = gs_allThreads.GetCount();
1744 if ( count != 0u )
1745 {
1746 wxLogDebug(wxT("%lu threads were not terminated by the application."),
1747 (unsigned long)count);
1748 }
1749 } // unlock mutex before deleting the threads as they lock it in their dtor
518b5d2f
VZ
1750
1751 for ( size_t n = 0u; n < count; n++ )
1752 {
bbfa0322
VZ
1753 // Delete calls the destructor which removes the current entry. We
1754 // should only delete the first one each time.
0ac77ef5 1755 gs_allThreads[0]->Delete();
518b5d2f
VZ
1756 }
1757
25b34b26
VZ
1758 delete gs_mutexAllThreads;
1759
518b5d2f
VZ
1760 // destroy GUI mutex
1761 gs_mutexGui->Unlock();
518b5d2f
VZ
1762 delete gs_mutexGui;
1763
1764 // and free TLD slot
1765 (void)pthread_key_delete(gs_keySelf);
be809868
VZ
1766
1767 delete gs_condAllDeleted;
1768 delete gs_mutexDeleteThread;
518b5d2f
VZ
1769}
1770
1771// ----------------------------------------------------------------------------
1772// global functions
1773// ----------------------------------------------------------------------------
1774
9fc3ad34
VZ
1775static void ScheduleThreadForDeletion()
1776{
be809868 1777 wxMutexLocker lock( *gs_mutexDeleteThread );
9fc3ad34
VZ
1778
1779 gs_nThreadsBeingDeleted++;
1780
2b5f62a0
VZ
1781 wxLogTrace(TRACE_THREADS, _T("%lu thread%s waiting to be deleted"),
1782 (unsigned long)gs_nThreadsBeingDeleted,
bc971e30 1783 gs_nThreadsBeingDeleted == 1 ? _T("") : _T("s"));
9fc3ad34
VZ
1784}
1785
1786static void DeleteThread(wxThread *This)
1787{
1788 // gs_mutexDeleteThread should be unlocked before signalling the condition
1789 // or wxThreadModule::OnExit() would deadlock
be809868 1790 wxMutexLocker locker( *gs_mutexDeleteThread );
9fc3ad34 1791
be809868 1792 wxLogTrace(TRACE_THREADS, _T("Thread %ld auto deletes."), This->GetId());
9fc3ad34 1793
be809868 1794 delete This;
9fc3ad34 1795
be809868 1796 wxCHECK_RET( gs_nThreadsBeingDeleted > 0,
9e84b847 1797 _T("no threads scheduled for deletion, yet we delete one?") );
9fc3ad34 1798
2b5f62a0
VZ
1799 wxLogTrace(TRACE_THREADS, _T("%lu scheduled for deletion threads left."),
1800 (unsigned long)gs_nThreadsBeingDeleted - 1);
4c460b34 1801
9fc3ad34
VZ
1802 if ( !--gs_nThreadsBeingDeleted )
1803 {
1804 // no more threads left, signal it
1805 gs_condAllDeleted->Signal();
9fc3ad34
VZ
1806 }
1807}
1808
d254213e 1809void wxMutexGuiEnterImpl()
518b5d2f 1810{
9fc3ad34 1811 gs_mutexGui->Lock();
518b5d2f
VZ
1812}
1813
d254213e 1814void wxMutexGuiLeaveImpl()
518b5d2f 1815{
9fc3ad34 1816 gs_mutexGui->Unlock();
518b5d2f 1817}
80cb83be 1818
9e84b847
VZ
1819// ----------------------------------------------------------------------------
1820// include common implementation code
1821// ----------------------------------------------------------------------------
1822
1823#include "wx/thrimpl.cpp"
1824
1825#endif // wxUSE_THREADS