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{
21 public:
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
42
43AbstractWorkerThread::AbstractWorkerThread( QObject *parent )
44 : QThread( parent ),
45 d( new AbstractWorkerThreadPrivate( this ) )
46{
47}
48
49AbstractWorkerThread::~AbstractWorkerThread()
50{
51 delete d;
52}
53
54void AbstractWorkerThread::ensureRunning()
55{
56 QMutexLocker locker( &d->m_runningMutex );
57 if ( !d->m_running ) {
58 if ( wait( 2 * WAIT_TIME ) ) {
59 d->m_running = true;
61 }
62 }
63}
64
65void AbstractWorkerThread::run()
66{
67 int waitAttempts = WAIT_ATTEMPTS;
68 while( !d->m_end ) {
69 d->m_runningMutex.lock();
70 if ( !workAvailable() ) {
71 waitAttempts--;
72 if ( !waitAttempts || d->m_end ) {
73 d->m_running = false;
74 d->m_runningMutex.unlock();
75 break;
76 }
77 else {
78 d->m_runningMutex.unlock();
79 msleep( WAIT_TIME );
80 }
81 }
82 else {
83 d->m_runningMutex.unlock();
84 work();
85
86 waitAttempts = WAIT_ATTEMPTS;
87 }
88 }
89}
90
91}
92
93#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 Fri Jun 21 2024 12:00:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.