]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/thread.cpp
added functions to read/write several elements at once (patch 754986)
[wxWidgets.git] / src / os2 / thread.cpp
index ac5b0d56cae02503374f16084c950906cdee35b6..884f66cff4ed4f2e399c68560083655f34e61929 100644 (file)
@@ -9,6 +9,10 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
+#ifdef __GNUG__
+    #pragma implementation "thread.h"
+#endif
+
 // ----------------------------------------------------------------------------
 // headers
 // ----------------------------------------------------------------------------
 #include <stdio.h>
 
 #include "wx/module.h"
+#include "wx/intl.h"
+#include "wx/utils.h"
+#include "wx/log.h"
 #include "wx/thread.h"
 
 #define INCL_DOSSEMAPHORES
 #define INCL_DOSPROCESS
 #define INCL_ERRORS
 #include <os2.h>
+#ifndef __EMX__
 #include <bseerr.h>
-
+#endif
 // the possible states of the thread ("=>" shows all possible transitions from
 // this state)
 enum wxThreadState
@@ -82,7 +90,9 @@ public:
     HMTX                            m_vMutex;
 };
 
-wxMutex::wxMutex()
+wxMutex::wxMutex(
+  wxMutexType                       eMutexType
+)
 {
     APIRET                          ulrc;
 
@@ -92,13 +102,10 @@ wxMutex::wxMutex()
     {
         wxLogSysError(_("Can not create mutex."));
     }
-    m_locked = 0;
 }
 
 wxMutex::~wxMutex()
 {
-    if (m_locked > 0)
-        wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked);
     ::DosCloseMutexSem(m_internal->m_vMutex);
     m_internal->m_vMutex = NULL;
 }
@@ -128,7 +135,6 @@ wxMutexError wxMutex::Lock()
         default:
             wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
     }
-    m_locked++;
     return wxMUTEX_NO_ERROR;
 }
 
@@ -140,7 +146,6 @@ wxMutexError wxMutex::TryLock()
     if (ulrc == ERROR_TIMEOUT || ulrc == ERROR_TOO_MANY_SEM_REQUESTS)
         return wxMUTEX_BUSY;
 
-    m_locked++;
     return wxMUTEX_NO_ERROR;
 }
 
@@ -148,9 +153,6 @@ wxMutexError wxMutex::Unlock()
 {
     APIRET                          ulrc;
 
-    if (m_locked > 0)
-        m_locked--;
-
     ulrc = ::DosReleaseMutexSem(m_internal->m_vMutex);
     if (ulrc != 0)
     {
@@ -167,7 +169,7 @@ wxMutexError wxMutex::Unlock()
 class wxConditionInternal
 {
 public:
-    inline wxConditionInternal ()
+    inline wxConditionInternal (wxMutex& rMutex) : m_vMutex(rMutex)
     {
         ::DosCreateEventSem(NULL, &m_vEvent, DC_SEM_SHARED, FALSE);
         if (!m_vEvent)
@@ -177,7 +179,7 @@ public:
         m_nWaiters = 0;
     }
 
-    inline bool Wait(
+    inline APIRET Wait(
       unsigned long                 ulTimeout
     )
     {
@@ -186,7 +188,7 @@ public:
         m_nWaiters++;
         ulrc = ::DosWaitEventSem(m_vEvent, ulTimeout);
         m_nWaiters--;
-        return (ulrc != ERROR_TIMEOUT);
+        return (ulrc);
     }
 
     inline ~wxConditionInternal ()
@@ -205,14 +207,15 @@ public:
 
     HEV                             m_vEvent;
     int                             m_nWaiters;
+    wxMutex&                        m_vMutex;
 };
 
-wxCondition::wxCondition()
+wxCondition::wxCondition(wxMutex& rMutex)
 {
     APIRET                          ulrc;
     ULONG                           ulCount;
 
-    m_internal = new wxConditionInternal;
+    m_internal = new wxConditionInternal(rMutex);
     ulrc = ::DosCreateEventSem(NULL, &m_internal->m_vEvent, 0L, FALSE);
     if (ulrc != 0)
     {
@@ -230,34 +233,80 @@ wxCondition::~wxCondition()
     m_internal = NULL;
 }
 
-void wxCondition::Wait()
+wxCondError wxCondition::Wait()
 {
-    (void)m_internal->Wait(SEM_INDEFINITE_WAIT);
+    APIRET                          rc = m_internal->Wait(SEM_INDEFINITE_WAIT);
+
+    switch(rc)
+    {
+        case NO_ERROR:
+            return wxCOND_NO_ERROR;
+        case ERROR_INVALID_HANDLE:
+            return wxCOND_INVALID;
+        case ERROR_TIMEOUT:
+            return wxCOND_TIMEOUT;
+        default:
+            return wxCOND_MISC_ERROR;
+    }
 }
 
-bool wxCondition::Wait(
-  unsigned long                     lSec
-, unsigned long                     lNsec)
+wxCondError wxCondition::WaitTimeout(
+  unsigned long                     lMilliSec
+)
 {
-    return m_internal->Wait(lSec*1000 + lNsec/1000000);
+    APIRET                          rc = m_internal->Wait(lMilliSec);
+
+    switch(rc)
+    {
+        case NO_ERROR:
+            return wxCOND_NO_ERROR;
+        case ERROR_INVALID_HANDLE:
+            return wxCOND_INVALID;
+        case ERROR_TIMEOUT:
+            return wxCOND_TIMEOUT;
+        default:
+            return wxCOND_MISC_ERROR;
+    }
 }
 
-void wxCondition::Signal()
+wxCondError wxCondition::Signal()
 {
-    ::DosPostEventSem(m_internal->m_vEvent);
+    APIRET                          rc = ::DosPostEventSem(m_internal->m_vEvent);
+
+    switch(rc)
+    {
+        case NO_ERROR:
+            return wxCOND_NO_ERROR;
+        case ERROR_INVALID_HANDLE:
+            return wxCOND_INVALID;
+        default:
+            return wxCOND_MISC_ERROR;
+    }
 }
 
-void wxCondition::Broadcast()
+wxCondError wxCondition::Broadcast()
 {
     int                             i;
+    APIRET                          rc = NO_ERROR;
 
     for (i = 0; i < m_internal->m_nWaiters; i++)
     {
-        if (::DosPostEventSem(m_internal->m_vEvent) != 0)
+        if ((rc = ::DosPostEventSem(m_internal->m_vEvent)) != NO_ERROR)
         {
             wxLogSysError(_("Couldn't change the state of event object."));
+            break;
         }
     }
+
+    switch(rc)
+    {
+        case NO_ERROR:
+            return wxCOND_NO_ERROR;
+        case ERROR_INVALID_HANDLE:
+            return wxCOND_INVALID;
+        default:
+            return wxCOND_MISC_ERROR;
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -314,7 +363,9 @@ public:
     }
 
     // create a new (suspended) thread (for the given thread object)
-    bool Create(wxThread* pThread);
+    bool Create( wxThread*    pThread
+                ,unsigned int uStackSize
+               );
 
     // suspend/resume/terminate
     bool Suspend();
@@ -412,6 +463,7 @@ void wxThreadInternal::SetPriority(
 
 bool wxThreadInternal::Create(
   wxThread*                         pThread
+, unsigned int                      uStackSize
 )
 {
     APIRET                          ulrc;
@@ -420,7 +472,7 @@ bool wxThreadInternal::Create(
                              ,(PFNTHREAD)wxThreadInternal::OS2ThreadStart
                              ,(ULONG)pThread
                              ,CREATE_SUSPENDED | STACK_SPARSE
-                             ,8192L
+                             ,(ULONG)uStackSize
                             );
     if(ulrc != 0)
     {
@@ -432,6 +484,9 @@ bool wxThreadInternal::Create(
     {
         SetPriority(m_nPriority);
     }
+
+    m_eState = STATE_NEW;
+
     return(TRUE);
 }
 
@@ -516,9 +571,11 @@ wxThread::~wxThread()
 // create/start thread
 // -------------------
 
-wxThreadError wxThread::Create()
+wxThreadError wxThread::Create(
+  unsigned int                      uStackSize
+)
 {
-    if ( !m_internal->Create(this) )
+    if ( !m_internal->Create(this, uStackSize) )
         return wxTHREAD_NO_RESOURCE;
 
     return wxTHREAD_NO_ERROR;
@@ -746,11 +803,15 @@ void wxThreadModule::OnExit()
     if (gs_pCritsectGui)
     {
         gs_pCritsectGui->Leave();
+#if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
         delete gs_pCritsectGui;
+#endif
         gs_pCritsectGui = NULL;
     }
 
+#if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
     wxDELETE(gs_pCritsectWaitingForGui);
+#endif
 }
 
 // ----------------------------------------------------------------------------