X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7c351dad14431c3e1aeeef6b86f61ba898d1d9ad..358fc25cc3f1ad77f3d98ca9ef7debd8e128eb88:/src/gtk1/threadpsx.cpp diff --git a/src/gtk1/threadpsx.cpp b/src/gtk1/threadpsx.cpp index 645fba49ab..2fb5106aa7 100644 --- a/src/gtk1/threadpsx.cpp +++ b/src/gtk1/threadpsx.cpp @@ -14,12 +14,18 @@ #include #include -#include #include +#include +#include "wx/thread.h" +#include "wx/module.h" +#include "wx/utils.h" +#include "wx/log.h" enum thread_state { STATE_IDLE = 0, STATE_RUNNING, + STATE_PAUSING, + STATE_PAUSED, STATE_CANCELED, STATE_EXITED }; @@ -28,10 +34,11 @@ enum thread_state { // Static variables ///////////////////////////////////////////////////////////////////////////// -#include "thread.h" - static pthread_t p_mainid; -wxMutex wxMainMutex; // controls access to all GUI functions +static wxMutex p_list_mutex; +static wxList p_threads_list; + +wxMutex *wxMainMutex; // controls access to all GUI functions ///////////////////////////////////////////////////////////////////////////// // GUI thread manager @@ -47,34 +54,34 @@ public: pthread_mutex_t p_mutex; }; -wxMutex::wxMutex(void) +wxMutex::wxMutex() { p_internal = new wxMutexInternal; pthread_mutex_init(&(p_internal->p_mutex), NULL); - m_locked = false; + m_locked = 0; } -wxMutex::~wxMutex(void) +wxMutex::~wxMutex() { - if (m_locked) - pthread_mutex_unlock(&(p_internal->p_mutex)); + if (m_locked > 0) + wxLogDebug( "wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked ); + pthread_mutex_destroy(&(p_internal->p_mutex)); delete p_internal; } -wxMutexError wxMutex::Lock(void) +wxMutexError wxMutex::Lock() { int err; err = pthread_mutex_lock(&(p_internal->p_mutex)); - switch (err) { - case EDEADLK: return MUTEX_DEAD_LOCK; - } + if (err == EDEADLK) + return MUTEX_DEAD_LOCK; m_locked++; return MUTEX_NO_ERROR; } -wxMutexError wxMutex::TryLock(void) +wxMutexError wxMutex::TryLock() { int err; @@ -88,9 +95,12 @@ wxMutexError wxMutex::TryLock(void) return MUTEX_NO_ERROR; } -wxMutexError wxMutex::Unlock(void) +wxMutexError wxMutex::Unlock() { - if (m_locked > 0) m_locked--; + if (m_locked > 0) + m_locked--; + else + return MUTEX_UNLOCKED; pthread_mutex_unlock(&(p_internal->p_mutex)); return MUTEX_NO_ERROR; } @@ -104,13 +114,13 @@ public: pthread_cond_t p_condition; }; -wxCondition::wxCondition(void) +wxCondition::wxCondition() { p_internal = new wxConditionInternal; pthread_cond_init(&(p_internal->p_condition), NULL); } -wxCondition::~wxCondition(void) +wxCondition::~wxCondition() { pthread_cond_destroy(&(p_internal->p_condition)); delete p_internal; @@ -130,12 +140,12 @@ bool wxCondition::Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec) return (pthread_cond_timedwait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex), &tspec) != ETIMEDOUT); } -void wxCondition::Signal(void) +void wxCondition::Signal() { pthread_cond_signal(&(p_internal->p_condition)); } -void wxCondition::Broadcast(void) +void wxCondition::Broadcast() { pthread_cond_broadcast(&(p_internal->p_condition)); } @@ -152,14 +162,24 @@ public: pthread_t thread_id; int state; int prio; + int defer_destroy; + int id; }; void *wxThreadInternal::PthreadStart(void *ptr) { wxThread *thread = (wxThread *)ptr; + // Add the current thread to the list + p_list_mutex.Lock(); + thread->p_internal->id = p_threads_list.Number(); + p_threads_list.Append((wxObject *)thread); + p_list_mutex.Unlock(); + + // Call the main entry pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); void* status = thread->Entry(); + thread->Exit(status); return NULL; @@ -195,6 +215,7 @@ wxThreadError wxThread::Create() return THREAD_NO_RESOURCE; } pthread_attr_destroy(&a); + return THREAD_NO_ERROR; } @@ -210,7 +231,7 @@ void wxThread::SetPriority(int prio) p_internal->prio = prio; } -int wxThread::GetPriority(void) +int wxThread::GetPriority() const { return p_internal->prio; } @@ -223,7 +244,7 @@ void wxThread::DeferDestroy(bool on) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); } -wxThreadError wxThread::Destroy(void) +wxThreadError wxThread::Destroy() { int res = 0; @@ -232,6 +253,27 @@ wxThreadError wxThread::Destroy(void) if (res == 0) p_internal->state = STATE_CANCELED; } + + return THREAD_NO_ERROR; +} + +wxThreadError wxThread::Pause() +{ + if (p_internal->state != STATE_RUNNING) + return THREAD_NOT_RUNNING; + + if (!p_internal->defer_destroy) + return THREAD_MISC_ERROR; + + p_internal->state = STATE_PAUSING; + return THREAD_NO_ERROR; +} + +wxThreadError wxThread::Resume() +{ + if (p_internal->state == STATE_PAUSING || p_internal->state == STATE_PAUSED) + p_internal->state = STATE_RUNNING; + return THREAD_NO_ERROR; } @@ -246,18 +288,32 @@ void *wxThread::Join() wxYield(); if (do_unlock) - wxMainMutex.Unlock(); + wxMainMutex->Unlock(); pthread_join(p_internal->thread_id, &status); if (do_unlock) - wxMainMutex.Lock(); + wxMainMutex->Lock(); + + p_list_mutex.Lock(); + delete p_threads_list.Nth(p_internal->id); + p_list_mutex.Unlock(); + p_internal->state = STATE_IDLE; } return status; } -unsigned long wxThread::GetID() +unsigned long wxThread::GetID() const +{ + return p_internal->id; +} + +wxThread *wxThread::GetThreadFromID(unsigned long id) { - return (unsigned long)p_internal->thread_id; + wxNode *node = p_threads_list.Nth(id); + + if (!node) + return NULL; + return (wxThread *)node->Data(); } void wxThread::Exit(void *status) @@ -271,14 +327,33 @@ void wxThread::Exit(void *status) void wxThread::TestDestroy() { + if (p_internal->state == STATE_PAUSING) { + p_internal->state = STATE_PAUSED; + while (p_internal->state == STATE_PAUSED) { + pthread_testcancel(); + usleep(1); + } + } pthread_testcancel(); } -bool wxThread::IsMain(void) +bool wxThread::IsMain() { return (bool)pthread_equal(pthread_self(), p_mainid); } +bool wxThread::IsRunning() const +{ + return (p_internal->state == STATE_RUNNING); +} + +bool wxThread::IsAlive() const +{ + return (p_internal->state == STATE_RUNNING) || + (p_internal->state == STATE_PAUSING) || + (p_internal->state == STATE_PAUSED); +} + wxThread::wxThread() { p_internal = new wxThreadInternal(); @@ -294,23 +369,27 @@ wxThread::~wxThread() // The default callback just joins the thread and throws away the result. void wxThread::OnExit() { + Join(); } // Automatic initialization class wxThreadModule : public wxModule { DECLARE_DYNAMIC_CLASS(wxThreadModule) public: - virtual bool OnInit(void) { + virtual bool OnInit() { + wxMainMutex = new wxMutex(); wxThreadGuiInit(); p_mainid = pthread_self(); - wxMainMutex.Lock(); +// p_threads_list = wxList(wxKEY_INTEGER); + wxMainMutex->Lock(); return TRUE; } - virtual void OnExit(void) { - wxMainMutex.Unlock(); + virtual void OnExit() { + wxMainMutex->Unlock(); wxThreadGuiExit(); + delete wxMainMutex; } };