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

Generated on Thu Mar 13 19:18:16 2008 for QVision by  doxygen 1.5.3