00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <iostream>
00026
00027 #include <QDebug>
00028 #include <QMutex>
00029 #include <QWaitCondition>
00030 #include <QApplication>
00031
00032 #include <QVWorker>
00033
00034 QVWorker::QVWorker(const QString name):QVPropertyContainer(name), numIterations(0), status(Running), triggerList(), minms(0)
00035 {
00036 qDebug() << "QVWorker::QVWorker(" << name << ")";
00037 Q_ASSERT_X(qvApp != NULL, "QVWorker::QVWorker()", "QVApplication doesn't exists");
00038 if (qvApp == NULL)
00039 {
00040 QString str = "QVWorker::QVWorker(): the QVWorker cannot be created before the QVApplication instance. Aborting now.";
00041 std::cerr << qPrintable(str) << std::endl;
00042 exit(1);
00043 }
00044
00045 addProperty<int>("max worker iterations", inputFlag | guiInvisible | internalProp, -1, "Stablishes maximal number of iterations to execute worker");
00046 maxIterations = getPropertyValue<int>("max worker iterations");
00047
00048 addProperty<bool>("stats enabled", inputFlag | guiInvisible | internalProp, TRUE, "Stablishes if the worker's cpu stats will be enabled");
00049 statsEnabled = getPropertyValue<bool>("stats enabled");
00050 if (statsEnabled) cpuStatControler = new QVStatControler();
00051 if (statsEnabled) addProperty<QVStat>("cpu stats", outputFlag, cpuStatControler->value(), "CPU stats's Statistics");
00052 else addProperty<QVStat>("cpu stats", outputFlag, QVStat(), "CPU stats's Statistics");
00053
00054
00055
00056 qDebug() << "QVWorker::QVWorker(" << name << ") <- return";
00057 };
00058
00059 QVWorker::QVWorker(const QVWorker &other):QThread(), QVPropertyContainer(other), statsEnabled(other.statsEnabled), numIterations(other.numIterations),
00060 maxIterations(other.maxIterations), status(other.status), triggerList(other.triggerList), iterationTime(other.iterationTime), curms(other.curms),
00061 minms(other.minms)
00062 {
00063 if (statsEnabled) cpuStatControler = new QVStatControler();
00064 }
00065
00066 QVWorker::~QVWorker()
00067 {
00068 if (statsEnabled)
00069 delete cpuStatControler;
00070
00071
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 void QVWorker::run()
00083 {
00084 qDebug() << "QVWorker::run()";
00085
00086 while(status != Finished)
00087 {
00088 qDebug() << "Processing events in worker " << qPrintable(getName());
00089
00090
00091
00092 qApp->processEvents();
00093
00094 qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00095
00096
00097
00098
00099 usleep(500);
00100
00101 switch (status)
00102 {
00103 case RunningOneStep:
00104 qDebug() << "QVWorker::iterate(): RunningOneStep";
00105 status = Paused;
00106
00107 case Running:
00108 iterationTime.start();
00109 foreach(QList<QVPropertyContainer *> level, slavesByLevel) {
00110 foreach(QVPropertyContainer * slave, level) {
00111 QVWorker* worker;
00112 if((worker = dynamic_cast<QVWorker*>(slave)) != NULL) {
00113 worker->workerIterate();
00114 }
00115 }
00116 }
00117 curms = iterationTime.elapsed();
00118 if(minms > curms)
00119 usleep(1000*(minms-curms));
00120
00121
00122
00123 break;
00124
00125 case Stoped:
00126
00127
00128
00129 readInputProperties();
00130 writeOutputProperties();
00131 usleep(100);
00132 break;
00133
00134
00135 case Paused:
00136 qDebug() << "QVWorker::iterate(): Paused";
00137 usleep(100);
00138 break;
00139
00140 case Finished:
00141 qDebug() << "QVWorker::iterate(): Finished";
00142 break;
00143 }
00144
00145 if (maxIterations != -1 && numIterations >= maxIterations)
00146 finish();
00147
00148 qDebug() << "QVWorker::iterate() <- return";
00149 }
00150
00151 unlink();
00152 qDebug() << "QVWorker::run() <- return";
00153 }
00154
00155 void QVWorker::workerIterate()
00156 {
00157 if (statsEnabled) cpuStatControler->step();
00158 emit startIteration();
00159 readInputProperties();
00160 timeFlag("System");
00161 if (status != Stoped) iterate();
00162 if (statsEnabled) setPropertyValue<QVStat>("cpu stats", cpuStatControler->value());
00163 writeOutputProperties();
00164 numIterations++;
00165 emit endIteration(getId(), getIteration());
00166 }
00167