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