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 // wxThread implementation
244 // ----------------------------------------------------------------------------
246 // wxThreadInternal class
247 // ----------------------
249 class wxThreadInternal
252 inline wxThreadInternal()
255 m_eState
= STATE_NEW
;
259 // create a new (suspended) thread (for the given thread object)
260 bool Create(wxThread
* pThread
);
262 // suspend/resume/terminate
265 inline void Cancel() { m_eState
= STATE_CANCELED
; }
268 inline void SetState(wxThreadState eState
) { m_eState
= eState
; }
269 inline wxThreadState
GetState() const { return m_eState
; }
272 inline void SetPriority(unsigned int nPriority
) { m_nPriority
= nPriority
; }
273 inline unsigned int GetPriority() const { return m_nPriority
; }
275 // thread handle and id
276 inline TID
GetHandle() const { return m_hThread
; }
277 TID
GetId() const { return m_hThread
; }
280 static DWORD
OS2ThreadStart(wxThread
*thread
);
283 // Threads in OS/2 have only an ID, so m_hThread is both it's handle and ID
284 // PM also has no real Tls mechanism to index pointers by so we'll just
285 // keep track of the wxWindows parent object here.
286 TID m_hThread
; // handle and ID of the thread
287 wxThreadState m_eState
; // state, see wxThreadState enum
288 unsigned int m_nPriority
; // thread priority in "wx" units
291 ULONG
wxThreadInternal::OS2ThreadStart(
297 DWORD dwRet
= (DWORD
)pThread
->Entry();
299 pThread
->p_internal
->SetState(STATE_EXITED
);
307 bool wxThreadInternal::Create(
313 ulrc
= ::DosCreateThread( &m_hThread
314 ,(PFNTHREAD
)wxThreadInternal::OS2ThreadStart
316 ,CREATE_SUSPENDED
| STACK_SPARSE
321 wxLogSysError(_("Can't create thread"));
326 // translate wxWindows priority to the PM one
327 ULONG ulOS2_Priority
;
329 if (m_nPriority
<= 20)
330 ulOS2_Priority
= PRTYC_NOCHANGE
;
331 else if (m_nPriority
<= 40)
332 ulOS2_Priority
= PRTYC_IDLETIME
;
333 else if (m_nPriority
<= 60)
334 ulOS2_Priority
= PRTYC_REGULAR
;
335 else if (m_nPriority
<= 80)
336 ulOS2_Priority
= PRTYC_TIMECRITICAL
;
337 else if (m_nPriority
<= 100)
338 ulOS2_Priority
= PRTYC_FOREGROUNDSERVER
;
341 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
342 ulOS2_Priority
= PRTYC_REGULAR
;
344 ulrc
= ::DosSetPriority( PRTYS_THREAD
351 wxLogSysError(_("Can't set thread priority"));
356 bool wxThreadInternal::Suspend()
358 ULONG ulrc
= ::DosSuspendThread(m_hThread
);
362 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
365 m_eState
= STATE_PAUSED
;
369 bool wxThreadInternal::Resume()
371 ULONG ulrc
= ::DosResumeThread(m_hThread
);
375 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
378 m_eState
= STATE_PAUSED
;
385 wxThread
*wxThread::This()
387 wxThread
* pThread
= m_pThread
;
391 bool wxThread::IsMain()
396 ::DosGetInfoBlocks(&ptib
, &ppib
);
398 if (ptib
->tib_ptib2
->tib2_ultid
== s_ulIdMainThread
)
407 void wxThread::Yield()
412 void wxThread::Sleep(
413 unsigned long ulMilliseconds
416 ::DosSleep(ulMilliseconds
);
419 // create/start thread
420 // -------------------
422 wxThreadError
wxThread::Create()
424 if ( !p_internal
->Create(this) )
425 return wxTHREAD_NO_RESOURCE
;
427 return wxTHREAD_NO_ERROR
;
430 wxThreadError
wxThread::Run()
432 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
434 if ( p_internal
->GetState() != STATE_NEW
)
436 // actually, it may be almost any state at all, not only STATE_RUNNING
437 return wxTHREAD_RUNNING
;
442 // suspend/resume thread
443 // ---------------------
445 wxThreadError
wxThread::Pause()
447 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
449 return p_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
452 wxThreadError
wxThread::Resume()
454 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
456 return p_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
462 wxThread::ExitCode
wxThread::Delete()
467 // Delete() is always safe to call, so consider all possible states
475 // set flag for wxIsWaitingForThread()
476 s_bWaitingForThread
= TRUE
;
483 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
485 p_internal
->Cancel();
486 hThread
= p_internal
->GetHandle();
489 // we can't just wait for the thread to terminate because it might be
490 // calling some GUI functions and so it will never terminate before we
491 // process the Windows messages that result from these functions
495 ulrc
= ::DosWaitThread( &hThread
500 case ERROR_INTERRUPT
:
501 case ERROR_INVALID_THREADID
:
503 wxLogSysError(_("Can not wait for thread termination"));
508 // thread we're waiting for terminated
511 case ERROR_THREAD_NOT_TERMINATED
:
512 // new message arrived, process it
513 if (!wxTheApp
->DoMessage())
515 // WM_QUIT received: kill the thread
521 // give the thread we're waiting for chance to exit
522 // from the GUI call it might have been in
523 if ((s_nWaitingForGui
> 0) && wxGuiOwnedByMainThread())
531 wxFAIL_MSG(wxT("unexpected result of DosWatiThread"));
537 s_bWaitingForThread
= FALSE
;
541 ::DosExit(EXIT_THREAD
, ulrc
);
547 wxThreadError
wxThread::Kill()
550 return wxTHREAD_NOT_RUNNING
;
552 ::DosKillThread(p_internal
->GetHandle());
554 return wxTHREAD_NO_ERROR
;
562 ::DosExit(EXIT_THREAD
, ULONG(pStatus
));
563 wxFAIL_MSG(wxT("Couldn't return from DosExit()!"));
566 void wxThread::SetPriority(
570 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
572 p_internal
->SetPriority(nPrio
);
575 unsigned int wxThread::GetPriority() const
577 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
579 return p_internal
->GetPriority();
582 unsigned long wxThread::GetID() const
584 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
586 return (unsigned long)p_internal
->GetId();
589 bool wxThread::IsRunning() const
591 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
593 return p_internal
->GetState() == STATE_RUNNING
;
596 bool wxThread::IsAlive() const
598 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
600 return (p_internal
->GetState() == STATE_RUNNING
) ||
601 (p_internal
->GetState() == STATE_PAUSED
);
604 bool wxThread::IsPaused() const
606 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
608 return (p_internal
->GetState() == STATE_PAUSED
);
611 bool wxThread::TestDestroy()
613 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
615 return p_internal
->GetState() == STATE_CANCELED
;
620 p_internal
= new wxThreadInternal();
623 wxThread::~wxThread()
628 // ----------------------------------------------------------------------------
629 // Automatic initialization for thread module
630 // ----------------------------------------------------------------------------
632 class wxThreadModule
: public wxModule
635 virtual bool OnInit();
636 virtual void OnExit();
639 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
642 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
644 bool wxThreadModule::OnInit()
646 s_pCritsectWaitingForGui
= new wxCriticalSection();
648 s_pCritsectGui
= new wxCriticalSection();
649 s_pCritsectGui
->Enter();
654 ::DosGetInfoBlocks(&ptib
, &ppib
);
656 s_ulIdMainThread
= ptib
->tib_ptib2
->tib2_ultid
;
660 void wxThreadModule::OnExit()
664 s_pCritsectGui
->Leave();
665 delete s_pCritsectGui
;
666 s_pCritsectGui
= NULL
;
669 wxDELETE(s_pCritsectWaitingForGui
);
672 // ----------------------------------------------------------------------------
674 // ----------------------------------------------------------------------------
676 // Does nothing under OS/2 [for now]
677 void WXDLLEXPORT
wxWakeUpMainThread()
681 void WXDLLEXPORT
wxMutexGuiLeave()
683 wxCriticalSectionLocker
enter(*s_pCritsectWaitingForGui
);
685 if ( wxThread::IsMain() )
687 s_bGuiOwnedByMainThread
= FALSE
;
691 // decrement the number of waiters now
692 wxASSERT_MSG( s_nWaitingForGui
> 0,
693 wxT("calling wxMutexGuiLeave() without entering it first?") );
697 wxWakeUpMainThread();
700 s_pCritsectGui
->Leave();
703 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
705 wxASSERT_MSG( wxThread::IsMain(),
706 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
708 wxCriticalSectionLocker
enter(*s_pCritsectWaitingForGui
);
710 if ( s_nWaitingForGui
== 0 )
712 // no threads are waiting for GUI - so we may acquire the lock without
713 // any danger (but only if we don't already have it)
714 if (!wxGuiOwnedByMainThread())
716 s_pCritsectGui
->Enter();
718 s_bGuiOwnedByMainThread
= TRUE
;
720 //else: already have it, nothing to do
724 // some threads are waiting, release the GUI lock if we have it
725 if (wxGuiOwnedByMainThread())
729 //else: some other worker thread is doing GUI
733 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
735 return s_bGuiOwnedByMainThread
;
738 bool WXDLLEXPORT
wxIsWaitingForThread()
740 return s_bWaitingForThread
;