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