Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49
50
51 #include "OpenFlipperThread.hh"
52 #include <iostream>
53
54
55 OpenFlipperThread::OpenFlipperThread( QString _jobId ) :
56 job_(0),
57 jobId_(_jobId)
58 {
59 }
60
61 OpenFlipperThread::~OpenFlipperThread() {
62 }
63
64 QString OpenFlipperThread::jobId() {
65 return jobId_;
66 }
67
68 void OpenFlipperThread::run()
69 {
70 if ( job_ == 0 ) {
71 // Create a job wrapper to run a slot from within this thread
72 job_ = new OpenFlipperJob( jobId_ );
73
74 // Connect the slot which should run in this thread. This has to be a DirectConnection !
75 // Otherwise the slot will run inside its owner context which will be the main loop!!
76 connect(job_, SIGNAL(process(QString)),this,SIGNAL(function(QString) ),Qt::DirectConnection);
77
78 // Connect the jobs finished function
79 connect(job_, SIGNAL(finished()),this,SLOT(slotJobFinished() ) );
80
81 // connect the function to start the job
82 connect(this,SIGNAL(startProcessingInternal()),job_,SLOT(startJobProcessing()),Qt::QueuedConnection);
83 }
84
85 // Thread is ready for processing now, tell core that we can continue.
86 startup_.release(1);
87
88 // Start event queue (possibly added events are already queued here
89 exec();
90
91 // TODO: Self destuction sometimes does not work!
92 // Seems to be a race condition!!!
93
94 // std::cerr << "Delete thread Object " << std::endl;
95 //
96 // deleteLater();
97
98 // std::cerr << "Deleted Thread Object" << std::endl;
99 }
100
101 void OpenFlipperThread::cancel() {
102 std::cerr << "Cancel not implemented" << std::endl;
103 }
104
105 void OpenFlipperThread::slotCancel( QString _jobId) {
106 std::cerr << "Thread : cancel received" << std::endl;
107 if ( _jobId == jobId_ )
108 cancel();
109 }
110
111 void OpenFlipperThread::slotJobFinished( ) {
112 emit finished( jobId_ );
113 job_ = 0;
114 quit();
115 }
116
117
118 void OpenFlipperThread::startProcessing() {
119
120 // Wait for thread to come up and connect its signals ... otherwise the signals might get lost
121 startup_.acquire(1);
122
123 // Tell internal wrapper to start with the processing
124 emit startProcessingInternal();
125 }
126
127 OpenFlipperJob::~OpenFlipperJob()
128 {
129 }
130
131 void OpenFlipperJob::startJobProcessing() {
132
133 // Actually start the process ( This function will block as it uses a direct connection )
134 // But it only blocks the current thread.
135 emit process(jobId_);
136
137 // Tell thread that the job is done.
138 emit finished();
139
140 // Cleanup this object
141 deleteLater();
142 }
OpenFlipperThread::cancel
virtual void cancel()
Cancel the job.
Definition: OpenFlipperThread.cc:101
OpenFlipperThread::run
void run()
Main processing.
Definition: OpenFlipperThread.cc:68
OpenFlipperThread::slotJobFinished
void slotJobFinished()
job has finished
Definition: OpenFlipperThread.cc:111
OpenFlipperJob::finished
void finished()
Job done.
OpenFlipperJob
Internal Job execution object.
Definition: OpenFlipperThread.hh:249
OpenFlipperThread::jobId
QString jobId()
get JobId get the Id of the current Job
Definition: OpenFlipperThread.cc:64
OpenFlipperThread::startProcessingInternal
void startProcessingInternal()
start processing of a function
OpenFlipperThread::finished
void finished(QString _jobId)
job done
OpenFlipperThread::startup_
QSemaphore startup_
Semaphore to control order of thread startup calls.
Definition: OpenFlipperThread.hh:228
OpenFlipperJob::process
void process(const QString _jobId="")
connection to actual processing function
OpenFlipperThread::slotCancel
void slotCancel(QString _jobId)
Cancel this job.
Definition: OpenFlipperThread.cc:105
OpenFlipperJob::jobId_
QString jobId_
The job's id.
Definition: OpenFlipperThread.hh:290
OpenFlipperThread::jobId_
QString jobId_
Id of the current job.
Definition: OpenFlipperThread.hh:218
OpenFlipperThread::startProcessing
void startProcessing()
start processing
Definition: OpenFlipperThread.cc:118
OpenFlipperThread::job_
OpenFlipperJob * job_
Internal job wrapper.
Definition: OpenFlipperThread.hh:211
OpenFlipperJob::startJobProcessing
void startJobProcessing()
slot to start processing
Definition: OpenFlipperThread.cc:131