]>
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__ |
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) | |
50 | enum 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 |
63 | static ThreadID gs_idMainThread = kNoThreadID ; |
64 | static bool gs_waitingForThread = FALSE ; | |
e9576ca5 | 65 | |
e7549107 | 66 | // ============================================================================ |
a959b088 | 67 | // MacOS implementation of thread classes |
e7549107 SC |
68 | // ============================================================================ |
69 | ||
a959b088 SC |
70 | class wxMacStCritical |
71 | { | |
72 | public : | |
6fe73788 RL |
73 | wxMacStCritical() |
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 |
89 | class wxMutexInternal |
90 | { | |
e9576ca5 | 91 | public: |
a959b088 SC |
92 | wxMutexInternal() |
93 | { | |
6fe73788 | 94 | m_owner = kNoThreadID ; |
a959b088 SC |
95 | } |
96 | ||
97 | ~wxMutexInternal() | |
98 | { | |
99 | } | |
100 | ||
101 | public: | |
102 | ThreadID m_owner ; | |
103 | wxArrayLong m_waiters ; | |
e9576ca5 SC |
104 | }; |
105 | ||
106 | wxMutex::wxMutex() | |
107 | { | |
a959b088 | 108 | m_internal = new wxMutexInternal; |
e7549107 | 109 | |
e9576ca5 SC |
110 | m_locked = 0; |
111 | } | |
112 | ||
113 | wxMutex::~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 | ||
123 | wxMutexError 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(¤t); | |
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 | |
133 | while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current) | |
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 | ||
146 | wxMutexError wxMutex::TryLock() | |
147 | { | |
6fe73788 | 148 | wxMacStCritical critical ; |
9c152f80 SC |
149 | if ( UMASystemIsInitialized() ) |
150 | { | |
151 | OSErr err ; | |
152 | ThreadID current = kNoThreadID; | |
153 | ::MacGetCurrentThread(¤t); | |
154 | // if we are not the owner, give an error back | |
155 | if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current ) | |
156 | return wxMUTEX_BUSY; | |
157 | ||
158 | m_internal->m_owner = current; | |
159 | } | |
e9576ca5 | 160 | m_locked++; |
a959b088 SC |
161 | |
162 | return wxMUTEX_NO_ERROR; | |
e9576ca5 SC |
163 | } |
164 | ||
165 | wxMutexError wxMutex::Unlock() | |
166 | { | |
9c152f80 SC |
167 | if ( UMASystemIsInitialized() ) |
168 | { | |
169 | OSErr err; | |
170 | err = ::ThreadBeginCritical(); | |
171 | ||
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; | |
181 | while (!m_internal->m_waiters.IsEmpty() && !found) | |
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 | |
186 | found = (err != threadNotFoundErr); | |
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 | ||
207 | class wxConditionInternal | |
208 | { | |
e9576ca5 | 209 | public: |
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 |
254 | wxCondition::wxCondition() |
255 | { | |
256 | m_internal = new wxConditionInternal; | |
e9576ca5 SC |
257 | } |
258 | ||
259 | wxCondition::~wxCondition() | |
260 | { | |
a959b088 | 261 | delete m_internal; |
e9576ca5 SC |
262 | } |
263 | ||
a959b088 | 264 | void wxCondition::Wait() |
e9576ca5 | 265 | { |
6fe73788 | 266 | (void)m_internal->Wait(0xFFFFFFFFL); |
e9576ca5 SC |
267 | } |
268 | ||
a959b088 | 269 | bool 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 | ||
275 | void 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 | ||
285 | void 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 |
309 | class wxThreadInternal |
310 | { | |
311 | public: | |
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; } |
a959b088 SC |
342 | |
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 | |
352 | private: | |
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 |
360 | static wxArrayPtrVoid s_threads ; |
361 | ||
362 | ThreadEntryUPP wxThreadInternal::s_threadEntry = NULL ; | |
363 | pascal 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 | } | |
392 | void wxThreadInternal::SetPriority(unsigned int priority) | |
393 | { | |
6fe73788 | 394 | // Priorities don't exist on Mac |
e7549107 SC |
395 | } |
396 | ||
6fe73788 | 397 | bool 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 | ||
425 | bool wxThreadInternal::Suspend() | |
426 | { | |
6fe73788 RL |
427 | OSErr err ; |
428 | ||
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 | ||
445 | bool wxThreadInternal::Resume() | |
446 | { | |
6fe73788 RL |
447 | ThreadID current ; |
448 | OSErr err ; | |
449 | err = MacGetCurrentThread( ¤t ) ; | |
450 | ||
451 | wxASSERT( err == noErr ) ; | |
452 | wxASSERT( current != m_tid ) ; | |
453 | ||
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; |
6fe73788 RL |
460 | |
461 | } | |
462 | err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID); | |
463 | wxASSERT( err == noErr ) ; | |
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 |
473 | wxThread *wxThread::This() |
474 | { | |
6fe73788 RL |
475 | wxMacStCritical critical ; |
476 | ||
477 | ThreadID current ; | |
478 | OSErr err ; | |
479 | ||
480 | err = MacGetCurrentThread( ¤t ) ; | |
481 | ||
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 | ||
492 | bool wxThread::IsMain() | |
493 | { | |
6fe73788 RL |
494 | ThreadID current ; |
495 | OSErr err ; | |
496 | ||
497 | err = MacGetCurrentThread( ¤t ) ; | |
a959b088 | 498 | return current == gs_idMainThread; |
e7549107 SC |
499 | } |
500 | ||
501 | #ifdef Yield | |
a959b088 | 502 | #undef Yield |
e7549107 SC |
503 | #endif |
504 | ||
505 | void wxThread::Yield() | |
506 | { | |
6fe73788 | 507 | ::YieldToAnyThread() ; |
e7549107 SC |
508 | } |
509 | ||
510 | void wxThread::Sleep(unsigned long milliseconds) | |
511 | { | |
6fe73788 RL |
512 | clock_t start = clock() ; |
513 | do | |
514 | { | |
515 | YieldToAnyThread() ; | |
516 | } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; | |
a959b088 SC |
517 | } |
518 | ||
519 | int 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 | ||
525 | bool wxThread::SetConcurrency(size_t level) | |
526 | { | |
527 | wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); | |
528 | ||
529 | // ok only for the default one | |
530 | if ( level == 0 ) | |
531 | return 0; | |
532 | ||
533 | // how many CPUs have we got? | |
534 | if ( GetCPUCount() == 1 ) | |
535 | { | |
536 | // don't bother with all this complicated stuff - on a single | |
537 | // processor system it doesn't make much sense anyhow | |
538 | return level == 1; | |
539 | } | |
540 | ||
541 | return TRUE ; | |
542 | } | |
543 | ||
544 | // ctor and dtor | |
545 | // ------------- | |
546 | ||
547 | wxThread::wxThread(wxThreadKind kind) | |
548 | { | |
549 | m_internal = new wxThreadInternal(); | |
550 | ||
551 | m_isDetached = kind == wxTHREAD_DETACHED; | |
552 | s_threads.Add( (void*) this ) ; | |
553 | } | |
554 | ||
555 | wxThread::~wxThread() | |
556 | { | |
6fe73788 | 557 | s_threads.Remove( (void*) this ) ; |
a959b088 | 558 | delete m_internal; |
e7549107 SC |
559 | } |
560 | ||
561 | // create/start thread | |
562 | // ------------------- | |
563 | ||
6fe73788 | 564 | wxThreadError wxThread::Create(unsigned int stackSize) |
e9576ca5 | 565 | { |
a959b088 SC |
566 | wxCriticalSectionLocker lock(m_critsect); |
567 | ||
6fe73788 | 568 | if ( !m_internal->Create(this, stackSize) ) |
e7549107 SC |
569 | return wxTHREAD_NO_RESOURCE; |
570 | ||
e9576ca5 SC |
571 | return wxTHREAD_NO_ERROR; |
572 | } | |
573 | ||
e7549107 | 574 | wxThreadError wxThread::Run() |
e9576ca5 | 575 | { |
e7549107 SC |
576 | wxCriticalSectionLocker lock(m_critsect); |
577 | ||
a959b088 | 578 | if ( m_internal->GetState() != STATE_NEW ) |
e7549107 SC |
579 | { |
580 | // actually, it may be almost any state at all, not only STATE_RUNNING | |
581 | return wxTHREAD_RUNNING; | |
582 | } | |
583 | ||
a959b088 | 584 | // the thread has just been created and is still suspended - let it run |
e7549107 | 585 | return Resume(); |
e9576ca5 SC |
586 | } |
587 | ||
e7549107 SC |
588 | // suspend/resume thread |
589 | // --------------------- | |
590 | ||
e9576ca5 SC |
591 | wxThreadError wxThread::Pause() |
592 | { | |
e7549107 SC |
593 | wxCriticalSectionLocker lock(m_critsect); |
594 | ||
a959b088 | 595 | return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
e9576ca5 SC |
596 | } |
597 | ||
598 | wxThreadError wxThread::Resume() | |
599 | { | |
e7549107 | 600 | wxCriticalSectionLocker lock(m_critsect); |
e9576ca5 | 601 | |
a959b088 | 602 | return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
e9576ca5 SC |
603 | } |
604 | ||
e7549107 SC |
605 | // stopping thread |
606 | // --------------- | |
607 | ||
a959b088 SC |
608 | wxThread::ExitCode wxThread::Wait() |
609 | { | |
610 | // although under MacOS we can wait for any thread, it's an error to | |
611 | // wait for a detached one in wxWin API | |
612 | wxCHECK_MSG( !IsDetached(), (ExitCode)-1, | |
613 | _T("can't wait for detached thread") ); | |
614 | ||
615 | ExitCode rc = (ExitCode)-1; | |
616 | ||
617 | (void)Delete(&rc); | |
618 | ||
619 | m_internal->Free(); | |
620 | ||
621 | return rc; | |
622 | } | |
623 | ||
624 | wxThreadError wxThread::Delete(ExitCode *pRc) | |
e9576ca5 | 625 | { |
e7549107 SC |
626 | ExitCode rc = 0; |
627 | ||
628 | // Delete() is always safe to call, so consider all possible states | |
a959b088 SC |
629 | |
630 | // has the thread started to run? | |
631 | bool shouldResume = FALSE; | |
632 | ||
633 | { | |
634 | wxCriticalSectionLocker lock(m_critsect); | |
635 | ||
636 | if ( m_internal->GetState() == STATE_NEW ) | |
637 | { | |
638 | // WinThreadStart() will see it and terminate immediately | |
639 | m_internal->SetState(STATE_EXITED); | |
640 | ||
641 | shouldResume = TRUE; | |
642 | } | |
643 | } | |
644 | ||
645 | // is the thread paused? | |
646 | if ( shouldResume || IsPaused() ) | |
e7549107 SC |
647 | Resume(); |
648 | ||
a959b088 | 649 | // does is still run? |
e7549107 SC |
650 | if ( IsRunning() ) |
651 | { | |
652 | if ( IsMain() ) | |
653 | { | |
654 | // set flag for wxIsWaitingForThread() | |
a959b088 | 655 | gs_waitingForThread = TRUE; |
e7549107 | 656 | |
a959b088 | 657 | #if wxUSE_GUI |
e7549107 | 658 | wxBeginBusyCursor(); |
a959b088 | 659 | #endif // wxUSE_GUI |
e7549107 SC |
660 | } |
661 | ||
a959b088 | 662 | // ask the thread to terminate |
e7549107 SC |
663 | { |
664 | wxCriticalSectionLocker lock(m_critsect); | |
665 | ||
a959b088 | 666 | m_internal->Cancel(); |
e7549107 SC |
667 | } |
668 | ||
a959b088 SC |
669 | #if wxUSE_GUI |
670 | // simply wait for the thread to terminate | |
6fe73788 RL |
671 | while( TestDestroy() ) |
672 | { | |
673 | ::YieldToAnyThread() ; | |
674 | } | |
a959b088 SC |
675 | #else // !wxUSE_GUI |
676 | // simply wait for the thread to terminate | |
6fe73788 RL |
677 | while( TestDestroy() ) |
678 | { | |
679 | ::YieldToAnyThread() ; | |
680 | } | |
a959b088 | 681 | #endif // wxUSE_GUI/!wxUSE_GUI |
e7549107 SC |
682 | |
683 | if ( IsMain() ) | |
684 | { | |
a959b088 | 685 | gs_waitingForThread = FALSE; |
e7549107 | 686 | |
a959b088 | 687 | #if wxUSE_GUI |
e7549107 | 688 | wxEndBusyCursor(); |
a959b088 | 689 | #endif // wxUSE_GUI |
e7549107 | 690 | } |
a959b088 | 691 | } |
e7549107 | 692 | |
a959b088 SC |
693 | // if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) ) |
694 | { | |
695 | wxLogLastError("GetExitCodeThread"); | |
e7549107 | 696 | |
a959b088 SC |
697 | rc = (ExitCode)-1; |
698 | } | |
e7549107 | 699 | |
a959b088 SC |
700 | if ( IsDetached() ) |
701 | { | |
702 | // if the thread exits normally, this is done in WinThreadStart, but in | |
703 | // this case it would have been too early because | |
704 | // MsgWaitForMultipleObject() would fail if the therad handle was | |
705 | // closed while we were waiting on it, so we must do it here | |
706 | delete this; | |
e7549107 SC |
707 | } |
708 | ||
a959b088 SC |
709 | // wxASSERT_MSG( (DWORD)rc != STILL_ACTIVE, |
710 | // wxT("thread must be already terminated.") ); | |
711 | ||
712 | if ( pRc ) | |
713 | *pRc = rc; | |
714 | ||
715 | return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR; | |
e9576ca5 SC |
716 | } |
717 | ||
e7549107 | 718 | wxThreadError wxThread::Kill() |
e9576ca5 | 719 | { |
e7549107 SC |
720 | if ( !IsRunning() ) |
721 | return wxTHREAD_NOT_RUNNING; | |
722 | ||
a959b088 | 723 | // if ( !::TerminateThread(m_internal->GetHandle(), (DWORD)-1) ) |
e7549107 SC |
724 | { |
725 | wxLogSysError(_("Couldn't terminate thread")); | |
726 | ||
727 | return wxTHREAD_MISC_ERROR; | |
728 | } | |
729 | ||
a959b088 SC |
730 | m_internal->Free(); |
731 | ||
732 | if ( IsDetached() ) | |
733 | { | |
734 | delete this; | |
735 | } | |
e7549107 SC |
736 | |
737 | return wxTHREAD_NO_ERROR; | |
e9576ca5 SC |
738 | } |
739 | ||
a959b088 | 740 | void wxThread::Exit(ExitCode status) |
e9576ca5 | 741 | { |
a959b088 | 742 | m_internal->Free(); |
e7549107 | 743 | |
a959b088 SC |
744 | if ( IsDetached() ) |
745 | { | |
746 | delete this; | |
747 | } | |
e7549107 | 748 | |
6fe73788 | 749 | m_internal->SetResult( status ) ; |
a959b088 | 750 | |
6fe73788 | 751 | /* |
a959b088 SC |
752 | #if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) |
753 | _endthreadex((unsigned)status); | |
754 | #else // !VC++ | |
755 | ::ExitThread((DWORD)status); | |
756 | #endif // VC++/!VC++ | |
757 | */ | |
e7549107 | 758 | wxFAIL_MSG(wxT("Couldn't return from ExitThread()!")); |
e9576ca5 SC |
759 | } |
760 | ||
a959b088 SC |
761 | // priority setting |
762 | // ---------------- | |
763 | ||
764 | // since all these calls are execute cooperatively we don't have to use the critical section | |
765 | ||
e7549107 | 766 | void wxThread::SetPriority(unsigned int prio) |
e9576ca5 | 767 | { |
a959b088 | 768 | m_internal->SetPriority(prio); |
e9576ca5 SC |
769 | } |
770 | ||
e7549107 | 771 | unsigned int wxThread::GetPriority() const |
e9576ca5 | 772 | { |
a959b088 | 773 | return m_internal->GetPriority(); |
e9576ca5 SC |
774 | } |
775 | ||
a959b088 | 776 | unsigned long wxThread::GetId() const |
e9576ca5 | 777 | { |
a959b088 | 778 | return (unsigned long)m_internal->GetId(); |
e9576ca5 SC |
779 | } |
780 | ||
e7549107 | 781 | bool wxThread::IsRunning() const |
e9576ca5 | 782 | { |
a959b088 | 783 | return m_internal->GetState() == STATE_RUNNING; |
e9576ca5 | 784 | } |
e9576ca5 SC |
785 | |
786 | bool wxThread::IsAlive() const | |
787 | { | |
a959b088 SC |
788 | return (m_internal->GetState() == STATE_RUNNING) || |
789 | (m_internal->GetState() == STATE_PAUSED); | |
e9576ca5 SC |
790 | } |
791 | ||
e7549107 | 792 | bool wxThread::IsPaused() const |
e9576ca5 | 793 | { |
a959b088 | 794 | return m_internal->GetState() == STATE_PAUSED; |
e9576ca5 SC |
795 | } |
796 | ||
e7549107 | 797 | bool wxThread::TestDestroy() |
e9576ca5 | 798 | { |
a959b088 | 799 | return m_internal->GetState() == STATE_CANCELED; |
e9576ca5 SC |
800 | } |
801 | ||
e7549107 SC |
802 | // ---------------------------------------------------------------------------- |
803 | // Automatic initialization for thread module | |
804 | // ---------------------------------------------------------------------------- | |
e9576ca5 | 805 | |
e7549107 SC |
806 | class wxThreadModule : public wxModule |
807 | { | |
e9576ca5 | 808 | public: |
e7549107 SC |
809 | virtual bool OnInit(); |
810 | virtual void OnExit(); | |
e9576ca5 | 811 | |
e7549107 SC |
812 | private: |
813 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
e9576ca5 SC |
814 | }; |
815 | ||
816 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) | |
817 | ||
e7549107 SC |
818 | bool wxThreadModule::OnInit() |
819 | { | |
6fe73788 RL |
820 | long response; |
821 | bool hasThreadManager ; | |
822 | hasThreadManager = Gestalt( gestaltThreadMgrAttr, &response) == noErr && response & 1; | |
5b781a67 SC |
823 | #if !TARGET_CARBON |
824 | #if GENERATINGCFM | |
6fe73788 RL |
825 | // verify presence of shared library |
826 | hasThreadManager = hasThreadManager && ((Ptr)NewThread != (Ptr)kUnresolvedCFragSymbolAddress); | |
5b781a67 | 827 | #endif |
a959b088 | 828 | #endif |
6fe73788 RL |
829 | if ( !hasThreadManager ) |
830 | { | |
831 | wxMessageBox( "Error" , "Thread Support is not available on this System" , wxOK ) ; | |
832 | return FALSE ; | |
833 | } | |
e7549107 SC |
834 | |
835 | // no error return for GetCurrentThreadId() | |
a959b088 | 836 | MacGetCurrentThread( &gs_idMainThread ) ; |
e7549107 SC |
837 | |
838 | return TRUE; | |
839 | } | |
840 | ||
841 | void wxThreadModule::OnExit() | |
842 | { | |
e7549107 SC |
843 | } |
844 | ||
845 | // ---------------------------------------------------------------------------- | |
a959b088 SC |
846 | // under MacOS we don't have currently preemptive threads, so any thread may access |
847 | // the GUI at any time | |
e7549107 SC |
848 | // ---------------------------------------------------------------------------- |
849 | ||
850 | void WXDLLEXPORT wxMutexGuiEnter() | |
851 | { | |
e7549107 SC |
852 | } |
853 | ||
854 | void WXDLLEXPORT wxMutexGuiLeave() | |
855 | { | |
e7549107 SC |
856 | } |
857 | ||
858 | void WXDLLEXPORT wxMutexGuiLeaveOrEnter() | |
859 | { | |
e7549107 SC |
860 | } |
861 | ||
862 | bool WXDLLEXPORT wxGuiOwnedByMainThread() | |
863 | { | |
a959b088 | 864 | return false ; |
e7549107 SC |
865 | } |
866 | ||
a959b088 | 867 | // wake up the main thread |
e7549107 SC |
868 | void WXDLLEXPORT wxWakeUpMainThread() |
869 | { | |
6fe73788 | 870 | wxMacWakeUp() ; |
e7549107 SC |
871 | } |
872 | ||
873 | bool WXDLLEXPORT wxIsWaitingForThread() | |
874 | { | |
a959b088 | 875 | return false ; |
e7549107 | 876 | } |
e7549107 SC |
877 | |
878 | #endif // wxUSE_THREADS | |
a959b088 | 879 | |
6fe73788 | 880 | // vi:sts=4:sw=4:et |