X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e4db172a3b318df9aff178eb6c5da149d56e0859..76e7cfab8fdb0c7862fd07e427af54181717fc62:/src/os2/thread.cpp diff --git a/src/os2/thread.cpp b/src/os2/thread.cpp index 33cb46646d..94bff21e7c 100644 --- a/src/os2/thread.cpp +++ b/src/os2/thread.cpp @@ -4,7 +4,6 @@ // Author: Original from Wolfram Gloger/Guilhem Lavaux/David Webster // Modified by: Stefan Neis // Created: 04/22/98 -// RCS-ID: $Id$ // Copyright: (c) Stefan Neis (2003) // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -23,11 +22,11 @@ #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/utils.h" #include @@ -57,7 +56,7 @@ 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 @@ -96,7 +95,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: @@ -113,7 +113,7 @@ wxMutexInternal::wxMutexInternal(wxMutexType WXUNUSED(eMutexType)) APIRET ulrc = ::DosCreateMutexSem(NULL, &m_vMutex, 0L, FALSE); if (ulrc != 0) { - wxLogSysError(_("Can not create mutex.")); + wxLogSysError(_("Cannot create mutex.")); m_vMutex = NULL; } } @@ -123,10 +123,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; @@ -136,6 +146,7 @@ wxMutexError wxMutexInternal::LockTimeout(ULONG ulMilliseconds) switch (ulrc) { case ERROR_TIMEOUT: + return wxMUTEX_TIMEOUT; case ERROR_TOO_MANY_SEM_REQUESTS: return wxMUTEX_BUSY; @@ -209,7 +220,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; @@ -217,7 +228,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; @@ -232,11 +243,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; @@ -260,7 +271,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); @@ -320,7 +331,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; } @@ -342,7 +353,7 @@ public: { m_hThread = 0; m_eState = STATE_NEW; - m_nPriority = WXTHREAD_DEFAULT_PRIORITY; + m_nPriority = wxPRIORITY_DEFAULT; } ~wxThreadInternal() @@ -485,7 +496,7 @@ bool wxThreadInternal::Create( wxThread* pThread, return false; } m_hThread = tid; - if (m_nPriority != WXTHREAD_DEFAULT_PRIORITY) + if (m_nPriority != wxPRIORITY_DEFAULT) { SetPriority(m_nPriority); } @@ -499,7 +510,7 @@ bool wxThreadInternal::Suspend() if (ulrc != 0) { - wxLogSysError(_("Can not suspend thread %lu"), m_hThread); + wxLogSysError(_("Cannot suspend thread %lu"), m_hThread); return false; } m_eState = STATE_PAUSED; @@ -513,7 +524,7 @@ bool wxThreadInternal::Resume() if (ulrc != 0) { - wxLogSysError(_("Can not resume thread %lu"), m_hThread); + wxLogSysError(_("Cannot resume thread %lu"), m_hThread); return false; } @@ -537,19 +548,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 @@ -559,13 +557,6 @@ void wxThread::Yield() ::DosSleep(0); } -void wxThread::Sleep( - unsigned long ulMilliseconds -) -{ - ::DosSleep(ulMilliseconds); -} - int wxThread::GetCPUCount() { ULONG CPUCount; @@ -579,18 +570,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 ) @@ -634,6 +625,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 @@ -669,18 +668,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; @@ -934,12 +933,8 @@ 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; } @@ -975,7 +970,7 @@ void WXDLLEXPORT wxWakeUpMainThread() #endif } -void WXDLLEXPORT wxMutexGuiEnter() +void wxMutexGuiEnterImpl() { // this would dead lock everything... wxASSERT_MSG( !wxThread::IsMain(), @@ -997,7 +992,7 @@ void WXDLLEXPORT wxMutexGuiEnter() gs_pCritsectGui->Enter(); } -void WXDLLEXPORT wxMutexGuiLeave() +void wxMutexGuiLeaveImpl() { wxCriticalSectionLocker enter(*gs_pCritsectWaitingForGui);