source: s10k/XmlTools/xmlcustomcode.h@ 1126

Last change on this file since 1126 was 1112, checked in by s10k, 5 years ago

Fix a deadlock, v2.0f

File size: 3.9 KB
RevLine 
[1073]1#ifndef XMLCUSTOMCODE_H
2#define XMLCUSTOMCODE_H
3
4#include "utilxmltools.h"
5
6#include <ctime> // script execution time calculation
7#include <QScriptEngine>
8#include <QTextStream>
9#include <QCoreApplication>
10
11#include <QThreadPool>
12#include <QtConcurrent/QtConcurrent>
13
14static constexpr double SLOW_SCRIPT_TIME = 0.1; // if a user script takes more than 0.1 seconds to execute give a warning.
15static constexpr int CUSTOM_FILES_PER_THREAD = 4;
16
17// Uses a singleton implementation (based on here: http://www.yolinux.com/TUTORIALS/C++Singleton.html)
18// which allows each thread to keep one script engine without always create/destruct them
19class XmlCustomCode
20{
21public:
22 static XmlCustomCode* getInstance();
23 void executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled);
24private:
25 static XmlCustomCode* uniqueInstance;
26
27 const int numThreads;
28 QThreadPool myThreadPool;
29
30 struct jsCustomCodeEngine{
[1112]31 /* No need to delete these... They will stay alive until program quits. */
[1073]32 QScriptEngine* scriptEngine;
33 QScriptValue* jsFunction;
34 QScriptValue* getXmlDataFunction;
35 QScriptValue* setXmlDataFunction;
[1112]36 QMutex *mutexForEngine;
[1073]37 };
38
39 QVector<jsCustomCodeEngine> jsScriptEngines;
40
41 XmlCustomCode(); // constructor is private (use getInstance)
42 XmlCustomCode(XmlCustomCode const&); // copy constructor is private
43 XmlCustomCode& operator=(XmlCustomCode const&); // assignment operator is private
44
45 void displayJsException(QScriptEngine &engine, QScriptValue &engineResult);
46
47 __attribute__((always_inline)) inline void customCodeUnwinding(const QString &fileName, QString &currXmlFileString,
[1112]48 QScriptEngine &engine, clock_t &begin, double elapsed_secs, QScriptValue &engineResult, QScriptValue &jsFunction,
49 QScriptValue &getXmlDataFunction, QScriptValue &setXmlDataFunction, const bool &backupsEnabled, const bool &verboseEnabled){
[1073]50 if(backupsEnabled){
51 UtilXmlTools::backupFile(fileName, verboseEnabled);
52 }
53
54 QFile currXmlFile(fileName);
55
56 if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){
57 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for read operation.");
58 }
59
60 currXmlFileString=QTextStream(&currXmlFile).readAll();
61
62 currXmlFile.close(); // close reading
63
64 setXmlDataFunction.call(setXmlDataFunction,QScriptValueList() << currXmlFileString);
65
66 begin = clock();
67
68 engineResult=jsFunction.call(); // main funtion allows to use return to exit prematurely from user code
69
70 if(verboseEnabled){
71 elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC;
72
73 // Warn the user if the script took much time
74 if(elapsed_secs>SLOW_SCRIPT_TIME){
75 std::cout << "Warning: Slow javascript code detected.\n" <<
76 "Warning: Script execution seconds: " << elapsed_secs
77 << std::endl;
78 }
79 }
80
81 if (engine.hasUncaughtException()) {
82 displayJsException(engine,engineResult);
83 }
84
85 if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){
86 UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for @CUSTOM_CODE write operation.");
87 }
88
89 engineResult=getXmlDataFunction.call();
90
91 if (engine.hasUncaughtException()) {
92 displayJsException(engine,engineResult);
93 }
94
95 QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
96
97 currXmlFile.close();
98 }
99};
100
101#endif // XMLCUSTOMCODE_H
Note: See TracBrowser for help on using the repository browser.