Developer Documentation
OpenFlipperThread.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 #include "OpenFlipperThread.hh"
46 #include <iostream>
47 
48 
49 OpenFlipperThread::OpenFlipperThread( QString _jobId ) :
50  job_(0),
51  jobId_(_jobId)
52 {
53 }
54 
55 OpenFlipperThread::~OpenFlipperThread() {
56 }
57 
59  return jobId_;
60 }
61 
63 {
64  if ( job_ == 0 ) {
65  // Create a job wrapper to run a slot from within this thread
66  job_ = new OpenFlipperJob( jobId_ );
67 
68  // Connect the slot which should run in this thread. This has to be a DirectConnection !
69  // Otherwise the slot will run inside its owner context which will be the main loop!!
70  connect(job_, SIGNAL(process(QString)),this,SIGNAL(function(QString) ),Qt::DirectConnection);
71 
72  // Connect the jobs finished function
73  connect(job_, SIGNAL(finished()),this,SLOT(slotJobFinished() ) );
74 
75  // connect the function to start the job
76  connect(this,SIGNAL(startProcessingInternal()),job_,SLOT(startJobProcessing()),Qt::QueuedConnection);
77  }
78 
79  // Thread is ready for processing now, tell core that we can continue.
80  startup_.release(1);
81 
82  // Start event queue (possibly added events are already queued here
83  exec();
84 
85 // TODO: Self destuction sometimes does not work!
86 // Seems to be a race condition!!!
87 
88 // std::cerr << "Delete thread Object " << std::endl;
89 //
90 // deleteLater();
91 
92 // std::cerr << "Deleted Thread Object" << std::endl;
93 }
94 
96  std::cerr << "Cancel not implemented" << std::endl;
97 }
98 
99 void OpenFlipperThread::slotCancel( QString _jobId) {
100  std::cerr << "Thread : cancel received" << std::endl;
101  if ( _jobId == jobId_ )
102  cancel();
103 }
104 
106  emit finished( jobId_ );
107  job_ = 0;
108  quit();
109 }
110 
111 
113 
114  // Wait for thread to come up and connect its signals ... otherwise the signals might get lost
115  startup_.acquire(1);
116 
117  // Tell internal wrapper to start with the processing
118  emit startProcessingInternal();
119 }
120 
121 OpenFlipperJob::~OpenFlipperJob()
122 {
123 }
124 
126 
127  // Actually start the process ( This function will block as it uses a direct connection )
128  // But it only blocks the current thread.
129  emit process(jobId_);
130 
131  // Tell thread that the job is done.
132  emit finished();
133 
134  // Cleanup this object
135  deleteLater();
136 }
virtual void cancel()
Cancel the job.
void run()
Main processing.
void startProcessing()
start processing
void slotJobFinished()
job has finished
void startJobProcessing()
slot to start processing
void slotCancel(QString _jobId)
Cancel this job.
Internal Job execution object.
QString jobId()
get JobId get the Id of the current Job