]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: threadpsx.cpp | |
3 | // Purpose: wxThread (Posix) Implementation | |
4 | // Author: Original from Wolfram Gloger/Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 04/22/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Wolfram Gloger (1996, 1997) | |
9 | // Guilhem Lavaux (1998) | |
10 | // Vadim Zeitlin (1999) | |
11 | // Robert Roebling (1999) | |
12 | // Licence: wxWindows licence | |
13 | ///////////////////////////////////////////////////////////////////////////// | |
14 | ||
15 | // ============================================================================ | |
16 | // declaration | |
17 | // ============================================================================ | |
18 | ||
19 | // ---------------------------------------------------------------------------- | |
20 | // headers | |
21 | // ---------------------------------------------------------------------------- | |
22 | ||
23 | #ifdef __GNUG__ | |
24 | #pragma implementation "thread.h" | |
25 | #endif | |
26 | ||
27 | // With simple makefiles, we must ignore the file body if not using | |
28 | // threads. | |
29 | #include "wx/setup.h" | |
30 | ||
31 | #if wxUSE_THREADS | |
32 | ||
33 | #include "wx/thread.h" | |
34 | #include "wx/module.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/log.h" | |
37 | #include "wx/intl.h" | |
38 | #include "wx/dynarray.h" | |
39 | ||
40 | #include <stdio.h> | |
41 | #include <unistd.h> | |
42 | #include <pthread.h> | |
43 | #include <errno.h> | |
44 | #include <time.h> | |
45 | ||
46 | #if HAVE_SCHED_H | |
47 | #include <sched.h> | |
48 | #endif | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // constants | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | // the possible states of the thread and transitions from them | |
55 | enum wxThreadState | |
56 | { | |
57 | STATE_NEW, // didn't start execution yet (=> RUNNING) | |
58 | STATE_RUNNING, // running (=> PAUSED or EXITED) | |
59 | STATE_PAUSED, // suspended (=> RUNNING or EXITED) | |
60 | STATE_EXITED // thread doesn't exist any more | |
61 | }; | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // types | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | WX_DEFINE_ARRAY(wxThread *, wxArrayThread); | |
68 | ||
69 | // ----------------------------------------------------------------------------- | |
70 | // global data | |
71 | // ----------------------------------------------------------------------------- | |
72 | ||
73 | // we keep the list of all threads created by the application to be able to | |
74 | // terminate them on exit if there are some left - otherwise the process would | |
75 | // be left in memory | |
76 | static wxArrayThread gs_allThreads; | |
77 | ||
78 | // the id of the main thread | |
79 | static pthread_t gs_tidMain; | |
80 | ||
81 | // the key for the pointer to the associated wxThread object | |
82 | static pthread_key_t gs_keySelf; | |
83 | ||
84 | // this mutex must be acquired before any call to a GUI function | |
85 | static wxMutex *gs_mutexGui; | |
86 | ||
87 | // ============================================================================ | |
88 | // implementation | |
89 | // ============================================================================ | |
90 | ||
91 | //-------------------------------------------------------------------- | |
92 | // wxMutex (Posix implementation) | |
93 | //-------------------------------------------------------------------- | |
94 | ||
95 | class wxMutexInternal | |
96 | { | |
97 | public: | |
98 | pthread_mutex_t p_mutex; | |
99 | }; | |
100 | ||
101 | wxMutex::wxMutex() | |
102 | { | |
103 | p_internal = new wxMutexInternal; | |
104 | ||
105 | pthread_mutex_init(&(p_internal->p_mutex), | |
106 | (pthread_mutexattr_t*) NULL ); | |
107 | m_locked = 0; | |
108 | } | |
109 | ||
110 | wxMutex::~wxMutex() | |
111 | { | |
112 | if (m_locked > 0) | |
113 | wxLogDebug(wxT("Freeing a locked mutex (%d locks)"), m_locked); | |
114 | ||
115 | pthread_mutex_destroy( &(p_internal->p_mutex) ); | |
116 | delete p_internal; | |
117 | } | |
118 | ||
119 | wxMutexError wxMutex::Lock() | |
120 | { | |
121 | int err = pthread_mutex_lock( &(p_internal->p_mutex) ); | |
122 | if (err == EDEADLK) | |
123 | { | |
124 | wxLogDebug(wxT("Locking this mutex would lead to deadlock!")); | |
125 | ||
126 | return wxMUTEX_DEAD_LOCK; | |
127 | } | |
128 | ||
129 | m_locked++; | |
130 | ||
131 | return wxMUTEX_NO_ERROR; | |
132 | } | |
133 | ||
134 | wxMutexError wxMutex::TryLock() | |
135 | { | |
136 | if (m_locked) | |
137 | { | |
138 | return wxMUTEX_BUSY; | |
139 | } | |
140 | ||
141 | int err = pthread_mutex_trylock( &(p_internal->p_mutex) ); | |
142 | switch (err) | |
143 | { | |
144 | case EBUSY: return wxMUTEX_BUSY; | |
145 | } | |
146 | ||
147 | m_locked++; | |
148 | ||
149 | return wxMUTEX_NO_ERROR; | |
150 | } | |
151 | ||
152 | wxMutexError wxMutex::Unlock() | |
153 | { | |
154 | if (m_locked > 0) | |
155 | { | |
156 | m_locked--; | |
157 | } | |
158 | else | |
159 | { | |
160 | wxLogDebug(wxT("Unlocking not locked mutex.")); | |
161 | ||
162 | return wxMUTEX_UNLOCKED; | |
163 | } | |
164 | ||
165 | pthread_mutex_unlock( &(p_internal->p_mutex) ); | |
166 | ||
167 | return wxMUTEX_NO_ERROR; | |
168 | } | |
169 | ||
170 | //-------------------------------------------------------------------- | |
171 | // wxCondition (Posix implementation) | |
172 | //-------------------------------------------------------------------- | |
173 | ||
174 | class wxConditionInternal | |
175 | { | |
176 | public: | |
177 | pthread_cond_t p_condition; | |
178 | }; | |
179 | ||
180 | wxCondition::wxCondition() | |
181 | { | |
182 | p_internal = new wxConditionInternal; | |
183 | pthread_cond_init( &(p_internal->p_condition), | |
184 | (pthread_condattr_t *) NULL ); | |
185 | } | |
186 | ||
187 | wxCondition::~wxCondition() | |
188 | { | |
189 | pthread_cond_destroy( &(p_internal->p_condition) ); | |
190 | ||
191 | delete p_internal; | |
192 | } | |
193 | ||
194 | void wxCondition::Wait(wxMutex& mutex) | |
195 | { | |
196 | pthread_cond_wait( &(p_internal->p_condition), &(mutex.p_internal->p_mutex) ); | |
197 | } | |
198 | ||
199 | bool wxCondition::Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec) | |
200 | { | |
201 | struct timespec tspec; | |
202 | ||
203 | tspec.tv_sec = time(0L)+sec; | |
204 | tspec.tv_nsec = nsec; | |
205 | return (pthread_cond_timedwait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex), &tspec) != ETIMEDOUT); | |
206 | } | |
207 | ||
208 | void wxCondition::Signal() | |
209 | { | |
210 | pthread_cond_signal( &(p_internal->p_condition) ); | |
211 | } | |
212 | ||
213 | void wxCondition::Broadcast() | |
214 | { | |
215 | pthread_cond_broadcast( &(p_internal->p_condition) ); | |
216 | } | |
217 | ||
218 | //-------------------------------------------------------------------- | |
219 | // wxThread (Posix implementation) | |
220 | //-------------------------------------------------------------------- | |
221 | ||
222 | class wxThreadInternal | |
223 | { | |
224 | public: | |
225 | wxThreadInternal(); | |
226 | ~wxThreadInternal(); | |
227 | ||
228 | // thread entry function | |
229 | static void *PthreadStart(void *ptr); | |
230 | ||
231 | #if HAVE_THREAD_CLEANUP_FUNCTIONS | |
232 | // thread exit function | |
233 | static void PthreadCleanup(void *ptr); | |
234 | #endif | |
235 | ||
236 | // thread actions | |
237 | // start the thread | |
238 | wxThreadError Run(); | |
239 | // ask the thread to terminate | |
240 | void Wait(); | |
241 | // wake up threads waiting for our termination | |
242 | void SignalExit(); | |
243 | // go to sleep until Resume() is called | |
244 | void Pause(); | |
245 | // resume the thread | |
246 | void Resume(); | |
247 | ||
248 | // accessors | |
249 | // priority | |
250 | int GetPriority() const { return m_prio; } | |
251 | void SetPriority(int prio) { m_prio = prio; } | |
252 | // state | |
253 | wxThreadState GetState() const { return m_state; } | |
254 | void SetState(wxThreadState state) { m_state = state; } | |
255 | // id | |
256 | pthread_t GetId() const { return m_threadId; } | |
257 | pthread_t *GetIdPtr() { return &m_threadId; } | |
258 | // "cancelled" flag | |
259 | void SetCancelFlag() { m_cancelled = TRUE; } | |
260 | bool WasCancelled() const { return m_cancelled; } | |
261 | ||
262 | private: | |
263 | pthread_t m_threadId; // id of the thread | |
264 | wxThreadState m_state; // see wxThreadState enum | |
265 | int m_prio; // in wxWindows units: from 0 to 100 | |
266 | ||
267 | // set when the thread should terminate | |
268 | bool m_cancelled; | |
269 | ||
270 | // this (mutex, cond) pair is used to synchronize the main thread and this | |
271 | // thread in several situations: | |
272 | // 1. The thread function blocks until condition is signaled by Run() when | |
273 | // it's initially created - this allows thread creation in "suspended" | |
274 | // state | |
275 | // 2. The Delete() function blocks until the condition is signaled when the | |
276 | // thread exits. | |
277 | // GL: On Linux, this may fail because we can have a deadlock in either | |
278 | // SignalExit() or Wait(): so we add m_end_mutex for the finalization. | |
279 | wxMutex m_mutex, m_end_mutex; | |
280 | wxCondition m_cond; | |
281 | ||
282 | // another (mutex, cond) pair for Pause()/Resume() usage | |
283 | // | |
284 | // VZ: it's possible that we might reuse the mutex and condition from above | |
285 | // for this too, but as I'm not at all sure that it won't create subtle | |
286 | // problems with race conditions between, say, Pause() and Delete() I | |
287 | // prefer this may be a bit less efficient but much safer solution | |
288 | wxMutex m_mutexSuspend; | |
289 | wxCondition m_condSuspend; | |
290 | }; | |
291 | ||
292 | void *wxThreadInternal::PthreadStart(void *ptr) | |
293 | { | |
294 | wxThread *thread = (wxThread *)ptr; | |
295 | wxThreadInternal *pthread = thread->p_internal; | |
296 | void *status; | |
297 | ||
298 | int rc = pthread_setspecific(gs_keySelf, thread); | |
299 | if ( rc != 0 ) | |
300 | { | |
301 | wxLogSysError(rc, _("Cannot start thread: error writing TLS")); | |
302 | ||
303 | return (void *)-1; | |
304 | } | |
305 | #if HAVE_THREAD_CLEANUP_FUNCTIONS | |
306 | // Install the cleanup handler. | |
307 | pthread_cleanup_push(wxThreadInternal::PthreadCleanup, ptr); | |
308 | #endif | |
309 | ||
310 | // wait for the condition to be signaled from Run() | |
311 | // mutex state: currently locked by the thread which created us | |
312 | pthread->m_cond.Wait(pthread->m_mutex); | |
313 | // mutex state: locked again on exit of Wait() | |
314 | ||
315 | // call the main entry | |
316 | status = thread->Entry(); | |
317 | ||
318 | #if HAVE_THREAD_CLEANUP_FUNCTIONS | |
319 | pthread_cleanup_pop(FALSE); | |
320 | #endif | |
321 | ||
322 | // terminate the thread | |
323 | thread->Exit(status); | |
324 | ||
325 | wxFAIL_MSG(wxT("wxThread::Exit() can't return.")); | |
326 | ||
327 | return NULL; | |
328 | } | |
329 | ||
330 | #if HAVE_THREAD_CLEANUP_FUNCTIONS | |
331 | // Only called when the thread is explicitely killed. | |
332 | ||
333 | void wxThreadInternal::PthreadCleanup(void *ptr) | |
334 | { | |
335 | wxThread *thread = (wxThread *) ptr; | |
336 | ||
337 | // The thread is already considered as finished. | |
338 | if (thread->p_internal->GetState() == STATE_EXITED) | |
339 | return; | |
340 | ||
341 | // first call user-level clean up code | |
342 | thread->OnExit(); | |
343 | ||
344 | // next wake up the threads waiting for us (OTOH, this function won't retur | |
345 | // until someone waited for us!) | |
346 | thread->p_internal->SetState(STATE_EXITED); | |
347 | ||
348 | thread->p_internal->SignalExit(); | |
349 | } | |
350 | #endif | |
351 | ||
352 | wxThreadInternal::wxThreadInternal() | |
353 | { | |
354 | m_state = STATE_NEW; | |
355 | m_cancelled = FALSE; | |
356 | m_prio = WXTHREAD_DEFAULT_PRIORITY; | |
357 | m_threadId = 0; | |
358 | ||
359 | // this mutex is locked during almost all thread lifetime - it will only be | |
360 | // unlocked in the very end | |
361 | m_mutex.Lock(); | |
362 | ||
363 | // this mutex is used by wxThreadInternal::Wait() and by | |
364 | // wxThreadInternal::SignalExit(). We don't use m_mutex because of a | |
365 | // possible deadlock in either Wait() or SignalExit(). | |
366 | m_end_mutex.Lock(); | |
367 | ||
368 | // this mutex is used in Pause()/Resume() and is also locked all the time | |
369 | // unless the thread is paused | |
370 | m_mutexSuspend.Lock(); | |
371 | } | |
372 | ||
373 | wxThreadInternal::~wxThreadInternal() | |
374 | { | |
375 | // GL: moved to SignalExit | |
376 | // m_mutexSuspend.Unlock(); | |
377 | ||
378 | // note that m_mutex will be unlocked by the thread which waits for our | |
379 | // termination | |
380 | ||
381 | // In the case, we didn't start the thread, all these mutex are locked: | |
382 | // we must unlock them. | |
383 | if (m_mutex.IsLocked()) | |
384 | m_mutex.Unlock(); | |
385 | ||
386 | if (m_end_mutex.IsLocked()) | |
387 | m_end_mutex.Unlock(); | |
388 | ||
389 | if (m_mutexSuspend.IsLocked()) | |
390 | m_mutexSuspend.Unlock(); | |
391 | } | |
392 | ||
393 | wxThreadError wxThreadInternal::Run() | |
394 | { | |
395 | wxCHECK_MSG( GetState() == STATE_NEW, wxTHREAD_RUNNING, | |
396 | wxT("thread may only be started once after successful Create()") ); | |
397 | ||
398 | // the mutex was locked on Create(), so we will be able to lock it again | |
399 | // only when the thread really starts executing and enters the wait - | |
400 | // otherwise we might signal the condition before anybody is waiting for it | |
401 | wxMutexLocker lock(m_mutex); | |
402 | m_cond.Signal(); | |
403 | ||
404 | m_state = STATE_RUNNING; | |
405 | ||
406 | return wxTHREAD_NO_ERROR; | |
407 | ||
408 | // now the mutex is unlocked back - but just to allow Wait() function to | |
409 | // terminate by relocking it, so the net result is that the worker thread | |
410 | // starts executing and the mutex is still locked | |
411 | } | |
412 | ||
413 | void wxThreadInternal::Wait() | |
414 | { | |
415 | wxCHECK_RET( WasCancelled(), wxT("thread should have been cancelled first") ); | |
416 | ||
417 | // if the thread we're waiting for is waiting for the GUI mutex, we will | |
418 | // deadlock so make sure we release it temporarily | |
419 | if ( wxThread::IsMain() ) | |
420 | wxMutexGuiLeave(); | |
421 | ||
422 | // entering Wait() releases the mutex thus allowing SignalExit() to acquire | |
423 | // it and to signal us its termination | |
424 | m_cond.Wait(m_end_mutex); | |
425 | ||
426 | // mutex is still in the locked state - relocked on exit from Wait(), so | |
427 | // unlock it - we don't need it any more, the thread has already terminated | |
428 | m_end_mutex.Unlock(); | |
429 | ||
430 | // After that, we wait for the real end of the other thread. | |
431 | pthread_join(GetId(), NULL); | |
432 | ||
433 | // reacquire GUI mutex | |
434 | if ( wxThread::IsMain() ) | |
435 | wxMutexGuiEnter(); | |
436 | } | |
437 | ||
438 | void wxThreadInternal::SignalExit() | |
439 | { | |
440 | // GL: Unlock mutexSuspend here. | |
441 | m_mutexSuspend.Unlock(); | |
442 | ||
443 | // as mutex is currently locked, this will block until some other thread | |
444 | // (normally the same which created this one) unlocks it by entering Wait() | |
445 | m_end_mutex.Lock(); | |
446 | ||
447 | // wake up all the threads waiting for our termination | |
448 | m_cond.Broadcast(); | |
449 | ||
450 | // after this call mutex will be finally unlocked | |
451 | m_end_mutex.Unlock(); | |
452 | } | |
453 | ||
454 | void wxThreadInternal::Pause() | |
455 | { | |
456 | // the state is set from the thread which pauses us first, this function | |
457 | // is called later so the state should have been already set | |
458 | wxCHECK_RET( m_state == STATE_PAUSED, | |
459 | wxT("thread must first be paused with wxThread::Pause().") ); | |
460 | ||
461 | // don't pause the thread which is being terminated - this would lead to | |
462 | // deadlock if the thread is paused after Delete() had called Resume() but | |
463 | // before it had time to call Wait() | |
464 | if ( WasCancelled() ) | |
465 | return; | |
466 | ||
467 | // wait until the condition is signaled from Resume() | |
468 | m_condSuspend.Wait(m_mutexSuspend); | |
469 | } | |
470 | ||
471 | void wxThreadInternal::Resume() | |
472 | { | |
473 | wxCHECK_RET( m_state == STATE_PAUSED, | |
474 | wxT("can't resume thread which is not suspended.") ); | |
475 | ||
476 | // we will be able to lock this mutex only when Pause() starts waiting | |
477 | wxMutexLocker lock(m_mutexSuspend); | |
478 | m_condSuspend.Signal(); | |
479 | ||
480 | SetState(STATE_RUNNING); | |
481 | } | |
482 | ||
483 | // ----------------------------------------------------------------------------- | |
484 | // static functions | |
485 | // ----------------------------------------------------------------------------- | |
486 | ||
487 | wxThread *wxThread::This() | |
488 | { | |
489 | return (wxThread *)pthread_getspecific(gs_keySelf); | |
490 | } | |
491 | ||
492 | bool wxThread::IsMain() | |
493 | { | |
494 | return (bool)pthread_equal(pthread_self(), gs_tidMain); | |
495 | } | |
496 | ||
497 | void wxThread::Yield() | |
498 | { | |
499 | sched_yield(); | |
500 | } | |
501 | ||
502 | void wxThread::Sleep(unsigned long milliseconds) | |
503 | { | |
504 | wxUsleep(milliseconds); | |
505 | } | |
506 | ||
507 | // ----------------------------------------------------------------------------- | |
508 | // creating thread | |
509 | // ----------------------------------------------------------------------------- | |
510 | ||
511 | wxThread::wxThread() | |
512 | { | |
513 | // add this thread to the global list of all threads | |
514 | gs_allThreads.Add(this); | |
515 | ||
516 | p_internal = new wxThreadInternal(); | |
517 | } | |
518 | ||
519 | wxThreadError wxThread::Create() | |
520 | { | |
521 | if (p_internal->GetState() != STATE_NEW) | |
522 | return wxTHREAD_RUNNING; | |
523 | ||
524 | // set up the thread attribute: right now, we only set thread priority | |
525 | pthread_attr_t attr; | |
526 | pthread_attr_init(&attr); | |
527 | ||
528 | #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS | |
529 | int prio; | |
530 | if ( pthread_attr_getschedpolicy(&attr, &prio) != 0 ) | |
531 | { | |
532 | wxLogError(_("Cannot retrieve thread scheduling policy.")); | |
533 | } | |
534 | ||
535 | int min_prio = sched_get_priority_min(prio), | |
536 | max_prio = sched_get_priority_max(prio); | |
537 | ||
538 | if ( min_prio == -1 || max_prio == -1 ) | |
539 | { | |
540 | wxLogError(_("Cannot get priority range for scheduling policy %d."), | |
541 | prio); | |
542 | } | |
543 | else if ( max_prio == min_prio ) | |
544 | { | |
545 | if ( p_internal->GetPriority() != WXTHREAD_DEFAULT_PRIORITY ) | |
546 | { | |
547 | // notify the programmer that this doesn't work here | |
548 | wxLogWarning(_("Thread priority setting is ignored.")); | |
549 | } | |
550 | //else: we have default priority, so don't complain | |
551 | ||
552 | // anyhow, don't do anything because priority is just ignored | |
553 | } | |
554 | else | |
555 | { | |
556 | struct sched_param sp; | |
557 | pthread_attr_getschedparam(&attr, &sp); | |
558 | sp.sched_priority = min_prio + | |
559 | (p_internal->GetPriority()*(max_prio-min_prio))/100; | |
560 | pthread_attr_setschedparam(&attr, &sp); | |
561 | } | |
562 | #endif // HAVE_THREAD_PRIORITY_FUNCTIONS | |
563 | ||
564 | #ifdef HAVE_PTHREAD_ATTR_SETSCOPE | |
565 | // this will make the threads created by this process really concurrent | |
566 | pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); | |
567 | #endif // HAVE_PTHREAD_ATTR_SETSCOPE | |
568 | ||
569 | // create the new OS thread object | |
570 | int rc = pthread_create(p_internal->GetIdPtr(), &attr, | |
571 | wxThreadInternal::PthreadStart, (void *)this); | |
572 | pthread_attr_destroy(&attr); | |
573 | ||
574 | if ( rc != 0 ) | |
575 | { | |
576 | p_internal->SetState(STATE_EXITED); | |
577 | return wxTHREAD_NO_RESOURCE; | |
578 | } | |
579 | ||
580 | return wxTHREAD_NO_ERROR; | |
581 | } | |
582 | ||
583 | wxThreadError wxThread::Run() | |
584 | { | |
585 | wxCHECK_MSG( p_internal->GetId(), wxTHREAD_MISC_ERROR, | |
586 | wxT("must call wxThread::Create() first") ); | |
587 | ||
588 | return p_internal->Run(); | |
589 | } | |
590 | ||
591 | // ----------------------------------------------------------------------------- | |
592 | // misc accessors | |
593 | // ----------------------------------------------------------------------------- | |
594 | ||
595 | void wxThread::SetPriority(unsigned int prio) | |
596 | { | |
597 | wxCHECK_RET( ((int)WXTHREAD_MIN_PRIORITY <= (int)prio) && | |
598 | ((int)prio <= (int)WXTHREAD_MAX_PRIORITY), | |
599 | wxT("invalid thread priority") ); | |
600 | ||
601 | wxCriticalSectionLocker lock(m_critsect); | |
602 | ||
603 | switch ( p_internal->GetState() ) | |
604 | { | |
605 | case STATE_NEW: | |
606 | // thread not yet started, priority will be set when it is | |
607 | p_internal->SetPriority(prio); | |
608 | break; | |
609 | ||
610 | case STATE_RUNNING: | |
611 | case STATE_PAUSED: | |
612 | #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS | |
613 | { | |
614 | struct sched_param sparam; | |
615 | sparam.sched_priority = prio; | |
616 | ||
617 | if ( pthread_setschedparam(p_internal->GetId(), | |
618 | SCHED_OTHER, &sparam) != 0 ) | |
619 | { | |
620 | wxLogError(_("Failed to set thread priority %d."), prio); | |
621 | } | |
622 | } | |
623 | #endif // HAVE_THREAD_PRIORITY_FUNCTIONS | |
624 | break; | |
625 | ||
626 | case STATE_EXITED: | |
627 | default: | |
628 | wxFAIL_MSG(wxT("impossible to set thread priority in this state")); | |
629 | } | |
630 | } | |
631 | ||
632 | unsigned int wxThread::GetPriority() const | |
633 | { | |
634 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); | |
635 | ||
636 | return p_internal->GetPriority(); | |
637 | } | |
638 | ||
639 | unsigned long wxThread::GetID() const | |
640 | { | |
641 | return (unsigned long)p_internal->GetId(); | |
642 | } | |
643 | ||
644 | // ----------------------------------------------------------------------------- | |
645 | // pause/resume | |
646 | // ----------------------------------------------------------------------------- | |
647 | ||
648 | wxThreadError wxThread::Pause() | |
649 | { | |
650 | wxCriticalSectionLocker lock(m_critsect); | |
651 | ||
652 | if ( p_internal->GetState() != STATE_RUNNING ) | |
653 | { | |
654 | wxLogDebug(wxT("Can't pause thread which is not running.")); | |
655 | ||
656 | return wxTHREAD_NOT_RUNNING; | |
657 | } | |
658 | ||
659 | p_internal->SetState(STATE_PAUSED); | |
660 | ||
661 | return wxTHREAD_NO_ERROR; | |
662 | } | |
663 | ||
664 | wxThreadError wxThread::Resume() | |
665 | { | |
666 | wxCriticalSectionLocker lock(m_critsect); | |
667 | ||
668 | if ( p_internal->GetState() == STATE_PAUSED ) | |
669 | { | |
670 | m_critsect.Leave(); | |
671 | p_internal->Resume(); | |
672 | m_critsect.Enter(); | |
673 | ||
674 | return wxTHREAD_NO_ERROR; | |
675 | } | |
676 | else | |
677 | { | |
678 | wxLogDebug(wxT("Attempt to resume a thread which is not paused.")); | |
679 | ||
680 | return wxTHREAD_MISC_ERROR; | |
681 | } | |
682 | } | |
683 | ||
684 | // ----------------------------------------------------------------------------- | |
685 | // exiting thread | |
686 | // ----------------------------------------------------------------------------- | |
687 | ||
688 | wxThread::ExitCode wxThread::Delete() | |
689 | { | |
690 | if (IsPaused()) | |
691 | Resume(); | |
692 | ||
693 | m_critsect.Enter(); | |
694 | wxThreadState state = p_internal->GetState(); | |
695 | ||
696 | // ask the thread to stop | |
697 | p_internal->SetCancelFlag(); | |
698 | ||
699 | m_critsect.Leave(); | |
700 | ||
701 | switch ( state ) | |
702 | { | |
703 | case STATE_NEW: | |
704 | case STATE_EXITED: | |
705 | // nothing to do | |
706 | break; | |
707 | ||
708 | case STATE_PAUSED: | |
709 | // resume the thread first | |
710 | Resume(); | |
711 | ||
712 | // fall through | |
713 | ||
714 | default: | |
715 | // wait until the thread stops | |
716 | p_internal->Wait(); | |
717 | } | |
718 | //GL: As we must auto-destroy, the destruction must happen here. | |
719 | delete this; | |
720 | ||
721 | return NULL; | |
722 | } | |
723 | ||
724 | wxThreadError wxThread::Kill() | |
725 | { | |
726 | switch ( p_internal->GetState() ) | |
727 | { | |
728 | case STATE_NEW: | |
729 | case STATE_EXITED: | |
730 | return wxTHREAD_NOT_RUNNING; | |
731 | ||
732 | default: | |
733 | #ifdef HAVE_PTHREAD_CANCEL | |
734 | if ( pthread_cancel(p_internal->GetId()) != 0 ) | |
735 | #endif | |
736 | { | |
737 | wxLogError(_("Failed to terminate a thread.")); | |
738 | ||
739 | return wxTHREAD_MISC_ERROR; | |
740 | } | |
741 | //GL: As we must auto-destroy, the destruction must happen here (2). | |
742 | delete this; | |
743 | ||
744 | return wxTHREAD_NO_ERROR; | |
745 | } | |
746 | } | |
747 | ||
748 | void wxThread::Exit(void *status) | |
749 | { | |
750 | // first call user-level clean up code | |
751 | OnExit(); | |
752 | ||
753 | // next wake up the threads waiting for us (OTOH, this function won't return | |
754 | // until someone waited for us!) | |
755 | p_internal->SignalExit(); | |
756 | ||
757 | p_internal->SetState(STATE_EXITED); | |
758 | ||
759 | // delete both C++ thread object and terminate the OS thread object | |
760 | // GL: This is very ugly and buggy ... | |
761 | // delete this; | |
762 | pthread_exit(status); | |
763 | } | |
764 | ||
765 | // also test whether we were paused | |
766 | bool wxThread::TestDestroy() | |
767 | { | |
768 | wxCriticalSectionLocker lock(m_critsect); | |
769 | ||
770 | if ( p_internal->GetState() == STATE_PAUSED ) | |
771 | { | |
772 | // leave the crit section or the other threads will stop too if they try | |
773 | // to call any of (seemingly harmless) IsXXX() functions while we sleep | |
774 | m_critsect.Leave(); | |
775 | ||
776 | p_internal->Pause(); | |
777 | ||
778 | // enter it back before it's finally left in lock object dtor | |
779 | m_critsect.Enter(); | |
780 | } | |
781 | ||
782 | return p_internal->WasCancelled(); | |
783 | } | |
784 | ||
785 | wxThread::~wxThread() | |
786 | { | |
787 | m_critsect.Enter(); | |
788 | if ( p_internal->GetState() != STATE_EXITED && | |
789 | p_internal->GetState() != STATE_NEW ) | |
790 | { | |
791 | wxLogDebug(wxT("The thread is being destroyed although it is still " | |
792 | "running! The application may crash.")); | |
793 | } | |
794 | ||
795 | m_critsect.Leave(); | |
796 | ||
797 | delete p_internal; | |
798 | ||
799 | // remove this thread from the global array | |
800 | gs_allThreads.Remove(this); | |
801 | } | |
802 | ||
803 | // ----------------------------------------------------------------------------- | |
804 | // state tests | |
805 | // ----------------------------------------------------------------------------- | |
806 | ||
807 | bool wxThread::IsRunning() const | |
808 | { | |
809 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); | |
810 | ||
811 | return p_internal->GetState() == STATE_RUNNING; | |
812 | } | |
813 | ||
814 | bool wxThread::IsAlive() const | |
815 | { | |
816 | wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect); | |
817 | ||
818 | switch ( p_internal->GetState() ) | |
819 | { | |
820 | case STATE_RUNNING: | |
821 | case STATE_PAUSED: | |
822 | return TRUE; | |
823 | ||
824 | default: | |
825 | return FALSE; | |
826 | } | |
827 | } | |
828 | ||
829 | bool wxThread::IsPaused() const | |
830 | { | |
831 | wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect); | |
832 | ||
833 | return (p_internal->GetState() == STATE_PAUSED); | |
834 | } | |
835 | ||
836 | //-------------------------------------------------------------------- | |
837 | // wxThreadModule | |
838 | //-------------------------------------------------------------------- | |
839 | ||
840 | class wxThreadModule : public wxModule | |
841 | { | |
842 | public: | |
843 | virtual bool OnInit(); | |
844 | virtual void OnExit(); | |
845 | ||
846 | private: | |
847 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
848 | }; | |
849 | ||
850 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) | |
851 | ||
852 | bool wxThreadModule::OnInit() | |
853 | { | |
854 | int rc = pthread_key_create(&gs_keySelf, NULL /* dtor function */); | |
855 | if ( rc != 0 ) | |
856 | { | |
857 | wxLogSysError(rc, _("Thread module initialization failed: " | |
858 | "failed to create thread key")); | |
859 | ||
860 | return FALSE; | |
861 | } | |
862 | ||
863 | gs_mutexGui = new wxMutex(); | |
864 | ||
865 | gs_tidMain = pthread_self(); | |
866 | ||
867 | gs_mutexGui->Lock(); | |
868 | ||
869 | return TRUE; | |
870 | } | |
871 | ||
872 | void wxThreadModule::OnExit() | |
873 | { | |
874 | wxASSERT_MSG( wxThread::IsMain(), wxT("only main thread can be here") ); | |
875 | ||
876 | // terminate any threads left | |
877 | size_t count = gs_allThreads.GetCount(); | |
878 | if ( count != 0u ) | |
879 | wxLogDebug(wxT("Some threads were not terminated by the application.")); | |
880 | ||
881 | for ( size_t n = 0u; n < count; n++ ) | |
882 | { | |
883 | // Delete calls the destructor which removes the current entry. We | |
884 | // should only delete the first one each time. | |
885 | gs_allThreads[0]->Delete(); | |
886 | } | |
887 | ||
888 | // destroy GUI mutex | |
889 | gs_mutexGui->Unlock(); | |
890 | ||
891 | delete gs_mutexGui; | |
892 | ||
893 | // and free TLD slot | |
894 | (void)pthread_key_delete(gs_keySelf); | |
895 | } | |
896 | ||
897 | // ---------------------------------------------------------------------------- | |
898 | // global functions | |
899 | // ---------------------------------------------------------------------------- | |
900 | ||
901 | void wxMutexGuiEnter() | |
902 | { | |
903 | gs_mutexGui->Lock(); | |
904 | } | |
905 | ||
906 | void wxMutexGuiLeave() | |
907 | { | |
908 | gs_mutexGui->Unlock(); | |
909 | } | |
910 | ||
911 | #endif | |
912 | // wxUSE_THREADS |