OpenFlipperThread.cc 5.31 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
  jobId_(_jobId)
{
}

OpenFlipperThread::~OpenFlipperThread() {
}

Matthias Möller's avatar
Matthias Möller committed
57
58
59
60
QString OpenFlipperThread::jobId() {
  return jobId_;
}

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

Jan Möbius's avatar
Jan Möbius committed
67
    // Connect the slot which should run in this thread. This has to be a DirectConnection !
68
    // Otherwise the slot will run inside its owner context which will be the main loop!!
69
    connect(job_, SIGNAL(process(QString)),this,SIGNAL(function(QString) ),Qt::DirectConnection);
Jan Möbius's avatar
Jan Möbius committed
70
71
72
73
74
75
76
77
    
    // 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);
  }

78
79
80
81
  // 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
82
  exec();
83
  
Jan Möbius's avatar
Jan Möbius committed
84
85
86
87
88
89
90
91
// 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;
92
93
94
95
96
97
98
}

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

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

void OpenFlipperThread::slotJobFinished( ) {
  emit finished( jobId_ );
Dirk Wilden's avatar
Dirk Wilden committed
106
  job_ = 0;
Jan Möbius's avatar
Jan Möbius committed
107
  quit();
Jan Möbius's avatar
Jan Möbius committed
108
109
110
111
112
}


void OpenFlipperThread::startProcessing() {
  
113
114
  // 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
115
116
117
118
119
  
  // Tell internal wrapper to start with the processing
  emit startProcessingInternal();
}

Jan Möbius's avatar
Jan Möbius committed
120
121
122
OpenFlipperJob::~OpenFlipperJob() 
{ 
}
Jan Möbius's avatar
Jan Möbius committed
123
124

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