MythTV  master
mythsignalingtimer.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  *
3  * Copyright (C) 2010 Jim Stichnoth, Daniel Kristjansson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "mythsignalingtimer.h"
21 #include "mythlogging.h"
22 
24  QObject *parent, const char *slot) :
25  QObject(parent), MThread("SignalingTimer")
26 {
27  connect(this, SIGNAL(timeout()), parent, slot,
28  Qt::QueuedConnection);
29 }
30 
32 {
34  wait();
35 }
36 
38 {
39  if (msec <= 0)
40  return;
41 
42  m_millisec = msec;
43 
44  QMutexLocker locker(&m_startStopLock);
45  if (!m_running)
46  {
47  m_dorun = true;
49  while (m_dorun && !m_running)
50  {
51  locker.unlock();
52  usleep(10 * 1000);
53  locker.relock();
54  }
55  }
56 }
57 
59 {
60  if (is_current_thread(this))
61  {
62  m_dorun = false;
63  return;
64  }
65 
66  QMutexLocker locker(&m_startStopLock);
67  if (m_running)
68  {
69  m_dorun = false;
70  m_timerWait.wakeAll();
71  locker.unlock();
72  wait();
73  }
74 }
75 
77 {
78  m_running = true;
79  RunProlog();
80  while (m_dorun)
81  {
82  QMutexLocker locker(&m_startStopLock);
83  if (m_dorun && !m_timerWait.wait(locker.mutex(), m_millisec))
84  {
85  locker.unlock();
86  emit timeout();
87  locker.relock();
88  }
89  }
90  RunEpilog();
91  m_running = false;
92 }
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
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
virtual void start(int msec)
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
volatile bool m_running
void timeout(void)
MythSignalingTimer(QObject *parent, const char *slot)
QWaitCondition m_timerWait
volatile uint64_t m_millisec
virtual void stop(void)
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
bool is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
Definition: mthread.cpp:41
static void usleep(unsigned long time)
Definition: mthread.cpp:348