]> git.saurik.com Git - wxWidgets.git/blame - src/os2/thread.cpp
Fixed bug which prevented toplevel window from reacting
[wxWidgets.git] / src / os2 / thread.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: thread.cpp
3// Purpose: wxThread Implementation. For Unix ports, see e.g. src/gtk
4// Author: Original from Wolfram Gloger/Guilhem Lavaux
d90895ac 5// Modified by: David Webster
0e320a79
DW
6// Created: 04/22/98
7// RCS-ID: $Id$
8// Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998)
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
d90895ac
DW
12// ----------------------------------------------------------------------------
13// headers
14// ----------------------------------------------------------------------------
0e320a79 15
d90895ac
DW
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
0e320a79
DW
18
19#if wxUSE_THREADS
20
d90895ac
DW
21#include <stdio.h>
22
d90895ac 23#include "wx/module.h"
19193a2c
KB
24#include "wx/intl.h"
25#include "wx/utils.h"
26#include "wx/log.h"
d90895ac
DW
27#include "wx/thread.h"
28
c5fb56c0
DW
29#define INCL_DOSSEMAPHORES
30#define INCL_DOSPROCESS
31#define INCL_ERRORS
32#include <os2.h>
33#include <bseerr.h>
34
d90895ac
DW
35// the possible states of the thread ("=>" shows all possible transitions from
36// this state)
37enum wxThreadState
38{
39 STATE_NEW, // didn't start execution yet (=> RUNNING)
40 STATE_RUNNING, // thread is running (=> PAUSED, CANCELED)
41 STATE_PAUSED, // thread is temporarily suspended (=> RUNNING)
42 STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED)
43 STATE_EXITED // thread is terminating
0e320a79
DW
44};
45
d90895ac
DW
46// ----------------------------------------------------------------------------
47// static variables
48// ----------------------------------------------------------------------------
0e320a79 49
d90895ac
DW
50// id of the main thread - the one which can call GUI functions without first
51// calling wxMutexGuiEnter()
c5fb56c0
DW
52static ULONG s_ulIdMainThread = 0;
53wxMutex* p_wxMainMutex;
54
55// OS2 substitute for Tls pointer the current parent thread object
56wxThread* m_pThread; // pointer to the wxWindows thread object
d90895ac
DW
57
58// if it's FALSE, some secondary thread is holding the GUI lock
43543d98 59static bool gs_bGuiOwnedByMainThread = TRUE;
0e320a79 60
d90895ac
DW
61// critical section which controls access to all GUI functions: any secondary
62// thread (i.e. except the main one) must enter this crit section before doing
63// any GUI calls
43543d98 64static wxCriticalSection *gs_pCritsectGui = NULL;
d90895ac
DW
65
66// critical section which protects s_nWaitingForGui variable
43543d98 67static wxCriticalSection *gs_pCritsectWaitingForGui = NULL;
d90895ac
DW
68
69// number of threads waiting for GUI in wxMutexGuiEnter()
43543d98 70static size_t gs_nWaitingForGui = 0;
d90895ac
DW
71
72// are we waiting for a thread termination?
43543d98 73static bool gs_bWaitingForThread = FALSE;
d90895ac
DW
74
75// ============================================================================
c5fb56c0 76// OS/2 implementation of thread classes
d90895ac
DW
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// wxMutex implementation
81// ----------------------------------------------------------------------------
82class wxMutexInternal
83{
0e320a79 84public:
c5fb56c0 85 HMTX m_vMutex;
0e320a79
DW
86};
87
88wxMutex::wxMutex()
89{
c5fb56c0
DW
90 APIRET ulrc;
91
d01cc696
DW
92 m_internal = new wxMutexInternal;
93 ulrc = ::DosCreateMutexSem(NULL, &m_internal->m_vMutex, 0L, FALSE);
c5fb56c0 94 if (ulrc != 0)
d90895ac
DW
95 {
96 wxLogSysError(_("Can not create mutex."));
97 }
0e320a79
DW
98 m_locked = 0;
99}
100
101wxMutex::~wxMutex()
102{
103 if (m_locked > 0)
d90895ac 104 wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked);
d01cc696
DW
105 ::DosCloseMutexSem(m_internal->m_vMutex);
106 m_internal->m_vMutex = NULL;
0e320a79
DW
107}
108
109wxMutexError wxMutex::Lock()
110{
c5fb56c0
DW
111 APIRET ulrc;
112
d01cc696 113 ulrc = ::DosRequestMutexSem(m_internal->m_vMutex, SEM_INDEFINITE_WAIT);
d90895ac 114
c5fb56c0 115 switch (ulrc)
d90895ac 116 {
c5fb56c0 117 case ERROR_TOO_MANY_SEM_REQUESTS:
d90895ac
DW
118 return wxMUTEX_BUSY;
119
c5fb56c0 120 case NO_ERROR:
d90895ac
DW
121 // ok
122 break;
123
c5fb56c0
DW
124 case ERROR_INVALID_HANDLE:
125 case ERROR_INTERRUPT:
126 case ERROR_SEM_OWNER_DIED:
d90895ac
DW
127 wxLogSysError(_("Couldn't acquire a mutex lock"));
128 return wxMUTEX_MISC_ERROR;
129
c5fb56c0 130 case ERROR_TIMEOUT:
d90895ac
DW
131 default:
132 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
133 }
0e320a79
DW
134 m_locked++;
135 return wxMUTEX_NO_ERROR;
136}
137
138wxMutexError wxMutex::TryLock()
139{
c5fb56c0 140 ULONG ulrc;
d90895ac 141
d01cc696 142 ulrc = ::DosRequestMutexSem(m_internal->m_vMutex, SEM_IMMEDIATE_RETURN /*0L*/);
c5fb56c0 143 if (ulrc == ERROR_TIMEOUT || ulrc == ERROR_TOO_MANY_SEM_REQUESTS)
d90895ac
DW
144 return wxMUTEX_BUSY;
145
0e320a79
DW
146 m_locked++;
147 return wxMUTEX_NO_ERROR;
148}
149
150wxMutexError wxMutex::Unlock()
151{
c5fb56c0
DW
152 APIRET ulrc;
153
0e320a79
DW
154 if (m_locked > 0)
155 m_locked--;
156
d01cc696 157 ulrc = ::DosReleaseMutexSem(m_internal->m_vMutex);
c5fb56c0 158 if (ulrc != 0)
d90895ac
DW
159 {
160 wxLogSysError(_("Couldn't release a mutex"));
161 return wxMUTEX_MISC_ERROR;
162 }
0e320a79
DW
163 return wxMUTEX_NO_ERROR;
164}
165
d90895ac
DW
166// ----------------------------------------------------------------------------
167// wxCondition implementation
168// ----------------------------------------------------------------------------
169
170class wxConditionInternal
171{
0e320a79 172public:
d01cc696
DW
173 inline wxConditionInternal ()
174 {
175 ::DosCreateEventSem(NULL, &m_vEvent, DC_SEM_SHARED, FALSE);
176 if (!m_vEvent)
177 {
178 wxLogSysError(_("Can not create event semaphore."));
179 }
180 m_nWaiters = 0;
181 }
182
183 inline bool Wait(
184 unsigned long ulTimeout
185 )
186 {
187 APIRET ulrc;
188
189 m_nWaiters++;
190 ulrc = ::DosWaitEventSem(m_vEvent, ulTimeout);
191 m_nWaiters--;
192 return (ulrc != ERROR_TIMEOUT);
193 }
194
195 inline ~wxConditionInternal ()
196 {
197 APIRET ulrc;
198
199 if (m_vEvent)
200 {
201 ulrc = ::DosCloseEventSem(m_vEvent);
202 if (!ulrc)
203 {
204 wxLogLastError("DosCloseEventSem(m_vEvent)");
205 }
206 }
207 }
208
c5fb56c0
DW
209 HEV m_vEvent;
210 int m_nWaiters;
0e320a79
DW
211};
212
213wxCondition::wxCondition()
214{
c5fb56c0
DW
215 APIRET ulrc;
216 ULONG ulCount;
217
d01cc696
DW
218 m_internal = new wxConditionInternal;
219 ulrc = ::DosCreateEventSem(NULL, &m_internal->m_vEvent, 0L, FALSE);
c5fb56c0 220 if (ulrc != 0)
d90895ac
DW
221 {
222 wxLogSysError(_("Can not create event object."));
223 }
d01cc696 224 m_internal->m_nWaiters = 0;
c5fb56c0 225 // ?? just for good measure?
d01cc696 226 ::DosResetEventSem(m_internal->m_vEvent, &ulCount);
0e320a79
DW
227}
228
229wxCondition::~wxCondition()
230{
d01cc696
DW
231 ::DosCloseEventSem(m_internal->m_vEvent);
232 delete m_internal;
233 m_internal = NULL;
0e320a79
DW
234}
235
d01cc696 236void wxCondition::Wait()
0e320a79 237{
43543d98 238 (void)m_internal->Wait(SEM_INDEFINITE_WAIT);
0e320a79
DW
239}
240
c5fb56c0 241bool wxCondition::Wait(
d01cc696
DW
242 unsigned long lSec
243, unsigned long lNsec)
0e320a79 244{
d01cc696 245 return m_internal->Wait(lSec*1000 + lNsec/1000000);
0e320a79
DW
246}
247
248void wxCondition::Signal()
249{
d01cc696 250 ::DosPostEventSem(m_internal->m_vEvent);
0e320a79
DW
251}
252
253void wxCondition::Broadcast()
254{
c5fb56c0 255 int i;
d90895ac 256
d01cc696 257 for (i = 0; i < m_internal->m_nWaiters; i++)
d90895ac 258 {
d01cc696 259 if (::DosPostEventSem(m_internal->m_vEvent) != 0)
d90895ac
DW
260 {
261 wxLogSysError(_("Couldn't change the state of event object."));
262 }
263 }
0e320a79
DW
264}
265
892b89f3
DW
266// ----------------------------------------------------------------------------
267// wxCriticalSection implementation
268// ----------------------------------------------------------------------------
269
270wxCriticalSection::wxCriticalSection()
271{
272}
273
274wxCriticalSection::~wxCriticalSection()
275{
276}
277
278void wxCriticalSection::Enter()
279{
280 ::DosEnterCritSec();
281}
282
283void wxCriticalSection::Leave()
284{
285 ::DosExitCritSec();
286}
287
d90895ac
DW
288// ----------------------------------------------------------------------------
289// wxThread implementation
290// ----------------------------------------------------------------------------
291
292// wxThreadInternal class
293// ----------------------
294
295class wxThreadInternal
296{
297public:
c5fb56c0 298 inline wxThreadInternal()
d90895ac
DW
299 {
300 m_hThread = 0;
c5fb56c0
DW
301 m_eState = STATE_NEW;
302 m_nPriority = 0;
d90895ac
DW
303 }
304
d01cc696
DW
305 ~wxThreadInternal()
306 {
307 Free();
308 }
309
310 void Free()
311 {
312 if (m_hThread)
313 {
314 ::DosExit(0,0);
315 m_hThread = 0;
316 }
317 }
318
d90895ac 319 // create a new (suspended) thread (for the given thread object)
c5fb56c0 320 bool Create(wxThread* pThread);
d90895ac
DW
321
322 // suspend/resume/terminate
323 bool Suspend();
324 bool Resume();
c5fb56c0 325 inline void Cancel() { m_eState = STATE_CANCELED; }
d90895ac
DW
326
327 // thread state
c5fb56c0
DW
328 inline void SetState(wxThreadState eState) { m_eState = eState; }
329 inline wxThreadState GetState() const { return m_eState; }
d90895ac
DW
330
331 // thread priority
d01cc696 332 void SetPriority(unsigned int nPriority);
c5fb56c0 333 inline unsigned int GetPriority() const { return m_nPriority; }
d90895ac
DW
334
335 // thread handle and id
c5fb56c0
DW
336 inline TID GetHandle() const { return m_hThread; }
337 TID GetId() const { return m_hThread; }
d90895ac
DW
338
339 // thread function
340 static DWORD OS2ThreadStart(wxThread *thread);
341
342private:
c5fb56c0
DW
343 // Threads in OS/2 have only an ID, so m_hThread is both it's handle and ID
344 // PM also has no real Tls mechanism to index pointers by so we'll just
345 // keep track of the wxWindows parent object here.
346 TID m_hThread; // handle and ID of the thread
347 wxThreadState m_eState; // state, see wxThreadState enum
348 unsigned int m_nPriority; // thread priority in "wx" units
d90895ac
DW
349};
350
c5fb56c0
DW
351ULONG wxThreadInternal::OS2ThreadStart(
352 wxThread* pThread
353)
d90895ac 354{
c5fb56c0 355 m_pThread = pThread;
d90895ac 356
c5fb56c0 357 DWORD dwRet = (DWORD)pThread->Entry();
d90895ac 358
d01cc696
DW
359 // enter m_critsect before changing the thread state
360 pThread->m_critsect.Enter();
361
43543d98 362 bool bWasCancelled = pThread->m_internal->GetState() == STATE_CANCELED;
d01cc696
DW
363
364 pThread->m_internal->SetState(STATE_EXITED);
43543d98 365 pThread->m_critsect.Leave();
d01cc696 366
c5fb56c0 367 pThread->OnExit();
d90895ac 368
d01cc696
DW
369 // if the thread was cancelled (from Delete()), then it the handle is still
370 // needed there
371 if (pThread->IsDetached() && !bWasCancelled)
372 {
373 // auto delete
43543d98 374 delete pThread;
d01cc696
DW
375 }
376 //else: the joinable threads handle will be closed when Wait() is done
c5fb56c0 377 return dwRet;
d90895ac
DW
378}
379
d01cc696
DW
380void wxThreadInternal::SetPriority(
381 unsigned int nPriority
c5fb56c0 382)
d90895ac 383{
c5fb56c0
DW
384 // translate wxWindows priority to the PM one
385 ULONG ulOS2_Priority;
43543d98 386 ULONG ulrc;
c5fb56c0 387
d01cc696
DW
388 m_nPriority = nPriority;
389
c5fb56c0
DW
390 if (m_nPriority <= 20)
391 ulOS2_Priority = PRTYC_NOCHANGE;
392 else if (m_nPriority <= 40)
393 ulOS2_Priority = PRTYC_IDLETIME;
394 else if (m_nPriority <= 60)
395 ulOS2_Priority = PRTYC_REGULAR;
396 else if (m_nPriority <= 80)
397 ulOS2_Priority = PRTYC_TIMECRITICAL;
398 else if (m_nPriority <= 100)
399 ulOS2_Priority = PRTYC_FOREGROUNDSERVER;
d90895ac
DW
400 else
401 {
402 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
c5fb56c0 403 ulOS2_Priority = PRTYC_REGULAR;
d90895ac 404 }
c5fb56c0
DW
405 ulrc = ::DosSetPriority( PRTYS_THREAD
406 ,ulOS2_Priority
407 ,0
408 ,(ULONG)m_hThread
409 );
410 if (ulrc != 0)
d90895ac
DW
411 {
412 wxLogSysError(_("Can't set thread priority"));
413 }
d01cc696
DW
414}
415
416bool wxThreadInternal::Create(
417 wxThread* pThread
418)
419{
420 APIRET ulrc;
421
422 ulrc = ::DosCreateThread( &m_hThread
423 ,(PFNTHREAD)wxThreadInternal::OS2ThreadStart
424 ,(ULONG)pThread
425 ,CREATE_SUSPENDED | STACK_SPARSE
426 ,8192L
427 );
428 if(ulrc != 0)
429 {
430 wxLogSysError(_("Can't create thread"));
431
432 return FALSE;
433 }
434 if (m_nPriority != WXTHREAD_DEFAULT_PRIORITY)
435 {
436 SetPriority(m_nPriority);
437 }
438 return(TRUE);
d90895ac
DW
439}
440
441bool wxThreadInternal::Suspend()
442{
c5fb56c0 443 ULONG ulrc = ::DosSuspendThread(m_hThread);
d90895ac 444
c5fb56c0
DW
445 if (ulrc != 0)
446 {
447 wxLogSysError(_("Can not suspend thread %lu"), m_hThread);
d90895ac
DW
448 return FALSE;
449 }
c5fb56c0
DW
450 m_eState = STATE_PAUSED;
451 return TRUE;
d90895ac
DW
452}
453
454bool wxThreadInternal::Resume()
455{
c5fb56c0 456 ULONG ulrc = ::DosResumeThread(m_hThread);
d90895ac 457
c5fb56c0
DW
458 if (ulrc != 0)
459 {
460 wxLogSysError(_("Can not suspend thread %lu"), m_hThread);
d90895ac
DW
461 return FALSE;
462 }
c5fb56c0
DW
463 m_eState = STATE_PAUSED;
464 return TRUE;
d90895ac
DW
465}
466
467// static functions
468// ----------------
469
470wxThread *wxThread::This()
471{
c5fb56c0
DW
472 wxThread* pThread = m_pThread;
473 return pThread;
d90895ac
DW
474}
475
476bool wxThread::IsMain()
477{
c5fb56c0
DW
478 PTIB ptib;
479 PPIB ppib;
480
481 ::DosGetInfoBlocks(&ptib, &ppib);
482
483 if (ptib->tib_ptib2->tib2_ultid == s_ulIdMainThread)
484 return TRUE;
d90895ac
DW
485 return FALSE;
486}
487
488#ifdef Yield
489 #undef Yield
490#endif
491
492void wxThread::Yield()
493{
d90895ac
DW
494 ::DosSleep(0);
495}
496
c5fb56c0
DW
497void wxThread::Sleep(
498 unsigned long ulMilliseconds
499)
d90895ac 500{
c5fb56c0 501 ::DosSleep(ulMilliseconds);
d90895ac
DW
502}
503
d01cc696
DW
504// ctor and dtor
505// -------------
506
507wxThread::wxThread(wxThreadKind kind)
508{
509 m_internal = new wxThreadInternal();
510
511 m_isDetached = kind == wxTHREAD_DETACHED;
512}
513
514wxThread::~wxThread()
515{
516 delete m_internal;
517}
518
d90895ac
DW
519// create/start thread
520// -------------------
521
0e320a79
DW
522wxThreadError wxThread::Create()
523{
d01cc696 524 if ( !m_internal->Create(this) )
d90895ac
DW
525 return wxTHREAD_NO_RESOURCE;
526
0e320a79
DW
527 return wxTHREAD_NO_ERROR;
528}
529
d90895ac 530wxThreadError wxThread::Run()
0e320a79 531{
c5fb56c0 532 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 533
d01cc696 534 if ( m_internal->GetState() != STATE_NEW )
d90895ac
DW
535 {
536 // actually, it may be almost any state at all, not only STATE_RUNNING
537 return wxTHREAD_RUNNING;
538 }
d90895ac 539 return Resume();
0e320a79
DW
540}
541
d90895ac
DW
542// suspend/resume thread
543// ---------------------
544
0e320a79
DW
545wxThreadError wxThread::Pause()
546{
c5fb56c0 547 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 548
d01cc696 549 return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
0e320a79
DW
550}
551
552wxThreadError wxThread::Resume()
553{
c5fb56c0 554 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
0e320a79 555
d01cc696 556 return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
0e320a79
DW
557}
558
d90895ac
DW
559// stopping thread
560// ---------------
561
d01cc696 562wxThread::ExitCode wxThread::Wait()
0e320a79 563{
d01cc696
DW
564 // although under Windows we can wait for any thread, it's an error to
565 // wait for a detached one in wxWin API
566 wxCHECK_MSG( !IsDetached(), (ExitCode)-1,
567 _T("can't wait for detached thread") );
568 ExitCode rc = (ExitCode)-1;
569 (void)Delete(&rc);
570 m_internal->Free();
571 return(rc);
572}
573
574wxThreadError wxThread::Delete(ExitCode *pRc)
575{
576 ExitCode rc = 0;
d90895ac
DW
577
578 // Delete() is always safe to call, so consider all possible states
c5fb56c0 579 if (IsPaused())
d90895ac
DW
580 Resume();
581
d01cc696
DW
582 TID hThread = m_internal->GetHandle();
583
c5fb56c0 584 if (IsRunning())
d90895ac 585 {
c5fb56c0 586 if (IsMain())
d90895ac
DW
587 {
588 // set flag for wxIsWaitingForThread()
43543d98 589 gs_bWaitingForThread = TRUE;
d01cc696
DW
590
591#if wxUSE_GUI
d90895ac 592 wxBeginBusyCursor();
d01cc696 593#endif // wxUSE_GUI
d90895ac
DW
594 }
595
d01cc696 596 // ask the thread to terminate
d90895ac 597 {
d01cc696
DW
598 wxCriticalSectionLocker lock(m_critsect);
599 m_internal->Cancel();
d90895ac
DW
600 }
601
d01cc696 602#if wxUSE_GUI
3b9e3455
DW
603 // need a way to finish GUI processing before killing the thread
604 // until then we just exit
605
606 if ((gs_nWaitingForGui > 0) && wxGuiOwnedByMainThread())
d01cc696 607 {
3b9e3455 608 wxMutexGuiLeave();
d01cc696 609 }
3b9e3455
DW
610#else // !wxUSE_GUI
611
612 // can't wait for yourself to end under OS/2 so just quit
613
d01cc696 614#endif // wxUSE_GUI/!wxUSE_GUI
d90895ac 615
d01cc696 616 if ( IsMain() )
d90895ac 617 {
43543d98 618 gs_bWaitingForThread = FALSE;
d01cc696
DW
619
620#if wxUSE_GUI
c5fb56c0 621 wxEndBusyCursor();
d01cc696 622#endif // wxUSE_GUI
d90895ac 623 }
d01cc696
DW
624 }
625
3b9e3455
DW
626 ::DosExit(0, 0);
627 // probably won't get this far, but
628 if (IsDetached())
d01cc696 629 {
d01cc696
DW
630 delete this;
631 }
632
d01cc696
DW
633 if ( pRc )
634 *pRc = rc;
635
636 return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR;
0e320a79
DW
637}
638
d90895ac 639wxThreadError wxThread::Kill()
0e320a79 640{
c5fb56c0 641 if (!IsRunning())
d90895ac
DW
642 return wxTHREAD_NOT_RUNNING;
643
d01cc696 644 ::DosKillThread(m_internal->GetHandle());
3b9e3455
DW
645 m_internal->Free();
646 if (IsDetached())
647 {
648 delete this;
649 }
d90895ac 650 return wxTHREAD_NO_ERROR;
0e320a79
DW
651}
652
c5fb56c0 653void wxThread::Exit(
3b9e3455 654 ExitCode pStatus
c5fb56c0 655)
0e320a79 656{
3b9e3455 657 m_internal->Free();
d90895ac 658 delete this;
c5fb56c0
DW
659 ::DosExit(EXIT_THREAD, ULONG(pStatus));
660 wxFAIL_MSG(wxT("Couldn't return from DosExit()!"));
0e320a79
DW
661}
662
c5fb56c0
DW
663void wxThread::SetPriority(
664 unsigned int nPrio
665)
0e320a79 666{
c5fb56c0 667 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 668
d01cc696 669 m_internal->SetPriority(nPrio);
0e320a79
DW
670}
671
d90895ac 672unsigned int wxThread::GetPriority() const
0e320a79 673{
c5fb56c0 674 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 675
d01cc696 676 return m_internal->GetPriority();
0e320a79
DW
677}
678
3b9e3455
DW
679unsigned long wxThread::GetId() const
680{
681 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast
682
683 return (unsigned long)m_internal->GetId();
684}
685
d90895ac 686bool wxThread::IsRunning() const
0e320a79 687{
c5fb56c0 688 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 689
3b9e3455 690 return(m_internal->GetState() == STATE_RUNNING);
0e320a79 691}
0e320a79
DW
692
693bool wxThread::IsAlive() const
694{
c5fb56c0 695 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 696
d01cc696
DW
697 return (m_internal->GetState() == STATE_RUNNING) ||
698 (m_internal->GetState() == STATE_PAUSED);
0e320a79
DW
699}
700
d90895ac 701bool wxThread::IsPaused() const
0e320a79 702{
c5fb56c0 703 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 704
d01cc696 705 return (m_internal->GetState() == STATE_PAUSED);
0e320a79
DW
706}
707
d90895ac 708bool wxThread::TestDestroy()
0e320a79 709{
c5fb56c0 710 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
d90895ac 711
d01cc696 712 return m_internal->GetState() == STATE_CANCELED;
0e320a79
DW
713}
714
d90895ac
DW
715// ----------------------------------------------------------------------------
716// Automatic initialization for thread module
717// ----------------------------------------------------------------------------
0e320a79 718
d90895ac
DW
719class wxThreadModule : public wxModule
720{
0e320a79 721public:
d90895ac
DW
722 virtual bool OnInit();
723 virtual void OnExit();
0e320a79 724
d90895ac
DW
725private:
726 DECLARE_DYNAMIC_CLASS(wxThreadModule)
0e320a79
DW
727};
728
729IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
730
d90895ac
DW
731bool wxThreadModule::OnInit()
732{
43543d98 733 gs_pCritsectWaitingForGui = new wxCriticalSection();
d90895ac 734
43543d98
DW
735 gs_pCritsectGui = new wxCriticalSection();
736 gs_pCritsectGui->Enter();
d90895ac 737
c5fb56c0
DW
738 PTIB ptib;
739 PPIB ppib;
d90895ac 740
c5fb56c0 741 ::DosGetInfoBlocks(&ptib, &ppib);
d90895ac 742
c5fb56c0 743 s_ulIdMainThread = ptib->tib_ptib2->tib2_ultid;
d90895ac
DW
744 return TRUE;
745}
746
747void wxThreadModule::OnExit()
748{
43543d98 749 if (gs_pCritsectGui)
d90895ac 750 {
43543d98 751 gs_pCritsectGui->Leave();
f6bcfd97 752#if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
43543d98 753 delete gs_pCritsectGui;
f6bcfd97 754#endif
43543d98 755 gs_pCritsectGui = NULL;
d90895ac
DW
756 }
757
f6bcfd97 758#if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
43543d98 759 wxDELETE(gs_pCritsectWaitingForGui);
f6bcfd97 760#endif
d90895ac
DW
761}
762
763// ----------------------------------------------------------------------------
c5fb56c0 764// Helper functions
d90895ac
DW
765// ----------------------------------------------------------------------------
766
c5fb56c0
DW
767// Does nothing under OS/2 [for now]
768void WXDLLEXPORT wxWakeUpMainThread()
d90895ac 769{
d90895ac
DW
770}
771
772void WXDLLEXPORT wxMutexGuiLeave()
773{
43543d98 774 wxCriticalSectionLocker enter(*gs_pCritsectWaitingForGui);
d90895ac
DW
775
776 if ( wxThread::IsMain() )
777 {
43543d98 778 gs_bGuiOwnedByMainThread = FALSE;
d90895ac
DW
779 }
780 else
781 {
782 // decrement the number of waiters now
43543d98 783 wxASSERT_MSG(gs_nWaitingForGui > 0,
d90895ac
DW
784 wxT("calling wxMutexGuiLeave() without entering it first?") );
785
43543d98 786 gs_nWaitingForGui--;
d90895ac
DW
787
788 wxWakeUpMainThread();
789 }
790
43543d98 791 gs_pCritsectGui->Leave();
d90895ac
DW
792}
793
794void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
795{
796 wxASSERT_MSG( wxThread::IsMain(),
797 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
798
43543d98 799 wxCriticalSectionLocker enter(*gs_pCritsectWaitingForGui);
d90895ac 800
43543d98 801 if (gs_nWaitingForGui == 0)
d90895ac
DW
802 {
803 // no threads are waiting for GUI - so we may acquire the lock without
804 // any danger (but only if we don't already have it)
c5fb56c0 805 if (!wxGuiOwnedByMainThread())
d90895ac 806 {
43543d98 807 gs_pCritsectGui->Enter();
d90895ac 808
43543d98 809 gs_bGuiOwnedByMainThread = TRUE;
d90895ac
DW
810 }
811 //else: already have it, nothing to do
812 }
813 else
814 {
815 // some threads are waiting, release the GUI lock if we have it
c5fb56c0 816 if (wxGuiOwnedByMainThread())
d90895ac
DW
817 {
818 wxMutexGuiLeave();
819 }
820 //else: some other worker thread is doing GUI
821 }
822}
823
824bool WXDLLEXPORT wxGuiOwnedByMainThread()
825{
43543d98 826 return gs_bGuiOwnedByMainThread;
d90895ac
DW
827}
828
9ed0fac8
DW
829bool WXDLLEXPORT wxIsWaitingForThread()
830{
43543d98 831 return gs_bWaitingForThread;
9ed0fac8
DW
832}
833
0e320a79
DW
834#endif
835 // wxUSE_THREADS