OpenFlipperThread.hh 10.3 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/
42 43 44 45 46 47


#ifndef OPENFLIPPERTHREAD_HH
#define OPENFLIPPERTHREAD_HH

#include <QThread>
48
#include <QSemaphore>
49 50 51

#include <OpenFlipper/common/GlobalDefines.hh>

Jan Möbius's avatar
Jan Möbius committed
52 53 54 55

class OpenFlipperJob;

/** \brief Thread handling class for OpenFlipper
56

57
    Instantiate this class in order to provide a thread
58 59 60 61
    to a plugin. Unless you don't need a specialized
    run() or cancel() function, it won't be necessary to
    reimplement this class. Just connect the signals
    for the thread to work properly.
Mike Kremer's avatar
Mike Kremer committed
62 63 64 65 66 67 68 69
        
    Note: Updating GUI elements of the main window from
    within a thread which is not the main thread can
    cause unexpected crashes to OpenFlipper. This introduces
    some major limitations to the usage of threads
    in plugins. These include avoiding the creation
    of new objects and generally every function that
    produces logging messages.
70

71
    The class should be used along with \ref processInterfacePage
72

73 74
    It is not necessary to derive from this class for most operations,
    but you can.
75
*/
76 77 78 79 80 81 82 83
class DLLEXPORT OpenFlipperThread : public QThread
{
  Q_OBJECT
  
  public:
    OpenFlipperThread( QString _jobId );
    ~OpenFlipperThread();
    
Jan Möbius's avatar
Jan Möbius committed
84 85 86 87 88 89
  //===========================================================================
  /** @name Advanced job processing
  * @{ */
  //===========================================================================
  public:
    
90 91
    /** \brief Main processing
    *
92
    * Either reimplement this function or connect the function()
93 94
    * signal. If this function is reimplemented, use signal state()
    * in order to inform the core about the job's current state.
95 96 97
    */
    virtual void run();
    
Jan Möbius's avatar
Jan Möbius committed
98 99 100 101 102
    /** \brief Cancel the job
    *
    * Reimplement this function to correctly terminate your job.
    * If your job is able to terminate correctly then you can reimplement this function
    * and stop your process.
103
    *
Jan Möbius's avatar
Jan Möbius committed
104 105
    */
    virtual void cancel();   
106 107
    
  public slots:
108 109 110 111 112
    /** \brief Cancel this job
     *
     * Call this slot with the correct job id to abort processing
     * This directly calls cancel()
     * This is only useful when you derived from this class, as other jobs might not react on this slot
113 114 115 116
    */
    void slotCancel( QString _jobId);
    
  signals:
Jan Möbius's avatar
Jan Möbius committed
117 118
    /** \brief Tell core about job state
    *
119 120 121 122 123
    * Use this signal to inform core about the job's state if and
    * only if the thread class inherits from OpenFlipperThread
    * and the virtual run() function is overridden. In all other cases
    * this signal won't be used and does not have to be connected to from
    * within the plugins.
Jan Möbius's avatar
Jan Möbius committed
124
    */
125
    void state( QString _jobId, int _state );
126

Jan Möbius's avatar
Jan Möbius committed
127
  /** @} */
128
    
Jan Möbius's avatar
Jan Möbius committed
129 130 131 132 133 134 135 136 137 138 139
    
  //===========================================================================
  /** @name Simple job processing
  * @{ */
  //===========================================================================
    
  signals:
    /**  \brief job function
    *
    * If you do not specialize the OpenFlipperThread, The function connected to this 
    * signal will be used as the processing function for your thread. When you call
140 141 142
    * \code
    * OpenFlipper::startProcessing();
    * \endcode
Jan Möbius's avatar
Jan Möbius committed
143
    * 
144 145 146
    * You have connect one of your slot using
    * \code
    * connect( OpenFlipperThread ,SIGNAL(function()),YourPlugin,SLOT(YourSlot(),Qt::DirectConnection) );
Jan Möbius's avatar
Jan Möbius committed
147
    * \endcode
148 149
    *
    * The default implementation will call your slot inside the new thread and the core will still respond.
150 151 152
    *
    * The optional parameter contains the job's id. In some cases the function that is to be called needs
    * the job's id for further processing (status updates, etc.)
153
    */
154
    void function(const QString _jobId = "");
Jan Möbius's avatar
Jan Möbius committed
155 156 157 158 159 160 161 162 163 164 165 166 167
    
    /** \brief job done
    *
    * This signal is emitted if your job has finished and should be used to tell the core that your job has finished.
    *
    */
    void finished( QString _jobId );
    
    
    
  public slots:
    /** \brief start processing
    *
168
    * This function will start the actual processing of a job.
Jan Möbius's avatar
Jan Möbius committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    */
    void startProcessing();
    
  /** @} */
    
    
  private slots:
    /** \brief job has finished
    * 
    * Called by the job wrapper, when the job is done.
    */
    void slotJobFinished();
  
  signals:
    /** \brief start processing of a function
    *
    * (INTERNAL!) \n
    * This signal is used to start the process execution through the wrapper
    */
    void startProcessingInternal();
    
  private: 
    /** \brief Internal job wrapper
    *
    * (INTERNAL!) \n
    * This wrapper is used to start slots inside the event queue of the current thread.
    * All slots defined in the QThread object live in the callers thread(The main thread).
    * All slots inside the wrapper live in the thread created by QThread. and therefore
    * do not lock the main event loop
    */
    OpenFlipperJob* job_;
    
    /** \brief Id of the current job
    *
    * This holds the id of the current job. Most functions only react if the correct id is passed
    * to the function.
    */
    QString jobId_;
    
208 209 210 211 212 213 214 215 216
    /** \brief Semaphore to control order of thread startup calls
    *
    * This variable is used to control the order of thread startup calls. The Thread itself is
    * started, and when it is up and running, the resource will be created. The core thread waits
    * for this resource and afterwards starts execution of the threads processing function.
    * Without this sync, the processing call of the main thread might get lost as the thread is
    * not online and listening for the signal.
    */
    QSemaphore startup_;
Jan Möbius's avatar
Jan Möbius committed
217 218 219

};

220

Jan Möbius's avatar
Jan Möbius committed
221 222 223 224 225 226 227
// ================================================================================
// Job wrapper
// ================================================================================

/** \brief Internal Job execution object
*
* This class is used to start a process within a thread. The thread object itself
228 229
* lives in the event queue of the object that created the thread. So every signal
* or slot within the QThread object will be dispatched in the core event loop and
Jan Möbius's avatar
Jan Möbius committed
230
* therefore lock the gui. 
231 232
* Objects created in the QThread will run in the threads event queue as well as their
* signals and slots. So QThread uses this object to actually run arbitrary threads
Jan Möbius's avatar
Jan Möbius committed
233
* inside the threads event queue.
234 235
*
* See \ref processInterfacePage for details.
Jan Möbius's avatar
Jan Möbius committed
236 237 238 239 240 241
*/
class DLLEXPORT OpenFlipperJob : public QObject
{
  Q_OBJECT
  
  public:
242
    OpenFlipperJob(const QString _jobId) : jobId_(_jobId) {}
Jan Möbius's avatar
Jan Möbius committed
243
    ~OpenFlipperJob();
Jan Möbius's avatar
Jan Möbius committed
244 245 246 247 248 249 250 251 252
  
  signals:
    /** \brief connection to actual processing function
    *
    * This signal has to be connected via a Qt::DirectConnection to an
    * arbitrary slot. This slot will be executed in the local event queue.
    * If this object is created in a QThread, than the slot will be run 
    * inside this thread.
    */
253
    void process(const QString _jobId = "");
Jan Möbius's avatar
Jan Möbius committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
    
    /** \brief Job done
    *
    * This signal is emitted, when the job has finished
    */
    void finished();
    
  public slots:
    /** \brief slot to start processing
    *
    * If this slot is called, the slot connected to process() will be executed
    * in the current thread.
    */
    void startJobProcessing();
268
    
269 270 271 272 273 274 275 276 277 278
  public:
    /// Set job's id
    void jobId(const QString& _jobId) { jobId_ = _jobId; }
    
    /// Get job's id
    QString jobId() const { return jobId_; }   
    
  private:
    /// The job's id
    QString jobId_;
279 280 281 282 283
};



#endif //OPENFLIPPERTHREAD_HH