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_prolog_executed(false), m_epilog_executed(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_prolog_executed)
122  {
123  LOG(VB_GENERAL, LOG_CRIT, "MThread prolog was never run!");
124  }
125  if (!m_epilog_executed)
126  {
127  LOG(VB_GENERAL, LOG_CRIT, "MThread epilog was never run!");
128  }
129  if (m_thread->isRunning())
130  {
131  LOG(VB_GENERAL, LOG_CRIT,
132  "MThread destructor called while thread still running!");
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  QSet<MThread*>::const_iterator it;
150  for (it = s_all_threads.begin(); it != s_all_threads.end(); ++it)
151  {
152  if ((*it)->isRunning())
153  {
154  badGuys.insert(*it);
155  (*it)->exit(1);
156  }
157  }
158 
159  if (badGuys.empty())
160  return;
161 
162  // logging has been stopped so we need to use iostream...
163  cerr<<"Error: Not all threads were shut down properly: "<<endl;
164  for (it = badGuys.begin(); it != badGuys.end(); ++it)
165  {
166  cerr<<"Thread "<<qPrintable((*it)->objectName())
167  <<" is still running"<<endl;
168  }
169  cerr<<endl;
170 
171  static const int kTimeout = 5000;
172  MythTimer t;
173  t.start();
174  for (it = badGuys.begin();
175  it != badGuys.end() && t.elapsed() < kTimeout; ++it)
176  {
177  int left = kTimeout - t.elapsed();
178  if (left > 0)
179  (*it)->wait(left);
180  }
181 }
182 
183 void MThread::GetAllThreadNames(QStringList &list)
184 {
185  QMutexLocker locker(&s_all_threads_lock);
186  QSet<MThread*>::const_iterator it;
187  for (it = s_all_threads.begin(); it != s_all_threads.end(); ++it)
188  list.push_back((*it)->objectName());
189 }
190 
191 void MThread::GetAllRunningThreadNames(QStringList &list)
192 {
193  QMutexLocker locker(&s_all_threads_lock);
194  QSet<MThread*>::const_iterator it;
195  for (it = s_all_threads.begin(); it != s_all_threads.end(); ++it)
196  {
197  if ((*it)->isRunning())
198  list.push_back((*it)->objectName());
199  }
200 }
201 
203 {
204  if (QThread::currentThread() != m_thread)
205  {
206  LOG(VB_GENERAL, LOG_CRIT,
207  "RunProlog can only be executed in the run() method of a thread.");
208  return;
209  }
210  setTerminationEnabled(false);
211  ThreadSetup(m_thread->objectName());
212  m_prolog_executed = true;
213 }
214 
216 {
217  if (QThread::currentThread() != m_thread)
218  {
219  LOG(VB_GENERAL, LOG_CRIT,
220  "RunEpilog can only be executed in the run() method of a thread.");
221  return;
222  }
223  ThreadCleanup();
224  m_epilog_executed = true;
225 }
226 
227 void MThread::ThreadSetup(const QString &name)
228 {
230 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
231  qsrand(MythDate::current().toTime_t() ^ QTime::currentTime().msec());
232 #elif QT_VERSION < QT_VERSION_CHECK(5,10,0)
233  qsrand(MythDate::current().toSecsSinceEpoch() ^ QTime::currentTime().msec());
234 #endif
235 }
236 
238 {
239  if (GetMythDB() && GetMythDB()->GetDBManager())
240  GetMythDB()->GetDBManager()->CloseDatabases();
242 }
243 
244 QThread *MThread::qthread(void)
245 {
246  return m_thread;
247 }
248 
249 void MThread::setObjectName(const QString &name)
250 {
251  m_thread->setObjectName(name);
252 }
253 
254 QString MThread::objectName(void) const
255 {
256  return m_thread->objectName();
257 }
258 
259 void MThread::setPriority(QThread::Priority priority)
260 {
261  m_thread->setPriority(priority);
262 }
263 
264 QThread::Priority MThread::priority(void) const
265 {
266  return m_thread->priority();
267 }
268 
269 bool MThread::isFinished(void) const
270 {
271  return m_thread->isFinished();
272 }
273 
274 bool MThread::isRunning(void) const
275 {
276  return m_thread->isRunning();
277 }
278 
280 {
281  m_thread->setStackSize(stackSize);
282 }
283 
285 {
286  return m_thread->stackSize();
287 }
288 
289 void MThread::exit(int retcode)
290 {
291  m_thread->exit(retcode);
292 }
293 
294 void MThread::start(QThread::Priority p)
295 {
296  m_prolog_executed = false;
297  m_epilog_executed = false;
298  m_thread->start(p);
299 }
300 
302 {
303  m_thread->terminate();
304 }
305 
306 void MThread::quit(void)
307 {
308  m_thread->quit();
309 }
310 
311 bool MThread::wait(unsigned long time)
312 {
313  if (m_thread->isRunning())
314  return m_thread->wait(time);
315  return true;
316 }
317 
318 void MThread::run(void)
319 {
320  RunProlog();
321  if (m_runnable)
322  m_runnable->run();
323  else
324  m_thread->QThreadRun();
325  RunEpilog();
326 }
327 
328 int MThread::exec(void)
329 {
330  return m_thread->exec();
331 }
332 
334 {
336 }
337 
338 void MThread::sleep(unsigned long time)
339 {
341 }
342 
343 void MThread::msleep(unsigned long time)
344 {
346 }
347 
348 void MThread::usleep(unsigned long time)
349 {
351 }
352 
353 /* vim: set expandtab tabstop=4 shiftwidth=4: */
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
void run(void) override
Definition: mthread.cpp:79
static QMutex s_all_threads_lock
Definition: mthread.cpp:99
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
void terminate(void)
Kill a thread unsafely.
Definition: mthread.cpp:301
void QThreadRun(void)
Definition: mthread.cpp:81
int m_purgeTimer
Definition: mthread.cpp:72
MThreadInternal * m_thread
Definition: mthread.h:135
QString objectName(void) const
Definition: mthread.cpp:254
static void SetTerminationEnabled(bool enabled=true)
Definition: mthread.cpp:88
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:318
static void USleep(unsigned long time)
Definition: mthread.cpp:93
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
static void sleep(unsigned long time)
Definition: mthread.cpp:338
unsigned int uint
Definition: compat.h:140
bool m_epilog_executed
Definition: mthread.h:138
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:768
void setObjectName(const QString &name)
Definition: mthread.cpp:249
static void msleep(unsigned long time)
Definition: mthread.cpp:343
void timerEvent(QTimerEvent *event) override
Definition: mthread.cpp:67
MThread(const QString &objectName)
Standard constructor.
Definition: mthread.cpp:102
int exec(void)
Definition: mthread.cpp:82
unsigned sleep(unsigned int x)
Definition: compat.h:159
void setStackSize(uint stackSize)
Definition: mthread.cpp:279
QRunnable * m_runnable
Definition: mthread.h:136
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
unsigned char t
Definition: ParseText.cpp:329
static void GetAllRunningThreadNames(QStringList &list)
Definition: mthread.cpp:191
void setPriority(QThread::Priority priority)
Definition: mthread.cpp:259
void exit(int retcode=0)
Use this to exit from the thread if you are using a Qt event loop.
Definition: mthread.cpp:289
bool isRunning(void) const
Definition: mthread.cpp:274
static QSet< MThread * > s_all_threads
Definition: mthread.cpp:100
MThreadInternal(MThread &parent)
Definition: mthread.cpp:78
bool m_prolog_executed
Definition: mthread.h:137
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:237
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:787
const char * name
Definition: ParseText.cpp:328
static void Sleep(unsigned long time)
Definition: mthread.cpp:91
static void GetAllThreadNames(QStringList &list)
Definition: mthread.cpp:183
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QThread::Priority priority(void) const
Definition: mthread.cpp:264
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
static void MSleep(unsigned long time)
Definition: mthread.cpp:92
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:244
static void ThreadSetup(const QString &)
This is to be called on startup in those few threads that haven't been ported to MThread.
Definition: mthread.cpp:227
uint stackSize(void) const
Definition: mthread.cpp:284
bool isFinished(void) const
Definition: mthread.cpp:269
bool is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
Definition: mthread.cpp:41
MythDB * GetMythDB(void)
Definition: mythdb.cpp:46
virtual ~MThread()
Definition: mthread.cpp:119
static void usleep(unsigned long time)
Definition: mthread.cpp:348
int exec(void)
Enters the qt event loop. call exit or quit to exit thread.
Definition: mthread.cpp:328
static void setTerminationEnabled(bool enabled=true)
Definition: mthread.cpp:333
MThread & m_parent
Definition: mthread.cpp:96
void quit(void)
calls exit(0)
Definition: mthread.cpp:306