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