]> git.saurik.com Git - wxWidgets.git/commitdiff
* Thread updates and cleanup (m_locked, MUTEX_UNLOCKED added)
authorGuilhem Lavaux <lavaux@easynet.fr>
Sun, 14 Jun 1998 15:28:28 +0000 (15:28 +0000)
committerGuilhem Lavaux <lavaux@easynet.fr>
Sun, 14 Jun 1998 15:28:28 +0000 (15:28 +0000)
* Updated the documentation

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@94 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/latex/wx/mutex.tex
include/wx/thread.h
src/gtk/threadgui.inc
src/gtk/threadno.cpp
src/gtk/threadpsx.cpp
src/gtk/threadsgi.cpp
src/gtk1/threadgui.inc
src/gtk1/threadno.cpp
src/gtk1/threadpsx.cpp
src/gtk1/threadsgi.cpp
src/msw/thread.cpp

index 5009f0b60b2a474780e04db873bea8a4bcef7039..9b992c4509f82fae6fb9126322d5a8d93a1527cb 100644 (file)
@@ -45,7 +45,7 @@ One of:
 \begin{twocollist}\itemsep=0pt
 \twocolitem{{\bf MUTEX\_NO\_ERROR}}{There was no error.}
 \twocolitem{{\bf MUTEX\_DEAD\_LOCK}}{A deadlock situation was detected.}
-\twocolitem{{\bf MUTEX\_BUSY}}{The thread is already running.}
+\twocolitem{{\bf MUTEX\_BUSY}}{The mutex is already locked by another thread.}
 \end{twocollist}
 
 \membersection{wxMutex::TryLock}\label{wxmutextrylock}
@@ -62,7 +62,7 @@ One of:
 \begin{twocollist}\itemsep=0pt
 \twocolitem{{\bf MUTEX\_NO\_ERROR}}{There was no error.}
 \twocolitem{{\bf MUTEX\_DEAD\_LOCK}}{A deadlock situation was detected.}
-\twocolitem{{\bf MUTEX\_BUSY}}{The thread is already running.}
+\twocolitem{{\bf MUTEX\_BUSY}}{The mutex is already locked by another thread.}
 \end{twocollist}
 
 \membersection{wxMutex::Unlock}\label{wxmutexunlock}
@@ -79,7 +79,8 @@ One of:
 \begin{twocollist}\itemsep=0pt
 \twocolitem{{\bf MUTEX\_NO\_ERROR}}{There was no error.}
 \twocolitem{{\bf MUTEX\_DEAD\_LOCK}}{A deadlock situation was detected.}
-\twocolitem{{\bf MUTEX\_BUSY}}{The thread is already running.}
+\twocolitem{{\bf MUTEX\_BUSY}}{The mutex is already locked by another thread.}
+\twocolitem{{\bf MUTEX\_UNLOCKED}}{The calling thread tries to unlock an unlocked mutex.}
 \end{twocollist}
 
 
index 08e462a442c6fa362d6acebaee2fec67cf8e6c10..64481d7c8854aa42327c6fc4828a57424604717c 100644 (file)
@@ -22,7 +22,8 @@
 typedef enum {
   MUTEX_NO_ERROR=0,
   MUTEX_DEAD_LOCK,             // Mutex has been already locked by THE CALLING thread 
-  MUTEX_BUSY                   // Mutex has been already locked by ONE thread
+  MUTEX_BUSY,                  // Mutex has been already locked by ONE thread
+  MUTEX_UNLOCKED
 } wxMutexError;
 
 typedef enum {
index 8195b0ddbdbb4c22ccc5828dc08f028314b3b146..165fef3c01b707352ffb92e9a2a2a694c00c3bf9 100644 (file)
@@ -49,7 +49,7 @@ ThreadExitProc(gpointer WXUNUSED(client), gint fid,
 }
 
 // Global initialization
-static void wxThreadGuiInit(void)
+static void wxThreadGuiInit()
 {
   pipe(p_thrd_pipe);
   p_thrd_inid = gdk_input_add(p_thrd_pipe[0], GDK_INPUT_READ,
@@ -57,7 +57,7 @@ static void wxThreadGuiInit(void)
 }
 
 // Global cleanup
-static void wxThreadGuiExit(void)
+static void wxThreadGuiExit()
 {
   gdk_input_remove(p_thrd_inid);
   close(p_thrd_pipe[0]);
index 9b1686c349befc96e8580dd8a1bbb4b1ef4612be..126a211fcacb3b039b2daf085deea32541b04664 100644 (file)
 
 wxMutex::wxMutex()
 {
-  m_locked = FALSE;
+  m_locked = 0;
 }
 
 wxMutex::~wxMutex()
 {
+  if (m_locked)
+    wxDebugMsg("wxMutex warning: destroying a locked mutex (%d locks)\n", m_locked);
 }
 
 MutexError wxMutex::Lock()
 {
-  m_locked = TRUE;
-  return NO_ERROR;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
 MutexError wxMutex::TryLock()
 {
-  m_locked = TRUE;
-  return NO_ERROR;
+  if (m_locked > 0)
+    return MUTEX_BUSY;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
 MutexError wxMutex::Unlock()
 {
-  m_locked = FALSE;
-  return NO_ERROR;
+  if (m_locked == 0)
+    return MUTEX_UNLOCKED;
+  m_locked--;
+  return MUTEX_NO_ERROR;
 }
 
 wxCondition::wxCondition()
@@ -76,12 +82,12 @@ ThreadError wxThread::Create()
 {
   p_internal->exit_status = Entry();
   OnExit();
-  return NO_ERROR;
+  return THREAD_NO_ERROR;
 }
 
 ThreadError wxThread::Destroy()
 {
-  return RUNNING;
+  return THREAD_RUNNING;
 }
 
 void wxThread::DeferDestroy()
@@ -135,22 +141,23 @@ void wxThread::OnExit()
   Join();
 }
 
-// Global initialization
-static void wxThreadInit(void *WXUNUSED(client))
-{
+
+// Automatic initialization
+class wxThreadModule : public wxModule {
+  DECLARE_DYNAMIC_CLASS(wxThreadModule)
+public:
+  bool OnInit();
+  void OnExit();
+};
+
+bool wxThreadModule::OnInit() {
   wxMainMutex.Lock();
+  return TRUE;
 }
 
-// Global cleanup
-static void wxThreadExit(void *WXUNUSED(client))
+void wxThreadModule::wxThreadExit()
 {
   wxMainMutex.Unlock();
 }
 
-// Let automatic initialization be performed from wxCommonInit().
-static struct
-wxThreadGlobal {
-  wxThreadGlobal() {
-      wxRegisterModuleFunction(wxThreadInit, wxThreadExit, NULL);
-  }
-} dummy;
+IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
index ce5396c6d7d052f0a376f09b98e372fce91eb375..07b510365478c28e818bfe3a53bac46fab601279 100644 (file)
@@ -51,13 +51,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 +69,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 +91,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;
 }
index 575349e961beebfcb1b917fedc5947ef0fcffea2..ea652677e1af0f8af54a7321b386de08697bb42a 100644 (file)
@@ -46,34 +46,44 @@ public:
 
 wxMutex::wxMutex()
 {
+  m_locked = 0;
   p_internal = new wxMutexInternal;
   init_lock(&(p_internal->p_mutex));
 }
 
 wxMutex::~wxMutex()
 {
+  if (m_locked > 0)
+    wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n",
+               m_locked);
+  delete p_internal;
 }
 
-wxMutex::MutexError wxMutex::Lock()
+wxMutexError wxMutex::Lock()
 {
   spin_lock(&(p_internal->p_mutex));
-  return NO_ERROR;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
-wxMutex::MutexError wxMutex::TryLock()
+wxMutexError wxMutex::TryLock()
 {
   if (acquire_lock(&(p_internal->p_mutex)) != 0)
-    return BUSY;
-  return NO_ERROR;
+    return MUTEX_BUSY;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
-wxMutex::MutexError wxMutex::Unlock()
+wxMutexError wxMutex::Unlock()
 {
+  if (m_locked == 0)
+    return MUTEX_UNLOCKED; 
   release_lock(&(p_internal->p_mutex));
-  return NO_ERROR;
+  m_locked--;
+  return MUTEX_NO_ERROR;
 }
 
-// GLH: Don't now how it works on SGI. Wolfram ?
+// GL: Don't know how it works on SGI. Wolfram ?
 
 wxCondition::wxCondition() {}
 wxCondition::~wxCondition() {}
@@ -116,16 +126,16 @@ void wxThread::Exit(void* status)
   _exit(0);
 }
 
-wxThread::ThreadError wxThread::Create()
+wxThreadError wxThread::Create()
 {
   if (p_internal->state != STATE_IDLE)
-    return RUNNING;
+    return THREAD_RUNNING;
   p_internal->state = STATE_RUNNING;
   if (sproc(p_internal->SprocStart, PR_SALL, this) < 0) {
     p_internal->state = STATE_IDLE;
-    return NO_RESOURCE;
+    return THREAD_NO_RESOURCE;
   }
-  return NO_ERROR;
+  return THREAD_NO_ERROR;
 }
 
 void wxThread::Destroy()
index 8195b0ddbdbb4c22ccc5828dc08f028314b3b146..165fef3c01b707352ffb92e9a2a2a694c00c3bf9 100644 (file)
@@ -49,7 +49,7 @@ ThreadExitProc(gpointer WXUNUSED(client), gint fid,
 }
 
 // Global initialization
-static void wxThreadGuiInit(void)
+static void wxThreadGuiInit()
 {
   pipe(p_thrd_pipe);
   p_thrd_inid = gdk_input_add(p_thrd_pipe[0], GDK_INPUT_READ,
@@ -57,7 +57,7 @@ static void wxThreadGuiInit(void)
 }
 
 // Global cleanup
-static void wxThreadGuiExit(void)
+static void wxThreadGuiExit()
 {
   gdk_input_remove(p_thrd_inid);
   close(p_thrd_pipe[0]);
index 9b1686c349befc96e8580dd8a1bbb4b1ef4612be..126a211fcacb3b039b2daf085deea32541b04664 100644 (file)
 
 wxMutex::wxMutex()
 {
-  m_locked = FALSE;
+  m_locked = 0;
 }
 
 wxMutex::~wxMutex()
 {
+  if (m_locked)
+    wxDebugMsg("wxMutex warning: destroying a locked mutex (%d locks)\n", m_locked);
 }
 
 MutexError wxMutex::Lock()
 {
-  m_locked = TRUE;
-  return NO_ERROR;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
 MutexError wxMutex::TryLock()
 {
-  m_locked = TRUE;
-  return NO_ERROR;
+  if (m_locked > 0)
+    return MUTEX_BUSY;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
 MutexError wxMutex::Unlock()
 {
-  m_locked = FALSE;
-  return NO_ERROR;
+  if (m_locked == 0)
+    return MUTEX_UNLOCKED;
+  m_locked--;
+  return MUTEX_NO_ERROR;
 }
 
 wxCondition::wxCondition()
@@ -76,12 +82,12 @@ ThreadError wxThread::Create()
 {
   p_internal->exit_status = Entry();
   OnExit();
-  return NO_ERROR;
+  return THREAD_NO_ERROR;
 }
 
 ThreadError wxThread::Destroy()
 {
-  return RUNNING;
+  return THREAD_RUNNING;
 }
 
 void wxThread::DeferDestroy()
@@ -135,22 +141,23 @@ void wxThread::OnExit()
   Join();
 }
 
-// Global initialization
-static void wxThreadInit(void *WXUNUSED(client))
-{
+
+// Automatic initialization
+class wxThreadModule : public wxModule {
+  DECLARE_DYNAMIC_CLASS(wxThreadModule)
+public:
+  bool OnInit();
+  void OnExit();
+};
+
+bool wxThreadModule::OnInit() {
   wxMainMutex.Lock();
+  return TRUE;
 }
 
-// Global cleanup
-static void wxThreadExit(void *WXUNUSED(client))
+void wxThreadModule::wxThreadExit()
 {
   wxMainMutex.Unlock();
 }
 
-// Let automatic initialization be performed from wxCommonInit().
-static struct
-wxThreadGlobal {
-  wxThreadGlobal() {
-      wxRegisterModuleFunction(wxThreadInit, wxThreadExit, NULL);
-  }
-} dummy;
+IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
index ce5396c6d7d052f0a376f09b98e372fce91eb375..07b510365478c28e818bfe3a53bac46fab601279 100644 (file)
@@ -51,13 +51,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 +69,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 +91,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;
 }
index 575349e961beebfcb1b917fedc5947ef0fcffea2..ea652677e1af0f8af54a7321b386de08697bb42a 100644 (file)
@@ -46,34 +46,44 @@ public:
 
 wxMutex::wxMutex()
 {
+  m_locked = 0;
   p_internal = new wxMutexInternal;
   init_lock(&(p_internal->p_mutex));
 }
 
 wxMutex::~wxMutex()
 {
+  if (m_locked > 0)
+    wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n",
+               m_locked);
+  delete p_internal;
 }
 
-wxMutex::MutexError wxMutex::Lock()
+wxMutexError wxMutex::Lock()
 {
   spin_lock(&(p_internal->p_mutex));
-  return NO_ERROR;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
-wxMutex::MutexError wxMutex::TryLock()
+wxMutexError wxMutex::TryLock()
 {
   if (acquire_lock(&(p_internal->p_mutex)) != 0)
-    return BUSY;
-  return NO_ERROR;
+    return MUTEX_BUSY;
+  m_locked++;
+  return MUTEX_NO_ERROR;
 }
 
-wxMutex::MutexError wxMutex::Unlock()
+wxMutexError wxMutex::Unlock()
 {
+  if (m_locked == 0)
+    return MUTEX_UNLOCKED; 
   release_lock(&(p_internal->p_mutex));
-  return NO_ERROR;
+  m_locked--;
+  return MUTEX_NO_ERROR;
 }
 
-// GLH: Don't now how it works on SGI. Wolfram ?
+// GL: Don't know how it works on SGI. Wolfram ?
 
 wxCondition::wxCondition() {}
 wxCondition::~wxCondition() {}
@@ -116,16 +126,16 @@ void wxThread::Exit(void* status)
   _exit(0);
 }
 
-wxThread::ThreadError wxThread::Create()
+wxThreadError wxThread::Create()
 {
   if (p_internal->state != STATE_IDLE)
-    return RUNNING;
+    return THREAD_RUNNING;
   p_internal->state = STATE_RUNNING;
   if (sproc(p_internal->SprocStart, PR_SALL, this) < 0) {
     p_internal->state = STATE_IDLE;
-    return NO_RESOURCE;
+    return THREAD_NO_RESOURCE;
   }
-  return NO_ERROR;
+  return THREAD_NO_ERROR;
 }
 
 void wxThread::Destroy()
index 86bbcfea6fb701b942140272be817c551912a07a..33ca0a645ad7b656c210967a899e2b7dd933fdfd 100644 (file)
@@ -61,6 +61,8 @@ wxMutex::wxMutex()
 
 wxMutex::~wxMutex()
 {
+  if (m_locked > 0)
+    wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked);
   CloseHandle(p_internal->p_mutex);
 }