File indexing completed on 2024-04-28 04:49:23

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 
0008 #include "k3bthread.h"
0009 #include "k3bthreadjob.h"
0010 #include "k3bprogressinfoevent.h"
0011 #include "k3bthreadjobcommunicationevent.h"
0012 
0013 #include <QDebug>
0014 #include <QList>
0015 #include <QTimer>
0016 
0017 
0018 static QList<K3b::Thread*> s_threads;
0019 
0020 
0021 
0022 class K3b::Thread::Private
0023 {
0024 public:
0025     K3b::ThreadJob* parentJob;
0026     bool success;
0027 };
0028 
0029 
0030 K3b::Thread::Thread( K3b::ThreadJob* parent )
0031     : QThread( parent )
0032 {
0033     d = new Private;
0034     d->parentJob = parent;
0035 
0036     s_threads.append(this);
0037 }
0038 
0039 
0040 K3b::Thread::~Thread()
0041 {
0042     s_threads.removeAll(this);
0043     delete d;
0044 }
0045 
0046 
0047 void K3b::Thread::run()
0048 {
0049     // default to false in case we need to terminate
0050     d->success = false;
0051 
0052     // run the job itself
0053     d->success = d->parentJob->run();
0054 }
0055 
0056 
0057 bool K3b::Thread::success() const
0058 {
0059     return d->success;
0060 }
0061 
0062 
0063 void K3b::Thread::ensureDone()
0064 {
0065     // we wait for 5 seconds before we terminate the thread
0066     QTimer::singleShot( 5000, this, SLOT(slotEnsureDoneTimeout()) );
0067 }
0068 
0069 
0070 void K3b::Thread::slotEnsureDoneTimeout()
0071 {
0072     if ( isRunning() ) {
0073         terminate();
0074         wait();
0075     }
0076 }
0077 
0078 
0079 void K3b::Thread::waitUntilFinished()
0080 {
0081     foreach( K3b::Thread* thread, s_threads ) {
0082         qDebug() << "Waiting for thread " << thread << Qt::endl;
0083         thread->wait();
0084     }
0085 
0086     qDebug() << "Thread waiting done." << Qt::endl;
0087 }
0088 
0089 #include "moc_k3bthread.cpp"