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 #include <qvgui/qvimagecanvas.h>
00033 
00034 QVWorker::QVWorker(QString name):QVPropertyHolder(name),
00035         cpuStat(), numIterations(0), status(Running), inited(false), initedParam(), errorStatus(), cameras()
00036         {
00037         qDebug() << "QVWorker::QVWorker(" << name << ")";
00038         Q_ASSERT_X(qvApp != NULL, "QVWorker::QVWorker()", "QVApplication doesn't exists");
00039         addProperty<bool>("print stats", inputFlag, false, "Enables realtime stats console output for worker");
00040         cpuStat.printStatsToConsole(getPropertyValue<bool>("print stats"));
00041         qDebug() << "QVWorker::QVWorker(" << name << ") <- return";
00042         };
00043                         
00044 QVWorker::~QVWorker()
00045         {
00046         qDebug() << "QVWorker::~QVWorker()";
00047         qDebug() << "QVWorker::~QVWorker() <- return";
00048         }
00049 
00050 void QVWorker::run()
00051         {
00052         qDebug() << "QVWorker::run()";
00053 
00054         while(1) {
00055                 iterate();
00056                 if(status == Finished) break;
00057         }
00058         unlink();
00059 
00060         qDebug() << "QVWorker::run() <- return";
00061         }
00062 
00063 
00064 void QVWorker::iterate()
00065         {
00066         qDebug() << "QVWorker::iterate()";
00067         qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00068 
00069         // Avoids "apparent hanging" (greedy ocupation of CPU by extremely fast
00070         // workers, such as paused ones).
00071         //usleep(100);
00072 
00073         cpuStat.step();
00074 
00075         switch (status)
00076                 {
00077                 case RunningOneStep:
00078                         qDebug() << "QVWorker::iterate(): RunningOneStep";
00079                         status = Paused;
00080 
00081                 case Running:
00082                         {
00083                         emit startIteration();
00084                         timeFlag("System");
00085                         readInputProperties();
00086                         worker();
00087                         writeOutputProperties();
00088                         numIterations++;
00089                         emit endIteration();
00090                         break;
00091                         }
00092 
00093                 case Paused:
00094                         qDebug() << "QVWorker::iterate(): Paused";
00095                         {
00096                         /*QMutex mutex;
00097                         QWaitCondition condition;
00098                         qDebug() << "QVWorker::iterate(): locking";
00099                         mutex.lock();
00100                         qDebug() << "QVWorker::iterate(): pausing 100 milisecs";
00101                         condition.wait(&mutex,100);
00102                         qDebug() << "QVWorker::iterate(): unlocking";
00103                         mutex.unlock();*/
00104                         }
00105                         break;
00106                                         
00107                 case Finished:
00108                         qDebug() << "QVWorker::iterate(): Finished";
00109                         break;
00110                 }
00111 
00112         qDebug() << "QVWorker::iterate() <- return";
00113         }
00114         
00115         /* If we need to process events in class QVWorker, we do something like this in QVWorker::run():
00116         QTimer timer;
00117         timer.start(0);
00118         connect(timer, SIGNAL(timeout()),this,SLOT(myquit()));
00119         while(1) {
00120                 iterate();
00121                 exec();
00122                 // if stopped disconnect(timer, SIGNAL(timeout()),this,SLOT(myquit())); BLA BLA
00123         }
00124         */

Generated on Thu Dec 13 13:06:25 2007 for QVision by  doxygen 1.5.3