OpenFlipperThread.cc 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  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.
//
//  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 Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
26 27 28
//   $Revision$
//   $Author$
//   $Date$
29 30 31 32 33 34 35
//
//=============================================================================


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

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

37
OpenFlipperThread::OpenFlipperThread( QString _jobId ) :
Jan Möbius's avatar
Jan Möbius committed
38
  job_(0),
39 40 41 42 43 44 45 46 47
  jobId_(_jobId)
{
}

OpenFlipperThread::~OpenFlipperThread() {
}

void OpenFlipperThread::run()
{
Jan Möbius's avatar
Jan Möbius committed
48 49 50
  if ( job_ == 0 ) {
    // Create a job wrapper to run a slot from within this thread
    job_ = new OpenFlipperJob();
51

Jan Möbius's avatar
Jan Möbius committed
52 53 54 55 56 57 58 59 60 61 62
    // Connect the slot which should run in this thread. This has to be a DirectConnection !
    // Otherwisse the slot will run inside its owner context which will be the main loop!!
    connect(job_, SIGNAL(process()),this,SIGNAL(function() ),Qt::DirectConnection);
    
    // 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);
  }

63 64 65 66
  // 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
67
  exec();
68
  
Jan Möbius's avatar
Jan Möbius committed
69 70 71 72 73 74 75 76
// 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;
77 78 79 80 81 82 83
}

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

void OpenFlipperThread::slotCancel( QString _jobId) {
Jan Möbius's avatar
Jan Möbius committed
84
 std::cerr << "Thread : cancel received" << std::endl;
85 86 87
 if ( _jobId == jobId_ )
  cancel(); 
}
Jan Möbius's avatar
Jan Möbius committed
88 89 90

void OpenFlipperThread::slotJobFinished( ) {
  emit finished( jobId_ );
Jan Möbius's avatar
Jan Möbius committed
91 92 93
  
  quit();
  
Jan Möbius's avatar
Jan Möbius committed
94 95 96 97 98
}


void OpenFlipperThread::startProcessing() {
  
99 100
  // 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
101 102 103 104 105
  
  // Tell internal wrapper to start with the processing
  emit startProcessingInternal();
}

Jan Möbius's avatar
Jan Möbius committed
106 107 108
OpenFlipperJob::~OpenFlipperJob() 
{ 
}
Jan Möbius's avatar
Jan Möbius committed
109 110

void OpenFlipperJob::startJobProcessing() {
111
  
Jan Möbius's avatar
Jan Möbius committed
112 113 114 115 116 117 118
  // Actually start the process ( This function will block as it uses a direct connection )
  // But it only blocks the current thread.
  emit process(); 
  
  // Tell thread that the job is done.
  emit finished();
  
Jan Möbius's avatar
Jan Möbius committed
119 120
  // Cleanup this object
  deleteLater();
Jan Möbius's avatar
Jan Möbius committed
121
}