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