]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/thread.cpp
added support for wxALWAYS_SHOW_SB (finally closes patch 410865 -- first still opened...)
[wxWidgets.git] / src / msw / thread.cpp
index 2180ddd994e8a548c97498fb39de7e8eb51b61aa..576f756780d69566bcec83e6f326cdb16c376c1c 100644 (file)
 #endif
 
 #ifndef WX_PRECOMP
-#    include "wx/wx.h"
+    #include "wx/intl.h"
+    #include "wx/app.h"
 #endif
 
 #if wxUSE_THREADS
 
+#include "wx/apptrait.h"
+
 #include "wx/msw/private.h"
 #include "wx/msw/missing.h"
 
@@ -174,6 +177,8 @@ private:
     wxMutexError LockTimeout(DWORD milliseconds);
 
     HANDLE m_mutex;
+
+    DECLARE_NO_COPY_CLASS(wxMutexInternal)
 };
 
 // all mutexes are recursive under Win32 so we don't use mutexType
@@ -243,7 +248,7 @@ wxMutexError wxMutexInternal::Unlock()
 {
     if ( !::ReleaseMutex(m_mutex) )
     {
-        wxLogLastError(_("ReleaseMutex()"));
+        wxLogLastError(_T("ReleaseMutex()"));
 
         return wxMUTEX_MISC_ERROR;
     }
@@ -265,13 +270,24 @@ public:
     bool IsOk() const { return m_semaphore != NULL; }
 
     wxSemaError Wait() { return WaitTimeout(INFINITE); }
-    wxSemaError TryWait() { return WaitTimeout(0); }
+
+    wxSemaError TryWait()
+    {
+        wxSemaError rc = WaitTimeout(0);
+        if ( rc == wxSEMA_TIMEOUT )
+            rc = wxSEMA_BUSY;
+
+        return rc;
+    }
+
     wxSemaError WaitTimeout(unsigned long milliseconds);
 
     wxSemaError Post();
 
 private:
     HANDLE m_semaphore;
+
+    DECLARE_NO_COPY_CLASS(wxSemaphoreInternal)
 };
 
 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount)
@@ -317,7 +333,7 @@ wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds)
            return wxSEMA_NO_ERROR;
 
         case WAIT_TIMEOUT:
-           return wxSEMA_BUSY;
+           return wxSEMA_TIMEOUT;
 
         default:
             wxLogLastError(_T("WaitForSingleObject(semaphore)"));
@@ -536,6 +552,8 @@ private:
     wxThreadState m_state;      // state, see wxThreadState enum
     unsigned int  m_priority;   // thread priority in "wx" units
     DWORD         m_tid;        // thread id
+
+    DECLARE_NO_COPY_CLASS(wxThreadInternal)
 };
 
 THREAD_RETVAL THREAD_CALLCONV wxThreadInternal::WinThreadStart(void *param)
@@ -614,6 +632,9 @@ void wxThreadInternal::SetPriority(unsigned int priority)
 
 bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
 {
+    wxASSERT_MSG( m_state == STATE_NEW && !m_hThread,
+                    _T("Create()ing thread twice?") );
+
     // for compilers which have it, we should use C RTL function for thread
     // creation instead of Win32 API one because otherwise we will have memory
     // leaks if the thread uses C RTL (and most threads do)
@@ -965,30 +986,8 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
 
     HANDLE hThread = m_internal->GetHandle();
 
-       // Check if thread is really still running.  There is a
-       // race condition in WinThreadStart between the time the
-       // m_internal->m_state is set to STATE_EXITED and the win32
-       // thread actually exits.  It can be flagged as STATE_EXITED
-       // and then we don't wait for it to exit.  This will cause
-       // GetExitCodeThread to return STILL_ACTIVE.
-       if ( !isRunning )
-       {
-               if ( !IsRunning() )
-               {
-                       if ( ::GetExitCodeThread(hThread, (LPDWORD)&rc) )
-                       {
-                               if ((DWORD)rc == STILL_ACTIVE)
-                                       isRunning = TRUE;
-                       }
-               }                                                                                                               
-               else
-               {
-                       isRunning = TRUE;
-               }
-       }
-
-    // does it still run?
-    if ( isRunning )
+    // does is still run?
+    if ( isRunning || IsRunning() )
     {
         if ( IsMain() )
         {
@@ -1004,10 +1003,11 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
             m_internal->Cancel();
         }
 
-#if wxUSE_GUI
         // we can't just wait for the thread to terminate because it might be
         // calling some GUI functions and so it will never terminate before we
         // process the Windows messages that result from these functions
+        // (note that even in console applications we might have to process
+        // messages if we use wxExecute() or timers or ...)
         DWORD result = 0;       // suppress warnings from broken compilers
         do
         {
@@ -1044,23 +1044,17 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
                     break;
 
                 case WAIT_OBJECT_0 + 1:
+                    // new message arrived, process it
                     {
-                        MSG    peekMsg;
-                        // Check if a new message has really arrived.
-                        // MsgWaitForMultipleObjects can indicate that a message
-                        // is ready for processing, but this message may be sucked
-                        // up by GetMessage and then GetMessage will hang and not
-                        // allow us to process the actual thread exit event.
-                        if (::PeekMessage(&peekMsg, (HWND) NULL, 0, 0, PM_NOREMOVE))
+                        wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits()
+                                                       : NULL;
+
+                        if ( traits && !traits->DoMessageFromThreadWait() )
                         {
-                            // new message arrived, process it
-                            if ( !wxTheApp->DoMessage() )
-                            {
-                                // WM_QUIT received: kill the thread
-                                Kill();
-                                                            
-                                return wxTHREAD_KILLED;
-                            }
+                            // WM_QUIT received: kill the thread
+                            Kill();
+
+                            return wxTHREAD_KILLED;
                         }
                     }
                     break;
@@ -1069,16 +1063,6 @@ wxThreadError wxThread::Delete(ExitCode *pRc)
                     wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
             }
         } while ( result != WAIT_OBJECT_0 );
-#else // !wxUSE_GUI
-        // simply wait for the thread to terminate
-        //
-        // OTOH, even console apps create windows (in wxExecute, for WinSock
-        // &c), so may be use MsgWaitForMultipleObject() too here?
-        if ( WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0 )
-        {
-            wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
-        }
-#endif // wxUSE_GUI/!wxUSE_GUI
 
         if ( IsMain() )
         {
@@ -1283,7 +1267,7 @@ void wxThreadModule::OnExit()
 // not a mutex, so the names are a bit confusing
 // ----------------------------------------------------------------------------
 
-void WXDLLEXPORT wxMutexGuiEnter()
+void WXDLLIMPEXP_BASE wxMutexGuiEnter()
 {
     // this would dead lock everything...
     wxASSERT_MSG( !wxThread::IsMain(),
@@ -1305,7 +1289,7 @@ void WXDLLEXPORT wxMutexGuiEnter()
     gs_critsectGui->Enter();
 }
 
-void WXDLLEXPORT wxMutexGuiLeave()
+void WXDLLIMPEXP_BASE wxMutexGuiLeave()
 {
     wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
 
@@ -1327,7 +1311,7 @@ void WXDLLEXPORT wxMutexGuiLeave()
     gs_critsectGui->Leave();
 }
 
-void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
+void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter()
 {
     wxASSERT_MSG( wxThread::IsMain(),
                   wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
@@ -1357,13 +1341,13 @@ void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
     }
 }
 
-bool WXDLLEXPORT wxGuiOwnedByMainThread()
+bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread()
 {
     return gs_bGuiOwnedByMainThread;
 }
 
 // wake up the main thread if it's in ::GetMessage()
-void WXDLLEXPORT wxWakeUpMainThread()
+void WXDLLIMPEXP_BASE wxWakeUpMainThread()
 {
     // sending any message would do - hopefully WM_NULL is harmless enough
     if ( !::PostThreadMessage(gs_idMainThread, WM_NULL, 0, 0) )
@@ -1373,7 +1357,7 @@ void WXDLLEXPORT wxWakeUpMainThread()
     }
 }
 
-bool WXDLLEXPORT wxIsWaitingForThread()
+bool WXDLLIMPEXP_BASE wxIsWaitingForThread()
 {
     return gs_waitingForThread;
 }