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
);
103 p_internal
->m_vMutex
= NULL
;
106 wxMutexError
wxMutex::Lock()
110 ulrc
= ::DosRequestMutexSem(p_internal
->m_vMutex
, SEM_INDEFINITE_WAIT
);
114 case ERROR_TOO_MANY_SEM_REQUESTS
:
121 case ERROR_INVALID_HANDLE
:
122 case ERROR_INTERRUPT
:
123 case ERROR_SEM_OWNER_DIED
:
124 wxLogSysError(_("Couldn't acquire a mutex lock"));
125 return wxMUTEX_MISC_ERROR
;
129 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
132 return wxMUTEX_NO_ERROR
;
135 wxMutexError
wxMutex::TryLock()
139 ulrc
= ::DosRequestMutexSem(p_internal
->m_vMutex
, SEM_IMMEDIATE_RETURN
/*0L*/);
140 if (ulrc
== ERROR_TIMEOUT
|| ulrc
== ERROR_TOO_MANY_SEM_REQUESTS
)
144 return wxMUTEX_NO_ERROR
;
147 wxMutexError
wxMutex::Unlock()
154 ulrc
= ::DosReleaseMutexSem(p_internal
->m_vMutex
);
157 wxLogSysError(_("Couldn't release a mutex"));
158 return wxMUTEX_MISC_ERROR
;
160 return wxMUTEX_NO_ERROR
;
163 // ----------------------------------------------------------------------------
164 // wxCondition implementation
165 // ----------------------------------------------------------------------------
167 class wxConditionInternal
174 wxCondition::wxCondition()
179 p_internal
= new wxConditionInternal
;
180 ulrc
= ::DosCreateEventSem(NULL
, &p_internal
->m_vEvent
, 0L, FALSE
);
183 wxLogSysError(_("Can not create event object."));
185 p_internal
->m_nWaiters
= 0;
186 // ?? just for good measure?
187 ::DosResetEventSem(p_internal
->m_vEvent
, &ulCount
);
190 wxCondition::~wxCondition()
192 ::DosCloseEventSem(p_internal
->m_vEvent
);
197 void wxCondition::Wait(
202 p_internal
->m_nWaiters
++;
203 ::DosWaitEventSem(p_internal
->m_vEvent
, SEM_INDEFINITE_WAIT
);
204 p_internal
->m_nWaiters
--;
208 bool wxCondition::Wait(
210 , unsigned long ulSec
211 , unsigned long ulMillisec
)
216 p_internal
->m_nWaiters
++;
217 ulrc
= ::DosWaitEventSem(p_internal
->m_vEvent
, ULONG((ulSec
* 1000L) + ulMillisec
));
218 p_internal
->m_nWaiters
--;
221 return (ulrc
!= ERROR_TIMEOUT
);
224 void wxCondition::Signal()
226 ::DosPostEventSem(p_internal
->m_vEvent
);
229 void wxCondition::Broadcast()
233 for (i
= 0; i
< p_internal
->m_nWaiters
; i
++)
235 if (::DosPostEventSem(p_internal
->m_vEvent
) != 0)
237 wxLogSysError(_("Couldn't change the state of event object."));
242 // ----------------------------------------------------------------------------
243 // wxCriticalSection implementation
244 // ----------------------------------------------------------------------------
246 wxCriticalSection::wxCriticalSection()
250 wxCriticalSection::~wxCriticalSection()
254 void wxCriticalSection::Enter()
259 void wxCriticalSection::Leave()
264 // ----------------------------------------------------------------------------
265 // wxThread implementation
266 // ----------------------------------------------------------------------------
268 // wxThreadInternal class
269 // ----------------------
271 class wxThreadInternal
274 inline wxThreadInternal()
277 m_eState
= STATE_NEW
;
281 // create a new (suspended) thread (for the given thread object)
282 bool Create(wxThread
* pThread
);
284 // suspend/resume/terminate
287 inline void Cancel() { m_eState
= STATE_CANCELED
; }
290 inline void SetState(wxThreadState eState
) { m_eState
= eState
; }
291 inline wxThreadState
GetState() const { return m_eState
; }
294 inline void SetPriority(unsigned int nPriority
) { m_nPriority
= nPriority
; }
295 inline unsigned int GetPriority() const { return m_nPriority
; }
297 // thread handle and id
298 inline TID
GetHandle() const { return m_hThread
; }
299 TID
GetId() const { return m_hThread
; }
302 static DWORD
OS2ThreadStart(wxThread
*thread
);
305 // Threads in OS/2 have only an ID, so m_hThread is both it's handle and ID
306 // PM also has no real Tls mechanism to index pointers by so we'll just
307 // keep track of the wxWindows parent object here.
308 TID m_hThread
; // handle and ID of the thread
309 wxThreadState m_eState
; // state, see wxThreadState enum
310 unsigned int m_nPriority
; // thread priority in "wx" units
313 ULONG
wxThreadInternal::OS2ThreadStart(
319 DWORD dwRet
= (DWORD
)pThread
->Entry();
321 pThread
->p_internal
->SetState(STATE_EXITED
);
329 bool wxThreadInternal::Create(
335 ulrc
= ::DosCreateThread( &m_hThread
336 ,(PFNTHREAD
)wxThreadInternal::OS2ThreadStart
338 ,CREATE_SUSPENDED
| STACK_SPARSE
343 wxLogSysError(_("Can't create thread"));
348 // translate wxWindows priority to the PM one
349 ULONG ulOS2_Priority
;
351 if (m_nPriority
<= 20)
352 ulOS2_Priority
= PRTYC_NOCHANGE
;
353 else if (m_nPriority
<= 40)
354 ulOS2_Priority
= PRTYC_IDLETIME
;
355 else if (m_nPriority
<= 60)
356 ulOS2_Priority
= PRTYC_REGULAR
;
357 else if (m_nPriority
<= 80)
358 ulOS2_Priority
= PRTYC_TIMECRITICAL
;
359 else if (m_nPriority
<= 100)
360 ulOS2_Priority
= PRTYC_FOREGROUNDSERVER
;
363 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
364 ulOS2_Priority
= PRTYC_REGULAR
;
366 ulrc
= ::DosSetPriority( PRTYS_THREAD
373 wxLogSysError(_("Can't set thread priority"));
378 bool wxThreadInternal::Suspend()
380 ULONG ulrc
= ::DosSuspendThread(m_hThread
);
384 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
387 m_eState
= STATE_PAUSED
;
391 bool wxThreadInternal::Resume()
393 ULONG ulrc
= ::DosResumeThread(m_hThread
);
397 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
400 m_eState
= STATE_PAUSED
;
407 wxThread
*wxThread::This()
409 wxThread
* pThread
= m_pThread
;
413 bool wxThread::IsMain()
418 ::DosGetInfoBlocks(&ptib
, &ppib
);
420 if (ptib
->tib_ptib2
->tib2_ultid
== s_ulIdMainThread
)
429 void wxThread::Yield()
434 void wxThread::Sleep(
435 unsigned long ulMilliseconds
438 ::DosSleep(ulMilliseconds
);
441 // create/start thread
442 // -------------------
444 wxThreadError
wxThread::Create()
446 if ( !p_internal
->Create(this) )
447 return wxTHREAD_NO_RESOURCE
;
449 return wxTHREAD_NO_ERROR
;
452 wxThreadError
wxThread::Run()
454 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
456 if ( p_internal
->GetState() != STATE_NEW
)
458 // actually, it may be almost any state at all, not only STATE_RUNNING
459 return wxTHREAD_RUNNING
;
464 // suspend/resume thread
465 // ---------------------
467 wxThreadError
wxThread::Pause()
469 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
471 return p_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
474 wxThreadError
wxThread::Resume()
476 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
478 return p_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
484 wxThread::ExitCode
wxThread::Delete()
489 // Delete() is always safe to call, so consider all possible states
497 // set flag for wxIsWaitingForThread()
498 s_bWaitingForThread
= TRUE
;
505 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
507 p_internal
->Cancel();
508 hThread
= p_internal
->GetHandle();
511 // we can't just wait for the thread to terminate because it might be
512 // calling some GUI functions and so it will never terminate before we
513 // process the Windows messages that result from these functions
517 ulrc
= ::DosWaitThread( &hThread
522 case ERROR_INTERRUPT
:
523 case ERROR_INVALID_THREADID
:
525 wxLogSysError(_("Can not wait for thread termination"));
530 // thread we're waiting for terminated
533 case ERROR_THREAD_NOT_TERMINATED
:
534 // new message arrived, process it
535 if (!wxTheApp
->DoMessage())
537 // WM_QUIT received: kill the thread
543 // give the thread we're waiting for chance to exit
544 // from the GUI call it might have been in
545 if ((s_nWaitingForGui
> 0) && wxGuiOwnedByMainThread())
553 wxFAIL_MSG(wxT("unexpected result of DosWatiThread"));
559 s_bWaitingForThread
= FALSE
;
563 ::DosExit(EXIT_THREAD
, ulrc
);
569 wxThreadError
wxThread::Kill()
572 return wxTHREAD_NOT_RUNNING
;
574 ::DosKillThread(p_internal
->GetHandle());
576 return wxTHREAD_NO_ERROR
;
584 ::DosExit(EXIT_THREAD
, ULONG(pStatus
));
585 wxFAIL_MSG(wxT("Couldn't return from DosExit()!"));
588 void wxThread::SetPriority(
592 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
594 p_internal
->SetPriority(nPrio
);
597 unsigned int wxThread::GetPriority() const
599 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
601 return p_internal
->GetPriority();
604 unsigned long wxThread::GetID() const
606 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
608 return (unsigned long)p_internal
->GetId();
611 bool wxThread::IsRunning() const
613 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
615 return p_internal
->GetState() == STATE_RUNNING
;
618 bool wxThread::IsAlive() const
620 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
622 return (p_internal
->GetState() == STATE_RUNNING
) ||
623 (p_internal
->GetState() == STATE_PAUSED
);
626 bool wxThread::IsPaused() const
628 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
630 return (p_internal
->GetState() == STATE_PAUSED
);
633 bool wxThread::TestDestroy()
635 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
637 return p_internal
->GetState() == STATE_CANCELED
;
642 p_internal
= new wxThreadInternal();
645 wxThread::~wxThread()
650 // ----------------------------------------------------------------------------
651 // Automatic initialization for thread module
652 // ----------------------------------------------------------------------------
654 class wxThreadModule
: public wxModule
657 virtual bool OnInit();
658 virtual void OnExit();
661 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
664 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
666 bool wxThreadModule::OnInit()
668 s_pCritsectWaitingForGui
= new wxCriticalSection();
670 s_pCritsectGui
= new wxCriticalSection();
671 s_pCritsectGui
->Enter();
676 ::DosGetInfoBlocks(&ptib
, &ppib
);
678 s_ulIdMainThread
= ptib
->tib_ptib2
->tib2_ultid
;
682 void wxThreadModule::OnExit()
686 s_pCritsectGui
->Leave();
687 delete s_pCritsectGui
;
688 s_pCritsectGui
= NULL
;
691 wxDELETE(s_pCritsectWaitingForGui
);
694 // ----------------------------------------------------------------------------
696 // ----------------------------------------------------------------------------
698 // Does nothing under OS/2 [for now]
699 void WXDLLEXPORT
wxWakeUpMainThread()
703 void WXDLLEXPORT
wxMutexGuiLeave()
705 wxCriticalSectionLocker
enter(*s_pCritsectWaitingForGui
);
707 if ( wxThread::IsMain() )
709 s_bGuiOwnedByMainThread
= FALSE
;
713 // decrement the number of waiters now
714 wxASSERT_MSG( s_nWaitingForGui
> 0,
715 wxT("calling wxMutexGuiLeave() without entering it first?") );
719 wxWakeUpMainThread();
722 s_pCritsectGui
->Leave();
725 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
727 wxASSERT_MSG( wxThread::IsMain(),
728 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
730 wxCriticalSectionLocker
enter(*s_pCritsectWaitingForGui
);
732 if ( s_nWaitingForGui
== 0 )
734 // no threads are waiting for GUI - so we may acquire the lock without
735 // any danger (but only if we don't already have it)
736 if (!wxGuiOwnedByMainThread())
738 s_pCritsectGui
->Enter();
740 s_bGuiOwnedByMainThread
= TRUE
;
742 //else: already have it, nothing to do
746 // some threads are waiting, release the GUI lock if we have it
747 if (wxGuiOwnedByMainThread())
751 //else: some other worker thread is doing GUI
755 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
757 return s_bGuiOwnedByMainThread
;
760 bool WXDLLEXPORT
wxIsWaitingForThread()
762 return s_bWaitingForThread
;