The threading model now copes better with active validation threads while closing documents.

This commit is contained in:
Gerald Schmidt 2008-07-14 16:21:48 +00:00
parent 8be8af2aa0
commit 62efcae940
3 changed files with 45 additions and 19 deletions

View File

@ -1,4 +1,5 @@
#include "wx/wx.h"
#include "xmlctrl.h"
#include "validationthread.h"
#include "wrapxerces.h"
#include <stdexcept>
@ -31,17 +32,23 @@ void *ValidationThread::Entry()
{
std::auto_ptr<WrapXerces> validator ( new WrapXerces() );
{
//wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if ( *myReleasePtr || TestDestroy() )
{
Exit();
return NULL;
}
}
bool res = validator->validateMemory (
myBuffer.c_str(),
mySystem.c_str(),
myBuffer.size() );
{
//wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if ( *myReleasePtr || TestDestroy() )
{
Exit();
@ -60,13 +67,16 @@ void *ValidationThread::Entry()
*myPositionPtr = std::make_pair ( 0, 0 );
*myMessagePtr = "";
}
}
return NULL;
}
void ValidationThread::OnExit()
{
if ( *myReleasePtr )
return;
*myFinishedPtr = true;
{
//wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if ( *myReleasePtr )
return;
*myFinishedPtr = true;
}
}

View File

@ -68,10 +68,12 @@ XmlCtrl::XmlCtrl (
validationThread = NULL;
validationStarted = false;
validationFinished = false;
validationRelease = false;
grammarFound = false;
validationRequired = (buffer) ? true : false; // NULL for plain XML template
validationReleasePtr = new bool;
*validationReleasePtr = false;
currentMaxLine = 1;
applyProperties ( propertiesParameter );
@ -122,6 +124,28 @@ XmlCtrl::XmlCtrl (
IndicatorSetForeground ( 0, *wxRED );
}
XmlCtrl::~XmlCtrl()
{
attributeMap.clear();
elementMap.clear();
entitySet.clear();
if ( validationStarted && !validationFinished )
{
*validationReleasePtr = true;
// don't delete validationReleasePtr because the thread will check the value before exiting
// this means that 1 variable of type bool will be leaked in this case
// the alternative is waiting for the validation thread to finish, which can take anything up to several minutes
return;
}
delete validationReleasePtr;
}
// taken from wxStyledNotebook (c) Eran Ifrah <eranif@bezeqint.net>
static wxColor LightColour ( const wxColour& color, int percent )
{
@ -2028,18 +2052,18 @@ bool XmlCtrl::backgroundValidate (
wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if ( validationStarted && !validationFinished )
{
validationRelease = true;
*validationReleasePtr = true;
return true; // wait for next idle cycle call from main app frame
}
validationRequired = false;
validationRelease = false;
*validationReleasePtr = false;
validationThread = new ValidationThread(
buffer,
system,
&validationFinished,
&validationSuccess,
&validationRelease,
validationReleasePtr,
&validationPosition,
&validationMessage
);

View File

@ -107,15 +107,7 @@ class XmlCtrl: public wxStyledTextCtrl
const wxPoint &position = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0 );
~XmlCtrl()
{
attributeMap.clear();
elementMap.clear();
entitySet.clear();
// don't delete thread in case it's gone - just set the release flag
validationRelease = true;
}
~XmlCtrl();
int getType();
int getParentCloseAngleBracket ( int pos, int range = USHRT_MAX * 4 );
void applyProperties (
@ -157,8 +149,8 @@ class XmlCtrl: public wxStyledTextCtrl
ValidationThread *validationThread;
bool validationStarted,
validationFinished,
validationSuccess,
validationRelease;
validationSuccess;
bool *validationReleasePtr;
std::pair<int, int> validationPosition;
std::string validationMessage;