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