]>
Commit | Line | Data |
---|---|---|
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__ |
9c152f80 | 38 | #include "wx/mac/uma.h" |
76a5e5d2 SC |
39 | #endif |
40 | ||
a959b088 SC |
41 | // ---------------------------------------------------------------------------- |
42 | // constants | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
e7549107 SC |
45 | // the possible states of the thread ("=>" shows all possible transitions from |
46 | // this state) | |
47 | enum wxThreadState | |
48 | { | |
49 | STATE_NEW, // didn't start execution yet (=> RUNNING) | |
50 | STATE_RUNNING, // thread is running (=> PAUSED, CANCELED) | |
51 | STATE_PAUSED, // thread is temporarily suspended (=> RUNNING) | |
52 | STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED) | |
53 | STATE_EXITED // thread is terminating | |
e9576ca5 SC |
54 | }; |
55 | ||
e7549107 | 56 | // ---------------------------------------------------------------------------- |
a959b088 | 57 | // this module globals |
e7549107 | 58 | // ---------------------------------------------------------------------------- |
169935ad | 59 | |
a959b088 SC |
60 | static ThreadID gs_idMainThread = kNoThreadID ; |
61 | static bool gs_waitingForThread = FALSE ; | |
e9576ca5 | 62 | |
e7549107 | 63 | // ============================================================================ |
a959b088 | 64 | // MacOS implementation of thread classes |
e7549107 SC |
65 | // ============================================================================ |
66 | ||
a959b088 SC |
67 | class wxMacStCritical |
68 | { | |
69 | public : | |
6fe73788 RL |
70 | wxMacStCritical() |
71 | { | |
9c152f80 SC |
72 | if ( UMASystemIsInitialized() ) |
73 | ThreadBeginCritical() ; | |
6fe73788 RL |
74 | } |
75 | ~wxMacStCritical() | |
76 | { | |
9c152f80 SC |
77 | if ( UMASystemIsInitialized() ) |
78 | ThreadEndCritical() ; | |
6fe73788 RL |
79 | } |
80 | }; | |
a959b088 | 81 | |
e7549107 SC |
82 | // ---------------------------------------------------------------------------- |
83 | // wxMutex implementation | |
84 | // ---------------------------------------------------------------------------- | |
a959b088 | 85 | |
e7549107 SC |
86 | class wxMutexInternal |
87 | { | |
e9576ca5 | 88 | public: |
a959b088 SC |
89 | wxMutexInternal() |
90 | { | |
6fe73788 | 91 | m_owner = kNoThreadID ; |
a959b088 SC |
92 | } |
93 | ||
94 | ~wxMutexInternal() | |
95 | { | |
96 | } | |
97 | ||
98 | public: | |
99 | ThreadID m_owner ; | |
100 | wxArrayLong m_waiters ; | |
e9576ca5 SC |
101 | }; |
102 | ||
103 | wxMutex::wxMutex() | |
104 | { | |
a959b088 | 105 | m_internal = new wxMutexInternal; |
e7549107 | 106 | |
e9576ca5 SC |
107 | m_locked = 0; |
108 | } | |
109 | ||
110 | wxMutex::~wxMutex() | |
111 | { | |
a959b088 SC |
112 | if ( m_locked > 0 ) |
113 | { | |
114 | wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked); | |
115 | } | |
116 | ||
117 | delete m_internal; | |
e9576ca5 SC |
118 | } |
119 | ||
120 | wxMutexError wxMutex::Lock() | |
121 | { | |
6fe73788 | 122 | wxMacStCritical critical ; |
9c152f80 | 123 | if ( UMASystemIsInitialized() ) |
6fe73788 | 124 | { |
9c152f80 SC |
125 | OSErr err ; |
126 | ThreadID current = kNoThreadID; | |
127 | err = ::MacGetCurrentThread(¤t); | |
128 | // if we are not the owner, add this thread to the list of waiting threads, stop this thread | |
129 | // and invoke the scheduler to continue executing the owner's thread | |
130 | while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current) | |
131 | { | |
132 | m_internal->m_waiters.Add(current); | |
133 | err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner); | |
134 | err = ::ThreadBeginCritical(); | |
135 | } | |
136 | m_internal->m_owner = current; | |
6fe73788 | 137 | } |
e9576ca5 | 138 | m_locked++; |
a959b088 | 139 | |
e9576ca5 SC |
140 | return wxMUTEX_NO_ERROR; |
141 | } | |
142 | ||
143 | wxMutexError wxMutex::TryLock() | |
144 | { | |
6fe73788 | 145 | wxMacStCritical critical ; |
9c152f80 SC |
146 | if ( UMASystemIsInitialized() ) |
147 | { | |
148 | OSErr err ; | |
149 | ThreadID current = kNoThreadID; | |
150 | ::MacGetCurrentThread(¤t); | |
151 | // if we are not the owner, give an error back | |
152 | if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current ) | |
153 | return wxMUTEX_BUSY; | |
154 | ||
155 | m_internal->m_owner = current; | |
156 | } | |
e9576ca5 | 157 | m_locked++; |
a959b088 SC |
158 | |
159 | return wxMUTEX_NO_ERROR; | |
e9576ca5 SC |
160 | } |
161 | ||
162 | wxMutexError wxMutex::Unlock() | |
163 | { | |
9c152f80 SC |
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_internal->m_owner = kNoThreadID; | |
174 | ||
175 | // now pass on to the first waiting thread | |
176 | ThreadID firstWaiting = kNoThreadID; | |
177 | bool found = false; | |
178 | while (!m_internal->m_waiters.IsEmpty() && !found) | |
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 | |
183 | found = (err != threadNotFoundErr); | |
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 | ||
204 | class wxConditionInternal | |
205 | { | |
e9576ca5 | 206 | public: |
a959b088 SC |
207 | wxConditionInternal() |
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 ; |
a959b088 | 249 | }; |
e7549107 | 250 | |
a959b088 SC |
251 | wxCondition::wxCondition() |
252 | { | |
253 | m_internal = new wxConditionInternal; | |
e9576ca5 SC |
254 | } |
255 | ||
256 | wxCondition::~wxCondition() | |
257 | { | |
a959b088 | 258 | delete m_internal; |
e9576ca5 SC |
259 | } |
260 | ||
a959b088 | 261 | void wxCondition::Wait() |
e9576ca5 | 262 | { |
6fe73788 | 263 | (void)m_internal->Wait(0xFFFFFFFFL); |
e9576ca5 SC |
264 | } |
265 | ||
a959b088 | 266 | bool wxCondition::Wait(unsigned long sec, |
e9576ca5 SC |
267 | unsigned long nsec) |
268 | { | |
a959b088 | 269 | return m_internal->Wait(sec*1000 + nsec/1000000); |
e9576ca5 SC |
270 | } |
271 | ||
272 | void 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 | ||
282 | void 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 | |
287 | for ( int 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 |
306 | class wxThreadInternal |
307 | { | |
308 | public: | |
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; } |
a959b088 SC |
339 | |
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 | |
349 | private: | |
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 |
357 | static wxArrayPtrVoid s_threads ; |
358 | ||
359 | ThreadEntryUPP wxThreadInternal::s_threadEntry = NULL ; | |
360 | pascal 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 | } | |
389 | void wxThreadInternal::SetPriority(unsigned int priority) | |
390 | { | |
6fe73788 | 391 | // Priorities don't exist on Mac |
e7549107 SC |
392 | } |
393 | ||
6fe73788 | 394 | bool 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 | ||
422 | bool wxThreadInternal::Suspend() | |
423 | { | |
6fe73788 RL |
424 | OSErr err ; |
425 | ||
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 | ||
442 | bool wxThreadInternal::Resume() | |
443 | { | |
6fe73788 RL |
444 | ThreadID current ; |
445 | OSErr err ; | |
446 | err = MacGetCurrentThread( ¤t ) ; | |
447 | ||
448 | wxASSERT( err == noErr ) ; | |
449 | wxASSERT( current != m_tid ) ; | |
450 | ||
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; |
6fe73788 RL |
457 | |
458 | } | |
459 | err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID); | |
460 | wxASSERT( err == noErr ) ; | |
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 |
470 | wxThread *wxThread::This() |
471 | { | |
6fe73788 RL |
472 | wxMacStCritical critical ; |
473 | ||
474 | ThreadID current ; | |
475 | OSErr err ; | |
476 | ||
477 | err = MacGetCurrentThread( ¤t ) ; | |
478 | ||
479 | for ( int i = 0 ; i < s_threads.Count() ; ++i ) | |
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 | ||
489 | bool wxThread::IsMain() | |
490 | { | |
6fe73788 RL |
491 | ThreadID current ; |
492 | OSErr err ; | |
493 | ||
494 | err = MacGetCurrentThread( ¤t ) ; | |
a959b088 | 495 | return current == gs_idMainThread; |
e7549107 SC |
496 | } |
497 | ||
498 | #ifdef Yield | |
a959b088 | 499 | #undef Yield |
e7549107 SC |
500 | #endif |
501 | ||
502 | void wxThread::Yield() | |
503 | { | |
6fe73788 | 504 | ::YieldToAnyThread() ; |
e7549107 SC |
505 | } |
506 | ||
507 | void wxThread::Sleep(unsigned long milliseconds) | |
508 | { | |
6fe73788 RL |
509 | clock_t start = clock() ; |
510 | do | |
511 | { | |
512 | YieldToAnyThread() ; | |
513 | } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; | |
a959b088 SC |
514 | } |
515 | ||
516 | int 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 | ||
522 | bool wxThread::SetConcurrency(size_t level) | |
523 | { | |
524 | wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); | |
525 | ||
526 | // ok only for the default one | |
527 | if ( level == 0 ) | |
528 | return 0; | |
529 | ||
530 | // how many CPUs have we got? | |
531 | if ( GetCPUCount() == 1 ) | |
532 | { | |
533 | // don't bother with all this complicated stuff - on a single | |
534 | // processor system it doesn't make much sense anyhow | |
535 | return level == 1; | |
536 | } | |
537 | ||
538 | return TRUE ; | |
539 | } | |
540 | ||
541 | // ctor and dtor | |
542 | // ------------- | |
543 | ||
544 | wxThread::wxThread(wxThreadKind kind) | |
545 | { | |
546 | m_internal = new wxThreadInternal(); | |
547 | ||
548 | m_isDetached = kind == wxTHREAD_DETACHED; | |
549 | s_threads.Add( (void*) this ) ; | |
550 | } | |
551 | ||
552 | wxThread::~wxThread() | |
553 | { | |
6fe73788 | 554 | s_threads.Remove( (void*) this ) ; |
a959b088 | 555 | delete m_internal; |
e7549107 SC |
556 | } |
557 | ||
558 | // create/start thread | |
559 | // ------------------- | |
560 | ||
6fe73788 | 561 | wxThreadError wxThread::Create(unsigned int stackSize) |
e9576ca5 | 562 | { |
a959b088 SC |
563 | wxCriticalSectionLocker lock(m_critsect); |
564 | ||
6fe73788 | 565 | if ( !m_internal->Create(this, stackSize) ) |
e7549107 SC |
566 | return wxTHREAD_NO_RESOURCE; |
567 | ||
e9576ca5 SC |
568 | return wxTHREAD_NO_ERROR; |
569 | } | |
570 | ||
e7549107 | 571 | wxThreadError wxThread::Run() |
e9576ca5 | 572 | { |
e7549107 SC |
573 | wxCriticalSectionLocker lock(m_critsect); |
574 | ||
a959b088 | 575 | if ( m_internal->GetState() != STATE_NEW ) |
e7549107 SC |
576 | { |
577 | // actually, it may be almost any state at all, not only STATE_RUNNING | |
578 | return wxTHREAD_RUNNING; | |
579 | } | |
580 | ||
a959b088 | 581 | // the thread has just been created and is still suspended - let it run |
e7549107 | 582 | return Resume(); |
e9576ca5 SC |
583 | } |
584 | ||
e7549107 SC |
585 | // suspend/resume thread |
586 | // --------------------- | |
587 | ||
e9576ca5 SC |
588 | wxThreadError wxThread::Pause() |
589 | { | |
e7549107 SC |
590 | wxCriticalSectionLocker lock(m_critsect); |
591 | ||
a959b088 | 592 | return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
e9576ca5 SC |
593 | } |
594 | ||
595 | wxThreadError wxThread::Resume() | |
596 | { | |
e7549107 | 597 | wxCriticalSectionLocker lock(m_critsect); |
e9576ca5 | 598 | |
a959b088 | 599 | return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
e9576ca5 SC |
600 | } |
601 | ||
e7549107 SC |
602 | // stopping thread |
603 | // --------------- | |
604 | ||
a959b088 SC |
605 | wxThread::ExitCode wxThread::Wait() |
606 | { | |
607 | // although under MacOS we can wait for any thread, it's an error to | |
608 | // wait for a detached one in wxWin API | |
609 | wxCHECK_MSG( !IsDetached(), (ExitCode)-1, | |
610 | _T("can't wait for detached thread") ); | |
611 | ||
612 | ExitCode rc = (ExitCode)-1; | |
613 | ||
614 | (void)Delete(&rc); | |
615 | ||
616 | m_internal->Free(); | |
617 | ||
618 | return rc; | |
619 | } | |
620 | ||
621 | wxThreadError wxThread::Delete(ExitCode *pRc) | |
e9576ca5 | 622 | { |
e7549107 SC |
623 | ExitCode rc = 0; |
624 | ||
625 | // Delete() is always safe to call, so consider all possible states | |
a959b088 SC |
626 | |
627 | // has the thread started to run? | |
628 | bool shouldResume = FALSE; | |
629 | ||
630 | { | |
631 | wxCriticalSectionLocker lock(m_critsect); | |
632 | ||
633 | if ( m_internal->GetState() == STATE_NEW ) | |
634 | { | |
635 | // WinThreadStart() will see it and terminate immediately | |
636 | m_internal->SetState(STATE_EXITED); | |
637 | ||
638 | shouldResume = TRUE; | |
639 | } | |
640 | } | |
641 | ||
642 | // is the thread paused? | |
643 | if ( shouldResume || IsPaused() ) | |
e7549107 SC |
644 | Resume(); |
645 | ||
a959b088 | 646 | // does is still run? |
e7549107 SC |
647 | if ( IsRunning() ) |
648 | { | |
649 | if ( IsMain() ) | |
650 | { | |
651 | // set flag for wxIsWaitingForThread() | |
a959b088 | 652 | gs_waitingForThread = TRUE; |
e7549107 | 653 | |
a959b088 | 654 | #if wxUSE_GUI |
e7549107 | 655 | wxBeginBusyCursor(); |
a959b088 | 656 | #endif // wxUSE_GUI |
e7549107 SC |
657 | } |
658 | ||
a959b088 | 659 | // ask the thread to terminate |
e7549107 SC |
660 | { |
661 | wxCriticalSectionLocker lock(m_critsect); | |
662 | ||
a959b088 | 663 | m_internal->Cancel(); |
e7549107 SC |
664 | } |
665 | ||
a959b088 SC |
666 | #if wxUSE_GUI |
667 | // simply wait for the thread to terminate | |
6fe73788 RL |
668 | while( TestDestroy() ) |
669 | { | |
670 | ::YieldToAnyThread() ; | |
671 | } | |
a959b088 SC |
672 | #else // !wxUSE_GUI |
673 | // simply wait for the thread to terminate | |
6fe73788 RL |
674 | while( TestDestroy() ) |
675 | { | |
676 | ::YieldToAnyThread() ; | |
677 | } | |
a959b088 | 678 | #endif // wxUSE_GUI/!wxUSE_GUI |
e7549107 SC |
679 | |
680 | if ( IsMain() ) | |
681 | { | |
a959b088 | 682 | gs_waitingForThread = FALSE; |
e7549107 | 683 | |
a959b088 | 684 | #if wxUSE_GUI |
e7549107 | 685 | wxEndBusyCursor(); |
a959b088 | 686 | #endif // wxUSE_GUI |
e7549107 | 687 | } |
a959b088 | 688 | } |
e7549107 | 689 | |
a959b088 SC |
690 | // if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) ) |
691 | { | |
692 | wxLogLastError("GetExitCodeThread"); | |
e7549107 | 693 | |
a959b088 SC |
694 | rc = (ExitCode)-1; |
695 | } | |
e7549107 | 696 | |
a959b088 SC |
697 | if ( IsDetached() ) |
698 | { | |
699 | // if the thread exits normally, this is done in WinThreadStart, but in | |
700 | // this case it would have been too early because | |
701 | // MsgWaitForMultipleObject() would fail if the therad handle was | |
702 | // closed while we were waiting on it, so we must do it here | |
703 | delete this; | |
e7549107 SC |
704 | } |
705 | ||
a959b088 SC |
706 | // wxASSERT_MSG( (DWORD)rc != STILL_ACTIVE, |
707 | // wxT("thread must be already terminated.") ); | |
708 | ||
709 | if ( pRc ) | |
710 | *pRc = rc; | |
711 | ||
712 | return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR; | |
e9576ca5 SC |
713 | } |
714 | ||
e7549107 | 715 | wxThreadError wxThread::Kill() |
e9576ca5 | 716 | { |
e7549107 SC |
717 | if ( !IsRunning() ) |
718 | return wxTHREAD_NOT_RUNNING; | |
719 | ||
a959b088 | 720 | // if ( !::TerminateThread(m_internal->GetHandle(), (DWORD)-1) ) |
e7549107 SC |
721 | { |
722 | wxLogSysError(_("Couldn't terminate thread")); | |
723 | ||
724 | return wxTHREAD_MISC_ERROR; | |
725 | } | |
726 | ||
a959b088 SC |
727 | m_internal->Free(); |
728 | ||
729 | if ( IsDetached() ) | |
730 | { | |
731 | delete this; | |
732 | } | |
e7549107 SC |
733 | |
734 | return wxTHREAD_NO_ERROR; | |
e9576ca5 SC |
735 | } |
736 | ||
a959b088 | 737 | void wxThread::Exit(ExitCode status) |
e9576ca5 | 738 | { |
a959b088 | 739 | m_internal->Free(); |
e7549107 | 740 | |
a959b088 SC |
741 | if ( IsDetached() ) |
742 | { | |
743 | delete this; | |
744 | } | |
e7549107 | 745 | |
6fe73788 | 746 | m_internal->SetResult( status ) ; |
a959b088 | 747 | |
6fe73788 | 748 | /* |
a959b088 SC |
749 | #if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) |
750 | _endthreadex((unsigned)status); | |
751 | #else // !VC++ | |
752 | ::ExitThread((DWORD)status); | |
753 | #endif // VC++/!VC++ | |
754 | */ | |
e7549107 | 755 | wxFAIL_MSG(wxT("Couldn't return from ExitThread()!")); |
e9576ca5 SC |
756 | } |
757 | ||
a959b088 SC |
758 | // priority setting |
759 | // ---------------- | |
760 | ||
761 | // since all these calls are execute cooperatively we don't have to use the critical section | |
762 | ||
e7549107 | 763 | void wxThread::SetPriority(unsigned int prio) |
e9576ca5 | 764 | { |
a959b088 | 765 | m_internal->SetPriority(prio); |
e9576ca5 SC |
766 | } |
767 | ||
e7549107 | 768 | unsigned int wxThread::GetPriority() const |
e9576ca5 | 769 | { |
a959b088 | 770 | return m_internal->GetPriority(); |
e9576ca5 SC |
771 | } |
772 | ||
a959b088 | 773 | unsigned long wxThread::GetId() const |
e9576ca5 | 774 | { |
a959b088 | 775 | return (unsigned long)m_internal->GetId(); |
e9576ca5 SC |
776 | } |
777 | ||
e7549107 | 778 | bool wxThread::IsRunning() const |
e9576ca5 | 779 | { |
a959b088 | 780 | return m_internal->GetState() == STATE_RUNNING; |
e9576ca5 | 781 | } |
e9576ca5 SC |
782 | |
783 | bool wxThread::IsAlive() const | |
784 | { | |
a959b088 SC |
785 | return (m_internal->GetState() == STATE_RUNNING) || |
786 | (m_internal->GetState() == STATE_PAUSED); | |
e9576ca5 SC |
787 | } |
788 | ||
e7549107 | 789 | bool wxThread::IsPaused() const |
e9576ca5 | 790 | { |
a959b088 | 791 | return m_internal->GetState() == STATE_PAUSED; |
e9576ca5 SC |
792 | } |
793 | ||
e7549107 | 794 | bool wxThread::TestDestroy() |
e9576ca5 | 795 | { |
a959b088 | 796 | return m_internal->GetState() == STATE_CANCELED; |
e9576ca5 SC |
797 | } |
798 | ||
e7549107 SC |
799 | // ---------------------------------------------------------------------------- |
800 | // Automatic initialization for thread module | |
801 | // ---------------------------------------------------------------------------- | |
e9576ca5 | 802 | |
e7549107 SC |
803 | class wxThreadModule : public wxModule |
804 | { | |
e9576ca5 | 805 | public: |
e7549107 SC |
806 | virtual bool OnInit(); |
807 | virtual void OnExit(); | |
e9576ca5 | 808 | |
e7549107 SC |
809 | private: |
810 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
e9576ca5 SC |
811 | }; |
812 | ||
813 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) | |
814 | ||
e7549107 SC |
815 | bool wxThreadModule::OnInit() |
816 | { | |
6fe73788 RL |
817 | long response; |
818 | bool hasThreadManager ; | |
819 | hasThreadManager = Gestalt( gestaltThreadMgrAttr, &response) == noErr && response & 1; | |
5b781a67 SC |
820 | #if !TARGET_CARBON |
821 | #if GENERATINGCFM | |
6fe73788 RL |
822 | // verify presence of shared library |
823 | hasThreadManager = hasThreadManager && ((Ptr)NewThread != (Ptr)kUnresolvedCFragSymbolAddress); | |
5b781a67 | 824 | #endif |
a959b088 | 825 | #endif |
6fe73788 RL |
826 | if ( !hasThreadManager ) |
827 | { | |
828 | wxMessageBox( "Error" , "Thread Support is not available on this System" , wxOK ) ; | |
829 | return FALSE ; | |
830 | } | |
e7549107 SC |
831 | |
832 | // no error return for GetCurrentThreadId() | |
a959b088 | 833 | MacGetCurrentThread( &gs_idMainThread ) ; |
e7549107 SC |
834 | |
835 | return TRUE; | |
836 | } | |
837 | ||
838 | void wxThreadModule::OnExit() | |
839 | { | |
e7549107 SC |
840 | } |
841 | ||
842 | // ---------------------------------------------------------------------------- | |
a959b088 SC |
843 | // under MacOS we don't have currently preemptive threads, so any thread may access |
844 | // the GUI at any time | |
e7549107 SC |
845 | // ---------------------------------------------------------------------------- |
846 | ||
847 | void WXDLLEXPORT wxMutexGuiEnter() | |
848 | { | |
e7549107 SC |
849 | } |
850 | ||
851 | void WXDLLEXPORT wxMutexGuiLeave() | |
852 | { | |
e7549107 SC |
853 | } |
854 | ||
855 | void WXDLLEXPORT wxMutexGuiLeaveOrEnter() | |
856 | { | |
e7549107 SC |
857 | } |
858 | ||
859 | bool WXDLLEXPORT wxGuiOwnedByMainThread() | |
860 | { | |
a959b088 | 861 | return false ; |
e7549107 SC |
862 | } |
863 | ||
a959b088 | 864 | // wake up the main thread |
e7549107 SC |
865 | void WXDLLEXPORT wxWakeUpMainThread() |
866 | { | |
6fe73788 | 867 | wxMacWakeUp() ; |
e7549107 SC |
868 | } |
869 | ||
870 | bool WXDLLEXPORT wxIsWaitingForThread() | |
871 | { | |
a959b088 | 872 | return false ; |
e7549107 | 873 | } |
e7549107 SC |
874 | |
875 | #endif // wxUSE_THREADS | |
a959b088 | 876 | |
6fe73788 | 877 | // vi:sts=4:sw=4:et |