44 return QThread::currentThread() == thread->
qthread();
51 return QThread::currentThread() == thread;
56 return QThread::currentThread() == thread.
qthread();
74 { QThread::setTerminationEnabled(enabled); }
93 m_prologExecuted(
false), m_epilogExecuted(
false)
104 LOG(VB_GENERAL, LOG_CRIT, QString(
"'%1': MThread prolog was never run!").arg(
objectName()));
108 LOG(VB_GENERAL, LOG_CRIT, QString(
"'%1': MThread epilog was never run! (%1)").arg(
objectName()));
112 LOG(VB_GENERAL, LOG_CRIT, QString(
"'%1': MThread destructor called while thread still running! (%1)")
129 QSet<MThread*> badGuys;
132 if (thread->isRunning())
134 badGuys.insert(thread);
143 std::cerr<<
"Error: Not all threads were shut down properly:\n";
144 for (
auto *thread : std::as_const(badGuys))
146 std::cerr<<
"Thread "<<qPrintable(thread->objectName())
147 <<
" is still running\n";
151 static constexpr std::chrono::milliseconds kTimeout { 5s };
154 for (
auto it = badGuys.cbegin();
155 it != badGuys.cend() &&
t.elapsed() < kTimeout; ++it)
157 auto left = kTimeout -
t.elapsed();
167 list.push_back(thread->objectName());
175 if (thread->isRunning())
176 list.push_back(thread->objectName());
182 if (QThread::currentThread() !=
m_thread)
184 LOG(VB_GENERAL, LOG_CRIT,
185 "RunProlog can only be executed in the run() method of a thread.");
195 if (QThread::currentThread() !=
m_thread)
197 LOG(VB_GENERAL, LOG_CRIT,
198 "RunEpilog can only be executed in the run() method of a thread.");
213 GetMythDB()->GetDBManager()->CloseDatabases();
288 if (time == std::chrono::milliseconds::max())
291 return m_thread->wait(time.count());
292 LOG(VB_GENERAL, LOG_CRIT,
293 QString(
"'%1': MThread::wait called for %1 ms!").arg(time.count()));
MThreadInternal(MThread &parent)
static void SetTerminationEnabled(bool enabled=true)
This is a wrapper around QThread that does several additional things.
MThreadInternal * m_thread
bool isRunning(void) const
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
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...
static void GetAllRunningThreadNames(QStringList &list)
int exec(void)
Enters the qt event loop. call exit or quit to exit thread.
void terminate(void)
Kill a thread unsafely.
bool isFinished(void) const
static void setTerminationEnabled(bool enabled=true)
static void ThreadCleanup(void)
This is to be called on exit in those few threads that haven't been ported to MThread.
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
void quit(void)
calls exit(0)
virtual void run(void)
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
uint stackSize(void) const
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
void setStackSize(uint stackSize)
static void GetAllThreadNames(QStringList &list)
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
void exit(int retcode=0)
Use this to exit from the thread if you are using a Qt event loop.
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
QThread::Priority priority(void) const
MThread(const QString &objectName)
Standard constructor.
QString objectName(void) const
void setPriority(QThread::Priority priority)
static void ThreadSetup(const QString &name)
This is to be called on startup in those few threads that haven't been ported to MThread.
void setObjectName(const QString &name)
A QElapsedTimer based timer to replace use of QTime as a timer.
void loggingDeregisterThread(void)
Deregister the current thread's name.
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
static QSet< MThread * > s_all_threads
static QMutex s_all_threads_lock
bool is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
void run(const QString &name, Class *object, void(Class::*fn)())