src/qvcore/qvworker.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024  
00025 #include <QDebug>
00026 #include <QMutex>
00027 #include <QWaitCondition>
00028 #include <QApplication>
00029 
00030 #include <iostream>
00031 
00032 #include <qvcore/qvworker.h>
00033 
00034 QVWorker::QVWorker(const QString name):QVPropertyContainer(name), cpuStatControler(), numIterations(0), status(Running), triggerList(), minms(0)
00035         {
00036         qDebug() << "QVWorker::QVWorker(" << name << ")";
00037         if (qvApp == NULL)
00038                 {
00039                 QString str = "QVWorker::QVWorker(): the QVWorker cannot be created before the QVApplication instance. Aborting now.";
00040                 std::cerr << qPrintable(str) << std::endl;
00041                 exit(1);
00042                 }
00043 
00044         Q_ASSERT_X(qvApp != NULL, "QVWorker::QVWorker()", "QVApplication doesn't exists");
00045         addProperty<int>("max worker iterations", inputFlag | guiInvisible | internalProp, -1, "Stablishes maximal number of iterations to execute worker.");
00046         addProperty<QVStat>("cpu stats", outputFlag | internalProp, cpuStatControler.value(), "CPU stats's Statistics");
00047 
00048         maxIterations = getPropertyValue<int>("max worker iterations");
00049         qDebug() << "QVWorker::QVWorker(" << name << ") <- return";
00050         };
00051 
00052 void QVWorker::unlink()
00053         {
00054         if(status == Finished)
00055                 QVPropertyContainer::unlink();
00056         else
00057                 std::cerr << "WARNING: A worker only can be unlinked if the worker's status is Finished." << std::endl;
00058         }
00059 
00060 void QVWorker::run()
00061         {
00062         qDebug() << "QVWorker::run()";
00063 
00064         while(status != Finished)
00065                 {
00066                 // First, we check if there are any pending signals, and if so, we
00067                 // execute their associated slots:
00068                 qApp->processEvents();
00069 
00070                 qDebug() << "QVWorker::iterate()";
00071                 qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00072         
00073                 // Avoids "apparent hanging" (greedy ocupation of CPU by extremely fast
00074                 // workers, such as paused ones). It is just 0.5 milliseconds, so
00075                 // it should not be appreciable in any practical situation.
00076                 usleep(500);
00077 
00078                 switch (status)
00079                         {
00080                         case RunningOneStep:
00081                                 qDebug() << "QVWorker::iterate(): RunningOneStep";
00082                                 status = Paused;
00083         
00084                         case Running:
00085                                 iterationTime.start();
00086                                 emit startIteration();
00087                                 cpuStatControler.step();
00088                                 timeFlag("System");
00089                                 readInputProperties();
00090                                 iterate();
00091                                 setPropertyValue<QVStat>("cpu stats", cpuStatControler.value());
00092                                 writeOutputProperties();
00093                                 numIterations++;
00094                                 emit endIteration();
00095                                 emit endIteration(getId(), getIteration());
00096                                 curms = iterationTime.elapsed();
00097                                 if(minms > curms)
00098                                         usleep(1000*(minms-curms));
00099                                 /*if(numIterations!=1) // First iteration time is too noisy:
00100                                         acumms = (acumms*(numIterations-2) + curms) / (numIterations-1);
00101                                 std::cout << "-----> curms=" << curms << " acumms=" << acumms << "\n";*/
00102                                 break;
00103         
00104                         case Paused:
00105                                 qDebug() << "QVWorker::iterate(): Paused";
00106                                 usleep(100); // This avoids spurious CPU consuming when paused.
00107                                 break;
00108                                                 
00109                         case Finished:
00110                                 qDebug() << "QVWorker::iterate(): Finished";
00111                                 break;
00112                         }
00113 
00114                 if (maxIterations != -1 && numIterations >= maxIterations)
00115                         finish();
00116 
00117                 qDebug() << "QVWorker::iterate() <- return";
00118                 }
00119 
00120         unlink();
00121         qDebug() << "QVWorker::run() <- return";
00122         }

Generated on Thu Jul 17 17:23:27 2008 for QVision by  doxygen 1.5.3