]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/thread.cpp
incomplete paste error
[wxWidgets.git] / src / os2 / thread.cpp
index 6bfb019a73190bc068e0eefb4abfeb8758f5fcd8..b17b17cb830e0751b3b636ddd0c597738cfff181 100644 (file)
@@ -6,14 +6,9 @@
 // Created:     04/22/98
 // RCS-ID:      $Id$
 // Copyright:   (c) Stefan Neis (2003)
-//
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-    #pragma implementation "thread.h"
-#endif
-
 // ----------------------------------------------------------------------------
 // headers
 // ----------------------------------------------------------------------------
 
 #if wxUSE_THREADS
 
-#include <stdio.h>
+#include "wx/thread.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/intl.h"
+    #include "wx/log.h"
+    #include "wx/app.h"
+    #include "wx/module.h"
+#endif //WX_PRECOMP
 
-#include "wx/app.h"
 #include "wx/apptrait.h"
-#include "wx/module.h"
-#include "wx/intl.h"
 #include "wx/utils.h"
-#include "wx/log.h"
-#include "wx/thread.h"
+
+#include <stdio.h>
 
 #define INCL_DOSSEMAPHORES
 #define INCL_DOSPROCESS
@@ -58,14 +57,14 @@ enum wxThreadState
 
 // id of the main thread - the one which can call GUI functions without first
 // calling wxMutexGuiEnter()
-static ULONG                        s_ulIdMainThread = 1;
+wxThreadIdType                      wxThread::ms_idMainThread = 0;
 wxMutex*                            p_wxMainMutex;
 
 // OS2 substitute for Tls pointer the current parent thread object
 wxThread*                           m_pThread;    // pointer to the wxWidgets thread object
 
-// if it's FALSE, some secondary thread is holding the GUI lock
-static bool gs_bGuiOwnedByMainThread = TRUE;
+// if it's false, some secondary thread is holding the GUI lock
+static bool gs_bGuiOwnedByMainThread = true;
 
 // critical section which controls access to all GUI functions: any secondary
 // thread (i.e. except the main one) must enter this crit section before doing
@@ -79,7 +78,7 @@ static wxCriticalSection *gs_pCritsectWaitingForGui = NULL;
 static size_t gs_nWaitingForGui = 0;
 
 // are we waiting for a thread termination?
-static bool gs_bWaitingForThread = FALSE;
+static bool gs_bWaitingForThread = false;
 
 // ============================================================================
 // OS/2 implementation of thread and related classes
@@ -97,7 +96,8 @@ public:
     bool IsOk() const { return m_vMutex != NULL; }
 
     wxMutexError Lock() { return LockTimeout(SEM_INDEFINITE_WAIT); }
-    wxMutexError TryLock() { return LockTimeout(SEM_IMMEDIATE_RETURN); }
+    wxMutexError Lock(unsigned long ms) { return LockTimeout(ms); }
+    wxMutexError TryLock();
     wxMutexError Unlock();
 
 private:
@@ -109,16 +109,12 @@ private:
 // (Calls to DosRequestMutexSem and DosReleaseMutexSem can be nested, but
 //  the request count for a semaphore cannot exceed 65535. If an attempt is
 //  made to exceed this number, ERROR_TOO_MANY_SEM_REQUESTS is returned.)
-wxMutexInternal::wxMutexInternal(
-  wxMutexType                       WXUNUSED(eMutexType)
-)
+wxMutexInternal::wxMutexInternal(wxMutexType WXUNUSED(eMutexType))
 {
-    APIRET                          ulrc;
-
-    ulrc = ::DosCreateMutexSem(NULL, &m_vMutex, 0L, FALSE);
+    APIRET ulrc = ::DosCreateMutexSem(NULL, &m_vMutex, 0L, FALSE);
     if (ulrc != 0)
     {
-        wxLogSysError(_("Can not create mutex."));
+        wxLogSysError(_("Cannot create mutex."));
         m_vMutex = NULL;
     }
 }
@@ -128,10 +124,20 @@ wxMutexInternal::~wxMutexInternal()
     if (m_vMutex)
     {
         if (::DosCloseMutexSem(m_vMutex))
-            wxLogLastError(_T("DosCloseMutexSem(mutex)"));
+        {
+            wxLogLastError(wxT("DosCloseMutexSem(mutex)"));
+        }
     }
 }
 
+wxMutexError wxMutexInternal::TryLock()
+{
+    const wxMutexError rc = LockTimeout( SEM_IMMEDIATE_RETURN );
+
+    // we have a special return code for timeout in this case
+    return rc == wxMUTEX_TIMEOUT ? wxMUTEX_BUSY : rc;
+}
+
 wxMutexError wxMutexInternal::LockTimeout(ULONG ulMilliseconds)
 {
     APIRET                          ulrc;
@@ -141,6 +147,7 @@ wxMutexError wxMutexInternal::LockTimeout(ULONG ulMilliseconds)
     switch (ulrc)
     {
         case ERROR_TIMEOUT:
+            return wxMUTEX_TIMEOUT;
         case ERROR_TOO_MANY_SEM_REQUESTS:
             return wxMUTEX_BUSY;
 
@@ -214,7 +221,7 @@ wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount)
     ulrc = ::DosCreateMutexSem(NULL, &m_vMutex, 0L, FALSE);
     if (ulrc != 0)
     {
-        wxLogLastError(_T("DosCreateMutexSem()"));
+        wxLogLastError(wxT("DosCreateMutexSem()"));
         m_vMutex = NULL;
         m_vEvent = NULL;
         return;
@@ -222,7 +229,7 @@ wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount)
     ulrc = ::DosCreateEventSem(NULL, &m_vEvent, 0L, FALSE);
     if ( ulrc != 0)
     {
-        wxLogLastError(_T("DosCreateEventSem()"));
+        wxLogLastError(wxT("DosCreateEventSem()"));
         ::DosCloseMutexSem(m_vMutex);
         m_vMutex = NULL;
         m_vEvent = NULL;
@@ -237,11 +244,11 @@ wxSemaphoreInternal::~wxSemaphoreInternal()
     {
         if ( ::DosCloseEventSem(m_vEvent) )
         {
-            wxLogLastError(_T("DosCloseEventSem(semaphore)"));
+            wxLogLastError(wxT("DosCloseEventSem(semaphore)"));
         }
         if ( ::DosCloseMutexSem(m_vMutex) )
         {
-            wxLogLastError(_T("DosCloseMutexSem(semaphore)"));
+            wxLogLastError(wxT("DosCloseMutexSem(semaphore)"));
         }
         else
             m_vEvent = NULL;
@@ -265,7 +272,7 @@ wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long ulMilliseconds)
                     return wxSEMA_TIMEOUT;
 
             default:
-                wxLogLastError(_T("DosWaitEventSem(semaphore)"));
+                wxLogLastError(wxT("DosWaitEventSem(semaphore)"));
                 return wxSEMA_MISC_ERROR;
         }
         ulrc = :: DosRequestMutexSem(m_vMutex, ulMilliseconds);
@@ -325,7 +332,7 @@ wxSemaError wxSemaphoreInternal::Post()
         return wxSEMA_OVERFLOW;
     if ( ulrc != NO_ERROR && ulrc != ERROR_ALREADY_POSTED )
     {
-        wxLogLastError(_T("DosPostEventSem(semaphore)"));
+        wxLogLastError(wxT("DosPostEventSem(semaphore)"));
 
         return wxSEMA_MISC_ERROR;
     }
@@ -347,7 +354,7 @@ public:
     {
         m_hThread = 0;
         m_eState = STATE_NEW;
-        m_nPriority = WXTHREAD_DEFAULT_PRIORITY;
+        m_nPriority = wxPRIORITY_DEFAULT;
     }
 
     ~wxThreadInternal()
@@ -389,11 +396,9 @@ private:
     unsigned int                    m_nPriority;  // thread priority in "wx" units
 };
 
-void wxThreadInternal::OS2ThreadStart(
-  void * pParam
-)
+void wxThreadInternal::OS2ThreadStart( void * pParam )
 {
-    DWORD                           dwRet;
+    DWORD dwRet;
     bool bWasCancelled;
 
     wxThread *pThread = (wxThread *)pParam;
@@ -406,7 +411,7 @@ void wxThreadInternal::OS2ThreadStart(
     if ( pThread->m_internal->GetState() == STATE_EXITED )
     {
         dwRet = (DWORD)-1;
-        bWasCancelled = TRUE;
+        bWasCancelled = true;
     }
     else // do run thread
     {
@@ -474,15 +479,14 @@ void wxThreadInternal::SetPriority(
     }
 }
 
-bool wxThreadInternal::Create(
-  wxThread*                         pThread
-, unsigned int                      uStackSize
-)
+bool wxThreadInternal::Create( wxThread* pThread,
+                               unsigned int uStackSize)
 {
-    int                          tid;
+    int tid;
 
     if (!uStackSize)
-      uStackSize = 131072;
+        uStackSize = 131072;
+
     pThread->m_critsect.Enter();
     tid = _beginthread(wxThreadInternal::OS2ThreadStart,
                              NULL, uStackSize, pThread);
@@ -490,38 +494,39 @@ bool wxThreadInternal::Create(
     {
         wxLogSysError(_("Can't create thread"));
 
-        return FALSE;
+        return false;
     }
     m_hThread = tid;
-    if (m_nPriority != WXTHREAD_DEFAULT_PRIORITY)
+    if (m_nPriority != wxPRIORITY_DEFAULT)
     {
         SetPriority(m_nPriority);
     }
 
-    return(TRUE);
+    return true;
 }
 
 bool wxThreadInternal::Suspend()
 {
-    ULONG                           ulrc = ::DosSuspendThread(m_hThread);
+    ULONG ulrc = ::DosSuspendThread(m_hThread);
 
     if (ulrc != 0)
     {
-        wxLogSysError(_("Can not suspend thread %lu"), m_hThread);
-        return FALSE;
+        wxLogSysError(_("Cannot suspend thread %lu"), m_hThread);
+        return false;
     }
     m_eState = STATE_PAUSED;
-    return TRUE;
+
+    return true;
 }
 
 bool wxThreadInternal::Resume()
 {
-    ULONG                           ulrc = ::DosResumeThread(m_hThread);
+    ULONG ulrc = ::DosResumeThread(m_hThread);
 
     if (ulrc != 0)
     {
-        wxLogSysError(_("Can not resume thread %lu"), m_hThread);
-        return FALSE;
+        wxLogSysError(_("Cannot resume thread %lu"), m_hThread);
+        return false;
     }
 
     // don't change the state from STATE_EXITED because it's special and means
@@ -532,7 +537,7 @@ bool wxThreadInternal::Resume()
         m_eState = STATE_RUNNING;
     }
 
-    return TRUE;
+    return true;
 }
 
 // static functions
@@ -544,18 +549,6 @@ wxThread *wxThread::This()
     return pThread;
 }
 
-bool wxThread::IsMain()
-{
-    PTIB                            ptib;
-    PPIB                            ppib;
-
-    ::DosGetInfoBlocks(&ptib, &ppib);
-
-    if (ptib->tib_ptib2->tib2_ultid == s_ulIdMainThread)
-        return TRUE;
-    return FALSE;
-}
-
 #ifdef Yield
     #undef Yield
 #endif
@@ -565,13 +558,6 @@ void wxThread::Yield()
     ::DosSleep(0);
 }
 
-void wxThread::Sleep(
-  unsigned long                     ulMilliseconds
-)
-{
-    ::DosSleep(ulMilliseconds);
-}
-
 int wxThread::GetCPUCount()
 {
     ULONG CPUCount;
@@ -585,18 +571,18 @@ int wxThread::GetCPUCount()
     return CPUCount;
 }
 
-unsigned long wxThread::GetCurrentId()
+wxThreadIdType wxThread::GetCurrentId()
 {
     PTIB                            ptib;
     PPIB                            ppib;
 
     ::DosGetInfoBlocks(&ptib, &ppib);
-    return (unsigned long) ptib->tib_ptib2->tib2_ultid;
+    return (wxThreadIdType) ptib->tib_ptib2->tib2_ultid;
 }
 
 bool wxThread::SetConcurrency(size_t level)
 {
-    wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
+    wxASSERT_MSG( IsMain(), wxT("should only be called from the main thread") );
 
     // ok only for the default one
     if ( level == 0 )
@@ -640,6 +626,14 @@ wxThreadError wxThread::Run()
 {
     wxCriticalSectionLocker         lock((wxCriticalSection &)m_critsect);
 
+    // Create the thread if it wasn't created yet with an explicit
+    // Create() call:
+    if ( !m_internal->GetHandle() )
+    {
+        if ( !m_internal->Create(this, 0) )
+            return wxTHREAD_NO_RESOURCE;
+    }
+
     if ( m_internal->GetState() != STATE_NEW )
     {
         // actually, it may be almost any state at all, not only STATE_RUNNING
@@ -675,18 +669,18 @@ wxThreadError wxThread::Resume()
 // stopping thread
 // ---------------
 
-wxThread::ExitCode wxThread::Wait()
+wxThread::ExitCode wxThread::Wait(wxThreadWait waitMode)
 {
     // although under Windows we can wait for any thread, it's an error to
     // wait for a detached one in wxWin API
     wxCHECK_MSG( !IsDetached(), (ExitCode)-1,
-                 _T("can't wait for detached thread") );
+                 wxT("can't wait for detached thread") );
     ExitCode rc = (ExitCode)-1;
-    (void)Delete(&rc);
+    (void)Delete(&rc, waitMode);
     return(rc);
 }
 
-wxThreadError wxThread::Delete(ExitCode *pRc)
+wxThreadError wxThread::Delete(ExitCode *pRc, wxThreadWait WXUNUSED(waitMode))
 {
     ExitCode rc = 0;
 
@@ -694,9 +688,9 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
 
     // we might need to resume the thread, but we might also not need to cancel
     // it if it doesn't run yet
-    bool shouldResume = FALSE,
-         shouldCancel = TRUE,
-         isRunning = FALSE;
+    bool shouldResume = false,
+         shouldCancel = true,
+         isRunning = false;
 
     // check if the thread already started to run
     {
@@ -711,10 +705,10 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
 
             Resume();   // it knows about STATE_EXITED special case
 
-            shouldCancel = FALSE;
-            isRunning = TRUE;
+            shouldCancel = false;
+            isRunning = true;
 
-            // shouldResume is correctly set to FALSE here
+            // shouldResume is correctly set to false here
         }
         else
         {
@@ -726,14 +720,14 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
     if ( shouldResume )
         Resume();
 
-    TID                             hThread = m_internal->GetHandle();
+    TID hThread = m_internal->GetHandle();
 
     if ( isRunning || IsRunning())
     {
         if (IsMain())
         {
             // set flag for wxIsWaitingForThread()
-            gs_bWaitingForThread = TRUE;
+            gs_bWaitingForThread = true;
         }
 
         // ask the thread to terminate
@@ -811,7 +805,7 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
 
         if ( IsMain() )
         {
-            gs_bWaitingForThread = FALSE;
+            gs_bWaitingForThread = false;
         }
     }
 
@@ -858,9 +852,7 @@ wxThreadError wxThread::Kill()
     return wxTHREAD_NO_ERROR;
 }
 
-void wxThread::Exit(
-  ExitCode                          pStatus
-)
+void wxThread::Exit(ExitCode WXUNUSED(pStatus))
 {
     delete this;
     _endthread();
@@ -942,13 +934,9 @@ bool wxThreadModule::OnInit()
     gs_pCritsectGui = new wxCriticalSection();
     gs_pCritsectGui->Enter();
 
-    PTIB                            ptib;
-    PPIB                            ppib;
-
-    ::DosGetInfoBlocks(&ptib, &ppib);
+    wxThread::ms_idMainThread = wxThread::GetCurrentId();
 
-    s_ulIdMainThread = ptib->tib_ptib2->tib2_ultid;
-    return TRUE;
+    return true;
 }
 
 void wxThreadModule::OnExit()
@@ -983,7 +971,7 @@ void WXDLLEXPORT wxWakeUpMainThread()
 #endif
 }
 
-void WXDLLEXPORT wxMutexGuiEnter()
+void wxMutexGuiEnterImpl()
 {
     // this would dead lock everything...
     wxASSERT_MSG( !wxThread::IsMain(),
@@ -1005,13 +993,13 @@ void WXDLLEXPORT wxMutexGuiEnter()
     gs_pCritsectGui->Enter();
 }
 
-void WXDLLEXPORT wxMutexGuiLeave()
+void wxMutexGuiLeaveImpl()
 {
     wxCriticalSectionLocker enter(*gs_pCritsectWaitingForGui);
 
     if ( wxThread::IsMain() )
     {
-        gs_bGuiOwnedByMainThread = FALSE;
+        gs_bGuiOwnedByMainThread = false;
     }
     else
     {
@@ -1042,7 +1030,7 @@ void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
         {
             gs_pCritsectGui->Enter();
 
-            gs_bGuiOwnedByMainThread = TRUE;
+            gs_bGuiOwnedByMainThread = true;
         }
         //else: already have it, nothing to do
     }