]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/threadpsx.cpp
OnExit() is called for modules which were initialized even if the init of
[wxWidgets.git] / src / gtk / threadpsx.cpp
index af1b1482cbd5cd932b08dc1ddfe8be2a17eb6c46..f4fd5908db566e78ab7ed3acd809f9f4e6fc465b 100644 (file)
@@ -24,6 +24,9 @@
 #include "wx/utils.h"
 #include "wx/log.h"
 
+#include "gdk/gdk.h"
+#include "gtk/gtk.h"
+
 enum thread_state 
 {
   STATE_IDLE = 0,
@@ -40,7 +43,7 @@ enum thread_state
 
 static pthread_t p_mainid;
 
-wxMutex *wxMainMutex; // controls access to all GUI functions
+wxMutex *wxMainMutex; /* controls access to all GUI functions */
 
 //--------------------------------------------------------------------
 // common GUI thread code
@@ -61,7 +64,7 @@ public:
 wxMutex::wxMutex()
 {
     p_internal = new wxMutexInternal;
-    pthread_mutex_init(&(p_internal->p_mutex), NULL);
+    pthread_mutex_init( &(p_internal->p_mutex), (const pthread_mutexattr_t*) NULL );
     m_locked = 0;
 }
 
@@ -70,47 +73,55 @@ wxMutex::~wxMutex()
     if (m_locked > 0)
         wxLogDebug( "wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked );
 
-    pthread_mutex_destroy(&(p_internal->p_mutex));
+    pthread_mutex_destroy( &(p_internal->p_mutex) );
     delete p_internal;
 }
 
 wxMutexError wxMutex::Lock()
 {
-    int err;
-
-    err = pthread_mutex_lock(&(p_internal->p_mutex));
+    int err = pthread_mutex_lock( &(p_internal->p_mutex) );
     if (err == EDEADLK)
-        return MUTEX_DEAD_LOCK;
+    {
+        return wxMUTEX_DEAD_LOCK;
+    }
        
     m_locked++;
-    return MUTEX_NO_ERROR;
+    
+    return wxMUTEX_NO_ERROR;
 }
 
 wxMutexError wxMutex::TryLock()
 {
-    int err;
-
     if (m_locked)
-        return MUTEX_BUSY;
+    {
+        return wxMUTEX_BUSY;
+    }
        
-    err = pthread_mutex_trylock(&(p_internal->p_mutex));
+    int err = pthread_mutex_trylock( &(p_internal->p_mutex) );
     switch (err) 
     {
-        case EBUSY: return MUTEX_BUSY;
+        case EBUSY: return wxMUTEX_BUSY;
     }
+    
     m_locked++;
-    return MUTEX_NO_ERROR;
+    
+    return wxMUTEX_NO_ERROR;
 }
 
 wxMutexError wxMutex::Unlock()
 {
     if (m_locked > 0)
+    {
         m_locked--;
+    }
     else
-        return MUTEX_UNLOCKED;
+    {
+        return wxMUTEX_UNLOCKED;
+    }
        
-    pthread_mutex_unlock(&(p_internal->p_mutex));
-    return MUTEX_NO_ERROR;
+    pthread_mutex_unlock( &(p_internal->p_mutex) );
+    
+    return wxMUTEX_NO_ERROR;
 }
 
 //--------------------------------------------------------------------
@@ -126,37 +137,38 @@ public:
 wxCondition::wxCondition()
 {
     p_internal = new wxConditionInternal;
-    pthread_cond_init(&(p_internal->p_condition), NULL);
+    pthread_cond_init( &(p_internal->p_condition), (const pthread_condattr_t *) NULL );
 }
 
 wxCondition::~wxCondition()
 {
-    pthread_cond_destroy(&(p_internal->p_condition));
+    pthread_cond_destroy( &(p_internal->p_condition) );
+    
     delete p_internal;
 }
 
 void wxCondition::Wait(wxMutex& mutex)
 {
-    pthread_cond_wait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex));
+    pthread_cond_wait( &(p_internal->p_condition), &(mutex.p_internal->p_mutex) );
 }
 
 bool wxCondition::Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec)
 {
     struct timespec tspec;
 
-    tspec.tv_sec = time(NULL)+sec;
+    tspec.tv_sec = time(0L)+sec;
     tspec.tv_nsec = nsec;
     return (pthread_cond_timedwait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex), &tspec) != ETIMEDOUT);
 }
 
 void wxCondition::Signal()
 {
-    pthread_cond_signal(&(p_internal->p_condition));
+    pthread_cond_signal( &(p_internal->p_condition) );
 }
 
 void wxCondition::Broadcast()
 {
-    pthread_cond_broadcast(&(p_internal->p_condition));
+    pthread_cond_broadcast( &(p_internal->p_condition) );
 }
 
 //--------------------------------------------------------------------
@@ -179,8 +191,8 @@ void *wxThreadInternal::PthreadStart(void *ptr)
 {
     wxThread *thread = (wxThread *)ptr;
 
-    // Call the main entry
-    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+    /* Call the main entry */
+    pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, (int*) NULL );
     void* status = thread->Entry();
 
     thread->Exit(status);
@@ -195,9 +207,9 @@ wxThreadError wxThread::Create()
     struct sched_param sp;
 
     if (p_internal->state != STATE_IDLE)
-        return THREAD_RUNNING;
+        return wxTHREAD_RUNNING;
 
-    // Change thread priority
+    /* Change thread priority */
     pthread_attr_init(&a);
     pthread_attr_getschedpolicy(&a, &p);
 
@@ -216,11 +228,11 @@ wxThreadError wxThread::Create()
     {
         p_internal->state = STATE_IDLE;
         pthread_attr_destroy(&a);
-        return THREAD_NO_RESOURCE;
+        return wxTHREAD_NO_RESOURCE;
     }
     pthread_attr_destroy(&a);
 
-    return THREAD_NO_ERROR;
+    return wxTHREAD_NO_ERROR;
 }
 
 void wxThread::SetPriority(int prio)
@@ -243,9 +255,9 @@ int wxThread::GetPriority() const
 void wxThread::DeferDestroy(bool on)
 {
     if (on)
-        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
+        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, (int*) NULL);
     else
-        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, (int*) NULL);
 }
 
 wxThreadError wxThread::Destroy()
@@ -259,19 +271,19 @@ wxThreadError wxThread::Destroy()
            p_internal->state = STATE_CANCELED;
     }
 
-    return THREAD_NO_ERROR;
+    return wxTHREAD_NO_ERROR;
 }
 
 wxThreadError wxThread::Pause()
 {
     if (p_internal->state != STATE_RUNNING)
-        return THREAD_NOT_RUNNING;
+        return wxTHREAD_NOT_RUNNING;
 
     if (!p_internal->defer_destroy)
-        return THREAD_MISC_ERROR;
+        return wxTHREAD_MISC_ERROR;
 
     p_internal->state = STATE_PAUSING;
-    return THREAD_NO_ERROR;
+    return wxTHREAD_NO_ERROR;
 }
 
 wxThreadError wxThread::Resume()
@@ -279,7 +291,7 @@ wxThreadError wxThread::Resume()
     if (p_internal->state == STATE_PAUSING || p_internal->state == STATE_PAUSED)
       p_internal->state = STATE_RUNNING;
 
-    return THREAD_NO_ERROR;
+    return wxTHREAD_NO_ERROR;
 }
 
 void *wxThread::Join()
@@ -319,7 +331,7 @@ void wxThread::Exit(void *status)
     pthread_exit(status);
 }
 
-void wxThread::TestDestroy()
+bool wxThread::TestDestroy()
 {
     if (p_internal->state == STATE_PAUSING) 
     {
@@ -330,7 +342,12 @@ void wxThread::TestDestroy()
             usleep(1);
         }
     }
+
+    // VZ: do I understand it correctly that it will terminate the thread all by
+    //     itself if it was cancelled?
     pthread_testcancel();
+    
+    return FALSE;
 }
 
 bool wxThread::IsMain()
@@ -362,7 +379,7 @@ wxThread::~wxThread()
     delete p_internal;
 }
 
-// The default callback just joins the thread and throws away the result.
+/* The default callback just joins the thread and throws away the result. */
 void wxThread::OnExit()
 {
     Join();
@@ -372,28 +389,32 @@ void wxThread::OnExit()
 // wxThreadModule 
 //--------------------------------------------------------------------
 
-class wxThreadModule : public wxModule 
+class wxThreadModule : public wxModule
 {
-  DECLARE_DYNAMIC_CLASS(wxThreadModule)
-  
 public:
-  virtual bool OnInit() 
-    {
-        wxMainMutex = new wxMutex();
-        wxThreadGuiInit();
-        p_mainid = pthread_self();
-       wxMainMutex->Lock();
-
-       return TRUE;
-    }
+    virtual bool OnInit();
+    virtual void OnExit();
 
-  virtual void OnExit() 
-  {
-      wxMainMutex->Unlock();
-      wxThreadGuiExit();
-      delete wxMainMutex;
-  }
+private:
+    DECLARE_DYNAMIC_CLASS(wxThreadModule)
 };
 
 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
 
+bool wxThreadModule::OnInit() 
+{
+    wxMainMutex = new wxMutex();
+    wxThreadGuiInit();
+    p_mainid = (int)getpid();
+    wxMainMutex->Lock();
+    return TRUE;
+}
+
+void wxThreadModule::OnExit()
+{
+    wxMainMutex->Unlock();
+    wxThreadGuiExit();
+    delete wxMainMutex;
+}
+
+