From 8be8af2aa05e96df549bf64dc5121645f61054b3 Mon Sep 17 00:00:00 2001 From: Gerald Schmidt Date: Thu, 10 Jul 2008 17:06:32 +0000 Subject: [PATCH] Further improvements to background validation: critical section locks now in place. --- src/validationthread.cpp | 41 +++++++++++--------- src/xmlcopyeditor.cpp | 12 +++--- src/xmlctrl.cpp | 81 ++++++++++++---------------------------- src/xmlctrl.h | 5 +-- 4 files changed, 54 insertions(+), 85 deletions(-) diff --git a/src/validationthread.cpp b/src/validationthread.cpp index 668a676..fd19470 100644 --- a/src/validationthread.cpp +++ b/src/validationthread.cpp @@ -30,31 +30,36 @@ ValidationThread::ValidationThread ( void *ValidationThread::Entry() { std::auto_ptr validator ( new WrapXerces() ); - - if ( *myReleasePtr || TestDestroy() ) - Exit(); + + if ( *myReleasePtr || TestDestroy() ) + { + Exit(); + return NULL; + } bool res = validator->validateMemory ( myBuffer.c_str(), mySystem.c_str(), myBuffer.size() ); - if ( *myReleasePtr || TestDestroy() ) - Exit(); - - if ( !res ) - { - *mySuccessPtr = res; - *myPositionPtr = validator->getErrorPosition(); - *myMessagePtr = validator->getLastError(); - } - else - { - *mySuccessPtr = true; - *myPositionPtr = std::make_pair ( 0, 0 ); - *myMessagePtr = ""; - } + if ( *myReleasePtr || TestDestroy() ) + { + Exit(); + return NULL; + } + if ( !res ) + { + *mySuccessPtr = res; + *myPositionPtr = validator->getErrorPosition(); + *myMessagePtr = validator->getLastError(); + } + else + { + *mySuccessPtr = true; + *myPositionPtr = std::make_pair ( 0, 0 ); + *myMessagePtr = ""; + } return NULL; } diff --git a/src/xmlcopyeditor.cpp b/src/xmlcopyeditor.cpp index 9b57b00..9c75fa6 100755 --- a/src/xmlcopyeditor.cpp +++ b/src/xmlcopyeditor.cpp @@ -179,7 +179,7 @@ BEGIN_EVENT_TABLE ( MyFrame, wxFrame ) #endif END_EVENT_TABLE() -IMPLEMENT_APP ( MyApp ) +IMPLEMENT_APP ( MyApp) MyApp::MyApp() : checker ( NULL ), server ( NULL ), connection ( NULL ), #ifdef __WXMSW__ @@ -1633,7 +1633,7 @@ void MyFrame::OnIdle ( wxIdleEvent& event ) if ( !parent.empty() && properties.validateAsYouType && doc->getValidationRequired() ) { // tbd: limit to parent element - doc->shallowValidate ( doc->LineFromPosition ( current ), true ); + doc->backgroundValidate(); } @@ -2742,7 +2742,7 @@ void MyFrame::newDocument ( const std::string& s, const std::string& path, bool insertSiblingPanel->update ( doc, wxEmptyString ); insertEntityPanel->update ( doc ); if ( properties.validateAsYouType ) - doc->shallowValidate(); + doc->backgroundValidate(); } void MyFrame::OnOpen ( wxCommandEvent& event ) @@ -3114,7 +3114,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile ) if ( !largeFile && ( properties.validateAsYouType && doc->getGrammarFound() ) ) { statusProgress ( _T ( "Validating document..." ) ); - doc->shallowValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen ); + doc->backgroundValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen ); statusProgress ( wxEmptyString ); } @@ -3629,7 +3629,7 @@ void MyFrame::OnValidateDTD ( wxCommandEvent& event ) doc->SetSelection ( cursorPos, cursorPos ); // shallow validate all - doc->shallowValidate(); // has to come first as it deletes all indicators + doc->backgroundValidate(); // has to come first as it deletes all indicators doc->setErrorIndicator ( posPair.first, posPair.second ); return; @@ -4748,7 +4748,7 @@ bool MyFrame::saveFile ( XmlDoc *doc, wxString& fileName, bool checkLastModified if ( properties.validateAsYouType && isXml ) { doc->clearErrorIndicators(); - doc->shallowValidate ( utf8Buffer.c_str(), doc->getFullFileName().mb_str(wxConvUTF8), utf8Buffer.size() ); + doc->backgroundValidate ( utf8Buffer.c_str(), doc->getFullFileName().mb_str(wxConvUTF8), utf8Buffer.size() ); } if ( !unlimitedUndo ) diff --git a/src/xmlctrl.cpp b/src/xmlctrl.cpp index 17b17f5..e5200f2 100755 --- a/src/xmlctrl.cpp +++ b/src/xmlctrl.cpp @@ -40,6 +40,9 @@ BEGIN_EVENT_TABLE ( XmlCtrl, wxStyledTextCtrl ) EVT_MIDDLE_DOWN ( XmlCtrl::OnMiddleDown ) END_EVENT_TABLE() +// global protection for validation threads +wxCriticalSection xmlcopyeditorCriticalSection; + XmlCtrl::XmlCtrl ( wxWindow *parent, XmlCtrlProperties propertiesParameter, @@ -143,20 +146,24 @@ void XmlCtrl::OnIdle ( wxIdleEvent& event ) adjustNoColumnWidth(); // exits if unchanged // poll validation thread output if any - if (validationStarted && validationFinished) + { - validationStarted = false; - MyFrame *frame = (MyFrame *)GetGrandParent(); - if ( validationSuccess ) + wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection ); + if (validationStarted && validationFinished) { - clearErrorIndicators ( GetLineCount() ); - frame->statusProgress ( wxEmptyString ); - } - else - { - clearErrorIndicators ( validationPosition.first -1 ); - setErrorIndicator ( validationPosition.first - 1, 0 ); - frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) ); + validationStarted = false; + MyFrame *frame = (MyFrame *)GetGrandParent(); + if ( validationSuccess ) + { + clearErrorIndicators ( GetLineCount() ); + frame->statusProgress ( wxEmptyString ); + } + else + { + clearErrorIndicators ( GetLineCount() ); + setErrorIndicator ( validationPosition.first - 1, 0 ); + frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) ); + } } } } @@ -1996,21 +2003,20 @@ std::string XmlCtrl::getElementStructure ( const wxString& element ) return ret; } -bool XmlCtrl::shallowValidate ( int maxLine, bool segmentOnly ) +bool XmlCtrl::backgroundValidate() { if ( !properties.validateAsYouType || type != FILE_TYPE_XML ) return true; - clearErrorIndicators ( GetLineCount() ); std::string bufferUtf8 = myGetTextRaw(); - return shallowValidate ( + return backgroundValidate ( bufferUtf8.c_str(), basePath.c_str(), bufferUtf8.size() ); } -bool XmlCtrl::shallowValidate ( +bool XmlCtrl::backgroundValidate ( const char *buffer, const char *system, size_t bufferLen @@ -2018,8 +2024,8 @@ bool XmlCtrl::shallowValidate ( { if ( !validationRequired ) return true; - + wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection ); if ( validationStarted && !validationFinished ) { validationRelease = true; @@ -2047,43 +2053,8 @@ bool XmlCtrl::shallowValidate ( validationStarted = true; validationFinished = false; - validationThread->Run(); return true; - -/* - std::auto_ptr validator ( new XmlShallowValidator ( - elementMap, - attributeMap, - requiredAttributeMap, - entitySet, - maxLine, - segmentOnly ) ); - - bool res = validator->parse ( buffer, bufferLen, ( segmentOnly ) ? false : true ); - if ( - !validator->getOverrideFailure() && - ( !res || !validator->isValid() ) ) - { - std::vector > positionVector = validator->getPositionVector(); - - if ( res == XML_STATUS_ERROR ) - { - positionVector.push_back ( validator->getErrorPosition() ); - } - - std::vector >::iterator it; - for ( it = positionVector.begin(); it != positionVector.end(); it++ ) - { - int line, column; - line = ( it->first - 1 ) + startLine; - column = it->second + columnOffset; - setErrorIndicator ( line, column ); - } - return false; - } - return true; -*/ } std::string XmlCtrl::myGetTextRaw() @@ -2106,12 +2077,6 @@ void XmlCtrl::setErrorIndicator ( int line, int column ) int length = endPos - startPos; - /* - from CellBuffer.cxx: - PLATFORM_ASSERT(lengthStyle == 0 || - (lengthStyle > 0 && lengthStyle + position < length)); - */ - if ( length == 0 || ( length > 0 && length + startPos < GetLength() ) ) { SetStyling ( length, wxSTC_INDIC2_MASK ); diff --git a/src/xmlctrl.h b/src/xmlctrl.h index b9c1a43..54968a7 100755 --- a/src/xmlctrl.h +++ b/src/xmlctrl.h @@ -1,4 +1,3 @@ - /* * Copyright 2005-2007 Gerald Schmidt. * @@ -145,8 +144,8 @@ class XmlCtrl: public wxStyledTextCtrl bool canInsertAt ( int pos ); int getTagStartPos ( int pos ); void toggleLineBackground(); - bool shallowValidate ( int maxLine = 0, bool segmentOnly = false ); - bool shallowValidate ( + bool backgroundValidate ( ); + bool backgroundValidate ( const char *buffer, const char *system, size_t bufferLen );