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

@@ -28,10 +28,10 @@
// Level 1: wxDC, OnSize (etc.) compatibility, but // Level 1: wxDC, OnSize (etc.) compatibility, but
// some new features such as event tables // some new features such as event tables
#define wxUSE_AUTOTRANS 1 #define wxUSE_AUTOTRANS 1
// Define wxTString // Define wxTString
#define wxUSE_POSTSCRIPT 0 #define wxUSE_POSTSCRIPT 0
// 0 for no PostScript device context // 0 for no PostScript device context
#define wxUSE_AFM_FOR_POSTSCRIPT 0 #define wxUSE_AFM_FOR_POSTSCRIPT 0
// 1 to use font metric files in GetTextExtent // 1 to use font metric files in GetTextExtent
#define wxUSE_METAFILE 1 #define wxUSE_METAFILE 1
@@ -120,7 +120,7 @@
// since you may well need to output // since you may well need to output
// an error log in a production // an error log in a production
// version (or non-debugging beta) // version (or non-debugging beta)
#define wxUSE_GLOBAL_MEMORY_OPERATORS 1 #define wxUSE_GLOBAL_MEMORY_OPERATORS 0
// In debug mode, cause new and delete to be redefined globally. // In debug mode, cause new and delete to be redefined globally.
// If this causes problems (e.g. link errors), set this to 0. // If this causes problems (e.g. link errors), set this to 0.
@@ -149,12 +149,15 @@
// VC++ 4.2 and above allows <iostream> and <iostream.h> // VC++ 4.2 and above allows <iostream> and <iostream.h>
// but you can't mix them. Set to 1 for <iostream.h>, // but you can't mix them. Set to 1 for <iostream.h>,
// 0 for <iostream> // 0 for <iostream>
#define wxUSE_WXCONFIG 1 #define wxUSE_WXCONFIG 1
// if enabled, compiles built-in OS independent wxConfig // if enabled, compiles built-in OS independent wxConfig
// class and it's file (any platform) and registry (Win) // class and it's file (any platform) and registry (Win)
// based implementations // based implementations
#define wxUSE_THREADS 1
// support for multithreaded applications: if
// 1, compile in thread classes (thread.h)
// and make the library thread safe
/* /*
* Finer detail * Finer detail
* *

View File

@@ -6,7 +6,7 @@
// Created: 06/16/98 // Created: 06/16/98
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux // Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__ #ifdef __GNUG__
@@ -18,15 +18,20 @@
#include "wx/wxprec.h" #include "wx/wxprec.h"
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#pragma hdrstop #pragma hdrstop
#endif #endif
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/wx.h" #include "wx/wx.h"
#endif #endif
#if !wxUSE_THREADS
#error "This sample requires thread support!"
#endif // wxUSE_THREADS
#include "wx/thread.h" #include "wx/thread.h"
#include "wx/dynarray.h" #include "wx/dynarray.h"
#include "wx/time.h"
// Define a new application type // Define a new application type
class MyApp: public wxApp class MyApp: public wxApp
@@ -43,7 +48,7 @@ class MyFrame: public wxFrame
public: public:
// ctor // ctor
MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h); MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
// operations // operations
void WriteText(const wxString& text) { m_txtctrl->WriteText(text); } void WriteText(const wxString& text) { m_txtctrl->WriteText(text); }
@@ -57,14 +62,15 @@ public:
void OnResumeThread(wxCommandEvent& event); void OnResumeThread(wxCommandEvent& event);
void OnSize(wxSizeEvent &event); void OnSize(wxSizeEvent &event);
bool OnClose(void) { return TRUE; } void OnIdle(wxIdleEvent &event);
bool OnClose() { return TRUE; }
public: public:
wxArrayThread m_threads; wxArrayThread m_threads;
private: private:
wxTextCtrl *m_txtctrl; wxTextCtrl *m_txtctrl;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
@@ -72,8 +78,12 @@ class MyThread: public wxThread
{ {
public: public:
MyThread(MyFrame *frame); MyThread(MyFrame *frame);
void *Entry(); // thread execution starts here
virtual void *Entry();
// write something to the text control
void WriteText(const wxString& text);
public: public:
size_t m_count; size_t m_count;
@@ -87,34 +97,56 @@ MyThread::MyThread(MyFrame *frame)
m_frame = frame; m_frame = frame;
} }
void MyThread::WriteText(const wxString& text)
{
wxString msg;
msg << wxTime().FormatTime() << ": " << text;
// before doing any GUI calls we must ensure that this thread is the only
// one doing it!
wxMutexGuiEnter();
m_frame->WriteText(msg);
wxMutexGuiLeave();
}
void *MyThread::Entry() void *MyThread::Entry()
{ {
wxString text; wxString text;
DeferDestroy(TRUE); DeferDestroy(TRUE);
while (1) {
TestDestroy();
wxMutexGuiEnter();
text.Printf("[%u] Thread 0x%x here.\n", ++m_count, GetID()); text.Printf("Thread 0x%x started.\n", GetID());
m_frame->WriteText(text); WriteText(text);
for ( m_count = 0; m_count < 20; m_count++ )
{
// check if we were asked to exit
if ( TestDestroy() )
break;
text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID());
WriteText(text);
wxMutexGuiLeave();
wxSleep(1); wxSleep(1);
} }
text.Printf("Thread 0x%x finished.\n", GetID());
WriteText(text);
return NULL; return NULL;
} }
// ID for the menu commands // ID for the menu commands
#define TEST_QUIT 1 enum
#define TEST_TEXT 101 {
#define TEST_ABOUT 102 TEST_QUIT = 1,
#define TEST_START_THREAD 103 TEST_TEXT = 101,
#define TEST_STOP_THREAD 104 TEST_ABOUT = 102,
#define TEST_PAUSE_THREAD 105 TEST_START_THREAD = 103,
#define TEST_RESUME_THREAD 106 TEST_STOP_THREAD = 104,
TEST_PAUSE_THREAD = 105,
TEST_RESUME_THREAD = 106
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(TEST_QUIT, MyFrame::OnQuit) EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
@@ -125,40 +157,41 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread) EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread)
EVT_SIZE(MyFrame::OnSize) EVT_SIZE(MyFrame::OnSize)
EVT_IDLE(MyFrame::OnIdle)
END_EVENT_TABLE() END_EVENT_TABLE()
// Create a new application object // Create a new application object
IMPLEMENT_APP (MyApp) IMPLEMENT_APP (MyApp)
// `Main program' equivalent, creating windows and returning main app frame // `Main program' equivalent, creating windows and returning main app frame
bool MyApp::OnInit(void) bool MyApp::OnInit()
{ {
// Create the main frame window // Create the main frame window
MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows thread sample", MyFrame *frame = new MyFrame((wxFrame *)NULL, "", 50, 50, 450, 340);
50, 50, 450, 340);
// Make a menubar // Make a menubar
wxMenu *file_menu = new wxMenu; wxMenu *file_menu = new wxMenu;
file_menu->Append(TEST_ABOUT, "&About"); file_menu->Append(TEST_ABOUT, "&About");
file_menu->Append(TEST_QUIT, "E&xit"); file_menu->Append(TEST_QUIT, "E&xit");
wxMenuBar *menu_bar = new wxMenuBar; wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "&File"); menu_bar->Append(file_menu, "&File");
wxMenu *thread_menu = new wxMenu; wxMenu *thread_menu = new wxMenu;
thread_menu->Append(TEST_START_THREAD, "Start a new thread"); thread_menu->Append(TEST_START_THREAD, "&Start a new thread");
thread_menu->Append(TEST_STOP_THREAD, "Stop a running thread"); thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread");
thread_menu->AppendSeparator(); thread_menu->AppendSeparator();
thread_menu->Append(TEST_PAUSE_THREAD, "Pause a running thread"); thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread");
thread_menu->Append(TEST_RESUME_THREAD, "Resume suspended thread"); thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread");
menu_bar->Append(thread_menu, "Thread"); menu_bar->Append(thread_menu, "&Thread");
frame->SetMenuBar(menu_bar); frame->SetMenuBar(menu_bar);
// Show the frame // Show the frame
frame->Show(TRUE); frame->Show(TRUE);
SetTopWindow(frame); SetTopWindow(frame);
return TRUE; return TRUE;
} }
@@ -167,8 +200,8 @@ MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
{ {
wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200), wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200),
wxTAB_TRAVERSAL ); wxTAB_TRAVERSAL);
m_txtctrl = new wxTextCtrl(panel, -1, "", wxPoint(10,30), wxSize(390, 190), m_txtctrl = new wxTextCtrl(panel, -1, "", wxPoint(10,30), wxSize(390, 190),
wxTE_MULTILINE); wxTE_MULTILINE);
@@ -178,30 +211,30 @@ MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
{ {
MyThread *thread = new MyThread(this); MyThread *thread = new MyThread(this);
thread->Create(); thread->Create();
m_threads.Add(thread); m_threads.Add(thread);
} }
void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
{ {
int no_thrd = m_threads.Count()-1; int no_thrd = m_threads.Count()-1;
if (no_thrd < 0) if (no_thrd < 0)
return; return;
delete (m_threads[no_thrd]); delete m_threads[no_thrd];
m_threads.Remove(no_thrd); m_threads.Remove(no_thrd);
} }
void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
{ {
// resume first suspended thread // resume first suspended thread
size_t n = 0; size_t n = 0;
while ( n < m_threads.Count() && m_threads[n]->IsPaused() ) while ( n < m_threads.Count() && m_threads[n]->IsPaused() )
n--; n--;
if ( n < 0 ) if ( n < 0 )
wxLogError("No thread to pause!"); wxLogError("No thread to pause!");
else else
@@ -210,39 +243,58 @@ void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
{ {
// pause last running thread // pause last running thread
int n = m_threads.Count() - 1; int n = m_threads.Count() - 1;
while ( n >= 0 && !m_threads[n]->IsRunning() ) while ( n >= 0 && !m_threads[n]->IsRunning() )
n--; n--;
if ( n < 0 ) if ( n < 0 )
wxLogError("No thread to pause!"); wxLogError("No thread to pause!");
else else
m_threads[n]->Pause(); m_threads[n]->Pause();
} }
void MyFrame::OnSize(wxSizeEvent& event ) // set the frame title indicating the current number of threads
void MyFrame::OnIdle(wxIdleEvent &event)
{
size_t nRunning = 0,
nCount = m_threads.Count();
for ( size_t n = 0; n < nCount; n++ )
{
if ( m_threads[n]->IsRunning() )
nRunning++;
}
wxString title;
title.Printf("wxWindows thread sample (%u threads, %u running).",
nCount, nRunning);
SetTitle(title);
}
void MyFrame::OnSize(wxSizeEvent& event)
{ {
wxFrame::OnSize(event); wxFrame::OnSize(event);
wxSize size( GetClientSize() ); wxSize size( GetClientSize() );
m_txtctrl->SetSize( 10, 30, size.x-20, size.y-40 ); m_txtctrl->SetSize( 10, 30, size.x-20, size.y-40 );
} }
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
{ {
unsigned int i; for ( size_t i = 0; i < m_threads.Count(); i++ )
for (i=0;i<m_threads.Count();i++) delete m_threads[i];
delete (m_threads[i]);
Close(TRUE); Close(TRUE);
} }
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
{ {
wxMessageDialog dialog(this, "wxThread sample (based on minimal)\nJulian Smart and Guilhem Lavaux", wxMessageDialog dialog(this, "wxThread sample (based on minimal)\n"
"About wxThread sample", wxYES_NO|wxCANCEL); "Julian Smart and Guilhem Lavaux",
"About wxThread sample",
wxOK | wxICON_INFORMATION);
dialog.ShowModal(); dialog.ShowModal();
} }

View File

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

View File

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