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