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 //PEDROE:
00051 void QVWorker::run()
00052         {
00053         qDebug() << "QVWorker::run()";
00054 
00055         while(1) {
00056                 iterate();
00057                 if(status == Finished) break;
00058         }
00059         unlink();
00060 
00061         qDebug() << "QVWorker::run() <- return";
00062         }
00063 
00064 
00065 void QVWorker::iterate()
00066         {
00067         qDebug() << "QVWorker::iterate()";
00068         qDebug() << "QVWorker::iterate(): iteration" << numIterations;
00069 
00070         // Avoids "apparent hanging" (greedy ocupation of CPU by extremely fast
00071         // workers, such as paused ones).
00072         //usleep(100);
00073 
00074         cpuStat.step();
00075 
00076         switch (status)
00077                 {
00078                 case RunningOneStep:
00079                         qDebug() << "QVWorker::iterate(): RunningOneStep";
00080                         status = Paused;
00081 
00082                 case Running:
00083                         {
00084                         emit startIteration();
00085                         timeFlag("System");
00086                         readInputProperties();
00087                         worker();
00088                         writeOutputProperties();
00089                         numIterations++;
00090                         emit endIteration();
00091                         break;
00092                         }
00093 
00094                 case Paused:
00095                         qDebug() << "QVWorker::iterate(): Paused";
00096                         {
00097                         /*QMutex mutex;
00098                         QWaitCondition condition;
00099                         qDebug() << "QVWorker::iterate(): locking";
00100                         mutex.lock();
00101                         qDebug() << "QVWorker::iterate(): pausing 100 milisecs";
00102                         condition.wait(&mutex,100);
00103                         qDebug() << "QVWorker::iterate(): unlocking";
00104                         mutex.unlock();*/
00105                         }
00106                         break;
00107                                         
00108                 case Finished:
00109                         qDebug() << "QVWorker::iterate(): Finished";
00110                         break;
00111                 }
00112 
00113         qDebug() << "QVWorker::iterate() <- return";
00114         }
00115         
00116         /* Si necesitamos procesar eventos en QVWorker, hacemos algo así como esto en QWorker::run():
00117         QTimer timer;
00118         timer.start(0);
00119         connect(timer, SIGNAL(timeout()),this,SLOT(myquit()));
00120         while(1) {
00121                 iterate();
00122                 exec();
00123                 //PEDROE if stopped disconnect(timer, SIGNAL(timeout()),this,SLOT(myquit())); BLA BLA
00124         }
00125         */

Generated on Fri Dec 7 12:20:59 2007 for QVision by  doxygen 1.5.3