Marble

AbstractWorkerThread.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2009 Bastian Holst <[email protected]>
4 //
5 
6 // Self
7 #include "AbstractWorkerThread.h"
8 #include "MarbleDebug.h"
9 
10 // Qt
11 #include <QMutex>
12 
13 namespace Marble
14 {
15 
16 const int WAIT_ATTEMPTS = 20;
17 const int WAIT_TIME = 100;
18 
19 class 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 
43 AbstractWorkerThread::AbstractWorkerThread( QObject *parent )
44  : QThread( parent ),
45  d( new AbstractWorkerThreadPrivate( this ) )
46 {
47 }
48 
49 AbstractWorkerThread::~AbstractWorkerThread()
50 {
51  delete d;
52 }
53 
54 void 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 
65 void 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"
void msleep(unsigned long msecs)
bool wait(QDeadlineTimer deadline)
void start(QThread::Priority priority)
Binds a QML item to a specific geodetic location in screen coordinates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Oct 3 2023 04:09:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.