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