]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/threadpsx.cpp
Prealpha, prebeta of new wxTreeCtrl for GTK. It is possible to AddRoot,
[wxWidgets.git] / src / gtk / threadpsx.cpp
index ce5396c6d7d052f0a376f09b98e372fce91eb375..9623b91c303a63aeef06bf525e80071857d1b9bf 100644 (file)
 
 #include <stdio.h>
 #include <unistd.h>
-#include <sched.h>
 #include <pthread.h>
+#include <errno.h>
+#include "wx/thread.h"
+#include "wx/module.h"
+#include "wx/utils.h"
 
 enum thread_state {
   STATE_IDLE = 0,
   STATE_RUNNING,
+  STATE_PAUSING,
+  STATE_PAUSED,
   STATE_CANCELED,
   STATE_EXITED
 };
@@ -28,9 +33,10 @@ enum thread_state {
 // Static variables
 /////////////////////////////////////////////////////////////////////////////
 
-#include "thread.h"
-
 static pthread_t p_mainid;
+static wxMutex p_list_mutex;
+static wxList p_threads_list;
+
 wxMutex wxMainMutex; // controls access to all GUI functions
 
 /////////////////////////////////////////////////////////////////////////////
@@ -51,13 +57,15 @@ wxMutex::wxMutex()
 {
   p_internal = new wxMutexInternal;
   pthread_mutex_init(&(p_internal->p_mutex), NULL);
-  m_locked = false;
+  m_locked = 0;
 }
 
 wxMutex::~wxMutex()
 {
-  if (m_locked)
-    pthread_mutex_unlock(&(p_internal->p_mutex));
+  if (m_locked > 0)
+    wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n",
+               m_locked);
+
   pthread_mutex_destroy(&(p_internal->p_mutex));
   delete p_internal;
 }
@@ -67,9 +75,8 @@ 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;
 }
@@ -90,7 +97,10 @@ wxMutexError wxMutex::TryLock()
 
 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;
 }
@@ -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;
 }
 
@@ -232,6 +253,27 @@ wxThreadError wxThread::Destroy()
     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;
 }
 
@@ -250,6 +292,11 @@ void *wxThread::Join()
     pthread_join(p_internal->thread_id, &status);
     if (do_unlock)
       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;
@@ -257,7 +304,16 @@ void *wxThread::Join()
 
 unsigned long wxThread::GetID() const
 {
-  return (unsigned long)p_internal->thread_id;
+  return p_internal->id;
+}
+
+wxThread *wxThread::GetThreadFromID(unsigned long 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() const
+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,6 +369,7 @@ wxThread::~wxThread()
 // The default callback just joins the thread and throws away the result.
 void wxThread::OnExit()
 {
+  Join();
 }
 
 // Automatic initialization
@@ -303,6 +379,7 @@ public:
   virtual bool OnInit() {
     wxThreadGuiInit();
     p_mainid = pthread_self();
+    p_threads_list = wxList(wxKEY_INTEGER);
     wxMainMutex.Lock();
 
     return TRUE;