MythTV  master
mthread.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  *
3  * Class MThread
4  *
5  * Copyright (C) Daniel Kristjansson 2011
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <iostream>
23 using namespace std;
24 
25 // Qt headers
26 #include <QStringList>
27 #include <QTimerEvent>
28 #include <QRunnable>
29 #include <QtGlobal>
30 #include <QSet>
31 
32 // MythTV headers
33 #include "mythlogging.h"
34 #include "mythdbcon.h"
35 #include "mythtimer.h"
36 #include "mythdate.h"
37 #include "logging.h"
38 #include "mthread.h"
39 #include "mythdb.h"
40 
42 {
43  if (!thread)
44  return false;
45  return QThread::currentThread() == thread->qthread();
46 }
47 
48 bool is_current_thread(QThread *thread)
49 {
50  if (!thread)
51  return false;
52  return QThread::currentThread() == thread;
53 }
54 
56 {
57  return QThread::currentThread() == thread.qthread();
58 }
59 
60 class DBPurgeHandler : public QObject
61 {
62  public:
64  {
65  m_purgeTimer = startTimer(5 * 60000);
66  }
67  void timerEvent(QTimerEvent *event) override // QObject
68  {
69  if (event->timerId() == m_purgeTimer)
70  GetMythDB()->GetDBManager()->PurgeIdleConnections(false);
71  }
73 };
74 
75 class MThreadInternal : public QThread
76 {
77  public:
78  explicit MThreadInternal(MThread &parent) : m_parent(parent) {}
79  void run(void) override { m_parent.run(); } // QThread
80 
81  void QThreadRun(void) { QThread::run(); }
82  int exec(void)
83  {
84  DBPurgeHandler ph;
85  return QThread::exec();
86  }
87 
88  static void SetTerminationEnabled(bool enabled = true)
89  { QThread::setTerminationEnabled(enabled); }
90 
91  static void Sleep(unsigned long time) { QThread::sleep(time); }
92  static void MSleep(unsigned long time) { QThread::msleep(time); }
93  static void USleep(unsigned long time) { QThread::usleep(time); }
94 
95  private:
97 };
98 
99 static QMutex s_all_threads_lock;
100 static QSet<MThread*> s_all_threads;
101 
102 MThread::MThread(const QString &objectName) :
103  m_thread(new MThreadInternal(*this))
104 {
105  m_thread->setObjectName(objectName);
106  QMutexLocker locker(&s_all_threads_lock);
107  s_all_threads.insert(this);
108 }
109 
110 MThread::MThread(const QString &objectName, QRunnable *runnable) :
111  m_thread(new MThreadInternal(*this)), m_runnable(runnable),
112  m_prologExecuted(false), m_epilogExecuted(false)
113 {
114  m_thread->setObjectName(objectName);
115  QMutexLocker locker(&s_all_threads_lock);
116  s_all_threads.insert(this);
117 }
118 
120 {
121  if (!m_prologExecuted)
122  {
123  LOG(VB_GENERAL, LOG_CRIT, QString("'%1': MThread prolog was never run!").arg(objectName()));
124  }
125  if (!m_epilogExecuted)
126  {
127  LOG(VB_GENERAL, LOG_CRIT, QString("'%1': MThread epilog was never run! (%1)").arg(objectName()));
128  }
129  if (m_thread->isRunning())
130  {
131  LOG(VB_GENERAL, LOG_CRIT, QString("'%1': MThread destructor called while thread still running! (%1)")
132  .arg(objectName()));
133  m_thread->wait();
134  }
135 
136  {
137  QMutexLocker locker(&s_all_threads_lock);
138  s_all_threads.remove(this);
139  }
140 
141  delete m_thread;
142  m_thread = nullptr;
143 }
144 
146 {
147  QMutexLocker locker(&s_all_threads_lock);
148  QSet<MThread*> badGuys;
149  for (auto *thread : qAsConst(s_all_threads))
150  {
151  if (thread->isRunning())
152  {
153  badGuys.insert(thread);
154  thread->exit(1);
155  }
156  }
157 
158  if (badGuys.empty())
159  return;
160 
161  // logging has been stopped so we need to use iostream...
162  cerr<<"Error: Not all threads were shut down properly: "<<endl;
163  for (auto *thread : qAsConst(badGuys))
164  {
165  cerr<<"Thread "<<qPrintable(thread->objectName())
166  <<" is still running"<<endl;
167  }
168  cerr<<endl;
169 
170  static const int kTimeout = 5000;
171  MythTimer t;
172  t.start();
173  for (auto it = badGuys.cbegin();
174  it != badGuys.cend() && t.elapsed() < kTimeout; ++it)
175  {
176  int left = kTimeout - t.elapsed();
177  if (left > 0)
178  (*it)->wait(left);
179  }
180 }
181 
182 void MThread::GetAllThreadNames(QStringList &list)
183 {
184  QMutexLocker locker(&s_all_threads_lock);
185  for (auto *thread : qAsConst(s_all_threads))
186  list.push_back(thread->objectName());
187 }
188 
189 void MThread::GetAllRunningThreadNames(QStringList &list)
190 {
191  QMutexLocker locker(&s_all_threads_lock);
192  for (auto *thread : qAsConst(s_all_threads))
193  {
194  if (thread->isRunning())
195  list.push_back(thread->objectName());
196  }
197 }
198 
200 {
201  if (QThread::currentThread() != m_thread)
202  {
203  LOG(VB_GENERAL, LOG_CRIT,
204  "RunProlog can only be executed in the run() method of a thread.");
205  return;
206  }
207  setTerminationEnabled(false);
208  ThreadSetup(m_thread->objectName());
209  m_prologExecuted = true;
210 }
211 
213 {
214  if (QThread::currentThread() != m_thread)
215  {
216  LOG(VB_GENERAL, LOG_CRIT,
217  "RunEpilog can only be executed in the run() method of a thread.");
218  return;
219  }
220  ThreadCleanup();
221  m_epilogExecuted = true;
222 }
223 
224 void MThread::ThreadSetup(const QString &name)
225 {
226  loggingRegisterThread(name);
227 #if QT_VERSION < QT_VERSION_CHECK(5,10,0)
228  qsrand(MythDate::current().toSecsSinceEpoch() ^ QTime::currentTime().msec());
229 #endif
230 }
231 
233 {
234  if (GetMythDB() && GetMythDB()->GetDBManager())
235  GetMythDB()->GetDBManager()->CloseDatabases();
237 }
238 
239 QThread *MThread::qthread(void)
240 {
241  return m_thread;
242 }
243 
244 void MThread::setObjectName(const QString &name)
245 {
246  m_thread->setObjectName(name);
247 }
248 
249 QString MThread::objectName(void) const
250 {
251  return m_thread->objectName();
252 }
253 
254 void MThread::setPriority(QThread::Priority priority)
255 {
256  m_thread->setPriority(priority);
257 }
258 
259 QThread::Priority MThread::priority(void) const
260 {
261  return m_thread->priority();
262 }
263 
264 bool MThread::isFinished(void) const
265 {
266  return m_thread->isFinished();
267 }
268 
269 bool MThread::isRunning(void) const
270 {
271  return m_thread->isRunning();
272 }
273 
275 {
276  m_thread->setStackSize(stackSize);
277 }
278 
280 {
281  return m_thread->stackSize();
282 }
283 
284 void MThread::exit(int retcode)
285 {
286  m_thread->exit(retcode);
287 }
288 
289 void MThread::start(QThread::Priority p)
290 {
291  m_prologExecuted = false;
292  m_epilogExecuted = false;
293  m_thread->start(p);
294 }
295 
297 {
298  m_thread->terminate();
299 }
300 
301 void MThread::quit(void)
302 {
303  m_thread->quit();
304 }
305 
306 bool MThread::wait(unsigned long time)
307 {
308  if (m_thread->isRunning())
309  return m_thread->wait(time);
310  return true;
311 }
312 
313 void MThread::run(void)
314 {
315  RunProlog();
316  if (m_runnable)
317  m_runnable->run();
318  else
319  m_thread->QThreadRun();
320  RunEpilog();
321 }
322 
323 int MThread::exec(void)
324 {
325  return m_thread->exec();
326 }
327 
329 {
331 }
332 
333 void MThread::sleep(unsigned long time)
334 {
336 }
337 
338 void MThread::msleep(unsigned long time)
339 {
341 }
342 
343 void MThread::usleep(unsigned long time)
344 {
346 }
347 
348 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MThread::exec
int exec(void)
Enters the qt event loop. call exit or quit to exit thread.
Definition: mthread.cpp:323
loggingDeregisterThread
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:789
MThread::msleep
static void msleep(unsigned long time)
Definition: mthread.cpp:338
MThreadInternal::exec
int exec(void)
Definition: mthread.cpp:82
MThread::MThread
MThread(const QString &objectName)
Standard constructor.
Definition: mthread.cpp:102
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:289
MThread::quit
void quit(void)
calls exit(0)
Definition: mthread.cpp:301
mythdb.h
false
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:14
MThreadInternal::run
void run(void) override
Definition: mthread.cpp:79
MThread::~MThread
virtual ~MThread()
Definition: mthread.cpp:119
DBPurgeHandler::timerEvent
void timerEvent(QTimerEvent *event) override
Definition: mthread.cpp:67
MThread::setTerminationEnabled
static void setTerminationEnabled(bool enabled=true)
Definition: mthread.cpp:328
MThread::terminate
void terminate(void)
Kill a thread unsafely.
Definition: mthread.cpp:296
MThread::m_prologExecuted
bool m_prologExecuted
Definition: mthread.h:138
MThread::setObjectName
void setObjectName(const QString &name)
Definition: mthread.cpp:244
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
MThreadInternal::SetTerminationEnabled
static void SetTerminationEnabled(bool enabled=true)
Definition: mthread.cpp:88
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MThread::RunProlog
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:199
is_current_thread
bool is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
Definition: mthread.cpp:41
GetMythDB
MythDB * GetMythDB(void)
Definition: mythdb.cpp:46
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
sleep
unsigned sleep(unsigned int x)
Definition: compat.h:159
mythdate.h
mythlogging.h
MThreadInternal
Definition: mthread.cpp:76
DBPurgeHandler::DBPurgeHandler
DBPurgeHandler()
Definition: mthread.cpp:63
hardwareprofile.config.p
p
Definition: config.py:33
loggingRegisterThread
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:770
MThread::setPriority
void setPriority(QThread::Priority priority)
Definition: mthread.cpp:254
hardwareprofile.i18n.t
t
Definition: i18n.py:36
MThreadInternal::MThreadInternal
MThreadInternal(MThread &parent)
Definition: mthread.cpp:78
MThread::qthread
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:239
MThread::RunEpilog
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:212
MThread::stackSize
uint stackSize(void) const
Definition: mthread.cpp:279
MThread::isFinished
bool isFinished(void) const
Definition: mthread.cpp:264
MThreadInternal::QThreadRun
void QThreadRun(void)
Definition: mthread.cpp:81
uint
unsigned int uint
Definition: compat.h:140
MThread::GetAllThreadNames
static void GetAllThreadNames(QStringList &list)
Definition: mthread.cpp:182
MThread::run
virtual void run(void)
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: mthread.cpp:313
MThread::m_epilogExecuted
bool m_epilogExecuted
Definition: mthread.h:139
MThread::priority
QThread::Priority priority(void) const
Definition: mthread.cpp:259
MThread::setStackSize
void setStackSize(uint stackSize)
Definition: mthread.cpp:274
MThreadInternal::MSleep
static void MSleep(unsigned long time)
Definition: mthread.cpp:92
MThreadInternal::USleep
static void USleep(unsigned long time)
Definition: mthread.cpp:93
s_all_threads_lock
static QMutex s_all_threads_lock
Definition: mthread.cpp:99
logging.h
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
MThread::GetAllRunningThreadNames
static void GetAllRunningThreadNames(QStringList &list)
Definition: mthread.cpp:189
MThread::exit
void exit(int retcode=0)
Use this to exit from the thread if you are using a Qt event loop.
Definition: mthread.cpp:284
MThread::isRunning
bool isRunning(void) const
Definition: mthread.cpp:269
MThread::m_thread
MThreadInternal * m_thread
Definition: mthread.h:136
MThread::Cleanup
static void Cleanup(void)
This will print out all the running threads, call exit(1) on each and then wait up to 5 seconds total...
Definition: mthread.cpp:145
MThread::objectName
QString objectName(void) const
Definition: mthread.cpp:249
mythtimer.h
MThread::ThreadCleanup
static void ThreadCleanup(void)
This is to be called on exit in those few threads that haven't been ported to MThread.
Definition: mthread.cpp:232
MThread::ThreadSetup
static void ThreadSetup(const QString &name)
This is to be called on startup in those few threads that haven't been ported to MThread.
Definition: mthread.cpp:224
s_all_threads
static QSet< MThread * > s_all_threads
Definition: mthread.cpp:100
MThread::wait
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:306
MThread::sleep
static void sleep(unsigned long time)
Definition: mthread.cpp:333
MThread::m_runnable
QRunnable * m_runnable
Definition: mthread.h:137
DBPurgeHandler::m_purgeTimer
int m_purgeTimer
Definition: mthread.cpp:72
MThreadInternal::m_parent
MThread & m_parent
Definition: mthread.cpp:96
MThreadInternal::Sleep
static void Sleep(unsigned long time)
Definition: mthread.cpp:91
DBPurgeHandler
Definition: mthread.cpp:61
MThread::usleep
static void usleep(unsigned long time)
Definition: mthread.cpp:343