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