1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxThread Implementation. For Unix ports, see e.g. src/gtk
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by: David Webster
8 // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/module.h"
27 #include "wx/thread.h"
29 // the possible states of the thread ("=>" shows all possible transitions from
33 STATE_NEW
, // didn't start execution yet (=> RUNNING)
34 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
35 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
36 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
37 STATE_EXITED
// thread is terminating
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // TLS index of the slot where we store the pointer to the current thread
45 static DWORD s_tlsThisThread
= 0xFFFFFFFF;
47 // id of the main thread - the one which can call GUI functions without first
48 // calling wxMutexGuiEnter()
49 static DWORD s_idMainThread
= 0;
51 // if it's FALSE, some secondary thread is holding the GUI lock
52 static bool s_bGuiOwnedByMainThread
= TRUE
;
54 // critical section which controls access to all GUI functions: any secondary
55 // thread (i.e. except the main one) must enter this crit section before doing
57 static wxCriticalSection
*s_critsectGui
= NULL
;
59 // critical section which protects s_nWaitingForGui variable
60 static wxCriticalSection
*s_critsectWaitingForGui
= NULL
;
62 // number of threads waiting for GUI in wxMutexGuiEnter()
63 static size_t s_nWaitingForGui
= 0;
65 // are we waiting for a thread termination?
66 static bool s_waitingForThread
= FALSE
;
68 // ============================================================================
69 // Windows implementation of thread classes
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wxMutex implementation
74 // ----------------------------------------------------------------------------
83 p_internal
= new wxMutexInternal
;
84 // p_internal->p_mutex = CreateMutex(NULL, FALSE, NULL);
85 if ( !p_internal
->p_mutex
)
87 wxLogSysError(_("Can not create mutex."));
96 wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked
);
97 // CloseHandle(p_internal->p_mutex);
100 wxMutexError
wxMutex::Lock()
106 ret = WaitForSingleObject(p_internal->p_mutex, INFINITE);
117 wxLogSysError(_("Couldn't acquire a mutex lock"));
118 return wxMUTEX_MISC_ERROR;
122 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
127 return wxMUTEX_NO_ERROR
;
130 wxMutexError
wxMutex::TryLock()
136 ret = WaitForSingleObject(p_internal->p_mutex, 0);
137 if (ret == WAIT_TIMEOUT || ret == WAIT_ABANDONED)
142 return wxMUTEX_NO_ERROR
;
145 wxMutexError
wxMutex::Unlock()
150 BOOL ret
= 0; // TODO: ReleaseMutex(p_internal->p_mutex);
153 wxLogSysError(_("Couldn't release a mutex"));
154 return wxMUTEX_MISC_ERROR
;
157 return wxMUTEX_NO_ERROR
;
160 // ----------------------------------------------------------------------------
161 // wxCondition implementation
162 // ----------------------------------------------------------------------------
164 class wxConditionInternal
171 wxCondition::wxCondition()
173 p_internal
= new wxConditionInternal
;
176 p_internal->event = CreateEvent(NULL, FALSE, FALSE, NULL);
177 if ( !p_internal->event )
179 wxLogSysError(_("Can not create event object."));
182 p_internal
->waiters
= 0;
185 wxCondition::~wxCondition()
187 // CloseHandle(p_internal->event);
190 void wxCondition::Wait(wxMutex
& mutex
)
193 p_internal
->waiters
++;
194 // WaitForSingleObject(p_internal->event, INFINITE);
195 p_internal
->waiters
--;
199 bool wxCondition::Wait(wxMutex
& mutex
,
206 p_internal
->waiters
++;
207 // ret = WaitForSingleObject(p_internal->event, (sec*1000)+(nsec/1000000));
208 p_internal
->waiters
--;
211 // return (ret != WAIT_TIMEOUT);
215 void wxCondition::Signal()
217 // SetEvent(p_internal->event);
220 void wxCondition::Broadcast()
226 for (i=0;i<p_internal->waiters;i++)
228 if ( SetEvent(p_internal->event) == 0 )
230 wxLogSysError(_("Couldn't change the state of event object."));
236 // ----------------------------------------------------------------------------
237 // wxCriticalSection implementation
238 // ----------------------------------------------------------------------------
239 #define CRITICAL_SECTION ULONG
240 class wxCriticalSectionInternal
243 // init the critical section object
244 wxCriticalSectionInternal()
245 { //::InitializeCriticalSection(&m_data);
248 // implicit cast to the associated data
249 operator CRITICAL_SECTION
*() { return &m_data
; }
251 // free the associated ressources
252 ~wxCriticalSectionInternal()
253 { //::DeleteCriticalSection(&m_data);
257 CRITICAL_SECTION m_data
;
260 wxCriticalSection::wxCriticalSection()
262 m_critsect
= new wxCriticalSectionInternal
;
265 wxCriticalSection::~wxCriticalSection()
270 void wxCriticalSection::Enter()
272 //TODO: ::EnterCriticalSection(*m_critsect);
275 void wxCriticalSection::Leave()
277 // TODO: ::LeaveCriticalSection(*m_critsect);
280 // ----------------------------------------------------------------------------
281 // wxThread implementation
282 // ----------------------------------------------------------------------------
284 // wxThreadInternal class
285 // ----------------------
287 class wxThreadInternal
294 m_priority
= 0; // TODO: WXTHREAD_DEFAULT_PRIORITY;
297 // create a new (suspended) thread (for the given thread object)
298 bool Create(wxThread
*thread
);
300 // suspend/resume/terminate
303 void Cancel() { m_state
= STATE_CANCELED
; }
306 void SetState(wxThreadState state
) { m_state
= state
; }
307 wxThreadState
GetState() const { return m_state
; }
310 void SetPriority(unsigned int priority
) { m_priority
= priority
; }
311 unsigned int GetPriority() const { return m_priority
; }
313 // thread handle and id
314 HANDLE
GetHandle() const { return m_hThread
; }
315 DWORD
GetId() const { return m_tid
; }
318 static DWORD
OS2ThreadStart(wxThread
*thread
);
321 HANDLE m_hThread
; // handle of the thread
322 wxThreadState m_state
; // state, see wxThreadState enum
323 unsigned int m_priority
; // thread priority in "wx" units
324 DWORD m_tid
; // thread id
327 DWORD
wxThreadInternal::OS2ThreadStart(wxThread
*thread
)
329 // store the thread object in the TLS
332 if ( !::TlsSetValue(s_tlsThisThread, thread) )
334 wxLogSysError(_("Can not start thread: error writing TLS."));
339 DWORD ret
= (DWORD
)thread
->Entry();
340 thread
->p_internal
->SetState(STATE_EXITED
);
348 bool wxThreadInternal::Create(wxThread
*thread
)
352 m_hThread = ::CreateThread
354 NULL, // default security
355 0, // default stack size
356 (LPTHREAD_START_ROUTINE) // thread entry point
357 wxThreadInternal::OS2ThreadStart, //
358 (LPVOID)thread, // parameter
359 CREATE_SUSPENDED, // flags
360 &m_tid // [out] thread id
363 if ( m_hThread == NULL )
365 wxLogSysError(_("Can't create thread"));
370 // translate wxWindows priority to the Windows one
372 if (m_priority <= 20)
373 win_priority = THREAD_PRIORITY_LOWEST;
374 else if (m_priority <= 40)
375 win_priority = THREAD_PRIORITY_BELOW_NORMAL;
376 else if (m_priority <= 60)
377 win_priority = THREAD_PRIORITY_NORMAL;
378 else if (m_priority <= 80)
379 win_priority = THREAD_PRIORITY_ABOVE_NORMAL;
380 else if (m_priority <= 100)
381 win_priority = THREAD_PRIORITY_HIGHEST;
384 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
385 win_priority = THREAD_PRIORITY_NORMAL;
388 if ( ::SetThreadPriority(m_hThread, win_priority) == 0 )
390 wxLogSysError(_("Can't set thread priority"));
396 bool wxThreadInternal::Suspend()
400 DWORD nSuspendCount = ::SuspendThread(m_hThread);
401 if ( nSuspendCount == (DWORD)-1 )
403 wxLogSysError(_("Can not suspend thread %x"), m_hThread);
408 m_state = STATE_PAUSED;
413 bool wxThreadInternal::Resume()
417 DWORD nSuspendCount = ::ResumeThread(m_hThread);
418 if ( nSuspendCount == (DWORD)-1 )
420 wxLogSysError(_("Can not resume thread %x"), m_hThread);
425 m_state = STATE_RUNNING;
433 wxThread
*wxThread::This()
435 wxThread
*thread
= NULL
; // TODO (wxThread *)::TlsGetValue(s_tlsThisThread);
439 // be careful, 0 may be a valid return value as well
440 if ( !thread && (::GetLastError() != NO_ERROR) )
442 wxLogSysError(_("Couldn't get the current thread pointer"));
450 bool wxThread::IsMain()
452 // TODO: return ::GetCurrentThreadId() == s_idMainThread;
460 void wxThread::Yield()
462 // 0 argument to Sleep() is special
466 void wxThread::Sleep(unsigned long milliseconds
)
468 ::DosSleep(milliseconds
);
471 // create/start thread
472 // -------------------
474 wxThreadError
wxThread::Create()
476 if ( !p_internal
->Create(this) )
477 return wxTHREAD_NO_RESOURCE
;
479 return wxTHREAD_NO_ERROR
;
482 wxThreadError
wxThread::Run()
484 wxCriticalSectionLocker
lock(m_critsect
);
486 if ( p_internal
->GetState() != STATE_NEW
)
488 // actually, it may be almost any state at all, not only STATE_RUNNING
489 return wxTHREAD_RUNNING
;
495 // suspend/resume thread
496 // ---------------------
498 wxThreadError
wxThread::Pause()
500 wxCriticalSectionLocker
lock(m_critsect
);
502 return p_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
505 wxThreadError
wxThread::Resume()
507 wxCriticalSectionLocker
lock(m_critsect
);
509 return p_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
515 wxThread::ExitCode
wxThread::Delete()
519 // Delete() is always safe to call, so consider all possible states
527 // set flag for wxIsWaitingForThread()
528 s_waitingForThread
= TRUE
;
535 wxCriticalSectionLocker
lock(m_critsect
);
537 p_internal
->Cancel();
538 hThread
= p_internal
->GetHandle();
541 // we can't just wait for the thread to terminate because it might be
542 // calling some GUI functions and so it will never terminate before we
543 // process the Windows messages that result from these functions
549 result = ::MsgWaitForMultipleObjects
551 1, // number of objects to wait for
552 &hThread, // the objects
553 FALSE, // don't wait for all objects
554 INFINITE, // no timeout
555 QS_ALLEVENTS // return as soon as there are any events
562 wxLogSysError(_("Can not wait for thread termination"));
567 // thread we're waiting for terminated
570 case WAIT_OBJECT_0 + 1:
571 // new message arrived, process it
572 if ( !wxTheApp->DoMessage() )
574 // WM_QUIT received: kill the thread
582 // give the thread we're waiting for chance to exit
583 // from the GUI call it might have been in
584 if ( (s_nWaitingForGui > 0) && wxGuiOwnedByMainThread() )
593 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
595 } while ( result != WAIT_OBJECT_0 );
599 s_waitingForThread
= FALSE
;
606 if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) )
608 wxLogLastError("GetExitCodeThread");
613 wxASSERT_MSG( (LPVOID)rc != (LPVOID)STILL_ACTIVE,
614 wxT("thread must be already terminated.") );
616 ::CloseHandle(hThread);
623 wxThreadError
wxThread::Kill()
626 return wxTHREAD_NOT_RUNNING
;
630 if ( !::TerminateThread(p_internal->GetHandle(), (DWORD)-1) )
632 wxLogSysError(_("Couldn't terminate thread"));
634 return wxTHREAD_MISC_ERROR;
639 return wxTHREAD_NO_ERROR
;
642 void wxThread::Exit(void *status
)
646 // TODO: ::ExitThread((DWORD)status);
648 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
651 void wxThread::SetPriority(unsigned int prio
)
653 wxCriticalSectionLocker
lock(m_critsect
);
655 p_internal
->SetPriority(prio
);
658 unsigned int wxThread::GetPriority() const
660 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
662 return p_internal
->GetPriority();
665 unsigned long wxThread::GetID() const
667 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
669 return (unsigned long)p_internal
->GetId();
672 bool wxThread::IsRunning() const
674 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
676 return p_internal
->GetState() == STATE_RUNNING
;
679 bool wxThread::IsAlive() const
681 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
683 return (p_internal
->GetState() == STATE_RUNNING
) ||
684 (p_internal
->GetState() == STATE_PAUSED
);
687 bool wxThread::IsPaused() const
689 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
691 return (p_internal
->GetState() == STATE_PAUSED
);
694 bool wxThread::TestDestroy()
696 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
698 return p_internal
->GetState() == STATE_CANCELED
;
703 p_internal
= new wxThreadInternal();
706 wxThread::~wxThread()
711 // ----------------------------------------------------------------------------
712 // Automatic initialization for thread module
713 // ----------------------------------------------------------------------------
715 class wxThreadModule
: public wxModule
718 virtual bool OnInit();
719 virtual void OnExit();
722 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
725 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
727 bool wxThreadModule::OnInit()
729 // allocate TLS index for storing the pointer to the current thread
732 s_tlsThisThread = ::TlsAlloc();
733 if ( s_tlsThisThread == 0xFFFFFFFF )
735 // in normal circumstances it will only happen if all other
736 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
737 // words, this should never happen
738 wxLogSysError(_("Thread module initialization failed: "
739 "impossible to allocate index in thread "
745 // main thread doesn't have associated wxThread object, so store 0 in the
750 if ( !::TlsSetValue(s_tlsThisThread, (LPVOID)0) )
752 ::TlsFree(s_tlsThisThread);
753 s_tlsThisThread = 0xFFFFFFFF;
755 wxLogSysError(_("Thread module initialization failed: "
756 "can not store value in thread local storage"));
761 s_critsectWaitingForGui
= new wxCriticalSection();
763 s_critsectGui
= new wxCriticalSection();
764 s_critsectGui
->Enter();
766 // no error return for GetCurrentThreadId()
767 // s_idMainThread = ::GetCurrentThreadId();
772 void wxThreadModule::OnExit()
776 if ( !::TlsFree(s_tlsThisThread) )
778 wxLogLastError("TlsFree failed.");
783 s_critsectGui
->Leave();
784 delete s_critsectGui
;
785 s_critsectGui
= NULL
;
788 wxDELETE(s_critsectWaitingForGui
);
791 // ----------------------------------------------------------------------------
792 // under Windows, these functions are implemented usign a critical section and
793 // not a mutex, so the names are a bit confusing
794 // ----------------------------------------------------------------------------
796 void WXDLLEXPORT
wxMutexGuiEnter()
798 // this would dead lock everything...
799 wxASSERT_MSG( !wxThread::IsMain(),
800 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
802 // the order in which we enter the critical sections here is crucial!!
804 // set the flag telling to the main thread that we want to do some GUI
806 wxCriticalSectionLocker
enter(*s_critsectWaitingForGui
);
811 wxWakeUpMainThread();
813 // now we may block here because the main thread will soon let us in
814 // (during the next iteration of OnIdle())
815 s_critsectGui
->Enter();
818 void WXDLLEXPORT
wxMutexGuiLeave()
820 wxCriticalSectionLocker
enter(*s_critsectWaitingForGui
);
822 if ( wxThread::IsMain() )
824 s_bGuiOwnedByMainThread
= FALSE
;
828 // decrement the number of waiters now
829 wxASSERT_MSG( s_nWaitingForGui
> 0,
830 wxT("calling wxMutexGuiLeave() without entering it first?") );
834 wxWakeUpMainThread();
837 s_critsectGui
->Leave();
840 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
842 wxASSERT_MSG( wxThread::IsMain(),
843 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
845 wxCriticalSectionLocker
enter(*s_critsectWaitingForGui
);
847 if ( s_nWaitingForGui
== 0 )
849 // no threads are waiting for GUI - so we may acquire the lock without
850 // any danger (but only if we don't already have it)
851 if ( !wxGuiOwnedByMainThread() )
853 s_critsectGui
->Enter();
855 s_bGuiOwnedByMainThread
= TRUE
;
857 //else: already have it, nothing to do
861 // some threads are waiting, release the GUI lock if we have it
862 if ( wxGuiOwnedByMainThread() )
866 //else: some other worker thread is doing GUI
870 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
872 return s_bGuiOwnedByMainThread
;
875 // wake up the main thread if it's in ::GetMessage()
876 void WXDLLEXPORT
wxWakeUpMainThread()
878 // sending any message would do - hopefully WM_NULL is harmless enough
881 if ( !::PostThreadMessage(s_idMainThread, WM_NULL, 0, 0) )
883 // should never happen
884 wxLogLastError("PostThreadMessage(WM_NULL)");
889 bool WXDLLEXPORT
wxIsWaitingForThread()
891 return s_waitingForThread
;