OpenFlipperThread.cc 5.26 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


#include "OpenFlipperThread.hh"
#include <iostream>

Jan Möbius's avatar
Jan Möbius committed
47

48
OpenFlipperThread::OpenFlipperThread( QString _jobId ) :
Jan Möbius's avatar
Jan Möbius committed
49
  job_(0),
50 51 52 53 54 55 56 57 58
  jobId_(_jobId)
{
}

OpenFlipperThread::~OpenFlipperThread() {
}

void OpenFlipperThread::run()
{
Jan Möbius's avatar
Jan Möbius committed
59 60
  if ( job_ == 0 ) {
    // Create a job wrapper to run a slot from within this thread
61
    job_ = new OpenFlipperJob( jobId_ );
62

Jan Möbius's avatar
Jan Möbius committed
63
    // Connect the slot which should run in this thread. This has to be a DirectConnection !
64
    // Otherwise the slot will run inside its owner context which will be the main loop!!
65
    connect(job_, SIGNAL(process(QString)),this,SIGNAL(function(QString) ),Qt::DirectConnection);
Jan Möbius's avatar
Jan Möbius committed
66 67 68 69 70 71 72 73
    
    // Connect the jobs finished function
    connect(job_, SIGNAL(finished()),this,SLOT(slotJobFinished() ) );
    
    // connect the function to start the job
    connect(this,SIGNAL(startProcessingInternal()),job_,SLOT(startJobProcessing()),Qt::QueuedConnection);
  }

74 75 76 77
  // Thread is ready for processing now, tell core that we can continue.
  startup_.release(1);

  // Start event queue (possibly added events are already queued here
Jan Möbius's avatar
Jan Möbius committed
78
  exec();
79
  
Jan Möbius's avatar
Jan Möbius committed
80 81 82 83 84 85 86 87
// TODO: Self destuction sometimes does not work! 
// Seems to be a race condition!!!

//   std::cerr << "Delete thread Object " << std::endl;
//   
//   deleteLater();
  
//   std::cerr << "Deleted Thread Object" << std::endl;
88 89 90 91 92 93 94
}

void OpenFlipperThread::cancel() {
  std::cerr << "Cancel not implemented" << std::endl;
}

void OpenFlipperThread::slotCancel( QString _jobId) {
Jan Möbius's avatar
Jan Möbius committed
95
 std::cerr << "Thread : cancel received" << std::endl;
96 97 98
 if ( _jobId == jobId_ )
  cancel(); 
}
Jan Möbius's avatar
Jan Möbius committed
99 100 101

void OpenFlipperThread::slotJobFinished( ) {
  emit finished( jobId_ );
Dirk Wilden's avatar
Dirk Wilden committed
102
  job_ = 0;
Jan Möbius's avatar
Jan Möbius committed
103
  quit();
Jan Möbius's avatar
Jan Möbius committed
104 105 106 107 108
}


void OpenFlipperThread::startProcessing() {
  
109 110
  // Wait for thread to come up and connect its signals ... otherwise the signals might get lost
  startup_.acquire(1);
Jan Möbius's avatar
Jan Möbius committed
111 112 113 114 115
  
  // Tell internal wrapper to start with the processing
  emit startProcessingInternal();
}

Jan Möbius's avatar
Jan Möbius committed
116 117 118
OpenFlipperJob::~OpenFlipperJob() 
{ 
}
Jan Möbius's avatar
Jan Möbius committed
119 120

void OpenFlipperJob::startJobProcessing() {
121
  
Jan Möbius's avatar
Jan Möbius committed
122 123
  // Actually start the process ( This function will block as it uses a direct connection )
  // But it only blocks the current thread.
124
  emit process(jobId_); 
Jan Möbius's avatar
Jan Möbius committed
125 126 127 128
  
  // Tell thread that the job is done.
  emit finished();
  
Jan Möbius's avatar
Jan Möbius committed
129 130
  // Cleanup this object
  deleteLater();
Jan Möbius's avatar
Jan Möbius committed
131
}