From 62efcae94052643ece092336cfef1f501906f7f3 Mon Sep 17 00:00:00 2001 From: Gerald Schmidt Date: Mon, 14 Jul 2008 16:21:48 +0000 Subject: [PATCH] The threading model now copes better with active validation threads while closing documents. --- src/validationthread.cpp | 18 ++++++++++++++---- src/xmlctrl.cpp | 32 ++++++++++++++++++++++++++++---- src/xmlctrl.h | 14 +++----------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/validationthread.cpp b/src/validationthread.cpp index fd19470..1ec4c9c 100644 --- a/src/validationthread.cpp +++ b/src/validationthread.cpp @@ -1,4 +1,5 @@ #include "wx/wx.h" +#include "xmlctrl.h" #include "validationthread.h" #include "wrapxerces.h" #include @@ -31,17 +32,23 @@ void *ValidationThread::Entry() { std::auto_ptr 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; + } } diff --git a/src/xmlctrl.cpp b/src/xmlctrl.cpp index e5200f2..9e98c54 100755 --- a/src/xmlctrl.cpp +++ b/src/xmlctrl.cpp @@ -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 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 ); diff --git a/src/xmlctrl.h b/src/xmlctrl.h index 54968a7..cb71a2b 100755 --- a/src/xmlctrl.h +++ b/src/xmlctrl.h @@ -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 validationPosition; std::string validationMessage;