1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxThread Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to make it work :-)
8 // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998),
9 // Vadim Zeitlin (1999)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "thread.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
24 #if defined(__BORLANDC__)
34 #include "wx/msw/private.h"
36 #include "wx/module.h"
37 #include "wx/thread.h"
39 // must have this symbol defined to get _beginthread/_endthread declarations
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the possible states of the thread ("=>" shows all possible transitions from
56 STATE_NEW
, // didn't start execution yet (=> RUNNING)
57 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
58 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
59 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
60 STATE_EXITED
// thread is terminating
63 // ----------------------------------------------------------------------------
64 // this module globals
65 // ----------------------------------------------------------------------------
67 // TLS index of the slot where we store the pointer to the current thread
68 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
70 // id of the main thread - the one which can call GUI functions without first
71 // calling wxMutexGuiEnter()
72 static DWORD gs_idMainThread
= 0;
74 // if it's FALSE, some secondary thread is holding the GUI lock
75 static bool gs_bGuiOwnedByMainThread
= TRUE
;
77 // critical section which controls access to all GUI functions: any secondary
78 // thread (i.e. except the main one) must enter this crit section before doing
80 static wxCriticalSection
*gs_critsectGui
= NULL
;
82 // critical section which protects gs_nWaitingForGui variable
83 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
85 // number of threads waiting for GUI in wxMutexGuiEnter()
86 static size_t gs_nWaitingForGui
= 0;
88 // are we waiting for a thread termination?
89 static bool gs_waitingForThread
= FALSE
;
91 // ============================================================================
92 // Windows implementation of thread classes
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxMutex implementation
97 // ----------------------------------------------------------------------------
107 p_internal
= new wxMutexInternal
;
108 p_internal
->p_mutex
= CreateMutex(NULL
, FALSE
, NULL
);
109 if ( !p_internal
->p_mutex
)
111 wxLogSysError(_("Can not create mutex."));
120 wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked
);
121 CloseHandle(p_internal
->p_mutex
);
124 wxMutexError
wxMutex::Lock()
128 ret
= WaitForSingleObject(p_internal
->p_mutex
, INFINITE
);
139 wxLogSysError(_("Couldn't acquire a mutex lock"));
140 return wxMUTEX_MISC_ERROR
;
144 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
148 return wxMUTEX_NO_ERROR
;
151 wxMutexError
wxMutex::TryLock()
155 ret
= WaitForSingleObject(p_internal
->p_mutex
, 0);
156 if (ret
== WAIT_TIMEOUT
|| ret
== WAIT_ABANDONED
)
160 return wxMUTEX_NO_ERROR
;
163 wxMutexError
wxMutex::Unlock()
168 BOOL ret
= ReleaseMutex(p_internal
->p_mutex
);
171 wxLogSysError(_("Couldn't release a mutex"));
172 return wxMUTEX_MISC_ERROR
;
175 return wxMUTEX_NO_ERROR
;
178 // ----------------------------------------------------------------------------
179 // wxCondition implementation
180 // ----------------------------------------------------------------------------
182 class wxConditionInternal
185 wxConditionInternal()
187 event
= ::CreateEvent(
188 NULL
, // default secutiry
189 FALSE
, // not manual reset
190 FALSE
, // nonsignaled initially
191 NULL
// nameless event
195 wxLogSysError(_("Can not create event object."));
200 bool Wait(wxMutex
& mutex
, DWORD timeout
)
205 // FIXME this should be MsgWaitForMultipleObjects() as well probably
206 DWORD rc
= ::WaitForSingleObject(event
, timeout
);
211 return rc
!= WAIT_TIMEOUT
;
214 ~wxConditionInternal()
218 if ( !::CloseHandle(event
) )
220 wxLogLastError("CloseHandle(event)");
229 wxCondition::wxCondition()
231 p_internal
= new wxConditionInternal
;
234 wxCondition::~wxCondition()
239 void wxCondition::Wait(wxMutex
& mutex
)
241 (void)p_internal
->Wait(mutex
, INFINITE
);
244 bool wxCondition::Wait(wxMutex
& mutex
,
248 return p_internal
->Wait(mutex
, sec
*1000 + nsec
/1000000);
251 void wxCondition::Signal()
253 // set the event to signaled: if a thread is already waiting on it, it will
254 // be woken up, otherwise the event will remain in the signaled state until
255 // someone waits on it. In any case, the system will return it to a non
256 // signalled state afterwards. If multiple threads are waiting, only one
258 if ( !::SetEvent(p_internal
->event
) )
260 wxLogLastError("SetEvent");
264 void wxCondition::Broadcast()
266 // this works because all these threads are already waiting and so each
267 // SetEvent() inside Signal() is really a PulseEvent() because the event
268 // state is immediately returned to non-signaled
269 for ( int i
= 0; i
< p_internal
->waiters
; i
++ )
275 // ----------------------------------------------------------------------------
276 // wxCriticalSection implementation
277 // ----------------------------------------------------------------------------
279 wxCriticalSection::wxCriticalSection()
281 wxASSERT_MSG( sizeof(CRITICAL_SECTION
) <= sizeof(m_buffer
),
282 _T("must increase buffer size in wx/thread.h") );
284 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
287 wxCriticalSection::~wxCriticalSection()
289 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
292 void wxCriticalSection::Enter()
294 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
297 void wxCriticalSection::Leave()
299 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
302 // ----------------------------------------------------------------------------
303 // wxThread implementation
304 // ----------------------------------------------------------------------------
306 // wxThreadInternal class
307 // ----------------------
309 class wxThreadInternal
316 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
328 if ( !::CloseHandle(m_hThread
) )
330 wxLogLastError("CloseHandle(thread)");
337 // create a new (suspended) thread (for the given thread object)
338 bool Create(wxThread
*thread
);
340 // suspend/resume/terminate
343 void Cancel() { m_state
= STATE_CANCELED
; }
346 void SetState(wxThreadState state
) { m_state
= state
; }
347 wxThreadState
GetState() const { return m_state
; }
350 void SetPriority(unsigned int priority
);
351 unsigned int GetPriority() const { return m_priority
; }
353 // thread handle and id
354 HANDLE
GetHandle() const { return m_hThread
; }
355 DWORD
GetId() const { return m_tid
; }
358 static DWORD
WinThreadStart(wxThread
*thread
);
361 HANDLE m_hThread
; // handle of the thread
362 wxThreadState m_state
; // state, see wxThreadState enum
363 unsigned int m_priority
; // thread priority in "wx" units
364 DWORD m_tid
; // thread id
367 DWORD
wxThreadInternal::WinThreadStart(wxThread
*thread
)
369 // store the thread object in the TLS
370 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
372 wxLogSysError(_("Can not start thread: error writing TLS."));
377 DWORD rc
= (DWORD
)thread
->Entry();
379 // enter m_critsect before changing the thread state
380 thread
->m_critsect
.Enter();
381 bool wasCancelled
= thread
->p_internal
->GetState() == STATE_CANCELED
;
382 thread
->p_internal
->SetState(STATE_EXITED
);
383 thread
->m_critsect
.Leave();
387 // if the thread was cancelled (from Delete()), then it the handle is still
389 if ( thread
->IsDetached() && !wasCancelled
)
394 //else: the joinable threads handle will be closed when Wait() is done
399 void wxThreadInternal::SetPriority(unsigned int priority
)
401 m_priority
= priority
;
403 // translate wxWindows priority to the Windows one
405 if (m_priority
<= 20)
406 win_priority
= THREAD_PRIORITY_LOWEST
;
407 else if (m_priority
<= 40)
408 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
409 else if (m_priority
<= 60)
410 win_priority
= THREAD_PRIORITY_NORMAL
;
411 else if (m_priority
<= 80)
412 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
413 else if (m_priority
<= 100)
414 win_priority
= THREAD_PRIORITY_HIGHEST
;
417 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
418 win_priority
= THREAD_PRIORITY_NORMAL
;
421 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
423 wxLogSysError(_("Can't set thread priority"));
427 bool wxThreadInternal::Create(wxThread
*thread
)
429 // for compilers which have it, we should use C RTL function for thread
430 // creation instead of Win32 API one because otherwise we will have memory
431 // leaks if the thread uses C RTL (and most threads do)
433 typedef unsigned (__stdcall
*RtlThreadStart
)(void *);
435 m_hThread
= (HANDLE
)_beginthreadex(NULL
, 0,
437 wxThreadInternal::WinThreadStart
,
438 thread
, CREATE_SUSPENDED
,
439 (unsigned int *)&m_tid
);
441 m_hThread
= ::CreateThread
443 NULL
, // default security
444 0, // default stack size
445 (LPTHREAD_START_ROUTINE
) // thread entry point
446 wxThreadInternal::WinThreadStart
, //
447 (LPVOID
)thread
, // parameter
448 CREATE_SUSPENDED
, // flags
449 &m_tid
// [out] thread id
453 if ( m_hThread
== NULL
)
455 wxLogSysError(_("Can't create thread"));
460 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
462 SetPriority(m_priority
);
468 bool wxThreadInternal::Suspend()
470 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
471 if ( nSuspendCount
== (DWORD
)-1 )
473 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
478 m_state
= STATE_PAUSED
;
483 bool wxThreadInternal::Resume()
485 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
486 if ( nSuspendCount
== (DWORD
)-1 )
488 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
493 m_state
= STATE_RUNNING
;
501 wxThread
*wxThread::This()
503 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
505 // be careful, 0 may be a valid return value as well
506 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
508 wxLogSysError(_("Couldn't get the current thread pointer"));
516 bool wxThread::IsMain()
518 return ::GetCurrentThreadId() == gs_idMainThread
;
521 void wxThread::Yield()
523 // 0 argument to Sleep() is special and means to just give away the rest of
528 void wxThread::Sleep(unsigned long milliseconds
)
530 ::Sleep(milliseconds
);
536 wxThread::wxThread(wxThreadKind kind
)
538 p_internal
= new wxThreadInternal();
540 m_isDetached
= kind
== wxTHREAD_DETACHED
;
543 wxThread::~wxThread()
548 // create/start thread
549 // -------------------
551 wxThreadError
wxThread::Create()
553 wxCriticalSectionLocker
lock(m_critsect
);
555 if ( !p_internal
->Create(this) )
556 return wxTHREAD_NO_RESOURCE
;
558 return wxTHREAD_NO_ERROR
;
561 wxThreadError
wxThread::Run()
563 wxCriticalSectionLocker
lock(m_critsect
);
565 if ( p_internal
->GetState() != STATE_NEW
)
567 // actually, it may be almost any state at all, not only STATE_RUNNING
568 return wxTHREAD_RUNNING
;
571 // the thread has just been created and is still suspended - let it run
575 // suspend/resume thread
576 // ---------------------
578 wxThreadError
wxThread::Pause()
580 wxCriticalSectionLocker
lock(m_critsect
);
582 return p_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
585 wxThreadError
wxThread::Resume()
587 wxCriticalSectionLocker
lock(m_critsect
);
589 return p_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
595 wxThread::ExitCode
wxThread::Wait()
597 // although under Windows we can wait for any thread, it's an error to
598 // wait for a detached one in wxWin API
599 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
600 _T("can't wait for detached thread") );
602 ExitCode rc
= (ExitCode
)-1;
611 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
615 // Delete() is always safe to call, so consider all possible states
619 HANDLE hThread
= p_internal
->GetHandle();
625 // set flag for wxIsWaitingForThread()
626 gs_waitingForThread
= TRUE
;
633 // ask the thread to terminate
635 wxCriticalSectionLocker
lock(m_critsect
);
637 p_internal
->Cancel();
641 // we can't just wait for the thread to terminate because it might be
642 // calling some GUI functions and so it will never terminate before we
643 // process the Windows messages that result from these functions
647 result
= ::MsgWaitForMultipleObjects
649 1, // number of objects to wait for
650 &hThread
, // the objects
651 FALSE
, // don't wait for all objects
652 INFINITE
, // no timeout
653 QS_ALLEVENTS
// return as soon as there are any events
660 wxLogSysError(_("Can not wait for thread termination"));
662 return wxTHREAD_KILLED
;
665 // thread we're waiting for terminated
668 case WAIT_OBJECT_0
+ 1:
669 // new message arrived, process it
670 if ( !wxTheApp
->DoMessage() )
672 // WM_QUIT received: kill the thread
675 return wxTHREAD_KILLED
;
680 // give the thread we're waiting for chance to exit
681 // from the GUI call it might have been in
682 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
691 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
693 } while ( result
!= WAIT_OBJECT_0
);
695 // simply wait for the thread to terminate
697 // OTOH, even console apps create windows (in wxExecute, for WinSock
698 // &c), so may be use MsgWaitForMultipleObject() too here?
699 if ( WaitForSingleObject(hThread
, INFINITE
) != WAIT_OBJECT_0
)
701 wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
703 #endif // wxUSE_GUI/!wxUSE_GUI
707 gs_waitingForThread
= FALSE
;
715 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
717 wxLogLastError("GetExitCodeThread");
724 // if the thread exits normally, this is done in WinThreadStart, but in
725 // this case it would have been too early because
726 // MsgWaitForMultipleObject() would fail if the therad handle was
727 // closed while we were waiting on it, so we must do it here
731 wxASSERT_MSG( (DWORD
)rc
!= STILL_ACTIVE
,
732 wxT("thread must be already terminated.") );
737 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
740 wxThreadError
wxThread::Kill()
743 return wxTHREAD_NOT_RUNNING
;
745 if ( !::TerminateThread(p_internal
->GetHandle(), (DWORD
)-1) )
747 wxLogSysError(_("Couldn't terminate thread"));
749 return wxTHREAD_MISC_ERROR
;
759 return wxTHREAD_NO_ERROR
;
762 void wxThread::Exit(ExitCode status
)
772 _endthreadex((unsigned)status
);
774 ::ExitThread((DWORD
)status
);
777 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
783 void wxThread::SetPriority(unsigned int prio
)
785 wxCriticalSectionLocker
lock(m_critsect
);
787 p_internal
->SetPriority(prio
);
790 unsigned int wxThread::GetPriority() const
792 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
794 return p_internal
->GetPriority();
797 unsigned long wxThread::GetId() const
799 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
801 return (unsigned long)p_internal
->GetId();
804 bool wxThread::IsRunning() const
806 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
808 return p_internal
->GetState() == STATE_RUNNING
;
811 bool wxThread::IsAlive() const
813 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
815 return (p_internal
->GetState() == STATE_RUNNING
) ||
816 (p_internal
->GetState() == STATE_PAUSED
);
819 bool wxThread::IsPaused() const
821 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
823 return p_internal
->GetState() == STATE_PAUSED
;
826 bool wxThread::TestDestroy()
828 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
830 return p_internal
->GetState() == STATE_CANCELED
;
833 // ----------------------------------------------------------------------------
834 // Automatic initialization for thread module
835 // ----------------------------------------------------------------------------
837 class wxThreadModule
: public wxModule
840 virtual bool OnInit();
841 virtual void OnExit();
844 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
847 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
849 bool wxThreadModule::OnInit()
851 // allocate TLS index for storing the pointer to the current thread
852 gs_tlsThisThread
= ::TlsAlloc();
853 if ( gs_tlsThisThread
== 0xFFFFFFFF )
855 // in normal circumstances it will only happen if all other
856 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
857 // words, this should never happen
858 wxLogSysError(_("Thread module initialization failed: "
859 "impossible to allocate index in thread "
865 // main thread doesn't have associated wxThread object, so store 0 in the
867 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
869 ::TlsFree(gs_tlsThisThread
);
870 gs_tlsThisThread
= 0xFFFFFFFF;
872 wxLogSysError(_("Thread module initialization failed: "
873 "can not store value in thread local storage"));
878 gs_critsectWaitingForGui
= new wxCriticalSection();
880 gs_critsectGui
= new wxCriticalSection();
881 gs_critsectGui
->Enter();
883 // no error return for GetCurrentThreadId()
884 gs_idMainThread
= ::GetCurrentThreadId();
889 void wxThreadModule::OnExit()
891 if ( !::TlsFree(gs_tlsThisThread
) )
893 wxLogLastError("TlsFree failed.");
896 if ( gs_critsectGui
)
898 gs_critsectGui
->Leave();
899 delete gs_critsectGui
;
900 gs_critsectGui
= NULL
;
903 delete gs_critsectWaitingForGui
;
904 gs_critsectWaitingForGui
= NULL
;
907 // ----------------------------------------------------------------------------
908 // under Windows, these functions are implemented using a critical section and
909 // not a mutex, so the names are a bit confusing
910 // ----------------------------------------------------------------------------
912 void WXDLLEXPORT
wxMutexGuiEnter()
914 // this would dead lock everything...
915 wxASSERT_MSG( !wxThread::IsMain(),
916 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
918 // the order in which we enter the critical sections here is crucial!!
920 // set the flag telling to the main thread that we want to do some GUI
922 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
927 wxWakeUpMainThread();
929 // now we may block here because the main thread will soon let us in
930 // (during the next iteration of OnIdle())
931 gs_critsectGui
->Enter();
934 void WXDLLEXPORT
wxMutexGuiLeave()
936 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
938 if ( wxThread::IsMain() )
940 gs_bGuiOwnedByMainThread
= FALSE
;
944 // decrement the number of waiters now
945 wxASSERT_MSG( gs_nWaitingForGui
> 0,
946 wxT("calling wxMutexGuiLeave() without entering it first?") );
950 wxWakeUpMainThread();
953 gs_critsectGui
->Leave();
956 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
958 wxASSERT_MSG( wxThread::IsMain(),
959 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
961 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
963 if ( gs_nWaitingForGui
== 0 )
965 // no threads are waiting for GUI - so we may acquire the lock without
966 // any danger (but only if we don't already have it)
967 if ( !wxGuiOwnedByMainThread() )
969 gs_critsectGui
->Enter();
971 gs_bGuiOwnedByMainThread
= TRUE
;
973 //else: already have it, nothing to do
977 // some threads are waiting, release the GUI lock if we have it
978 if ( wxGuiOwnedByMainThread() )
982 //else: some other worker thread is doing GUI
986 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
988 return gs_bGuiOwnedByMainThread
;
991 // wake up the main thread if it's in ::GetMessage()
992 void WXDLLEXPORT
wxWakeUpMainThread()
994 // sending any message would do - hopefully WM_NULL is harmless enough
995 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
997 // should never happen
998 wxLogLastError("PostThreadMessage(WM_NULL)");
1002 bool WXDLLEXPORT
wxIsWaitingForThread()
1004 return gs_waitingForThread
;
1007 #endif // wxUSE_THREADS