]> git.saurik.com Git - wxWidgets.git/blob - src/unix/threadpsx.cpp
a54694647ccf92fd869abe677b123438d33295f0
[wxWidgets.git] / src / unix / threadpsx.cpp
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(wxThreadKind kind)
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 m_isDetached = kind == wxTHREAD_DETACHED;
519 }
520
521 wxThreadError wxThread::Create()
522 {
523 if (p_internal->GetState() != STATE_NEW)
524 return wxTHREAD_RUNNING;
525
526 // set up the thread attribute: right now, we only set thread priority
527 pthread_attr_t attr;
528 pthread_attr_init(&attr);
529
530 #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
531 int prio;
532 if ( pthread_attr_getschedpolicy(&attr, &prio) != 0 )
533 {
534 wxLogError(_("Cannot retrieve thread scheduling policy."));
535 }
536
537 int min_prio = sched_get_priority_min(prio),
538 max_prio = sched_get_priority_max(prio);
539
540 if ( min_prio == -1 || max_prio == -1 )
541 {
542 wxLogError(_("Cannot get priority range for scheduling policy %d."),
543 prio);
544 }
545 else if ( max_prio == min_prio )
546 {
547 if ( p_internal->GetPriority() != WXTHREAD_DEFAULT_PRIORITY )
548 {
549 // notify the programmer that this doesn't work here
550 wxLogWarning(_("Thread priority setting is ignored."));
551 }
552 //else: we have default priority, so don't complain
553
554 // anyhow, don't do anything because priority is just ignored
555 }
556 else
557 {
558 struct sched_param sp;
559 pthread_attr_getschedparam(&attr, &sp);
560 sp.sched_priority = min_prio +
561 (p_internal->GetPriority()*(max_prio-min_prio))/100;
562 pthread_attr_setschedparam(&attr, &sp);
563 }
564 #endif // HAVE_THREAD_PRIORITY_FUNCTIONS
565
566 #ifdef HAVE_PTHREAD_ATTR_SETSCOPE
567 // this will make the threads created by this process really concurrent
568 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
569 #endif // HAVE_PTHREAD_ATTR_SETSCOPE
570
571 // create the new OS thread object
572 int rc = pthread_create(p_internal->GetIdPtr(), &attr,
573 wxThreadInternal::PthreadStart, (void *)this);
574 pthread_attr_destroy(&attr);
575
576 if ( rc != 0 )
577 {
578 p_internal->SetState(STATE_EXITED);
579 return wxTHREAD_NO_RESOURCE;
580 }
581
582 return wxTHREAD_NO_ERROR;
583 }
584
585 wxThreadError wxThread::Run()
586 {
587 wxCHECK_MSG( p_internal->GetId(), wxTHREAD_MISC_ERROR,
588 wxT("must call wxThread::Create() first") );
589
590 return p_internal->Run();
591 }
592
593 // -----------------------------------------------------------------------------
594 // misc accessors
595 // -----------------------------------------------------------------------------
596
597 void wxThread::SetPriority(unsigned int prio)
598 {
599 wxCHECK_RET( ((int)WXTHREAD_MIN_PRIORITY <= (int)prio) &&
600 ((int)prio <= (int)WXTHREAD_MAX_PRIORITY),
601 wxT("invalid thread priority") );
602
603 wxCriticalSectionLocker lock(m_critsect);
604
605 switch ( p_internal->GetState() )
606 {
607 case STATE_NEW:
608 // thread not yet started, priority will be set when it is
609 p_internal->SetPriority(prio);
610 break;
611
612 case STATE_RUNNING:
613 case STATE_PAUSED:
614 #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
615 {
616 struct sched_param sparam;
617 sparam.sched_priority = prio;
618
619 if ( pthread_setschedparam(p_internal->GetId(),
620 SCHED_OTHER, &sparam) != 0 )
621 {
622 wxLogError(_("Failed to set thread priority %d."), prio);
623 }
624 }
625 #endif // HAVE_THREAD_PRIORITY_FUNCTIONS
626 break;
627
628 case STATE_EXITED:
629 default:
630 wxFAIL_MSG(wxT("impossible to set thread priority in this state"));
631 }
632 }
633
634 unsigned int wxThread::GetPriority() const
635 {
636 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
637
638 return p_internal->GetPriority();
639 }
640
641 unsigned long wxThread::GetId() const
642 {
643 return (unsigned long)p_internal->GetId();
644 }
645
646 // -----------------------------------------------------------------------------
647 // pause/resume
648 // -----------------------------------------------------------------------------
649
650 wxThreadError wxThread::Pause()
651 {
652 wxCriticalSectionLocker lock(m_critsect);
653
654 if ( p_internal->GetState() != STATE_RUNNING )
655 {
656 wxLogDebug(wxT("Can't pause thread which is not running."));
657
658 return wxTHREAD_NOT_RUNNING;
659 }
660
661 p_internal->SetState(STATE_PAUSED);
662
663 return wxTHREAD_NO_ERROR;
664 }
665
666 wxThreadError wxThread::Resume()
667 {
668 wxCriticalSectionLocker lock(m_critsect);
669
670 if ( p_internal->GetState() == STATE_PAUSED )
671 {
672 m_critsect.Leave();
673 p_internal->Resume();
674 m_critsect.Enter();
675
676 return wxTHREAD_NO_ERROR;
677 }
678 else
679 {
680 wxLogDebug(wxT("Attempt to resume a thread which is not paused."));
681
682 return wxTHREAD_MISC_ERROR;
683 }
684 }
685
686 // -----------------------------------------------------------------------------
687 // exiting thread
688 // -----------------------------------------------------------------------------
689
690 wxThread::ExitCode Wait()
691 {
692 wxFAIL_MSG("TODO");
693
694 return 0;
695 }
696
697 wxThreadError wxThread::Delete(ExitCode *rc)
698 {
699 if (IsPaused())
700 Resume();
701
702 m_critsect.Enter();
703 wxThreadState state = p_internal->GetState();
704
705 // ask the thread to stop
706 p_internal->SetCancelFlag();
707
708 m_critsect.Leave();
709
710 switch ( state )
711 {
712 case STATE_NEW:
713 case STATE_EXITED:
714 // nothing to do
715 break;
716
717 case STATE_PAUSED:
718 // resume the thread first
719 Resume();
720
721 // fall through
722
723 default:
724 // wait until the thread stops
725 p_internal->Wait();
726 }
727
728 return wxTHREAD_NO_ERROR;
729 }
730
731 wxThreadError wxThread::Kill()
732 {
733 switch ( p_internal->GetState() )
734 {
735 case STATE_NEW:
736 case STATE_EXITED:
737 return wxTHREAD_NOT_RUNNING;
738
739 default:
740 #ifdef HAVE_PTHREAD_CANCEL
741 if ( pthread_cancel(p_internal->GetId()) != 0 )
742 #endif
743 {
744 wxLogError(_("Failed to terminate a thread."));
745
746 return wxTHREAD_MISC_ERROR;
747 }
748 //GL: As we must auto-destroy, the destruction must happen here (2).
749 delete this;
750
751 return wxTHREAD_NO_ERROR;
752 }
753 }
754
755 void wxThread::Exit(ExitCode status)
756 {
757 // first call user-level clean up code
758 OnExit();
759
760 // next wake up the threads waiting for us (OTOH, this function won't return
761 // until someone waited for us!)
762 p_internal->SignalExit();
763
764 p_internal->SetState(STATE_EXITED);
765
766 // delete both C++ thread object and terminate the OS thread object
767 // GL: This is very ugly and buggy ...
768 // delete this;
769 pthread_exit(status);
770 }
771
772 // also test whether we were paused
773 bool wxThread::TestDestroy()
774 {
775 wxCriticalSectionLocker lock(m_critsect);
776
777 if ( p_internal->GetState() == STATE_PAUSED )
778 {
779 // leave the crit section or the other threads will stop too if they try
780 // to call any of (seemingly harmless) IsXXX() functions while we sleep
781 m_critsect.Leave();
782
783 p_internal->Pause();
784
785 // enter it back before it's finally left in lock object dtor
786 m_critsect.Enter();
787 }
788
789 return p_internal->WasCancelled();
790 }
791
792 wxThread::~wxThread()
793 {
794 m_critsect.Enter();
795 if ( p_internal->GetState() != STATE_EXITED &&
796 p_internal->GetState() != STATE_NEW )
797 {
798 wxLogDebug(wxT("The thread is being destroyed although it is still "
799 "running! The application may crash."));
800 }
801
802 m_critsect.Leave();
803
804 delete p_internal;
805
806 // remove this thread from the global array
807 gs_allThreads.Remove(this);
808 }
809
810 // -----------------------------------------------------------------------------
811 // state tests
812 // -----------------------------------------------------------------------------
813
814 bool wxThread::IsRunning() const
815 {
816 wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect);
817
818 return p_internal->GetState() == STATE_RUNNING;
819 }
820
821 bool wxThread::IsAlive() const
822 {
823 wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect);
824
825 switch ( p_internal->GetState() )
826 {
827 case STATE_RUNNING:
828 case STATE_PAUSED:
829 return TRUE;
830
831 default:
832 return FALSE;
833 }
834 }
835
836 bool wxThread::IsPaused() const
837 {
838 wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect);
839
840 return (p_internal->GetState() == STATE_PAUSED);
841 }
842
843 //--------------------------------------------------------------------
844 // wxThreadModule
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 int rc = pthread_key_create(&gs_keySelf, NULL /* dtor function */);
862 if ( rc != 0 )
863 {
864 wxLogSysError(rc, _("Thread module initialization failed: "
865 "failed to create thread key"));
866
867 return FALSE;
868 }
869
870 gs_mutexGui = new wxMutex();
871
872 gs_tidMain = pthread_self();
873
874 gs_mutexGui->Lock();
875
876 return TRUE;
877 }
878
879 void wxThreadModule::OnExit()
880 {
881 wxASSERT_MSG( wxThread::IsMain(), wxT("only main thread can be here") );
882
883 // terminate any threads left
884 size_t count = gs_allThreads.GetCount();
885 if ( count != 0u )
886 wxLogDebug(wxT("Some threads were not terminated by the application."));
887
888 for ( size_t n = 0u; n < count; n++ )
889 {
890 // Delete calls the destructor which removes the current entry. We
891 // should only delete the first one each time.
892 gs_allThreads[0]->Delete();
893 }
894
895 // destroy GUI mutex
896 gs_mutexGui->Unlock();
897
898 delete gs_mutexGui;
899
900 // and free TLD slot
901 (void)pthread_key_delete(gs_keySelf);
902 }
903
904 // ----------------------------------------------------------------------------
905 // global functions
906 // ----------------------------------------------------------------------------
907
908 void wxMutexGuiEnter()
909 {
910 gs_mutexGui->Lock();
911 }
912
913 void wxMutexGuiLeave()
914 {
915 gs_mutexGui->Unlock();
916 }
917
918 #endif
919 // wxUSE_THREADS