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