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 <qvcore/qvworker.h>
00031 
00032 QVWorker::QVWorker(QString name):QVPropertyHolder(name), cpuStat(), numIterations(0), status(Running), triggerList(), minms(0)
00033         {
00034         qDebug() << "QVWorker::QVWorker(" << name << ")";
00035         Q_ASSERT_X(qvApp != NULL, "QVWorker::QVWorker()", "QVApplication doesn't exists");
00036         addProperty<bool>("print stats", inputFlag, false, "Enables realtime stats console output for worker");
00037         cpuStat.printStatsToConsole(getPropertyValue<bool>("print stats"));
00038         qDebug() << "QVWorker::QVWorker(" << name << ") <- return";
00039         };
00040 
00041 
00042 void QVWorker::run()
00043         {
00044         qDebug() << "QVWorker::run()";
00045 
00046         while(status != Finished)
00047                 {
00048                 // First, we check if there are any pending signals, and if so, we
00049                 // execute their associated slots:
00050                 qApp->processEvents();
00051 
00052                 qDebug() << "QVWorker::iterate()";
00053                 qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00054         
00055                 // Avoids "apparent hanging" (greedy ocupation of CPU by extremely fast
00056                 // workers, such as paused ones).
00057                 // usleep(10);
00058 
00059                 // Initialize step for cpu statistics:  
00060                 cpuStat.step();
00061         
00062                 switch (status)
00063                         {
00064                         case RunningOneStep:
00065                                 qDebug() << "QVWorker::iterate(): RunningOneStep";
00066                                 status = Paused;
00067         
00068                         case Running:
00069                                 iterationTime.start();
00070                                 emit startIteration();
00071                                 timeFlag("System");
00072                                 readInputProperties();
00073                                 iterate();
00074                                 writeOutputProperties();
00075                                 numIterations++;
00076                                 emit endIteration();
00077                                 curms = iterationTime.elapsed();
00078                                 if(minms > curms)
00079                                         usleep(1000*(minms-curms));
00080                                 /*if(numIterations!=1) // First iteration time is too noisy:
00081                                         acumms = (acumms*(numIterations-2) + curms) / (numIterations-1);
00082                                 std::cout << "-----> curms=" << curms << " acumms=" << acumms << "\n";*/
00083                                 break;
00084         
00085                         case Paused:
00086                                 qDebug() << "QVWorker::iterate(): Paused";
00087                                 usleep(100); // This avoids spurious CPU consuming when paused.
00088                                 break;
00089                                                 
00090                         case Finished:
00091                                 qDebug() << "QVWorker::iterate(): Finished";
00092                                 break;
00093                         }       
00094                 qDebug() << "QVWorker::iterate() <- return";
00095                 }
00096 
00097         unlink();
00098         qDebug() << "QVWorker::run() <- return";
00099         }

Generated on Wed Jan 16 18:41:28 2008 for QVision by  doxygen 1.5.3