Marble

AbstractWorkerThread.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
4//
5
6// Self
7#include "AbstractWorkerThread.h"
8#include "MarbleDebug.h"
9
10// Qt
11#include <QMutex>
12
13namespace Marble
14{
15
16const int WAIT_ATTEMPTS = 20;
17const int WAIT_TIME = 100;
18
19class AbstractWorkerThreadPrivate
20{
21public:
22 explicit AbstractWorkerThreadPrivate(AbstractWorkerThread *parent)
23 : m_running(false)
24 , m_end(false)
25 , m_parent(parent)
26 {
27 }
28
29 ~AbstractWorkerThreadPrivate()
30 {
31 m_end = true;
32 m_parent->wait(1000);
33 }
34
35 QMutex m_runningMutex;
36 bool m_running;
37 bool m_end;
38
39 AbstractWorkerThread *m_parent;
40};
41
42AbstractWorkerThread::AbstractWorkerThread(QObject *parent)
43 : QThread(parent)
44 , d(new AbstractWorkerThreadPrivate(this))
45{
46}
47
48AbstractWorkerThread::~AbstractWorkerThread()
49{
50 delete d;
51}
52
53void AbstractWorkerThread::ensureRunning()
54{
55 QMutexLocker locker(&d->m_runningMutex);
56 if (!d->m_running) {
57 if (wait(2 * WAIT_TIME)) {
58 d->m_running = true;
60 }
61 }
62}
63
64void AbstractWorkerThread::run()
65{
66 int waitAttempts = WAIT_ATTEMPTS;
67 while (!d->m_end) {
68 d->m_runningMutex.lock();
69 if (!workAvailable()) {
70 waitAttempts--;
71 if (!waitAttempts || d->m_end) {
72 d->m_running = false;
73 d->m_runningMutex.unlock();
74 break;
75 } else {
76 d->m_runningMutex.unlock();
77 msleep(WAIT_TIME);
78 }
79 } else {
80 d->m_runningMutex.unlock();
81 work();
82
83 waitAttempts = WAIT_ATTEMPTS;
84 }
85 }
86}
87
88}
89
90#include "moc_AbstractWorkerThread.cpp"
Q_SCRIPTABLE Q_NOREPLY void start()
Binds a QML item to a specific geodetic location in screen coordinates.
bool wait(QDeadlineTimer deadline)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.