Changeset 1112 for s10k


Ignore:
Timestamp:
Sep 8, 2019, 3:21:41 PM (5 years ago)
Author:
s10k
Message:

Fix a deadlock, v2.0f

Location:
s10k/XmlTools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • s10k/XmlTools/utilglobalvars.h

    r1111 r1112  
    2424
    2525static const QString AppName = "XmlTools";
    26 static const QString AppVersion = "2.0e";
     26static const QString AppVersion = "2.0f";
    2727#ifdef Q_OS_WIN
    2828static const QString AppExecutable=AppName+".exe";
  • s10k/XmlTools/xmlcustomcode.cpp

    r1073 r1112  
    3737        e.getXmlDataFunction = new QScriptValue(e.scriptEngine->evaluate("(function getXmlData() { return $xmlData; })"));
    3838        e.setXmlDataFunction = new QScriptValue(e.scriptEngine->evaluate("(function setXmlData(newXmlData) { $xmlData=newXmlData; })"));
    39         e.isAvailable = true;
     39        e.mutexForEngine = new QMutex;
    4040
    4141        // Add echo function so user can debug the code
     
    7575    if(filesToProcess.size()<=CUSTOM_FILES_PER_THREAD || numThreads == 1){
    7676
    77         jsCustomCodeEngine &jsEngine = getAvailableJsEngine();
     77        jsCustomCodeEngine &jsEngine = this->jsScriptEngines.first();
    7878
    7979        // Process all XmlFiles
     
    8888        QVector<QFuture<void>> executingThreads;
    8989
    90         for(int i=0; i<=filesToProcess.size()-CUSTOM_FILES_PER_THREAD; i+=CUSTOM_FILES_PER_THREAD){
     90        for(int i=0, iteration = 0; i<=filesToProcess.size()-CUSTOM_FILES_PER_THREAD; i+=CUSTOM_FILES_PER_THREAD, ++iteration){
    9191
    92             executingThreads <<
    93             QtConcurrent::run(&this->myThreadPool, [=]()
     92            executingThreads << QtConcurrent::run(&this->myThreadPool, [=]()
    9493            {
    95                 mutexIsAvailable.lock();
    96                 jsCustomCodeEngine &jsEngine = getAvailableJsEngine();
    97                 jsEngine.isAvailable = false;
    98                 mutexIsAvailable.unlock();
     94
     95                const int indexJsEngine = iteration % this->numThreads;
     96
     97                jsCustomCodeEngine &jsEngine = this->jsScriptEngines[indexJsEngine];
     98
     99                jsEngine.mutexForEngine->lock();
    99100
    100101                QString currXmlFileStringThread;
     
    116117                                    *jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
    117118
    118                 mutexIsAvailable.lock();
    119                 jsEngine.isAvailable = true;
    120                 mutexIsAvailable.unlock();
     119                jsEngine.mutexForEngine->unlock();
    121120            });
    122121        }
     
    131130            int alreadyProcessedFiles=(filesToProcess.size()/CUSTOM_FILES_PER_THREAD)*CUSTOM_FILES_PER_THREAD;
    132131
    133             jsCustomCodeEngine &jsEngine = getAvailableJsEngine();
     132            jsCustomCodeEngine &jsEngine = this->jsScriptEngines.first();
    134133
    135134            for(int i=alreadyProcessedFiles; i<filesToProcess.size(); i++){
     
    147146    }
    148147}
    149 
    150 XmlCustomCode::jsCustomCodeEngine& XmlCustomCode::getAvailableJsEngine(){
    151     for(jsCustomCodeEngine &e : this->jsScriptEngines){
    152         if(e.isAvailable){
    153             return e;
    154         }
    155     }
    156 
    157     throw std::runtime_error("Could't find an available js engine for custom command.");
    158 }
  • s10k/XmlTools/xmlcustomcode.h

    r1073 r1112  
    2727    const int numThreads;
    2828    QThreadPool myThreadPool;
    29     QMutex mutexIsAvailable;
    3029
    3130    struct jsCustomCodeEngine{
     31        /* No need to delete these... They will stay alive until program quits. */
    3232        QScriptEngine* scriptEngine;
    3333        QScriptValue* jsFunction;
    3434        QScriptValue* getXmlDataFunction;
    3535        QScriptValue* setXmlDataFunction;
    36         bool isAvailable;
     36        QMutex *mutexForEngine;
    3737    };
    3838
     
    4444
    4545    void displayJsException(QScriptEngine &engine, QScriptValue &engineResult);
    46     jsCustomCodeEngine& getAvailableJsEngine();
    4746
    4847    __attribute__((always_inline)) inline void customCodeUnwinding(const QString &fileName, QString &currXmlFileString,
    49     QScriptEngine &engine, clock_t &begin, double elapsed_secs, QScriptValue &engineResult, QScriptValue &jsFunction,
    50     QScriptValue &getXmlDataFunction, QScriptValue &setXmlDataFunction, const bool &backupsEnabled, const bool &verboseEnabled){
     48                                                                   QScriptEngine &engine, clock_t &begin, double elapsed_secs, QScriptValue &engineResult, QScriptValue &jsFunction,
     49                                                                   QScriptValue &getXmlDataFunction, QScriptValue &setXmlDataFunction, const bool &backupsEnabled, const bool &verboseEnabled){
    5150        if(backupsEnabled){
    5251            UtilXmlTools::backupFile(fileName, verboseEnabled);
Note: See TracChangeset for help on using the changeset viewer.