Thread fixes (but they still don't work at all...)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1309 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-01-02 22:55:03 +00:00
parent d524867f4c
commit 3222fde2af
4 changed files with 439 additions and 274 deletions

View File

@@ -38,6 +38,7 @@
#include "wx/msw/private.h"
#include "wx/log.h"
#include "wx/module.h"
#include "wx/thread.h"
#if wxUSE_WX_RESOURCES
#include "wx/resource.h"
@@ -844,45 +845,52 @@ bool wxApp::ProcessMessage(WXMSG *Msg)
void wxApp::OnIdle(wxIdleEvent& event)
{
static bool inOnIdle = FALSE;
static bool s_inOnIdle = FALSE;
// Avoid recursion (via ProcessEvent default case)
if (inOnIdle)
return;
// Avoid recursion (via ProcessEvent default case)
if ( s_inOnIdle )
return;
inOnIdle = TRUE;
s_inOnIdle = TRUE;
// 'Garbage' collection of windows deleted with Close().
DeletePendingObjects();
// 'Garbage' collection of windows deleted with Close().
DeletePendingObjects();
// flush the logged messages if any
wxLog *pLog = wxLog::GetActiveTarget();
if ( pLog != NULL && pLog->HasPendingMessages() )
pLog->Flush();
// flush the logged messages if any
wxLog *pLog = wxLog::GetActiveTarget();
if ( pLog != NULL && pLog->HasPendingMessages() )
pLog->Flush();
// Send OnIdle events to all windows
bool needMore = SendIdleEvents();
// bool needMore = FALSE;
// Send OnIdle events to all windows
if ( SendIdleEvents() )
{
// SendIdleEvents() returns TRUE if at least one window requested more
// idle events
event.RequestMore(TRUE);
}
if (needMore)
event.RequestMore(TRUE);
// give a chance to all other threads to perform GUI calls
wxMutexGuiLeave();
::Sleep(0);
wxMutexGuiEnter();
inOnIdle = FALSE;
s_inOnIdle = FALSE;
}
// Send idle event to all top-level windows
bool wxApp::SendIdleEvents()
{
bool needMore = FALSE;
wxNode* node = wxTopLevelWindows.First();
while (node)
{
wxWindow* win = (wxWindow*) node->Data();
if (SendIdleEvents(win))
wxNode* node = wxTopLevelWindows.First();
while (node)
{
wxWindow* win = (wxWindow*) node->Data();
if (SendIdleEvents(win))
needMore = TRUE;
node = node->Next();
}
node = node->Next();
}
return needMore;
}

View File

@@ -10,244 +10,331 @@
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
#pragma implementation "thread.h"
#pragma implementation "thread.h"
#endif
// this is here to regen the precompiled header in the ide compile otherwise the
// compiler crashes in vc5 (nfi why)
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#if defined(__BORLANDC__)
#pragma hdrstop
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#include "wx/wx.h"
#endif
#if wxUSE_THREADS
#include <stdio.h>
#include <windows.h>
#include "wx/module.h"
#include "wx/thread.h"
enum thread_state {
STATE_IDLE = 0,
STATE_RUNNING,
STATE_CANCELED,
STATE_EXITED
enum thread_state
{
STATE_IDLE = 0,
STATE_RUNNING,
STATE_CANCELED,
STATE_EXITED
};
/////////////////////////////////////////////////////////////////////////////
// Static variables
/////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// static variables
// ----------------------------------------------------------------------------
static HANDLE p_mainid;
wxMutex *wxMainMutex; // controls access to all GUI functions
// id of the main thread - the one which can call GUI functions without first
// calling wxMutexGuiEnter()
static HANDLE s_idMainThread;
/////////////////////////////////////////////////////////////////////////////
// Windows implementation
/////////////////////////////////////////////////////////////////////////////
// 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
// any GUI calls
static wxCriticalSection *s_critsectGui;
class wxMutexInternal {
// ============================================================================
// Windows implementation of thread classes
// ============================================================================
// ----------------------------------------------------------------------------
// wxMutex implementation
// ----------------------------------------------------------------------------
class wxMutexInternal
{
public:
HANDLE p_mutex;
HANDLE p_mutex;
};
wxMutex::wxMutex()
{
p_internal = new wxMutexInternal;
p_internal->p_mutex = CreateMutex(NULL, FALSE, NULL);
if ( !p_internal->p_mutex )
{
wxLogSysError(_("Can not create mutex."));
}
p_internal = new wxMutexInternal;
p_internal->p_mutex = CreateMutex(NULL, FALSE, NULL);
if ( !p_internal->p_mutex )
{
wxLogSysError(_("Can not create mutex."));
}
m_locked = 0;
m_locked = 0;
}
wxMutex::~wxMutex()
{
if (m_locked > 0)
wxLogDebug("Warning: freeing a locked mutex (%d locks).", m_locked);
CloseHandle(p_internal->p_mutex);
if (m_locked > 0)
wxLogDebug("Warning: freeing a locked mutex (%d locks).", m_locked);
CloseHandle(p_internal->p_mutex);
}
wxMutexError wxMutex::Lock()
{
DWORD ret;
DWORD ret;
ret = WaitForSingleObject(p_internal->p_mutex, INFINITE);
if (ret == WAIT_ABANDONED)
return wxMUTEX_BUSY;
ret = WaitForSingleObject(p_internal->p_mutex, INFINITE);
switch ( ret )
{
case WAIT_ABANDONED:
return wxMUTEX_BUSY;
m_locked++;
return wxMUTEX_NO_ERROR;
case WAIT_OBJECT_0:
// ok
break;
case WAIT_FAILED:
wxLogSysError(_("Couldn't acquire a mutex lock"));
return wxMUTEX_MISC_ERROR;
case WAIT_TIMEOUT:
default:
wxFAIL_MSG("impossible return value in wxMutex::Lock");
}
m_locked++;
return wxMUTEX_NO_ERROR;
}
wxMutexError wxMutex::TryLock()
{
DWORD ret;
DWORD ret;
ret = WaitForSingleObject(p_internal->p_mutex, 0);
if (ret == WAIT_TIMEOUT || ret == WAIT_ABANDONED)
return wxMUTEX_BUSY;
ret = WaitForSingleObject(p_internal->p_mutex, 0);
if (ret == WAIT_TIMEOUT || ret == WAIT_ABANDONED)
return wxMUTEX_BUSY;
m_locked++;
return wxMUTEX_NO_ERROR;
m_locked++;
return wxMUTEX_NO_ERROR;
}
wxMutexError wxMutex::Unlock()
{
if (m_locked > 0)
m_locked--;
if (m_locked > 0)
m_locked--;
BOOL ret = ReleaseMutex(p_internal->p_mutex);
if ( ret != 0 )
{
wxLogSysError(_("Couldn't release a mutex"));
return wxMUTEX_MISC_ERROR;
}
BOOL ret = ReleaseMutex(p_internal->p_mutex);
if ( ret == 0 )
{
wxLogSysError(_("Couldn't release a mutex"));
return wxMUTEX_MISC_ERROR;
}
return wxMUTEX_NO_ERROR;
return wxMUTEX_NO_ERROR;
}
class wxConditionInternal {
// ----------------------------------------------------------------------------
// wxCondition implementation
// ----------------------------------------------------------------------------
class wxConditionInternal
{
public:
HANDLE event;
int waiters;
HANDLE event;
int waiters;
};
wxCondition::wxCondition()
{
p_internal = new wxConditionInternal;
p_internal->event = CreateEvent(NULL, FALSE, FALSE, NULL);
if ( !p_internal->event )
{
wxLogSysError(_("Can not create event object."));
}
p_internal = new wxConditionInternal;
p_internal->event = CreateEvent(NULL, FALSE, FALSE, NULL);
if ( !p_internal->event )
{
wxLogSysError(_("Can not create event object."));
}
p_internal->waiters = 0;
p_internal->waiters = 0;
}
wxCondition::~wxCondition()
{
CloseHandle(p_internal->event);
CloseHandle(p_internal->event);
}
void wxCondition::Wait(wxMutex& mutex)
{
mutex.Unlock();
p_internal->waiters++;
WaitForSingleObject(p_internal->event, INFINITE);
p_internal->waiters--;
mutex.Lock();
mutex.Unlock();
p_internal->waiters++;
WaitForSingleObject(p_internal->event, INFINITE);
p_internal->waiters--;
mutex.Lock();
}
bool wxCondition::Wait(wxMutex& mutex, unsigned long sec,
bool wxCondition::Wait(wxMutex& mutex,
unsigned long sec,
unsigned long nsec)
{
DWORD ret;
DWORD ret;
mutex.Unlock();
p_internal->waiters++;
ret = WaitForSingleObject(p_internal->event, (sec*1000)+(nsec/1000000));
p_internal->waiters--;
mutex.Lock();
mutex.Unlock();
p_internal->waiters++;
ret = WaitForSingleObject(p_internal->event, (sec*1000)+(nsec/1000000));
p_internal->waiters--;
mutex.Lock();
return (ret != WAIT_TIMEOUT);
return (ret != WAIT_TIMEOUT);
}
void wxCondition::Signal()
{
SetEvent(p_internal->event);
SetEvent(p_internal->event);
}
void wxCondition::Broadcast()
{
int i;
int i;
for (i=0;i<p_internal->waiters;i++)
{
if ( SetEvent(p_internal->event) == 0 )
for (i=0;i<p_internal->waiters;i++)
{
wxLogSysError(_("Couldn't change the state of event object."));
if ( SetEvent(p_internal->event) == 0 )
{
wxLogSysError(_("Couldn't change the state of event object."));
}
}
}
}
class wxThreadInternal {
public:
static DWORD WinThreadStart(LPVOID arg);
// ----------------------------------------------------------------------------
// wxCriticalSection implementation
// ----------------------------------------------------------------------------
HANDLE thread_id;
int state;
int prio, defer;
DWORD tid;
class wxCriticalSectionInternal
{
public:
// init the critical section object
wxCriticalSectionInternal()
{ ::InitializeCriticalSection(&m_data); }
// implicit cast to the associated data
operator CRITICAL_SECTION *() { return &m_data; }
// free the associated ressources
~wxCriticalSectionInternal()
{ ::DeleteCriticalSection(&m_data); }
private:
CRITICAL_SECTION m_data;
};
wxCriticalSection::wxCriticalSection()
{
m_critsect = new wxCriticalSectionInternal;
}
wxCriticalSection::~wxCriticalSection()
{
delete m_critsect;
}
void wxCriticalSection::Enter()
{
::EnterCriticalSection(*m_critsect);
}
void wxCriticalSection::Leave()
{
::LeaveCriticalSection(*m_critsect);
}
// ----------------------------------------------------------------------------
// wxThread implementation
// ----------------------------------------------------------------------------
class wxThreadInternal
{
public:
static DWORD WinThreadStart(LPVOID arg);
HANDLE thread_id;
int state;
int prio, defer;
DWORD tid;
};
DWORD wxThreadInternal::WinThreadStart(LPVOID arg)
{
wxThread *ptr = (wxThread *)arg;
DWORD ret;
wxThread *ptr = (wxThread *)arg;
DWORD ret;
ret = (DWORD)ptr->Entry();
ptr->p_internal->state = STATE_EXITED;
ret = (DWORD)ptr->Entry();
ptr->p_internal->state = STATE_EXITED;
return ret;
return ret;
}
wxThreadError wxThread::Create()
{
int prio = p_internal->prio;
int prio = p_internal->prio;
p_internal->thread_id = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)wxThreadInternal::WinThreadStart,
(void *)this, CREATE_SUSPENDED, &p_internal->tid);
p_internal->thread_id = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)wxThreadInternal::WinThreadStart,
(void *)this, CREATE_SUSPENDED, &p_internal->tid);
if ( p_internal->thread_id == NULL )
{
wxLogSysError(_("Can't create thread"));
return wxTHREAD_NO_RESOURCE;
}
if ( p_internal->thread_id == NULL )
{
wxLogSysError(_("Can't create thread"));
return wxTHREAD_NO_RESOURCE;
}
int win_prio;
if (prio <= 20)
win_prio = THREAD_PRIORITY_LOWEST;
else if (prio <= 40)
win_prio = THREAD_PRIORITY_BELOW_NORMAL;
else if (prio <= 60)
win_prio = THREAD_PRIORITY_NORMAL;
else if (prio <= 80)
win_prio = THREAD_PRIORITY_ABOVE_NORMAL;
else if (prio <= 100)
win_prio = THREAD_PRIORITY_HIGHEST;
else
{
wxFAIL_MSG("invalid value of thread priority parameter");
win_prio = THREAD_PRIORITY_NORMAL;
}
int win_prio;
if (prio <= 20)
win_prio = THREAD_PRIORITY_LOWEST;
else if (prio <= 40)
win_prio = THREAD_PRIORITY_BELOW_NORMAL;
else if (prio <= 60)
win_prio = THREAD_PRIORITY_NORMAL;
else if (prio <= 80)
win_prio = THREAD_PRIORITY_ABOVE_NORMAL;
else if (prio <= 100)
win_prio = THREAD_PRIORITY_HIGHEST;
else
{
wxFAIL_MSG("invalid value of thread priority parameter");
win_prio = THREAD_PRIORITY_NORMAL;
}
SetThreadPriority(p_internal->thread_id, win_prio);
SetThreadPriority(p_internal->thread_id, win_prio);
ResumeThread(p_internal->thread_id);
p_internal->state = STATE_RUNNING;
return wxTHREAD_NO_ERROR;
ResumeThread(p_internal->thread_id);
p_internal->state = STATE_RUNNING;
return wxTHREAD_NO_ERROR;
}
wxThreadError wxThread::Destroy()
{
if (p_internal->state != STATE_RUNNING)
return wxTHREAD_NOT_RUNNING;
if (p_internal->state != STATE_RUNNING)
return wxTHREAD_NOT_RUNNING;
if (p_internal->defer == FALSE)
TerminateThread(p_internal->thread_id, 0);
else
p_internal->state = STATE_CANCELED;
if ( p_internal->defer )
// soft termination: just set the flag and wait until the thread
// auto terminates
p_internal->state = STATE_CANCELED;
else
// kill the thread
TerminateThread(p_internal->thread_id, 0);
return wxTHREAD_NO_ERROR;
return wxTHREAD_NO_ERROR;
}
wxThreadError wxThread::Pause()
@@ -278,121 +365,136 @@ wxThreadError wxThread::Resume()
void wxThread::Exit(void *status)
{
p_internal->state = STATE_EXITED;
ExitThread((DWORD)status);
p_internal->state = STATE_EXITED;
ExitThread((DWORD)status);
}
void wxThread::SetPriority(int prio)
{
p_internal->prio = prio;
p_internal->prio = prio;
}
int wxThread::GetPriority() const
{
return p_internal->prio;
return p_internal->prio;
}
void wxThread::DeferDestroy(bool on)
{
p_internal->defer = on;
p_internal->defer = on;
}
void wxThread::TestDestroy()
bool wxThread::TestDestroy()
{
if (p_internal->state == STATE_CANCELED)
ExitThread(0);
return p_internal->state == STATE_CANCELED;
}
void *wxThread::Join()
{
DWORD exit_code;
DWORD exit_code;
if (p_internal->state == STATE_IDLE)
return NULL;
if (p_internal->state == STATE_IDLE)
return NULL;
if (wxThread::IsMain())
wxMainMutex->Unlock();
WaitForSingleObject(p_internal->thread_id, INFINITE);
if (wxThread::IsMain())
wxMainMutex->Lock();
if (wxThread::IsMain())
s_critsectGui->Leave();
WaitForSingleObject(p_internal->thread_id, INFINITE);
if (wxThread::IsMain())
s_critsectGui->Enter();
GetExitCodeThread(p_internal->thread_id, &exit_code);
CloseHandle(p_internal->thread_id);
GetExitCodeThread(p_internal->thread_id, &exit_code);
CloseHandle(p_internal->thread_id);
p_internal->state = STATE_IDLE;
p_internal->state = STATE_IDLE;
return (void *)exit_code;
return (void *)exit_code;
}
unsigned long wxThread::GetID() const
{
return (unsigned long)p_internal->tid;
return (unsigned long)p_internal->tid;
}
bool wxThread::IsRunning() const
{
return (p_internal->state == STATE_RUNNING);
return (p_internal->state == STATE_RUNNING);
}
bool wxThread::IsAlive() const
{
return (p_internal->state == STATE_RUNNING);
return (p_internal->state == STATE_RUNNING);
}
bool wxThread::IsMain()
{
return (GetCurrentThread() == p_mainid);
return (GetCurrentThread() == s_idMainThread);
}
wxThread::wxThread()
{
p_internal = new wxThreadInternal();
p_internal = new wxThreadInternal();
p_internal->defer = FALSE;
p_internal->prio = WXTHREAD_DEFAULT_PRIORITY;
p_internal->state = STATE_IDLE;
p_internal->defer = FALSE;
p_internal->prio = WXTHREAD_DEFAULT_PRIORITY;
p_internal->state = STATE_IDLE;
}
wxThread::~wxThread()
{
Destroy();
Join();
delete p_internal;
Destroy();
Join();
delete p_internal;
}
// The default callback just joins the thread and throws away the result.
void wxThread::OnExit()
{
Join();
}
// GUI mutex functions
// ----------------------------------------------------------------------------
// Automatic initialization for thread module
// ----------------------------------------------------------------------------
class wxThreadModule : public wxModule
{
public:
virtual bool OnInit();
virtual void OnExit();
private:
DECLARE_DYNAMIC_CLASS(wxThreadModule)
};
IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
bool wxThreadModule::OnInit()
{
s_critsectGui = new wxCriticalSection();
s_critsectGui->Enter();
s_idMainThread = GetCurrentThread();
return TRUE;
}
void wxThreadModule::OnExit()
{
if ( s_critsectGui )
{
s_critsectGui->Leave();
delete s_critsectGui;
s_critsectGui = NULL;
}
}
// under Windows, these functions are implemented usign a critical section and
// not a mutex, so the names are a bit confusing
void WXDLLEXPORT wxMutexGuiEnter()
{
wxFAIL_MSG("not implemented");
//s_critsectGui->Enter();
}
void WXDLLEXPORT wxMutexGuiLeave()
{
wxFAIL_MSG("not implemented");
//s_critsectGui->Leave();
}
// Automatic initialization
IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
bool wxThreadModule::OnInit()
{
wxMainMutex = new wxMutex();
p_mainid = GetCurrentThread();
wxMainMutex->Lock();
return TRUE;
}
void wxThreadModule::OnExit()
{
wxMainMutex->Unlock();
delete wxMainMutex;
};
#endif // wxUSE_THREADS