]> git.saurik.com Git - wxWidgets.git/blame - src/msw/thread.cpp
some wxArtProv docs
[wxWidgets.git] / src / msw / thread.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: thread.cpp
3// Purpose: wxThread Implementation
4// Author: Original from Wolfram Gloger/Guilhem Lavaux
bee503b0 5// Modified by: Vadim Zeitlin to make it work :-)
2bda0e17
KB
6// Created: 04/22/98
7// RCS-ID: $Id$
bee503b0
VZ
8// Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998),
9// Vadim Zeitlin (1999)
2bda0e17
KB
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13#ifdef __GNUG__
3222fde2 14 #pragma implementation "thread.h"
2bda0e17
KB
15#endif
16
3222fde2
VZ
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
a3b46648 21// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
22#include "wx/wxprec.h"
23
24#if defined(__BORLANDC__)
3222fde2 25 #pragma hdrstop
2bda0e17
KB
26#endif
27
28#ifndef WX_PRECOMP
e0256755 29# include "wx/wx.h"
2bda0e17
KB
30#endif
31
3222fde2
VZ
32#if wxUSE_THREADS
33
0d0512bd 34#include "wx/msw/private.h"
3222fde2 35
2bda0e17
KB
36#include "wx/module.h"
37#include "wx/thread.h"
38
e0256755
GRG
39#ifdef Yield
40# undef Yield
41#endif
42
b568d04f
VZ
43// must have this symbol defined to get _beginthread/_endthread declarations
44#ifndef _MT
45 #define _MT
46#endif
47
9f51f2b6
VZ
48#if defined(__BORLANDC__)
49 #if !defined(__MT__)
50 // I can't set -tWM in the IDE (anyone?) so have to do this
51 #define __MT__
52 #endif
53
54 #if !defined(__MFC_COMPAT__)
55 // Needed to know about _beginthreadex etc..
56 #define __MFC_COMPAT__
57 #endif
58#endif // BC++
59
60// define wxUSE_BEGIN_THREAD if the compiler has _beginthreadex() function
61// which should be used instead of Win32 ::CreateThread() if possible
e0256755
GRG
62#if defined(__VISUALC__) || \
63 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
9f51f2b6
VZ
64 (defined(__GNUG__) && defined(__MSVCRT__)) || \
65 defined(__WATCOMC__)
ccebc98a 66
9f51f2b6
VZ
67 #undef wxUSE_BEGIN_THREAD
68 #define wxUSE_BEGIN_THREAD
8536082d
BJ
69#endif
70
9f51f2b6
VZ
71#ifdef wxUSE_BEGIN_THREAD
72 // this is where _beginthreadex() is declared
b568d04f 73 #include <process.h>
9f51f2b6
VZ
74
75 // the return type of the thread function entry point
76 typedef unsigned THREAD_RETVAL;
77
78 // the calling convention of the thread function entry point
79 #define THREAD_CALLCONV __stdcall
80#else
81 // the settings for CreateThread()
82 typedef DWORD THREAD_RETVAL;
83 #define THREAD_CALLCONV WINAPI
b568d04f
VZ
84#endif
85
86// ----------------------------------------------------------------------------
87// constants
88// ----------------------------------------------------------------------------
89
bf1852e1
VZ
90// the possible states of the thread ("=>" shows all possible transitions from
91// this state)
92enum wxThreadState
93{
94 STATE_NEW, // didn't start execution yet (=> RUNNING)
95 STATE_RUNNING, // thread is running (=> PAUSED, CANCELED)
96 STATE_PAUSED, // thread is temporarily suspended (=> RUNNING)
97 STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED)
98 STATE_EXITED // thread is terminating
2bda0e17
KB
99};
100
3222fde2 101// ----------------------------------------------------------------------------
b568d04f 102// this module globals
3222fde2 103// ----------------------------------------------------------------------------
2bda0e17 104
bf1852e1 105// TLS index of the slot where we store the pointer to the current thread
b568d04f 106static DWORD gs_tlsThisThread = 0xFFFFFFFF;
bf1852e1 107
3222fde2
VZ
108// id of the main thread - the one which can call GUI functions without first
109// calling wxMutexGuiEnter()
b568d04f 110static DWORD gs_idMainThread = 0;
bee503b0
VZ
111
112// if it's FALSE, some secondary thread is holding the GUI lock
b568d04f 113static bool gs_bGuiOwnedByMainThread = TRUE;
2bda0e17 114
3222fde2
VZ
115// critical section which controls access to all GUI functions: any secondary
116// thread (i.e. except the main one) must enter this crit section before doing
117// any GUI calls
b568d04f 118static wxCriticalSection *gs_critsectGui = NULL;
bee503b0 119
b568d04f
VZ
120// critical section which protects gs_nWaitingForGui variable
121static wxCriticalSection *gs_critsectWaitingForGui = NULL;
bee503b0
VZ
122
123// number of threads waiting for GUI in wxMutexGuiEnter()
b568d04f 124static size_t gs_nWaitingForGui = 0;
2bda0e17 125
bf1852e1 126// are we waiting for a thread termination?
b568d04f 127static bool gs_waitingForThread = FALSE;
bf1852e1 128
3222fde2
VZ
129// ============================================================================
130// Windows implementation of thread classes
131// ============================================================================
132
133// ----------------------------------------------------------------------------
134// wxMutex implementation
135// ----------------------------------------------------------------------------
0d0512bd 136
3222fde2
VZ
137class wxMutexInternal
138{
2bda0e17 139public:
7f684264
VZ
140 wxMutexInternal()
141 {
142 m_mutex = ::CreateMutex(NULL, FALSE, NULL);
143 if ( !m_mutex )
144 {
145 wxLogSysError(_("Can not create mutex"));
146 }
147 }
148
be809868 149 ~wxMutexInternal() { if ( m_mutex ) ::CloseHandle(m_mutex); }
7f684264
VZ
150
151public:
152 HANDLE m_mutex;
2bda0e17
KB
153};
154
ee4f8c2a 155wxMutex::wxMutex()
2bda0e17 156{
9fc3ad34 157 m_internal = new wxMutexInternal;
a6b0bd49 158
3222fde2 159 m_locked = 0;
2bda0e17
KB
160}
161
ee4f8c2a 162wxMutex::~wxMutex()
2bda0e17 163{
7f684264
VZ
164 if ( m_locked > 0 )
165 {
166 wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked);
167 }
168
169 delete m_internal;
2bda0e17
KB
170}
171
ee4f8c2a 172wxMutexError wxMutex::Lock()
2bda0e17 173{
3222fde2
VZ
174 DWORD ret;
175
7f684264 176 ret = WaitForSingleObject(m_internal->m_mutex, INFINITE);
3222fde2
VZ
177 switch ( ret )
178 {
179 case WAIT_ABANDONED:
180 return wxMUTEX_BUSY;
181
182 case WAIT_OBJECT_0:
183 // ok
184 break;
2bda0e17 185
3222fde2
VZ
186 case WAIT_FAILED:
187 wxLogSysError(_("Couldn't acquire a mutex lock"));
188 return wxMUTEX_MISC_ERROR;
2bda0e17 189
3222fde2
VZ
190 case WAIT_TIMEOUT:
191 default:
223d09f6 192 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
3222fde2
VZ
193 }
194
195 m_locked++;
196 return wxMUTEX_NO_ERROR;
2bda0e17
KB
197}
198
ee4f8c2a 199wxMutexError wxMutex::TryLock()
2bda0e17 200{
3222fde2 201 DWORD ret;
2bda0e17 202
7f684264 203 ret = WaitForSingleObject(m_internal->m_mutex, 0);
3222fde2
VZ
204 if (ret == WAIT_TIMEOUT || ret == WAIT_ABANDONED)
205 return wxMUTEX_BUSY;
2bda0e17 206
3222fde2
VZ
207 m_locked++;
208 return wxMUTEX_NO_ERROR;
2bda0e17
KB
209}
210
ee4f8c2a 211wxMutexError wxMutex::Unlock()
2bda0e17 212{
3222fde2
VZ
213 if (m_locked > 0)
214 m_locked--;
2bda0e17 215
7f684264 216 BOOL ret = ReleaseMutex(m_internal->m_mutex);
3222fde2
VZ
217 if ( ret == 0 )
218 {
219 wxLogSysError(_("Couldn't release a mutex"));
220 return wxMUTEX_MISC_ERROR;
221 }
a6b0bd49 222
3222fde2 223 return wxMUTEX_NO_ERROR;
2bda0e17
KB
224}
225
be809868
VZ
226// ==========================================================================
227// wxSemaphore
228// ==========================================================================
3222fde2 229
be809868
VZ
230// --------------------------------------------------------------------------
231// wxSemaphoreInternal
232// --------------------------------------------------------------------------
233
234class wxSemaphoreInternal
3222fde2 235{
2bda0e17 236public:
be809868
VZ
237 wxSemaphoreInternal( int initialcount = 0, int maxcount = 0 );
238 ~wxSemaphoreInternal();
239
240 void Wait();
241 bool TryWait();
242
243 bool Wait( unsigned long timeout_millis );
244
245 void Post();
246
247private:
248 HANDLE m_semaphore;
249};
250
251wxSemaphoreInternal::wxSemaphoreInternal( int initialcount, int maxcount )
252{
253 if ( maxcount == 0 )
b568d04f 254 {
be809868
VZ
255 // make it practically infinite
256 maxcount = INT_MAX;
257 }
4d1c1c3c 258
be809868
VZ
259 m_semaphore = ::CreateSemaphore( NULL, initialcount, maxcount, NULL );
260 if ( !m_semaphore )
261 {
262 wxLogLastError(_T("CreateSemaphore()"));
b568d04f 263 }
be809868
VZ
264}
265
266wxSemaphoreInternal::~wxSemaphoreInternal()
267{
268 CloseHandle( m_semaphore );
269}
b568d04f 270
be809868
VZ
271void wxSemaphoreInternal::Wait()
272{
273 if ( ::WaitForSingleObject( m_semaphore, INFINITE ) != WAIT_OBJECT_0 )
b568d04f 274 {
be809868
VZ
275 wxLogLastError(_T("WaitForSingleObject"));
276 }
277}
b568d04f 278
be809868
VZ
279bool wxSemaphoreInternal::TryWait()
280{
281 return Wait(0);
282}
b568d04f 283
be809868
VZ
284bool wxSemaphoreInternal::Wait( unsigned long timeout_millis )
285{
286 DWORD result = ::WaitForSingleObject( m_semaphore, timeout_millis );
287
288 switch ( result )
289 {
290 case WAIT_OBJECT_0:
291 return TRUE;
b568d04f 292
be809868
VZ
293 case WAIT_TIMEOUT:
294 break;
295
296 default:
297 wxLogLastError(_T("WaitForSingleObject()"));
b568d04f
VZ
298 }
299
be809868
VZ
300 return FALSE;
301}
302
303void wxSemaphoreInternal::Post()
304{
305 if ( !::ReleaseSemaphore( m_semaphore, 1, NULL ) )
4d1c1c3c 306 {
be809868 307 wxLogLastError(_T("ReleaseSemaphore"));
4d1c1c3c 308 }
be809868
VZ
309}
310
311// --------------------------------------------------------------------------
312// wxSemaphore
313// --------------------------------------------------------------------------
314
315wxSemaphore::wxSemaphore( int initialcount, int maxcount )
316{
317 m_internal = new wxSemaphoreInternal( initialcount, maxcount );
318}
319
320wxSemaphore::~wxSemaphore()
321{
322 delete m_internal;
323}
324
325void wxSemaphore::Wait()
326{
327 m_internal->Wait();
328}
329
330bool wxSemaphore::TryWait()
331{
332 return m_internal->TryWait();
333}
334
335bool wxSemaphore::Wait( unsigned long timeout_millis )
336{
337 return m_internal->Wait( timeout_millis );
338}
339
340void wxSemaphore::Post()
341{
342 m_internal->Post();
343}
4d1c1c3c 344
be809868
VZ
345
346// ==========================================================================
347// wxCondition
348// ==========================================================================
349
350// --------------------------------------------------------------------------
351// wxConditionInternal
352// --------------------------------------------------------------------------
353
354class wxConditionInternal
355{
356public:
c112e100 357 wxConditionInternal(wxMutex& mutex);
be809868
VZ
358
359 void Wait();
360
361 bool Wait( unsigned long timeout_millis );
362
363 void Signal();
364
365 void Broadcast();
366
367private:
368 int m_numWaiters;
369 wxMutex m_mutexNumWaiters;
370
c112e100 371 wxMutex& m_mutex;
be809868
VZ
372
373 wxSemaphore m_semaphore;
11632c43
RD
374
375 DECLARE_NO_COPY_CLASS(wxConditionInternal)
be809868
VZ
376};
377
c112e100
VZ
378wxConditionInternal::wxConditionInternal(wxMutex& mutex)
379 : m_mutex(mutex)
be809868 380{
be809868
VZ
381
382 m_numWaiters = 0;
383}
384
be809868
VZ
385void wxConditionInternal::Wait()
386{
387 // increment the number of waiters
388 m_mutexNumWaiters.Lock();
389 m_numWaiters++;
390 m_mutexNumWaiters.Unlock();
391
c112e100 392 m_mutex.Unlock();
be809868
VZ
393
394 // a potential race condition can occur here
395 //
396 // after a thread increments nwaiters, and unlocks the mutex and before the
397 // semaphore.Wait() is called, if another thread can cause a signal to be
398 // generated
399 //
400 // this race condition is handled by using a semaphore and incrementing the
401 // semaphore only if 'nwaiters' is greater that zero since the semaphore,
402 // can 'remember' signals the race condition will not occur
403
404 // wait ( if necessary ) and decrement semaphore
405 m_semaphore.Wait();
406
c112e100 407 m_mutex.Lock();
be809868
VZ
408}
409
410bool wxConditionInternal::Wait( unsigned long timeout_millis )
411{
412 m_mutexNumWaiters.Lock();
413 m_numWaiters++;
414 m_mutexNumWaiters.Unlock();
415
c112e100 416 m_mutex.Unlock();
be809868
VZ
417
418 // a race condition can occur at this point in the code
419 //
420 // please see the comments in Wait(), for details
421
422 bool success = TRUE;
423
424 bool result = m_semaphore.Wait( timeout_millis );
425
426 if ( !result )
4d1c1c3c 427 {
be809868
VZ
428 // another potential race condition exists here it is caused when a
429 // 'waiting' thread timesout, and returns from WaitForSingleObject, but
430 // has not yet decremented 'nwaiters'.
431 //
432 // at this point if another thread calls signal() then the semaphore
433 // will be incremented, but the waiting thread will miss it.
434 //
435 // to handle this particular case, the waiting thread calls
436 // WaitForSingleObject again with a timeout of 0, after locking
437 // 'nwaiters_mutex'. this call does not block because of the zero
438 // timeout, but will allow the waiting thread to catch the missed
439 // signals.
440 m_mutexNumWaiters.Lock();
441 result = m_semaphore.Wait( 0 );
442
443 if ( !result )
4d1c1c3c 444 {
be809868
VZ
445 m_numWaiters--;
446 success = FALSE;
4d1c1c3c 447 }
be809868
VZ
448
449 m_mutexNumWaiters.Unlock();
4d1c1c3c
VZ
450 }
451
c112e100 452 m_mutex.Lock();
be809868
VZ
453
454 return success;
455}
456
457void wxConditionInternal::Signal()
458{
459 m_mutexNumWaiters.Lock();
460
461 if ( m_numWaiters > 0 )
b568d04f 462 {
be809868
VZ
463 // increment the semaphore by 1
464 m_semaphore.Post();
465
466 m_numWaiters--;
b568d04f
VZ
467 }
468
be809868
VZ
469 m_mutexNumWaiters.Unlock();
470}
4d1c1c3c 471
be809868
VZ
472void wxConditionInternal::Broadcast()
473{
474 m_mutexNumWaiters.Lock();
2bda0e17 475
be809868
VZ
476 while ( m_numWaiters > 0 )
477 {
478 m_semaphore.Post();
479 m_numWaiters--;
480 }
481
482 m_mutexNumWaiters.Unlock();
483}
484
485// ----------------------------------------------------------------------------
486// wxCondition implementation
487// ----------------------------------------------------------------------------
488
c112e100 489wxCondition::wxCondition(wxMutex& mutex)
2bda0e17 490{
c112e100 491 m_internal = new wxConditionInternal( mutex );
2bda0e17
KB
492}
493
ee4f8c2a 494wxCondition::~wxCondition()
2bda0e17 495{
9fc3ad34 496 delete m_internal;
2bda0e17
KB
497}
498
9fc3ad34 499void wxCondition::Wait()
2bda0e17 500{
c112e100 501 m_internal->Wait();
2bda0e17
KB
502}
503
be809868 504bool wxCondition::Wait( unsigned long timeout_millis )
2bda0e17 505{
c112e100 506 return m_internal->Wait(timeout_millis);
2bda0e17
KB
507}
508
ee4f8c2a 509void wxCondition::Signal()
2bda0e17 510{
c112e100 511 m_internal->Signal();
2bda0e17
KB
512}
513
ee4f8c2a 514void wxCondition::Broadcast()
2bda0e17 515{
c112e100 516 m_internal->Broadcast();
2bda0e17
KB
517}
518
3222fde2
VZ
519// ----------------------------------------------------------------------------
520// wxCriticalSection implementation
521// ----------------------------------------------------------------------------
522
3222fde2
VZ
523wxCriticalSection::wxCriticalSection()
524{
35cd9dba
GT
525#ifdef __WXDEBUG__
526 // Done this way to stop warnings during compilation about statement
be809868 527 // always being FALSE
6fe73788
RL
528 int csSize = sizeof(CRITICAL_SECTION);
529 int bSize = sizeof(m_buffer);
35cd9dba 530 wxASSERT_MSG( csSize <= bSize,
0d0512bd 531 _T("must increase buffer size in wx/thread.h") );
35cd9dba 532#endif
0d0512bd
VZ
533
534 ::InitializeCriticalSection((CRITICAL_SECTION *)m_buffer);
3222fde2
VZ
535}
536
537wxCriticalSection::~wxCriticalSection()
538{
0d0512bd 539 ::DeleteCriticalSection((CRITICAL_SECTION *)m_buffer);
3222fde2
VZ
540}
541
542void wxCriticalSection::Enter()
543{
0d0512bd 544 ::EnterCriticalSection((CRITICAL_SECTION *)m_buffer);
3222fde2
VZ
545}
546
547void wxCriticalSection::Leave()
548{
0d0512bd 549 ::LeaveCriticalSection((CRITICAL_SECTION *)m_buffer);
3222fde2
VZ
550}
551
552// ----------------------------------------------------------------------------
553// wxThread implementation
554// ----------------------------------------------------------------------------
555
bf1852e1
VZ
556// wxThreadInternal class
557// ----------------------
558
3222fde2
VZ
559class wxThreadInternal
560{
2bda0e17 561public:
bf1852e1
VZ
562 wxThreadInternal()
563 {
564 m_hThread = 0;
565 m_state = STATE_NEW;
566 m_priority = WXTHREAD_DEFAULT_PRIORITY;
567 }
568
b568d04f
VZ
569 ~wxThreadInternal()
570 {
571 Free();
572 }
573
574 void Free()
575 {
576 if ( m_hThread )
577 {
578 if ( !::CloseHandle(m_hThread) )
579 {
f6bcfd97 580 wxLogLastError(wxT("CloseHandle(thread)"));
b568d04f
VZ
581 }
582
583 m_hThread = 0;
584 }
585 }
586
bf1852e1 587 // create a new (suspended) thread (for the given thread object)
6fe73788 588 bool Create(wxThread *thread, unsigned int stackSize);
bf1852e1
VZ
589
590 // suspend/resume/terminate
591 bool Suspend();
592 bool Resume();
593 void Cancel() { m_state = STATE_CANCELED; }
594
595 // thread state
596 void SetState(wxThreadState state) { m_state = state; }
597 wxThreadState GetState() const { return m_state; }
598
599 // thread priority
b568d04f 600 void SetPriority(unsigned int priority);
bf1852e1
VZ
601 unsigned int GetPriority() const { return m_priority; }
602
603 // thread handle and id
604 HANDLE GetHandle() const { return m_hThread; }
605 DWORD GetId() const { return m_tid; }
606
607 // thread function
9f51f2b6 608 static THREAD_RETVAL THREAD_CALLCONV WinThreadStart(void *thread);
2bda0e17 609
bf1852e1
VZ
610private:
611 HANDLE m_hThread; // handle of the thread
612 wxThreadState m_state; // state, see wxThreadState enum
613 unsigned int m_priority; // thread priority in "wx" units
614 DWORD m_tid; // thread id
2bda0e17
KB
615};
616
08d7397c 617THREAD_RETVAL THREAD_CALLCONV wxThreadInternal::WinThreadStart(void *param)
2bda0e17 618{
9f51f2b6 619 THREAD_RETVAL rc;
f6bcfd97
BP
620 bool wasCancelled;
621
622 // first of all, check whether we hadn't been cancelled already and don't
623 // start the user code at all then
9f51f2b6 624 wxThread *thread = (wxThread *)param;
696e1ea0
VZ
625 if ( thread->m_internal->GetState() == STATE_EXITED )
626 {
9f51f2b6 627 rc = (THREAD_RETVAL)-1;
f6bcfd97 628 wasCancelled = TRUE;
696e1ea0 629 }
f6bcfd97 630 else // do run thread
bf1852e1 631 {
f6bcfd97
BP
632 // store the thread object in the TLS
633 if ( !::TlsSetValue(gs_tlsThisThread, thread) )
634 {
635 wxLogSysError(_("Can not start thread: error writing TLS."));
bf1852e1 636
f6bcfd97
BP
637 return (DWORD)-1;
638 }
bf1852e1 639
9f51f2b6 640 rc = (THREAD_RETVAL)thread->Entry();
b568d04f 641
f6bcfd97
BP
642 // enter m_critsect before changing the thread state
643 thread->m_critsect.Enter();
644 wasCancelled = thread->m_internal->GetState() == STATE_CANCELED;
645 thread->m_internal->SetState(STATE_EXITED);
646 thread->m_critsect.Leave();
647 }
b568d04f 648
bee503b0 649 thread->OnExit();
2bda0e17 650
f6bcfd97 651 // if the thread was cancelled (from Delete()), then its handle is still
b568d04f
VZ
652 // needed there
653 if ( thread->IsDetached() && !wasCancelled )
654 {
655 // auto delete
656 delete thread;
657 }
658 //else: the joinable threads handle will be closed when Wait() is done
bf1852e1 659
b568d04f 660 return rc;
2bda0e17
KB
661}
662
b568d04f 663void wxThreadInternal::SetPriority(unsigned int priority)
2bda0e17 664{
b568d04f 665 m_priority = priority;
3222fde2 666
bf1852e1
VZ
667 // translate wxWindows priority to the Windows one
668 int win_priority;
669 if (m_priority <= 20)
670 win_priority = THREAD_PRIORITY_LOWEST;
671 else if (m_priority <= 40)
672 win_priority = THREAD_PRIORITY_BELOW_NORMAL;
673 else if (m_priority <= 60)
674 win_priority = THREAD_PRIORITY_NORMAL;
675 else if (m_priority <= 80)
676 win_priority = THREAD_PRIORITY_ABOVE_NORMAL;
677 else if (m_priority <= 100)
678 win_priority = THREAD_PRIORITY_HIGHEST;
3222fde2
VZ
679 else
680 {
223d09f6 681 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
bf1852e1 682 win_priority = THREAD_PRIORITY_NORMAL;
3222fde2
VZ
683 }
684
b568d04f 685 if ( !::SetThreadPriority(m_hThread, win_priority) )
bee503b0
VZ
686 {
687 wxLogSysError(_("Can't set thread priority"));
688 }
b568d04f
VZ
689}
690
6fe73788 691bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
b568d04f
VZ
692{
693 // for compilers which have it, we should use C RTL function for thread
694 // creation instead of Win32 API one because otherwise we will have memory
695 // leaks if the thread uses C RTL (and most threads do)
9f51f2b6 696#ifdef wxUSE_BEGIN_THREAD
414c639c
VZ
697
698 // Watcom is reported to not like 0 stack size (which means "use default"
699 // for the other compilers and is also the default value for stackSize)
700#ifdef __WATCOMC__
701 if ( !stackSize )
702 stackSize = 10240;
703#endif // __WATCOMC__
704
9f51f2b6
VZ
705 m_hThread = (HANDLE)_beginthreadex
706 (
6fe73788
RL
707 NULL, // default security
708 stackSize,
709 wxThreadInternal::WinThreadStart, // entry point
710 thread,
711 CREATE_SUSPENDED,
712 (unsigned int *)&m_tid
9f51f2b6 713 );
611cb666 714#else // compiler doesn't have _beginthreadex
b568d04f
VZ
715 m_hThread = ::CreateThread
716 (
717 NULL, // default security
414c639c 718 stackSize, // stack size
9f51f2b6 719 wxThreadInternal::WinThreadStart, // thread entry point
b568d04f
VZ
720 (LPVOID)thread, // parameter
721 CREATE_SUSPENDED, // flags
722 &m_tid // [out] thread id
723 );
611cb666 724#endif // _beginthreadex/CreateThread
b568d04f
VZ
725
726 if ( m_hThread == NULL )
727 {
728 wxLogSysError(_("Can't create thread"));
729
730 return FALSE;
731 }
732
733 if ( m_priority != WXTHREAD_DEFAULT_PRIORITY )
734 {
735 SetPriority(m_priority);
736 }
3222fde2 737
bf1852e1 738 return TRUE;
2bda0e17
KB
739}
740
bf1852e1 741bool wxThreadInternal::Suspend()
2bda0e17 742{
bf1852e1
VZ
743 DWORD nSuspendCount = ::SuspendThread(m_hThread);
744 if ( nSuspendCount == (DWORD)-1 )
bee503b0 745 {
bf1852e1
VZ
746 wxLogSysError(_("Can not suspend thread %x"), m_hThread);
747
748 return FALSE;
bee503b0 749 }
2bda0e17 750
bf1852e1
VZ
751 m_state = STATE_PAUSED;
752
753 return TRUE;
a6b0bd49
VZ
754}
755
bf1852e1 756bool wxThreadInternal::Resume()
a6b0bd49 757{
bf1852e1 758 DWORD nSuspendCount = ::ResumeThread(m_hThread);
a6b0bd49
VZ
759 if ( nSuspendCount == (DWORD)-1 )
760 {
bf1852e1 761 wxLogSysError(_("Can not resume thread %x"), m_hThread);
a6b0bd49 762
bf1852e1 763 return FALSE;
a6b0bd49
VZ
764 }
765
f6bcfd97
BP
766 // don't change the state from STATE_EXITED because it's special and means
767 // we are going to terminate without running any user code - if we did it,
768 // the codei n Delete() wouldn't work
769 if ( m_state != STATE_EXITED )
770 {
771 m_state = STATE_RUNNING;
772 }
bee503b0 773
bf1852e1 774 return TRUE;
a6b0bd49
VZ
775}
776
bf1852e1
VZ
777// static functions
778// ----------------
779
780wxThread *wxThread::This()
a6b0bd49 781{
b568d04f 782 wxThread *thread = (wxThread *)::TlsGetValue(gs_tlsThisThread);
bf1852e1
VZ
783
784 // be careful, 0 may be a valid return value as well
785 if ( !thread && (::GetLastError() != NO_ERROR) )
a6b0bd49 786 {
bf1852e1 787 wxLogSysError(_("Couldn't get the current thread pointer"));
a6b0bd49 788
bf1852e1 789 // return NULL...
a6b0bd49
VZ
790 }
791
bf1852e1
VZ
792 return thread;
793}
794
795bool wxThread::IsMain()
796{
b568d04f 797 return ::GetCurrentThreadId() == gs_idMainThread;
bf1852e1
VZ
798}
799
c25a510b
JS
800#ifdef Yield
801#undef Yield
802#endif
803
bf1852e1
VZ
804void wxThread::Yield()
805{
b568d04f
VZ
806 // 0 argument to Sleep() is special and means to just give away the rest of
807 // our timeslice
bf1852e1
VZ
808 ::Sleep(0);
809}
810
811void wxThread::Sleep(unsigned long milliseconds)
812{
813 ::Sleep(milliseconds);
814}
815
ef8d96c2
VZ
816int wxThread::GetCPUCount()
817{
28a4627c
VZ
818 SYSTEM_INFO si;
819 GetSystemInfo(&si);
820
821 return si.dwNumberOfProcessors;
ef8d96c2
VZ
822}
823
4958ea8f
RD
824unsigned long wxThread::GetCurrentId()
825{
826 return (unsigned long)::GetCurrentThreadId();
827}
828
ef8d96c2
VZ
829bool wxThread::SetConcurrency(size_t level)
830{
28a4627c
VZ
831 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
832
ef8d96c2 833 // ok only for the default one
28a4627c
VZ
834 if ( level == 0 )
835 return 0;
836
837 // get system affinity mask first
838 HANDLE hProcess = ::GetCurrentProcess();
839 DWORD dwProcMask, dwSysMask;
840 if ( ::GetProcessAffinityMask(hProcess, &dwProcMask, &dwSysMask) == 0 )
841 {
842 wxLogLastError(_T("GetProcessAffinityMask"));
843
844 return FALSE;
845 }
846
847 // how many CPUs have we got?
848 if ( dwSysMask == 1 )
849 {
850 // don't bother with all this complicated stuff - on a single
851 // processor system it doesn't make much sense anyhow
852 return level == 1;
853 }
854
855 // calculate the process mask: it's a bit vector with one bit per
856 // processor; we want to schedule the process to run on first level
857 // CPUs
858 DWORD bit = 1;
859 while ( bit )
860 {
861 if ( dwSysMask & bit )
862 {
863 // ok, we can set this bit
864 dwProcMask |= bit;
865
866 // another process added
867 if ( !--level )
868 {
869 // and that's enough
870 break;
871 }
872 }
873
874 // next bit
875 bit <<= 1;
876 }
877
878 // could we set all bits?
879 if ( level != 0 )
880 {
881 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level);
882
883 return FALSE;
884 }
885
886 // set it: we can't link to SetProcessAffinityMask() because it doesn't
887 // exist in Win9x, use RT binding instead
888
696e1ea0 889 typedef BOOL (*SETPROCESSAFFINITYMASK)(HANDLE, DWORD);
28a4627c
VZ
890
891 // can use static var because we're always in the main thread here
892 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask = NULL;
893
894 if ( !pfnSetProcessAffinityMask )
895 {
896 HMODULE hModKernel = ::LoadLibrary(_T("kernel32"));
897 if ( hModKernel )
898 {
899 pfnSetProcessAffinityMask = (SETPROCESSAFFINITYMASK)
f6bcfd97 900 ::GetProcAddress(hModKernel, "SetProcessAffinityMask");
28a4627c
VZ
901 }
902
903 // we've discovered a MT version of Win9x!
904 wxASSERT_MSG( pfnSetProcessAffinityMask,
f6bcfd97 905 _T("this system has several CPUs but no SetProcessAffinityMask function?") );
28a4627c
VZ
906 }
907
908 if ( !pfnSetProcessAffinityMask )
909 {
910 // msg given above - do it only once
911 return FALSE;
912 }
913
696e1ea0 914 if ( pfnSetProcessAffinityMask(hProcess, dwProcMask) == 0 )
28a4627c
VZ
915 {
916 wxLogLastError(_T("SetProcessAffinityMask"));
917
918 return FALSE;
919 }
920
921 return TRUE;
ef8d96c2
VZ
922}
923
b568d04f
VZ
924// ctor and dtor
925// -------------
926
927wxThread::wxThread(wxThreadKind kind)
928{
9fc3ad34 929 m_internal = new wxThreadInternal();
b568d04f
VZ
930
931 m_isDetached = kind == wxTHREAD_DETACHED;
932}
933
934wxThread::~wxThread()
935{
9fc3ad34 936 delete m_internal;
b568d04f
VZ
937}
938
bf1852e1
VZ
939// create/start thread
940// -------------------
941
6fe73788 942wxThreadError wxThread::Create(unsigned int stackSize)
bf1852e1 943{
b568d04f
VZ
944 wxCriticalSectionLocker lock(m_critsect);
945
6fe73788 946 if ( !m_internal->Create(this, stackSize) )
bf1852e1 947 return wxTHREAD_NO_RESOURCE;
bee503b0 948
a6b0bd49 949 return wxTHREAD_NO_ERROR;
2bda0e17
KB
950}
951
bf1852e1 952wxThreadError wxThread::Run()
2bda0e17 953{
bf1852e1
VZ
954 wxCriticalSectionLocker lock(m_critsect);
955
9fc3ad34 956 if ( m_internal->GetState() != STATE_NEW )
bf1852e1
VZ
957 {
958 // actually, it may be almost any state at all, not only STATE_RUNNING
959 return wxTHREAD_RUNNING;
960 }
961
b568d04f 962 // the thread has just been created and is still suspended - let it run
bf1852e1 963 return Resume();
2bda0e17
KB
964}
965
bf1852e1
VZ
966// suspend/resume thread
967// ---------------------
968
969wxThreadError wxThread::Pause()
2bda0e17 970{
bf1852e1
VZ
971 wxCriticalSectionLocker lock(m_critsect);
972
9fc3ad34 973 return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
2bda0e17
KB
974}
975
bf1852e1 976wxThreadError wxThread::Resume()
2bda0e17 977{
bf1852e1
VZ
978 wxCriticalSectionLocker lock(m_critsect);
979
9fc3ad34 980 return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
2bda0e17
KB
981}
982
bf1852e1
VZ
983// stopping thread
984// ---------------
985
b568d04f
VZ
986wxThread::ExitCode wxThread::Wait()
987{
988 // although under Windows we can wait for any thread, it's an error to
989 // wait for a detached one in wxWin API
990 wxCHECK_MSG( !IsDetached(), (ExitCode)-1,
991 _T("can't wait for detached thread") );
992
993 ExitCode rc = (ExitCode)-1;
994
995 (void)Delete(&rc);
996
9fc3ad34 997 m_internal->Free();
b568d04f
VZ
998
999 return rc;
1000}
1001
1002wxThreadError wxThread::Delete(ExitCode *pRc)
2bda0e17 1003{
bf1852e1
VZ
1004 ExitCode rc = 0;
1005
1006 // Delete() is always safe to call, so consider all possible states
696e1ea0 1007
f6bcfd97
BP
1008 // we might need to resume the thread, but we might also not need to cancel
1009 // it if it doesn't run yet
1010 bool shouldResume = FALSE,
1011 shouldCancel = TRUE,
1012 isRunning = FALSE;
696e1ea0 1013
f6bcfd97 1014 // check if the thread already started to run
696e1ea0
VZ
1015 {
1016 wxCriticalSectionLocker lock(m_critsect);
1017
1018 if ( m_internal->GetState() == STATE_NEW )
1019 {
f6bcfd97
BP
1020 // WinThreadStart() will see it and terminate immediately, no need
1021 // to cancel the thread - but we still need to resume it to let it
1022 // run
696e1ea0
VZ
1023 m_internal->SetState(STATE_EXITED);
1024
f6bcfd97
BP
1025 Resume(); // it knows about STATE_EXITED special case
1026
1027 shouldCancel = FALSE;
1028 isRunning = TRUE;
1029
1030 // shouldResume is correctly set to FALSE here
1031 }
1032 else
1033 {
1034 shouldResume = IsPaused();
696e1ea0
VZ
1035 }
1036 }
1037
f6bcfd97
BP
1038 // resume the thread if it is paused
1039 if ( shouldResume )
bf1852e1
VZ
1040 Resume();
1041
9fc3ad34 1042 HANDLE hThread = m_internal->GetHandle();
b568d04f 1043
696e1ea0 1044 // does is still run?
f6bcfd97 1045 if ( isRunning || IsRunning() )
bf1852e1
VZ
1046 {
1047 if ( IsMain() )
1048 {
1049 // set flag for wxIsWaitingForThread()
b568d04f 1050 gs_waitingForThread = TRUE;
bf1852e1 1051
b568d04f 1052#if wxUSE_GUI
bf1852e1 1053 wxBeginBusyCursor();
b568d04f 1054#endif // wxUSE_GUI
bf1852e1
VZ
1055 }
1056
b568d04f 1057 // ask the thread to terminate
f6bcfd97 1058 if ( shouldCancel )
bf1852e1
VZ
1059 {
1060 wxCriticalSectionLocker lock(m_critsect);
1061
9fc3ad34 1062 m_internal->Cancel();
bf1852e1
VZ
1063 }
1064
b568d04f 1065#if wxUSE_GUI
bf1852e1
VZ
1066 // we can't just wait for the thread to terminate because it might be
1067 // calling some GUI functions and so it will never terminate before we
1068 // process the Windows messages that result from these functions
1069 DWORD result;
1070 do
1071 {
1072 result = ::MsgWaitForMultipleObjects
1073 (
1074 1, // number of objects to wait for
1075 &hThread, // the objects
1076 FALSE, // don't wait for all objects
1077 INFINITE, // no timeout
1078 QS_ALLEVENTS // return as soon as there are any events
1079 );
1080
1081 switch ( result )
1082 {
1083 case 0xFFFFFFFF:
1084 // error
1085 wxLogSysError(_("Can not wait for thread termination"));
1086 Kill();
b568d04f 1087 return wxTHREAD_KILLED;
bf1852e1
VZ
1088
1089 case WAIT_OBJECT_0:
1090 // thread we're waiting for terminated
1091 break;
1092
1093 case WAIT_OBJECT_0 + 1:
1094 // new message arrived, process it
1095 if ( !wxTheApp->DoMessage() )
1096 {
1097 // WM_QUIT received: kill the thread
1098 Kill();
1099
b568d04f 1100 return wxTHREAD_KILLED;
bf1852e1
VZ
1101 }
1102
1103 if ( IsMain() )
1104 {
1105 // give the thread we're waiting for chance to exit
1106 // from the GUI call it might have been in
b568d04f 1107 if ( (gs_nWaitingForGui > 0) && wxGuiOwnedByMainThread() )
bf1852e1
VZ
1108 {
1109 wxMutexGuiLeave();
1110 }
1111 }
1112
1113 break;
1114
1115 default:
223d09f6 1116 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
bf1852e1
VZ
1117 }
1118 } while ( result != WAIT_OBJECT_0 );
b568d04f
VZ
1119#else // !wxUSE_GUI
1120 // simply wait for the thread to terminate
1121 //
1122 // OTOH, even console apps create windows (in wxExecute, for WinSock
1123 // &c), so may be use MsgWaitForMultipleObject() too here?
1124 if ( WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0 )
1125 {
1126 wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
1127 }
1128#endif // wxUSE_GUI/!wxUSE_GUI
bf1852e1
VZ
1129
1130 if ( IsMain() )
1131 {
b568d04f 1132 gs_waitingForThread = FALSE;
bf1852e1 1133
b568d04f 1134#if wxUSE_GUI
bf1852e1 1135 wxEndBusyCursor();
b568d04f 1136#endif // wxUSE_GUI
bf1852e1 1137 }
b568d04f 1138 }
bf1852e1 1139
10671c67
VZ
1140 // although the thread might be already in the EXITED state it might not
1141 // have terminated yet and so we are not sure that it has actually
1142 // terminated if the "if" above hadn't been taken
1143 do
b568d04f 1144 {
10671c67
VZ
1145 if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) )
1146 {
1147 wxLogLastError(wxT("GetExitCodeThread"));
bf1852e1 1148
10671c67
VZ
1149 rc = (ExitCode)-1;
1150 }
1151 } while ( (DWORD)rc == STILL_ACTIVE );
bf1852e1 1152
b568d04f
VZ
1153 if ( IsDetached() )
1154 {
1155 // if the thread exits normally, this is done in WinThreadStart, but in
1156 // this case it would have been too early because
f6bcfd97 1157 // MsgWaitForMultipleObject() would fail if the thread handle was
b568d04f
VZ
1158 // closed while we were waiting on it, so we must do it here
1159 delete this;
bf1852e1
VZ
1160 }
1161
b568d04f
VZ
1162 if ( pRc )
1163 *pRc = rc;
1164
1165 return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR;
2bda0e17
KB
1166}
1167
bf1852e1 1168wxThreadError wxThread::Kill()
2bda0e17 1169{
bf1852e1
VZ
1170 if ( !IsRunning() )
1171 return wxTHREAD_NOT_RUNNING;
1172
9fc3ad34 1173 if ( !::TerminateThread(m_internal->GetHandle(), (DWORD)-1) )
bf1852e1
VZ
1174 {
1175 wxLogSysError(_("Couldn't terminate thread"));
1176
1177 return wxTHREAD_MISC_ERROR;
1178 }
1179
9fc3ad34 1180 m_internal->Free();
b568d04f
VZ
1181
1182 if ( IsDetached() )
1183 {
1184 delete this;
1185 }
bf1852e1
VZ
1186
1187 return wxTHREAD_NO_ERROR;
2bda0e17
KB
1188}
1189
b568d04f 1190void wxThread::Exit(ExitCode status)
2bda0e17 1191{
9fc3ad34 1192 m_internal->Free();
2bda0e17 1193
b568d04f
VZ
1194 if ( IsDetached() )
1195 {
1196 delete this;
1197 }
1198
9f51f2b6 1199#ifdef wxUSE_BEGIN_THREAD
b568d04f
VZ
1200 _endthreadex((unsigned)status);
1201#else // !VC++
bf1852e1 1202 ::ExitThread((DWORD)status);
b568d04f 1203#endif // VC++/!VC++
2bda0e17 1204
223d09f6 1205 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
bf1852e1 1206}
2bda0e17 1207
b568d04f
VZ
1208// priority setting
1209// ----------------
1210
bf1852e1
VZ
1211void wxThread::SetPriority(unsigned int prio)
1212{
1213 wxCriticalSectionLocker lock(m_critsect);
1214
9fc3ad34 1215 m_internal->SetPriority(prio);
bf1852e1 1216}
2bda0e17 1217
bf1852e1
VZ
1218unsigned int wxThread::GetPriority() const
1219{
b568d04f 1220 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
2bda0e17 1221
9fc3ad34 1222 return m_internal->GetPriority();
2bda0e17
KB
1223}
1224
b568d04f 1225unsigned long wxThread::GetId() const
2bda0e17 1226{
b568d04f 1227 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
bf1852e1 1228
9fc3ad34 1229 return (unsigned long)m_internal->GetId();
2bda0e17
KB
1230}
1231
72fd19a1
JS
1232bool wxThread::IsRunning() const
1233{
b568d04f 1234 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
bf1852e1 1235
9fc3ad34 1236 return m_internal->GetState() == STATE_RUNNING;
72fd19a1
JS
1237}
1238
1239bool wxThread::IsAlive() const
1240{
b568d04f 1241 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
bf1852e1 1242
9fc3ad34
VZ
1243 return (m_internal->GetState() == STATE_RUNNING) ||
1244 (m_internal->GetState() == STATE_PAUSED);
72fd19a1
JS
1245}
1246
a737331d
GL
1247bool wxThread::IsPaused() const
1248{
b568d04f 1249 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
a737331d 1250
9fc3ad34 1251 return m_internal->GetState() == STATE_PAUSED;
a737331d
GL
1252}
1253
8c10faf1 1254bool wxThread::TestDestroy()
2bda0e17 1255{
b568d04f 1256 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
bf1852e1 1257
9fc3ad34 1258 return m_internal->GetState() == STATE_CANCELED;
2bda0e17
KB
1259}
1260
3222fde2
VZ
1261// ----------------------------------------------------------------------------
1262// Automatic initialization for thread module
1263// ----------------------------------------------------------------------------
2bda0e17 1264
3222fde2 1265class wxThreadModule : public wxModule
a6b0bd49 1266{
3222fde2
VZ
1267public:
1268 virtual bool OnInit();
1269 virtual void OnExit();
d524867f 1270
3222fde2
VZ
1271private:
1272 DECLARE_DYNAMIC_CLASS(wxThreadModule)
1273};
d524867f
RR
1274
1275IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
1276
3222fde2 1277bool wxThreadModule::OnInit()
d524867f 1278{
bf1852e1 1279 // allocate TLS index for storing the pointer to the current thread
b568d04f
VZ
1280 gs_tlsThisThread = ::TlsAlloc();
1281 if ( gs_tlsThisThread == 0xFFFFFFFF )
bf1852e1
VZ
1282 {
1283 // in normal circumstances it will only happen if all other
1284 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1285 // words, this should never happen
f6bcfd97 1286 wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage"));
bf1852e1
VZ
1287
1288 return FALSE;
1289 }
1290
1291 // main thread doesn't have associated wxThread object, so store 0 in the
1292 // TLS instead
b568d04f 1293 if ( !::TlsSetValue(gs_tlsThisThread, (LPVOID)0) )
bf1852e1 1294 {
b568d04f
VZ
1295 ::TlsFree(gs_tlsThisThread);
1296 gs_tlsThisThread = 0xFFFFFFFF;
bf1852e1 1297
f6bcfd97 1298 wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage"));
bf1852e1
VZ
1299
1300 return FALSE;
1301 }
1302
b568d04f 1303 gs_critsectWaitingForGui = new wxCriticalSection();
bee503b0 1304
b568d04f
VZ
1305 gs_critsectGui = new wxCriticalSection();
1306 gs_critsectGui->Enter();
bee503b0 1307
bf1852e1 1308 // no error return for GetCurrentThreadId()
b568d04f 1309 gs_idMainThread = ::GetCurrentThreadId();
3222fde2 1310
d524867f
RR
1311 return TRUE;
1312}
1313
3222fde2 1314void wxThreadModule::OnExit()
d524867f 1315{
b568d04f 1316 if ( !::TlsFree(gs_tlsThisThread) )
bf1852e1 1317 {
f6bcfd97 1318 wxLogLastError(wxT("TlsFree failed."));
bf1852e1
VZ
1319 }
1320
b568d04f 1321 if ( gs_critsectGui )
3222fde2 1322 {
b568d04f
VZ
1323 gs_critsectGui->Leave();
1324 delete gs_critsectGui;
1325 gs_critsectGui = NULL;
3222fde2 1326 }
bee503b0 1327
b568d04f
VZ
1328 delete gs_critsectWaitingForGui;
1329 gs_critsectWaitingForGui = NULL;
3222fde2
VZ
1330}
1331
bee503b0 1332// ----------------------------------------------------------------------------
b568d04f 1333// under Windows, these functions are implemented using a critical section and
3222fde2 1334// not a mutex, so the names are a bit confusing
bee503b0
VZ
1335// ----------------------------------------------------------------------------
1336
3222fde2
VZ
1337void WXDLLEXPORT wxMutexGuiEnter()
1338{
bee503b0
VZ
1339 // this would dead lock everything...
1340 wxASSERT_MSG( !wxThread::IsMain(),
223d09f6 1341 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
bee503b0
VZ
1342
1343 // the order in which we enter the critical sections here is crucial!!
1344
1345 // set the flag telling to the main thread that we want to do some GUI
1346 {
b568d04f 1347 wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
bee503b0 1348
b568d04f 1349 gs_nWaitingForGui++;
bee503b0
VZ
1350 }
1351
1352 wxWakeUpMainThread();
1353
1354 // now we may block here because the main thread will soon let us in
1355 // (during the next iteration of OnIdle())
b568d04f 1356 gs_critsectGui->Enter();
3222fde2
VZ
1357}
1358
1359void WXDLLEXPORT wxMutexGuiLeave()
1360{
b568d04f 1361 wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
bee503b0
VZ
1362
1363 if ( wxThread::IsMain() )
1364 {
b568d04f 1365 gs_bGuiOwnedByMainThread = FALSE;
bee503b0
VZ
1366 }
1367 else
1368 {
4d1c1c3c 1369 // decrement the number of threads waiting for GUI access now
b568d04f 1370 wxASSERT_MSG( gs_nWaitingForGui > 0,
223d09f6 1371 wxT("calling wxMutexGuiLeave() without entering it first?") );
bee503b0 1372
b568d04f 1373 gs_nWaitingForGui--;
bee503b0
VZ
1374
1375 wxWakeUpMainThread();
1376 }
1377
b568d04f 1378 gs_critsectGui->Leave();
bee503b0
VZ
1379}
1380
1381void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
1382{
1383 wxASSERT_MSG( wxThread::IsMain(),
223d09f6 1384 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
bee503b0 1385
b568d04f 1386 wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
bee503b0 1387
b568d04f 1388 if ( gs_nWaitingForGui == 0 )
bee503b0
VZ
1389 {
1390 // no threads are waiting for GUI - so we may acquire the lock without
1391 // any danger (but only if we don't already have it)
1392 if ( !wxGuiOwnedByMainThread() )
1393 {
b568d04f 1394 gs_critsectGui->Enter();
bee503b0 1395
b568d04f 1396 gs_bGuiOwnedByMainThread = TRUE;
bee503b0
VZ
1397 }
1398 //else: already have it, nothing to do
1399 }
1400 else
1401 {
1402 // some threads are waiting, release the GUI lock if we have it
1403 if ( wxGuiOwnedByMainThread() )
1404 {
1405 wxMutexGuiLeave();
1406 }
1407 //else: some other worker thread is doing GUI
1408 }
1409}
1410
1411bool WXDLLEXPORT wxGuiOwnedByMainThread()
1412{
b568d04f 1413 return gs_bGuiOwnedByMainThread;
bee503b0
VZ
1414}
1415
1416// wake up the main thread if it's in ::GetMessage()
1417void WXDLLEXPORT wxWakeUpMainThread()
1418{
1419 // sending any message would do - hopefully WM_NULL is harmless enough
b568d04f 1420 if ( !::PostThreadMessage(gs_idMainThread, WM_NULL, 0, 0) )
bee503b0
VZ
1421 {
1422 // should never happen
f6bcfd97 1423 wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
bee503b0 1424 }
3222fde2 1425}
d524867f 1426
bf1852e1
VZ
1427bool WXDLLEXPORT wxIsWaitingForThread()
1428{
b568d04f 1429 return gs_waitingForThread;
bf1852e1
VZ
1430}
1431
3222fde2 1432#endif // wxUSE_THREADS
6fe73788
RL
1433
1434// vi:sts=4:sw=4:et