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 qDebug() << "QVWorker::QVWorker(" << name << ") <- return";
00055 };
00056
00057 QVWorker::QVWorker(const QVWorker &other):QThread(), QVPropertyContainer(other), statsEnabled(other.statsEnabled), numIterations(other.numIterations),
00058 maxIterations(other.maxIterations), status(other.status), triggerList(other.triggerList), iterationTime(other.iterationTime), curms(other.curms),
00059 minms(other.minms)
00060 {
00061 if (statsEnabled) cpuStatControler = new QVStatControler();
00062 }
00063
00064 QVWorker::~QVWorker()
00065 {
00066 if (statsEnabled)
00067 delete cpuStatControler;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 void QVWorker::run()
00079 {
00080 qDebug() << "QVWorker::run()";
00081
00082 while(status != Finished)
00083 {
00084
00085
00086 qApp->processEvents();
00087
00088 qDebug() << "QVWorker::iterate()";
00089 qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00090
00091
00092
00093
00094 usleep(500);
00095
00096 switch (status)
00097 {
00098 case RunningOneStep:
00099 qDebug() << "QVWorker::iterate(): RunningOneStep";
00100 status = Paused;
00101
00102 case Stoped:
00103 case Running:
00104 iterationTime.start();
00105 if (statsEnabled) cpuStatControler->step();
00106 emit startIteration();
00107 readInputProperties();
00108 timeFlag("System");
00109 if (status == Running) iterate();
00110 if (statsEnabled) setPropertyValue<QVStat>("cpu stats", cpuStatControler->value());
00111 writeOutputProperties();
00112 numIterations++;
00113 emit endIteration(getId(), getIteration());
00114 curms = iterationTime.elapsed();
00115 if(minms > curms)
00116 usleep(1000*(minms-curms));
00117
00118
00119
00120 break;
00121
00122 case Paused:
00123 qDebug() << "QVWorker::iterate(): Paused";
00124 usleep(100);
00125 break;
00126
00127 case Finished:
00128 qDebug() << "QVWorker::iterate(): Finished";
00129 break;
00130 }
00131
00132 if (maxIterations != -1 && numIterations >= maxIterations)
00133 finish();
00134
00135 qDebug() << "QVWorker::iterate() <- return";
00136 }
00137
00138 unlink();
00139 qDebug() << "QVWorker::run() <- return";
00140 }