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 #include <QVCameraWorker>
00034
00035 QVWorker::QVWorker(const QString name):QVPropertyContainer(name), numIterations(0), status(Running), triggerList(), minms(0)
00036 {
00037 qDebug() << "QVWorker::QVWorker(" << name << ")";
00038 Q_ASSERT_X(qvApp != NULL, "QVWorker::QVWorker()", "QVApplication doesn't exists");
00039 if (qvApp == NULL)
00040 {
00041 QString str = "QVWorker::QVWorker(): the QVWorker cannot be created before the QVApplication instance. Aborting now.";
00042 std::cerr << qPrintable(str) << std::endl;
00043 exit(1);
00044 }
00045
00046 addProperty<int>("max worker iterations", inputFlag | guiInvisible | internalProp, -1, "Stablishes maximal number of iterations to execute worker");
00047 maxIterations = getPropertyValue<int>("max worker iterations");
00048
00049 addProperty<bool>("stats enabled", inputFlag | guiInvisible | internalProp, TRUE, "Stablishes if the worker's cpu stats will be enabled");
00050 statsEnabled = getPropertyValue<bool>("stats enabled");
00051 if (statsEnabled) cpuStatControler = new QVStatControler();
00052 if (statsEnabled) addProperty<QVStat>("cpu stats", outputFlag, cpuStatControler->value(), "CPU stats's Statistics");
00053 else addProperty<QVStat>("cpu stats", outputFlag, QVStat(), "CPU stats's Statistics");
00054
00055 informer.setParent(this);
00056
00057 addProperty<int>("print stats frequency", inputFlag | guiInvisible | internalProp, 0, "Number of iterations to print CPU usage statistics.");
00058 const int printStatsFrequency = getPropertyValue<int>("print stats frequency");
00059
00060 if (printStatsFrequency > 0)
00061 setPrintStatsFrequency(printStatsFrequency);
00062
00063 qDebug() << "QVWorker::QVWorker(" << name << ") <- return";
00064 };
00065
00066 QVWorker::QVWorker(const QVWorker &other):QThread(), QVPropertyContainer(other), statsEnabled(other.statsEnabled), numIterations(other.numIterations),
00067 maxIterations(other.maxIterations), status(other.status), triggerList(other.triggerList), iterationTime(other.iterationTime), curms(other.curms),
00068 minms(other.minms)
00069 {
00070 if (statsEnabled) cpuStatControler = new QVStatControler();
00071 }
00072
00073 QVWorker::~QVWorker()
00074 {
00075 if (statsEnabled)
00076 delete cpuStatControler;
00077
00078 informer.setParent(0);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 void QVWorker::run()
00090 {
00091 qDebug() << "QVWorker::run()";
00092
00093 while(status != Finished)
00094 {
00095 qDebug() << "Processing events in worker " << qPrintable(getName());
00096
00097
00098
00099 qApp->processEvents();
00100
00101 qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00102
00103
00104
00105
00106 usleep(1000);
00107
00108 switch (status)
00109 {
00110 case RunningOneStep:
00111 qDebug() << "QVWorker::iterate(): RunningOneStep";
00112 status = Paused;
00113
00114 case Running:
00115 iterationTime.start();
00116 foreach(QList<QVPropertyContainer *> level, slavesByLevel)
00117 foreach(QVPropertyContainer * slave, level)
00118 if(dynamic_cast<QVWorker *>(slave) != NULL) ((QVWorker *)slave)->workerIterate();
00119
00120 curms = iterationTime.elapsed();
00121 if(minms > curms)
00122 usleep(1000*(minms-curms));
00123
00124
00125
00126 break;
00127
00128 case Stoped:
00129
00130
00131
00132 readInputProperties();
00133 writeOutputProperties();
00134 usleep(100);
00135 break;
00136
00137
00138 case Paused:
00139 qDebug() << "QVWorker::iterate(): Paused";
00140 if(dynamic_cast<QVCameraWorker*>(this) != NULL)
00141 {
00142
00143
00144 readInputProperties();
00145 writeOutputProperties();
00146 }
00147 usleep(100);
00148 break;
00149
00150 case Finished:
00151 qDebug() << "QVWorker::iterate(): Finished";
00152 break;
00153 }
00154
00155 if (maxIterations != -1 && numIterations >= maxIterations)
00156 finish();
00157
00158 qDebug() << "QVWorker::iterate() <- return";
00159 }
00160
00161
00162 foreach(QList<QVPropertyContainer *> level, slavesByLevel)
00163 foreach(QVPropertyContainer * slave, level)
00164 if(dynamic_cast<QVWorker *>(slave) != NULL) ((QVWorker *)slave)->moveToThread(qvApp->thread());
00165
00166 foreach(QList<QVPropertyContainer *> level, slavesByLevel)
00167 foreach(QVPropertyContainer * slave, level)
00168 if(dynamic_cast<QVWorker *>(slave) != NULL) {
00169 QMutexLocker locker(&qvApp->mutex);
00170 ((QVWorker *)slave)->unlink();
00171 }
00172
00173 qApp->processEvents();
00174
00175 qDebug() << "QVWorker::run() <- return";
00176 }
00177
00178 void QVWorker::workerIterate()
00179 {
00180 if (statsEnabled) cpuStatControler->step();
00181 emit startIteration();
00182 readInputProperties();
00183 timeFlag("System");
00184 iterate();
00185 if (statsEnabled) setPropertyValue<QVStat>("cpu stats", cpuStatControler->value());
00186 writeOutputProperties();
00187 numIterations++;
00188 emit endIteration(getId(), getIteration());
00189 }
00190