]> git.saurik.com Git - wxWidgets.git/blame - src/mac/thread.cpp
initial value set
[wxWidgets.git] / src / mac / thread.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: thread.cpp
e7549107 3// Purpose: wxThread Implementation
a959b088
SC
4// Author: Original from Wolfram Gloger/Guilhem Lavaux/Vadim Zeitlin
5// Modified by: Stefan Csomor
e9576ca5
SC
6// Created: 04/22/98
7// RCS-ID: $Id$
e7549107 8// Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998),
a959b088 9// Vadim Zeitlin (1999) , Stefan Csomor (2000)
e9576ca5
SC
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13#ifdef __GNUG__
e7549107 14 #pragma implementation "thread.h"
e9576ca5
SC
15#endif
16
e7549107
SC
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#if defined(__BORLANDC__)
25 #pragma hdrstop
26#endif
27
28#ifndef WX_PRECOMP
29 #include "wx/wx.h"
30#endif
31
32#if wxUSE_THREADS
33
e9576ca5
SC
34#include "wx/module.h"
35#include "wx/thread.h"
e9576ca5 36
76a5e5d2 37#ifdef __WXMAC__
66a09d47 38#include <Threads.h>
9c152f80 39#include "wx/mac/uma.h"
76a5e5d2
SC
40#endif
41
a959b088
SC
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
e7549107
SC
46// the possible states of the thread ("=>" shows all possible transitions from
47// this state)
48enum wxThreadState
49{
50 STATE_NEW, // didn't start execution yet (=> RUNNING)
51 STATE_RUNNING, // thread is running (=> PAUSED, CANCELED)
52 STATE_PAUSED, // thread is temporarily suspended (=> RUNNING)
53 STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED)
54 STATE_EXITED // thread is terminating
e9576ca5
SC
55};
56
e7549107 57// ----------------------------------------------------------------------------
a959b088 58// this module globals
e7549107 59// ----------------------------------------------------------------------------
169935ad 60
a959b088
SC
61static ThreadID gs_idMainThread = kNoThreadID ;
62static bool gs_waitingForThread = FALSE ;
e9576ca5 63
e7549107 64// ============================================================================
a959b088 65// MacOS implementation of thread classes
e7549107
SC
66// ============================================================================
67
a959b088
SC
68class wxMacStCritical
69{
70public :
ea736fec 71 wxMacStCritical()
6fe73788 72 {
9c152f80
SC
73 if ( UMASystemIsInitialized() )
74 ThreadBeginCritical() ;
6fe73788
RL
75 }
76 ~wxMacStCritical()
77 {
9c152f80
SC
78 if ( UMASystemIsInitialized() )
79 ThreadEndCritical() ;
6fe73788
RL
80 }
81};
a959b088 82
e7549107
SC
83// ----------------------------------------------------------------------------
84// wxMutex implementation
85// ----------------------------------------------------------------------------
a959b088 86
e7549107
SC
87class wxMutexInternal
88{
e9576ca5 89public:
a959b088
SC
90 wxMutexInternal()
91 {
6fe73788 92 m_owner = kNoThreadID ;
a959b088
SC
93 }
94
ea736fec 95 ~wxMutexInternal()
a959b088
SC
96 {
97 }
98
99public:
100 ThreadID m_owner ;
101 wxArrayLong m_waiters ;
e9576ca5
SC
102};
103
104wxMutex::wxMutex()
105{
a959b088 106 m_internal = new wxMutexInternal;
e7549107 107
e9576ca5
SC
108 m_locked = 0;
109}
110
111wxMutex::~wxMutex()
112{
a959b088
SC
113 if ( m_locked > 0 )
114 {
115 wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked);
116 }
117
118 delete m_internal;
e9576ca5
SC
119}
120
121wxMutexError wxMutex::Lock()
122{
6fe73788 123 wxMacStCritical critical ;
9c152f80 124 if ( UMASystemIsInitialized() )
6fe73788 125 {
9c152f80
SC
126 OSErr err ;
127 ThreadID current = kNoThreadID;
128 err = ::MacGetCurrentThread(&current);
129 // if we are not the owner, add this thread to the list of waiting threads, stop this thread
130 // and invoke the scheduler to continue executing the owner's thread
ea736fec 131 while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current)
9c152f80
SC
132 {
133 m_internal->m_waiters.Add(current);
134 err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner);
135 err = ::ThreadBeginCritical();
136 }
137 m_internal->m_owner = current;
6fe73788 138 }
e9576ca5 139 m_locked++;
a959b088 140
e9576ca5
SC
141 return wxMUTEX_NO_ERROR;
142}
143
144wxMutexError wxMutex::TryLock()
145{
6fe73788 146 wxMacStCritical critical ;
9c152f80 147 if ( UMASystemIsInitialized() )
ea736fec 148 {
9c152f80
SC
149 ThreadID current = kNoThreadID;
150 ::MacGetCurrentThread(&current);
151 // if we are not the owner, give an error back
ea736fec 152 if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current )
9c152f80 153 return wxMUTEX_BUSY;
ea736fec 154
9c152f80
SC
155 m_internal->m_owner = current;
156 }
e9576ca5 157 m_locked++;
a959b088
SC
158
159 return wxMUTEX_NO_ERROR;
e9576ca5
SC
160}
161
162wxMutexError wxMutex::Unlock()
163{
9c152f80 164 if ( UMASystemIsInitialized() )
ea736fec 165 {
9c152f80
SC
166 OSErr err;
167 err = ::ThreadBeginCritical();
ea736fec 168
9c152f80
SC
169 if (m_locked > 0)
170 m_locked--;
171
172 // this mutex is not owned by anybody anmore
173 m_internal->m_owner = kNoThreadID;
174
175 // now pass on to the first waiting thread
176 ThreadID firstWaiting = kNoThreadID;
177 bool found = false;
ea736fec 178 while (!m_internal->m_waiters.IsEmpty() && !found)
9c152f80
SC
179 {
180 firstWaiting = m_internal->m_waiters[0];
181 err = ::SetThreadState(firstWaiting, kReadyThreadState, kNoThreadID);
182 // in case this was not successful (dead thread), we just loop on and reset the id
ea736fec 183 found = (err != threadNotFoundErr);
9c152f80
SC
184 if ( !found )
185 firstWaiting = kNoThreadID ;
186 m_internal->m_waiters.RemoveAt(0) ;
187 }
188 // now we have a valid firstWaiting thread, which has been scheduled to run next, just end the
189 // critical section and invoke the scheduler
190 err = ::SetThreadStateEndCritical(kCurrentThreadID, kReadyThreadState, firstWaiting);
191 }
192 else
6fe73788 193 {
9c152f80
SC
194 if (m_locked > 0)
195 m_locked--;
6fe73788 196 }
e9576ca5
SC
197 return wxMUTEX_NO_ERROR;
198}
199
e7549107
SC
200// ----------------------------------------------------------------------------
201// wxCondition implementation
202// ----------------------------------------------------------------------------
203
204class wxConditionInternal
205{
e9576ca5 206public:
71110b40 207 wxConditionInternal(wxMutex& mutex) : m_mutex(mutex)
a959b088 208 {
6fe73788 209 m_excessSignals = 0 ;
a959b088
SC
210 }
211 ~wxConditionInternal()
212 {
213 }
e9576ca5 214
a959b088 215 bool Wait(unsigned long msectimeout)
e7549107 216 {
6fe73788
RL
217 wxMacStCritical critical ;
218 if ( m_excessSignals > 0 )
219 {
220 --m_excessSignals ;
221 return TRUE ;
222 }
223 else if ( msectimeout == 0 )
224 {
225 return FALSE ;
226 }
227 else
228 {
229 }
230 /*
a959b088
SC
231 waiters++;
232
233 // FIXME this should be MsgWaitForMultipleObjects() as well probably
234 DWORD rc = ::WaitForSingleObject(event, timeout);
235
236 waiters--;
237
238 return rc != WAIT_TIMEOUT;
239 */
240 return TRUE ;
e7549107 241 }
6fe73788
RL
242 void Signal()
243 {
244 wxMacStCritical critical ;
245 }
a959b088
SC
246
247 wxArrayLong m_waiters ;
6fe73788 248 wxInt32 m_excessSignals ;
71110b40 249 wxMutex& m_mutex;
a959b088 250};
e7549107 251
71110b40 252wxCondition::wxCondition(wxMutex& mutex)
a959b088 253{
71110b40 254 m_internal = new wxConditionInternal(mutex);
e9576ca5
SC
255}
256
257wxCondition::~wxCondition()
258{
a959b088 259 delete m_internal;
e9576ca5
SC
260}
261
a959b088 262void wxCondition::Wait()
e9576ca5 263{
6fe73788 264 (void)m_internal->Wait(0xFFFFFFFFL);
e9576ca5
SC
265}
266
71110b40 267bool wxCondition::Wait(unsigned long timeout_millis)
e9576ca5 268{
71110b40 269 return m_internal->Wait(timeout_millis);
e9576ca5
SC
270}
271
272void wxCondition::Signal()
273{
a959b088
SC
274 // set the event to signaled: if a thread is already waiting on it, it will
275 // be woken up, otherwise the event will remain in the signaled state until
276 // someone waits on it. In any case, the system will return it to a non
277 // signalled state afterwards. If multiple threads are waiting, only one
278 // will be woken up.
6fe73788 279 m_internal->Signal() ;
e9576ca5
SC
280}
281
282void wxCondition::Broadcast()
283{
a959b088
SC
284 // this works because all these threads are already waiting and so each
285 // SetEvent() inside Signal() is really a PulseEvent() because the event
286 // state is immediately returned to non-signaled
d84afea9 287 for ( size_t i = 0; i < m_internal->m_waiters.Count(); i++ )
e7549107 288 {
a959b088 289 Signal();
e7549107 290 }
e9576ca5
SC
291}
292
e7549107
SC
293// ----------------------------------------------------------------------------
294// wxCriticalSection implementation
295// ----------------------------------------------------------------------------
296
a959b088 297// it's implemented as a mutex on mac os, so it is defined in the headers
e7549107
SC
298
299// ----------------------------------------------------------------------------
300// wxThread implementation
301// ----------------------------------------------------------------------------
302
303// wxThreadInternal class
304// ----------------------
305
e7549107
SC
306class wxThreadInternal
307{
308public:
309 wxThreadInternal()
310 {
a959b088 311 m_tid = kNoThreadID ;
e7549107
SC
312 m_state = STATE_NEW;
313 m_priority = WXTHREAD_DEFAULT_PRIORITY;
314 }
315
a959b088
SC
316 ~wxThreadInternal()
317 {
318 }
319
320 void Free()
321 {
322 }
323
e7549107 324 // create a new (suspended) thread (for the given thread object)
6fe73788 325 bool Create(wxThread *thread, unsigned int stackSize);
e7549107
SC
326
327 // suspend/resume/terminate
328 bool Suspend();
329 bool Resume();
330 void Cancel() { m_state = STATE_CANCELED; }
331
332 // thread state
333 void SetState(wxThreadState state) { m_state = state; }
334 wxThreadState GetState() const { return m_state; }
335
336 // thread priority
a959b088 337 void SetPriority(unsigned int priority);
e7549107 338 unsigned int GetPriority() const { return m_priority; }
ea736fec 339
a959b088
SC
340 void SetResult( void *res ) { m_result = res ; }
341 void *GetResult() { return m_result ; }
e7549107
SC
342
343 // thread handle and id
a959b088 344 ThreadID GetId() const { return m_tid; }
e7549107
SC
345
346 // thread function
6fe73788 347 static pascal void* MacThreadStart(wxThread* arg);
e7549107
SC
348
349private:
6fe73788
RL
350 wxThreadState m_state; // state, see wxThreadState enum
351 unsigned int m_priority; // thread priority in "wx" units
352 ThreadID m_tid; // thread id
353 void* m_result;
354 static ThreadEntryUPP s_threadEntry ;
e7549107
SC
355};
356
a959b088
SC
357static wxArrayPtrVoid s_threads ;
358
359ThreadEntryUPP wxThreadInternal::s_threadEntry = NULL ;
360pascal void* wxThreadInternal::MacThreadStart(wxThread *thread)
e7549107 361{
a959b088
SC
362 // first of all, check whether we hadn't been cancelled already
363 if ( thread->m_internal->GetState() == STATE_EXITED )
e7549107 364 {
a959b088 365 return (void*)-1;
e7549107
SC
366 }
367
a959b088
SC
368 void* rc = thread->Entry();
369
370 // enter m_critsect before changing the thread state
371 thread->m_critsect.Enter();
372 bool wasCancelled = thread->m_internal->GetState() == STATE_CANCELED;
373 thread->m_internal->SetState(STATE_EXITED);
374 thread->m_critsect.Leave();
375
e7549107
SC
376 thread->OnExit();
377
a959b088
SC
378 // if the thread was cancelled (from Delete()), then it the handle is still
379 // needed there
380 if ( thread->IsDetached() && !wasCancelled )
381 {
382 // auto delete
383 delete thread;
384 }
385 //else: the joinable threads handle will be closed when Wait() is done
e7549107 386
a959b088
SC
387 return rc;
388}
389void wxThreadInternal::SetPriority(unsigned int priority)
390{
6fe73788 391 // Priorities don't exist on Mac
e7549107
SC
392}
393
6fe73788 394bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
e7549107 395{
6fe73788
RL
396 if ( s_threadEntry == NULL )
397 {
398 s_threadEntry = NewThreadEntryUPP( (ThreadEntryProcPtr) MacThreadStart ) ;
399 }
400 OSErr err = NewThread( kCooperativeThread,
401 s_threadEntry,
402 (void*) thread,
403 stackSize,
404 kNewSuspend,
405 &m_result,
406 &m_tid );
a959b088
SC
407
408 if ( err != noErr )
e7549107
SC
409 {
410 wxLogSysError(_("Can't create thread"));
e7549107
SC
411 return FALSE;
412 }
413
a959b088 414 if ( m_priority != WXTHREAD_DEFAULT_PRIORITY )
e7549107 415 {
a959b088 416 SetPriority(m_priority);
e7549107
SC
417 }
418
419 return TRUE;
420}
421
422bool wxThreadInternal::Suspend()
423{
6fe73788 424 OSErr err ;
ea736fec 425
6fe73788 426 ::ThreadBeginCritical();
e7549107 427
6fe73788 428 if ( m_state != STATE_RUNNING )
a959b088 429 {
6fe73788 430 ::ThreadEndCritical() ;
a959b088 431 wxLogSysError(_("Can not suspend thread %x"), m_tid);
e7549107
SC
432 return FALSE;
433 }
434
435 m_state = STATE_PAUSED;
436
6fe73788 437 err = ::SetThreadStateEndCritical(m_tid, kStoppedThreadState, kNoThreadID);
a959b088 438
e7549107
SC
439 return TRUE;
440}
441
442bool wxThreadInternal::Resume()
443{
6fe73788
RL
444 ThreadID current ;
445 OSErr err ;
446 err = MacGetCurrentThread( &current ) ;
447
448 wxASSERT( err == noErr ) ;
449 wxASSERT( current != m_tid ) ;
ea736fec 450
6fe73788
RL
451 ::ThreadBeginCritical();
452 if ( m_state != STATE_PAUSED && m_state != STATE_NEW )
453 {
454 ::ThreadEndCritical() ;
a959b088 455 wxLogSysError(_("Can not resume thread %x"), m_tid);
e7549107 456 return FALSE;
ea736fec 457
6fe73788
RL
458 }
459 err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID);
460 wxASSERT( err == noErr ) ;
ea736fec 461
e7549107 462 m_state = STATE_RUNNING;
6fe73788
RL
463 ::ThreadEndCritical() ;
464 ::YieldToAnyThread() ;
e7549107
SC
465 return TRUE;
466}
467
468// static functions
469// ----------------
e7549107
SC
470wxThread *wxThread::This()
471{
6fe73788 472 wxMacStCritical critical ;
ea736fec 473
6fe73788
RL
474 ThreadID current ;
475 OSErr err ;
ea736fec 476
6fe73788 477 err = MacGetCurrentThread( &current ) ;
ea736fec 478
d84afea9 479 for ( size_t i = 0 ; i < s_threads.Count() ; ++i )
6fe73788
RL
480 {
481 if ( ( (wxThread*) s_threads[i] )->GetId() == current )
482 return (wxThread*) s_threads[i] ;
483 }
e7549107 484
a959b088
SC
485 wxLogSysError(_("Couldn't get the current thread pointer"));
486 return NULL;
e7549107
SC
487}
488
489bool wxThread::IsMain()
490{
6fe73788
RL
491 ThreadID current ;
492 OSErr err ;
ea736fec 493
6fe73788 494 err = MacGetCurrentThread( &current ) ;
a959b088 495 return current == gs_idMainThread;
e7549107
SC
496}
497
498#ifdef Yield
a959b088 499#undef Yield
e7549107
SC
500#endif
501
502void wxThread::Yield()
503{
6fe73788 504 ::YieldToAnyThread() ;
e7549107
SC
505}
506
507void wxThread::Sleep(unsigned long milliseconds)
508{
6fe73788 509 clock_t start = clock() ;
ea736fec 510 do
6fe73788
RL
511 {
512 YieldToAnyThread() ;
513 } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
a959b088
SC
514}
515
516int wxThread::GetCPUCount()
517{
6fe73788 518 // we will use whatever MP API will be used for the new MP Macs
a959b088
SC
519 return 1;
520}
521
ea736fec
RD
522unsigned long wxThread::GetCurrentId()
523{
524 ThreadID current ;
525 MacGetCurrentThread( &current ) ;
526 return (unsigned long)current;
527}
528
a959b088
SC
529bool wxThread::SetConcurrency(size_t level)
530{
531 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
532
533 // ok only for the default one
534 if ( level == 0 )
535 return 0;
536
537 // how many CPUs have we got?
538 if ( GetCPUCount() == 1 )
539 {
540 // don't bother with all this complicated stuff - on a single
541 // processor system it doesn't make much sense anyhow
542 return level == 1;
543 }
ea736fec 544
a959b088
SC
545 return TRUE ;
546}
547
548// ctor and dtor
549// -------------
550
551wxThread::wxThread(wxThreadKind kind)
552{
553 m_internal = new wxThreadInternal();
554
555 m_isDetached = kind == wxTHREAD_DETACHED;
556 s_threads.Add( (void*) this ) ;
557}
558
559wxThread::~wxThread()
560{
6fe73788 561 s_threads.Remove( (void*) this ) ;
a959b088 562 delete m_internal;
e7549107
SC
563}
564
565// create/start thread
566// -------------------
567
6fe73788 568wxThreadError wxThread::Create(unsigned int stackSize)
e9576ca5 569{
a959b088
SC
570 wxCriticalSectionLocker lock(m_critsect);
571
6fe73788 572 if ( !m_internal->Create(this, stackSize) )
e7549107
SC
573 return wxTHREAD_NO_RESOURCE;
574
e9576ca5
SC
575 return wxTHREAD_NO_ERROR;
576}
577
e7549107 578wxThreadError wxThread::Run()
e9576ca5 579{
e7549107
SC
580 wxCriticalSectionLocker lock(m_critsect);
581
a959b088 582 if ( m_internal->GetState() != STATE_NEW )
e7549107
SC
583 {
584 // actually, it may be almost any state at all, not only STATE_RUNNING
585 return wxTHREAD_RUNNING;
586 }
587
a959b088 588 // the thread has just been created and is still suspended - let it run
e7549107 589 return Resume();
e9576ca5
SC
590}
591
e7549107
SC
592// suspend/resume thread
593// ---------------------
594
e9576ca5
SC
595wxThreadError wxThread::Pause()
596{
e7549107
SC
597 wxCriticalSectionLocker lock(m_critsect);
598
a959b088 599 return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
e9576ca5
SC
600}
601
602wxThreadError wxThread::Resume()
603{
e7549107 604 wxCriticalSectionLocker lock(m_critsect);
e9576ca5 605
a959b088 606 return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR;
e9576ca5
SC
607}
608
e7549107
SC
609// stopping thread
610// ---------------
611
a959b088
SC
612wxThread::ExitCode wxThread::Wait()
613{
614 // although under MacOS we can wait for any thread, it's an error to
615 // wait for a detached one in wxWin API
616 wxCHECK_MSG( !IsDetached(), (ExitCode)-1,
617 _T("can't wait for detached thread") );
618
619 ExitCode rc = (ExitCode)-1;
620
621 (void)Delete(&rc);
622
623 m_internal->Free();
624
625 return rc;
626}
627
628wxThreadError wxThread::Delete(ExitCode *pRc)
e9576ca5 629{
e7549107
SC
630 ExitCode rc = 0;
631
632 // Delete() is always safe to call, so consider all possible states
a959b088
SC
633
634 // has the thread started to run?
635 bool shouldResume = FALSE;
636
637 {
638 wxCriticalSectionLocker lock(m_critsect);
639
640 if ( m_internal->GetState() == STATE_NEW )
641 {
642 // WinThreadStart() will see it and terminate immediately
643 m_internal->SetState(STATE_EXITED);
644
645 shouldResume = TRUE;
646 }
647 }
648
649 // is the thread paused?
650 if ( shouldResume || IsPaused() )
e7549107
SC
651 Resume();
652
a959b088 653 // does is still run?
e7549107
SC
654 if ( IsRunning() )
655 {
656 if ( IsMain() )
657 {
658 // set flag for wxIsWaitingForThread()
a959b088 659 gs_waitingForThread = TRUE;
e7549107 660
a959b088 661#if wxUSE_GUI
e7549107 662 wxBeginBusyCursor();
a959b088 663#endif // wxUSE_GUI
e7549107
SC
664 }
665
a959b088 666 // ask the thread to terminate
e7549107
SC
667 {
668 wxCriticalSectionLocker lock(m_critsect);
669
a959b088 670 m_internal->Cancel();
e7549107
SC
671 }
672
a959b088
SC
673#if wxUSE_GUI
674 // simply wait for the thread to terminate
6fe73788
RL
675 while( TestDestroy() )
676 {
677 ::YieldToAnyThread() ;
678 }
a959b088
SC
679#else // !wxUSE_GUI
680 // simply wait for the thread to terminate
6fe73788
RL
681 while( TestDestroy() )
682 {
683 ::YieldToAnyThread() ;
684 }
a959b088 685#endif // wxUSE_GUI/!wxUSE_GUI
e7549107
SC
686
687 if ( IsMain() )
688 {
a959b088 689 gs_waitingForThread = FALSE;
e7549107 690
a959b088 691#if wxUSE_GUI
e7549107 692 wxEndBusyCursor();
a959b088 693#endif // wxUSE_GUI
e7549107 694 }
a959b088 695 }
e7549107 696
a959b088
SC
697 // if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) )
698 {
699 wxLogLastError("GetExitCodeThread");
e7549107 700
a959b088
SC
701 rc = (ExitCode)-1;
702 }
e7549107 703
a959b088
SC
704 if ( IsDetached() )
705 {
706 // if the thread exits normally, this is done in WinThreadStart, but in
707 // this case it would have been too early because
708 // MsgWaitForMultipleObject() would fail if the therad handle was
709 // closed while we were waiting on it, so we must do it here
710 delete this;
e7549107
SC
711 }
712
a959b088
SC
713 // wxASSERT_MSG( (DWORD)rc != STILL_ACTIVE,
714 // wxT("thread must be already terminated.") );
715
716 if ( pRc )
717 *pRc = rc;
718
719 return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR;
e9576ca5
SC
720}
721
e7549107 722wxThreadError wxThread::Kill()
e9576ca5 723{
e7549107
SC
724 if ( !IsRunning() )
725 return wxTHREAD_NOT_RUNNING;
726
a959b088 727// if ( !::TerminateThread(m_internal->GetHandle(), (DWORD)-1) )
e7549107
SC
728 {
729 wxLogSysError(_("Couldn't terminate thread"));
730
731 return wxTHREAD_MISC_ERROR;
732 }
733
a959b088
SC
734 m_internal->Free();
735
736 if ( IsDetached() )
737 {
738 delete this;
739 }
e7549107
SC
740
741 return wxTHREAD_NO_ERROR;
e9576ca5
SC
742}
743
a959b088 744void wxThread::Exit(ExitCode status)
e9576ca5 745{
a959b088 746 m_internal->Free();
e7549107 747
a959b088
SC
748 if ( IsDetached() )
749 {
750 delete this;
751 }
e7549107 752
6fe73788 753 m_internal->SetResult( status ) ;
a959b088 754
ea736fec 755/*
a959b088
SC
756#if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500))
757 _endthreadex((unsigned)status);
758#else // !VC++
759 ::ExitThread((DWORD)status);
760#endif // VC++/!VC++
761*/
e7549107 762 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
e9576ca5
SC
763}
764
a959b088
SC
765// priority setting
766// ----------------
767
768// since all these calls are execute cooperatively we don't have to use the critical section
769
e7549107 770void wxThread::SetPriority(unsigned int prio)
e9576ca5 771{
a959b088 772 m_internal->SetPriority(prio);
e9576ca5
SC
773}
774
e7549107 775unsigned int wxThread::GetPriority() const
e9576ca5 776{
a959b088 777 return m_internal->GetPriority();
e9576ca5
SC
778}
779
a959b088 780unsigned long wxThread::GetId() const
e9576ca5 781{
a959b088 782 return (unsigned long)m_internal->GetId();
e9576ca5
SC
783}
784
e7549107 785bool wxThread::IsRunning() const
e9576ca5 786{
a959b088 787 return m_internal->GetState() == STATE_RUNNING;
e9576ca5 788}
e9576ca5
SC
789
790bool wxThread::IsAlive() const
791{
a959b088
SC
792 return (m_internal->GetState() == STATE_RUNNING) ||
793 (m_internal->GetState() == STATE_PAUSED);
e9576ca5
SC
794}
795
e7549107 796bool wxThread::IsPaused() const
e9576ca5 797{
a959b088 798 return m_internal->GetState() == STATE_PAUSED;
e9576ca5
SC
799}
800
e7549107 801bool wxThread::TestDestroy()
e9576ca5 802{
a959b088 803 return m_internal->GetState() == STATE_CANCELED;
e9576ca5
SC
804}
805
e7549107
SC
806// ----------------------------------------------------------------------------
807// Automatic initialization for thread module
808// ----------------------------------------------------------------------------
e9576ca5 809
e7549107
SC
810class wxThreadModule : public wxModule
811{
e9576ca5 812public:
e7549107
SC
813 virtual bool OnInit();
814 virtual void OnExit();
e9576ca5 815
e7549107
SC
816private:
817 DECLARE_DYNAMIC_CLASS(wxThreadModule)
e9576ca5
SC
818};
819
820IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
821
e7549107
SC
822bool wxThreadModule::OnInit()
823{
6fe73788
RL
824 long response;
825 bool hasThreadManager ;
826 hasThreadManager = Gestalt( gestaltThreadMgrAttr, &response) == noErr && response & 1;
5b781a67
SC
827#if !TARGET_CARBON
828#if GENERATINGCFM
6fe73788
RL
829 // verify presence of shared library
830 hasThreadManager = hasThreadManager && ((Ptr)NewThread != (Ptr)kUnresolvedCFragSymbolAddress);
5b781a67 831#endif
a959b088 832#endif
6fe73788
RL
833 if ( !hasThreadManager )
834 {
835 wxMessageBox( "Error" , "Thread Support is not available on this System" , wxOK ) ;
836 return FALSE ;
837 }
e7549107
SC
838
839 // no error return for GetCurrentThreadId()
a959b088 840 MacGetCurrentThread( &gs_idMainThread ) ;
e7549107
SC
841
842 return TRUE;
843}
844
845void wxThreadModule::OnExit()
846{
e7549107
SC
847}
848
849// ----------------------------------------------------------------------------
a959b088
SC
850// under MacOS we don't have currently preemptive threads, so any thread may access
851// the GUI at any time
e7549107
SC
852// ----------------------------------------------------------------------------
853
854void WXDLLEXPORT wxMutexGuiEnter()
855{
e7549107
SC
856}
857
858void WXDLLEXPORT wxMutexGuiLeave()
859{
e7549107
SC
860}
861
862void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
863{
e7549107
SC
864}
865
866bool WXDLLEXPORT wxGuiOwnedByMainThread()
867{
a959b088 868 return false ;
e7549107
SC
869}
870
ea736fec 871// wake up the main thread
e7549107
SC
872void WXDLLEXPORT wxWakeUpMainThread()
873{
6fe73788 874 wxMacWakeUp() ;
e7549107
SC
875}
876
877bool WXDLLEXPORT wxIsWaitingForThread()
878{
a959b088 879 return false ;
e7549107 880}
e7549107
SC
881
882#endif // wxUSE_THREADS
a959b088 883
6fe73788 884// vi:sts=4:sw=4:et