Go to the documentation of this file.
25 #include <QStringList>
26 #include <QTimerEvent>
44 return QThread::currentThread() == thread->
qthread();
51 return QThread::currentThread() == thread;
56 return QThread::currentThread() == thread.
qthread();
69 GetMythDB()->GetDBManager()->PurgeIdleConnections(
false);
84 return QThread::exec();
88 { QThread::setTerminationEnabled(enabled); }
90 static void USleep(std::chrono::microseconds time) { QThread::usleep(time.count()); }
109 m_prologExecuted(
false), m_epilogExecuted(
false)
120 LOG(VB_GENERAL, LOG_CRIT, QString(
"'%1': MThread prolog was never run!").arg(
objectName()));
124 LOG(VB_GENERAL, LOG_CRIT, QString(
"'%1': MThread epilog was never run! (%1)").arg(
objectName()));
128 LOG(VB_GENERAL, LOG_CRIT, QString(
"'%1': MThread destructor called while thread still running! (%1)")
145 QSet<MThread*> badGuys;
148 if (thread->isRunning())
150 badGuys.insert(thread);
159 std::cerr<<
"Error: Not all threads were shut down properly: "<<std::endl;
160 for (
auto *thread : std::as_const(badGuys))
162 std::cerr<<
"Thread "<<qPrintable(thread->objectName())
163 <<
" is still running"<<std::endl;
165 std::cerr<<std::endl;
167 static constexpr std::chrono::milliseconds kTimeout { 5s };
170 for (
auto it = badGuys.cbegin();
171 it != badGuys.cend() &&
t.elapsed() < kTimeout; ++it)
173 auto left = kTimeout -
t.elapsed();
183 list.push_back(thread->objectName());
191 if (thread->isRunning())
192 list.push_back(thread->objectName());
198 if (QThread::currentThread() !=
m_thread)
200 LOG(VB_GENERAL, LOG_CRIT,
201 "RunProlog can only be executed in the run() method of a thread.");
211 if (QThread::currentThread() !=
m_thread)
213 LOG(VB_GENERAL, LOG_CRIT,
214 "RunEpilog can only be executed in the run() method of a thread.");
229 GetMythDB()->GetDBManager()->CloseDatabases();
304 if (time == std::chrono::milliseconds::max())
307 return m_thread->wait(time.count());
308 LOG(VB_GENERAL, LOG_CRIT,
309 QString(
"'%1': MThread::wait called for %1 ms!").arg(time.count()));
int exec(void)
Enters the qt event loop. call exit or quit to exit thread.
void loggingDeregisterThread(void)
Deregister the current thread's name.
MThread(const QString &objectName)
Standard constructor.
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
void quit(void)
calls exit(0)
A QElapsedTimer based timer to replace use of QTime as a timer.
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
void timerEvent(QTimerEvent *event) override
static void USleep(std::chrono::microseconds time)
static void setTerminationEnabled(bool enabled=true)
void terminate(void)
Kill a thread unsafely.
static void usleep(std::chrono::microseconds time)
void setObjectName(const QString &name)
static void SetTerminationEnabled(bool enabled=true)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
bool is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
void setPriority(QThread::Priority priority)
MThreadInternal(MThread &parent)
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
void run(const QString &name, Class *object, void(Class::*fn)())
uint stackSize(void) const
bool isFinished(void) const
static void GetAllThreadNames(QStringList &list)
virtual void run(void)
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
QThread::Priority priority(void) const
void setStackSize(uint stackSize)
static QMutex s_all_threads_lock
This is a wrapper around QThread that does several additional things.
static void GetAllRunningThreadNames(QStringList &list)
void exit(int retcode=0)
Use this to exit from the thread if you are using a Qt event loop.
bool isRunning(void) const
MThreadInternal * m_thread
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...
QString objectName(void) const
static void ThreadCleanup(void)
This is to be called on exit in those few threads that haven't been ported to MThread.
static void ThreadSetup(const QString &name)
This is to be called on startup in those few threads that haven't been ported to MThread.
static QSet< MThread * > s_all_threads