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"
23 #include "wx/module.h"
24 #include "wx/thread.h"
26 #define INCL_DOSSEMAPHORES
27 #define INCL_DOSPROCESS
32 // the possible states of the thread ("=>" shows all possible transitions from
36 STATE_NEW
, // didn't start execution yet (=> RUNNING)
37 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
38 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
39 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
40 STATE_EXITED
// thread is terminating
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // id of the main thread - the one which can call GUI functions without first
48 // calling wxMutexGuiEnter()
49 static ULONG s_ulIdMainThread
= 0;
50 wxMutex
* p_wxMainMutex
;
52 // OS2 substitute for Tls pointer the current parent thread object
53 wxThread
* m_pThread
; // pointer to the wxWindows thread object
55 // if it's FALSE, some secondary thread is holding the GUI lock
56 static bool s_bGuiOwnedByMainThread
= TRUE
;
58 // critical section which controls access to all GUI functions: any secondary
59 // thread (i.e. except the main one) must enter this crit section before doing
61 static wxCriticalSection
*s_pCritsectGui
= NULL
;
63 // critical section which protects s_nWaitingForGui variable
64 static wxCriticalSection
*s_pCritsectWaitingForGui
= NULL
;
66 // number of threads waiting for GUI in wxMutexGuiEnter()
67 static size_t s_nWaitingForGui
= 0;
69 // are we waiting for a thread termination?
70 static bool s_bWaitingForThread
= FALSE
;
72 // ============================================================================
73 // OS/2 implementation of thread classes
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // wxMutex implementation
78 // ----------------------------------------------------------------------------
89 p_internal
= new wxMutexInternal
;
90 ulrc
= ::DosCreateMutexSem(NULL
, &p_internal
->m_vMutex
, 0L, FALSE
);
93 wxLogSysError(_("Can not create mutex."));
101 wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked
);
102 ::DosCloseMutexSem(p_internal
->m_vMutex
);
107 wxMutexError
wxMutex::Lock()
111 ulrc
= ::DosRequestMutexSem(p_internal
->m_vMutex
, SEM_INDEFINITE_WAIT
);
115 case ERROR_TOO_MANY_SEM_REQUESTS
:
122 case ERROR_INVALID_HANDLE
:
123 case ERROR_INTERRUPT
:
124 case ERROR_SEM_OWNER_DIED
:
125 wxLogSysError(_("Couldn't acquire a mutex lock"));
126 return wxMUTEX_MISC_ERROR
;
130 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
133 return wxMUTEX_NO_ERROR
;
136 wxMutexError
wxMutex::TryLock()
140 ulrc
= ::DosRequestMutexSem(p_internal
->m_vMutex
, SEM_IMMEDIATE_RETURN
/*0L*/);
141 if (ulrc
== ERROR_TIMEOUT
|| ulrc
== ERROR_TOO_MANY_SEM_REQUESTS
)
145 return wxMUTEX_NO_ERROR
;
148 wxMutexError
wxMutex::Unlock()
155 ulrc
= ::DosReleaseMutexSem(p_internal
->m_vMutex
);
158 wxLogSysError(_("Couldn't release a mutex"));
159 return wxMUTEX_MISC_ERROR
;
161 return wxMUTEX_NO_ERROR
;
164 // ----------------------------------------------------------------------------
165 // wxCondition implementation
166 // ----------------------------------------------------------------------------
168 class wxConditionInternal
175 wxCondition::wxCondition()
180 p_internal
= new wxConditionInternal
;
181 ulrc
= ::DosCreateEventSem(NULL
, &p_internal
->m_vEvent
, 0L, FALSE
);
184 wxLogSysError(_("Can not create event object."));
186 p_internal
->m_nWaiters
= 0;
187 // ?? just for good measure?
188 ::DosResetEventSem(p_internal
->m_vEvent
, &ulCount
);
191 wxCondition::~wxCondition()
193 ::DosCloseEventSem(p_internal
->m_vEvent
);
198 void wxCondition::Wait(
203 p_internal
->m_nWaiters
++;
204 ::DosWaitEventSem(p_internal
->m_vEvent
, SEM_INDEFINITE_WAIT
);
205 p_internal
->m_nWaiters
--;
209 bool wxCondition::Wait(
211 , unsigned long ulSec
212 , unsigned long ulMillisec
)
217 p_internal
->m_nWaiters
++;
218 ulrc
= ::DosWaitEventSem(p_internal
->m_vEvent
, ULONG((ulSec
* 1000L) + ulMillisec
));
219 p_internal
->m_nWaiters
--;
222 return (ulrc
!= ERROR_TIMEOUT
);
225 void wxCondition::Signal()
227 ::DosPostEventSem(p_internal
->m_vEvent
);
230 void wxCondition::Broadcast()
234 for (i
= 0; i
< p_internal
->m_nWaiters
; i
++)
236 if (::DosPostEventSem(p_internal
->m_vEvent
) != 0)
238 wxLogSysError(_("Couldn't change the state of event object."));
243 // ----------------------------------------------------------------------------
244 // wxCriticalSection implementation
245 // ----------------------------------------------------------------------------
247 void wxCriticalSection::Enter()
252 void wxCriticalSection::Leave()
257 // ----------------------------------------------------------------------------
258 // wxThread implementation
259 // ----------------------------------------------------------------------------
261 // wxThreadInternal class
262 // ----------------------
264 class wxThreadInternal
267 inline wxThreadInternal()
270 m_eState
= STATE_NEW
;
274 // create a new (suspended) thread (for the given thread object)
275 bool Create(wxThread
* pThread
);
277 // suspend/resume/terminate
280 inline void Cancel() { m_eState
= STATE_CANCELED
; }
283 inline void SetState(wxThreadState eState
) { m_eState
= eState
; }
284 inline wxThreadState
GetState() const { return m_eState
; }
287 inline void SetPriority(unsigned int nPriority
) { m_nPriority
= nPriority
; }
288 inline unsigned int GetPriority() const { return m_nPriority
; }
290 // thread handle and id
291 inline TID
GetHandle() const { return m_hThread
; }
292 TID
GetId() const { return m_hThread
; }
295 static DWORD
OS2ThreadStart(wxThread
*thread
);
298 // Threads in OS/2 have only an ID, so m_hThread is both it's handle and ID
299 // PM also has no real Tls mechanism to index pointers by so we'll just
300 // keep track of the wxWindows parent object here.
301 TID m_hThread
; // handle and ID of the thread
302 wxThreadState m_eState
; // state, see wxThreadState enum
303 unsigned int m_nPriority
; // thread priority in "wx" units
306 ULONG
wxThreadInternal::OS2ThreadStart(
312 DWORD dwRet
= (DWORD
)pThread
->Entry();
314 pThread
->p_internal
->SetState(STATE_EXITED
);
322 bool wxThreadInternal::Create(
328 ulrc
= ::DosCreateThread( &m_hThread
329 ,(PFNTHREAD
)wxThreadInternal::OS2ThreadStart
331 ,CREATE_SUSPENDED
| STACK_SPARSE
336 wxLogSysError(_("Can't create thread"));
341 // translate wxWindows priority to the PM one
342 ULONG ulOS2_Priority
;
344 if (m_nPriority
<= 20)
345 ulOS2_Priority
= PRTYC_NOCHANGE
;
346 else if (m_nPriority
<= 40)
347 ulOS2_Priority
= PRTYC_IDLETIME
;
348 else if (m_nPriority
<= 60)
349 ulOS2_Priority
= PRTYC_REGULAR
;
350 else if (m_nPriority
<= 80)
351 ulOS2_Priority
= PRTYC_TIMECRITICAL
;
352 else if (m_nPriority
<= 100)
353 ulOS2_Priority
= PRTYC_FOREGROUNDSERVER
;
356 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
357 ulOS2_Priority
= PRTYC_REGULAR
;
359 ulrc
= ::DosSetPriority( PRTYS_THREAD
366 wxLogSysError(_("Can't set thread priority"));
371 bool wxThreadInternal::Suspend()
373 ULONG ulrc
= ::DosSuspendThread(m_hThread
);
377 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
380 m_eState
= STATE_PAUSED
;
384 bool wxThreadInternal::Resume()
386 ULONG ulrc
= ::DosResumeThread(m_hThread
);
390 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
393 m_eState
= STATE_PAUSED
;
400 wxThread
*wxThread::This()
402 wxThread
* pThread
= m_pThread
;
406 bool wxThread::IsMain()
411 ::DosGetInfoBlocks(&ptib
, &ppib
);
413 if (ptib
->tib_ptib2
->tib2_ultid
== s_ulIdMainThread
)
422 void wxThread::Yield()
427 void wxThread::Sleep(
428 unsigned long ulMilliseconds
431 ::DosSleep(ulMilliseconds
);
434 // create/start thread
435 // -------------------
437 wxThreadError
wxThread::Create()
439 if ( !p_internal
->Create(this) )
440 return wxTHREAD_NO_RESOURCE
;
442 return wxTHREAD_NO_ERROR
;
445 wxThreadError
wxThread::Run()
447 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
449 if ( p_internal
->GetState() != STATE_NEW
)
451 // actually, it may be almost any state at all, not only STATE_RUNNING
452 return wxTHREAD_RUNNING
;
457 // suspend/resume thread
458 // ---------------------
460 wxThreadError
wxThread::Pause()
462 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
464 return p_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
467 wxThreadError
wxThread::Resume()
469 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
471 return p_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
477 wxThread::ExitCode
wxThread::Delete()
482 // Delete() is always safe to call, so consider all possible states
490 // set flag for wxIsWaitingForThread()
491 s_bWaitingForThread
= TRUE
;
498 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
500 p_internal
->Cancel();
501 hThread
= p_internal
->GetHandle();
504 // we can't just wait for the thread to terminate because it might be
505 // calling some GUI functions and so it will never terminate before we
506 // process the Windows messages that result from these functions
510 ulrc
= ::DosWaitThread( &hThread
515 case ERROR_INTERRUPT
:
516 case ERROR_INVALID_THREADID
:
518 wxLogSysError(_("Can not wait for thread termination"));
523 // thread we're waiting for terminated
526 case ERROR_THREAD_NOT_TERMINATED
:
527 // new message arrived, process it
528 if (!wxTheApp
->DoMessage())
530 // WM_QUIT received: kill the thread
536 // give the thread we're waiting for chance to exit
537 // from the GUI call it might have been in
538 if ((s_nWaitingForGui
> 0) && wxGuiOwnedByMainThread())
546 wxFAIL_MSG(wxT("unexpected result of DosWatiThread"));
552 s_bWaitingForThread
= FALSE
;
556 ::DosExit(EXIT_THREAD
, ulrc
);
562 wxThreadError
wxThread::Kill()
565 return wxTHREAD_NOT_RUNNING
;
567 ::DosKillThread(p_internal
->GetHandle());
569 return wxTHREAD_NO_ERROR
;
577 ::DosExit(EXIT_THREAD
, ULONG(pStatus
));
578 wxFAIL_MSG(wxT("Couldn't return from DosExit()!"));
581 void wxThread::SetPriority(
585 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
587 p_internal
->SetPriority(nPrio
);
590 unsigned int wxThread::GetPriority() const
592 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
594 return p_internal
->GetPriority();
597 unsigned long wxThread::GetID() const
599 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
601 return (unsigned long)p_internal
->GetId();
604 bool wxThread::IsRunning() const
606 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
608 return p_internal
->GetState() == STATE_RUNNING
;
611 bool wxThread::IsAlive() const
613 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
615 return (p_internal
->GetState() == STATE_RUNNING
) ||
616 (p_internal
->GetState() == STATE_PAUSED
);
619 bool wxThread::IsPaused() const
621 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
623 return (p_internal
->GetState() == STATE_PAUSED
);
626 bool wxThread::TestDestroy()
628 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
630 return p_internal
->GetState() == STATE_CANCELED
;
635 p_internal
= new wxThreadInternal();
638 wxThread::~wxThread()
643 // ----------------------------------------------------------------------------
644 // Automatic initialization for thread module
645 // ----------------------------------------------------------------------------
647 class wxThreadModule
: public wxModule
650 virtual bool OnInit();
651 virtual void OnExit();
654 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
657 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
659 bool wxThreadModule::OnInit()
661 s_pCritsectWaitingForGui
= new wxCriticalSection();
663 s_pCritsectGui
= new wxCriticalSection();
664 s_pCritsectGui
->Enter();
669 ::DosGetInfoBlocks(&ptib
, &ppib
);
671 s_ulIdMainThread
= ptib
->tib_ptib2
->tib2_ultid
;
675 void wxThreadModule::OnExit()
679 s_pCritsectGui
->Leave();
680 delete s_pCritsectGui
;
681 s_pCritsectGui
= NULL
;
684 wxDELETE(s_pCritsectWaitingForGui
);
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
691 // Does nothing under OS/2 [for now]
692 void WXDLLEXPORT
wxWakeUpMainThread()
696 void WXDLLEXPORT
wxMutexGuiLeave()
698 wxCriticalSectionLocker
enter(*s_pCritsectWaitingForGui
);
700 if ( wxThread::IsMain() )
702 s_bGuiOwnedByMainThread
= FALSE
;
706 // decrement the number of waiters now
707 wxASSERT_MSG( s_nWaitingForGui
> 0,
708 wxT("calling wxMutexGuiLeave() without entering it first?") );
712 wxWakeUpMainThread();
715 s_pCritsectGui
->Leave();
718 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
720 wxASSERT_MSG( wxThread::IsMain(),
721 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
723 wxCriticalSectionLocker
enter(*s_pCritsectWaitingForGui
);
725 if ( s_nWaitingForGui
== 0 )
727 // no threads are waiting for GUI - so we may acquire the lock without
728 // any danger (but only if we don't already have it)
729 if (!wxGuiOwnedByMainThread())
731 s_pCritsectGui
->Enter();
733 s_bGuiOwnedByMainThread
= TRUE
;
735 //else: already have it, nothing to do
739 // some threads are waiting, release the GUI lock if we have it
740 if (wxGuiOwnedByMainThread())
744 //else: some other worker thread is doing GUI
748 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
750 return s_bGuiOwnedByMainThread
;