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