QGpCoreTools/ParallelLoop.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2007-06-20
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef PARALLELLOOP_H
00029 #define PARALLELLOOP_H
00030 
00031 #include <time.h>
00032 
00033 #include "QGpCoreToolsDLLExport.h"
00034 #include "Thread.h"
00035 
00036 namespace QGpCoreTools {
00037 
00038 class LoopTask;
00039 
00040 class QGPCORETOOLS_EXPORT ParallelLoop : public QObject
00041 {
00042   Q_OBJECT
00043 public:
00044   ParallelLoop(QObject * parent=0) : QObject(parent) {}
00045 
00046   void start(int iStart, int iEnd, bool forceParallel=false);
00047   void terminate();
00048   void waitFinished();
00049 
00050 protected:
00051   virtual LoopTask * newTask()=0;
00052 protected slots:
00053   virtual void taskFinished();
00054 private slots:
00055   void statusChanged(QString msg);
00056   void progressChanged(int value);
00057   void progressInit(int maximumValue);
00058 signals:
00059   void statusChanged(int processIndex, QString msg);
00060   void progressChanged(int processIndex, int progress);
00061   void progressInit(int processIndex, int maximumValue);
00062   void finished();
00063 private:
00064   inline LoopTask * newTask(int iEnd);
00065   struct TaskInfo {
00066     int index;
00067     int maximumProgress;
00068   };
00069   QMap<LoopTask *, TaskInfo> _tasks;
00070   QMutex _indexMutex;
00071   int _index;
00072 };
00073 
00074 class QGPCORETOOLS_EXPORT LoopTask : public Thread
00075 {
00076   Q_OBJECT
00077 public:
00078   LoopTask() : Thread() {_terminated=false;}
00079 
00080   void terminate() {_terminated.fetchAndStoreOrdered(true);}
00081   bool terminated() {return _terminated.testAndSetOrdered(true,true);}
00082 
00083   void setIndex(int * index, QMutex * mutex) {_index=index; _indexMutex=mutex;}
00084   void setEndIndex(int index) {_endIndex=index;}
00085 protected:
00086   virtual void run();
00087   virtual void run(int index)=0;
00088   void setProgressValue(int value);
00089   void setProgressMaximum(int value) {emit progressInit(value);}
00090   void setStatus(QString msg) {emit statusChanged(msg);}
00091   int endIndex() const {return _endIndex;}
00092 signals:
00093   void statusChanged(QString msg);
00094   void progressInit(int maximumValue);
00095   void progressChanged(int value);
00096 private:
00097   QAtomicInt _terminated;
00098   QMutex * _indexMutex;
00099   int * _index;
00100   int _endIndex;
00101   QAtomicInt _lastProgressTime;
00102 };
00103 
00104 inline void LoopTask::setProgressValue(int value)
00105 {
00106   TRACE;
00107   int now=time(0);
00108   if(_lastProgressTime.fetchAndStoreOrdered(now)!=now) {
00109     progressChanged(value);
00110   }
00111 }
00112 
00113 } // namespace QGpCoreTools
00114 
00115 #endif // PARALLELLOOP_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines