Issue #77 Simplify thread utils

This commit is contained in:
Mat Sutcliffe
2020-08-24 16:10:33 +01:00
parent 689997e3f1
commit b7f6b06491
3 changed files with 20 additions and 75 deletions

View File

@@ -14,80 +14,38 @@
#include <QtGlobal>
#include <QPointer>
#include <QTimer>
#include <thread>
#include <sstream>
namespace BlackMisc
{
bool CThreadUtils::isCurrentThreadObjectThread(const QObject *toBeTested)
{
Q_ASSERT_X(toBeTested, Q_FUNC_INFO, "missing QObject");
Q_ASSERT_X(toBeTested->thread(), Q_FUNC_INFO, "missing thread");
return (QThread::currentThread() == toBeTested->thread());
return QThread::currentThread() == toBeTested->thread();
}
bool CThreadUtils::isApplicationThreadObjectThread(const QObject *toBeTested)
{
Q_ASSERT_X(toBeTested, Q_FUNC_INFO, "missing QObject");
Q_ASSERT_X(toBeTested->thread(), Q_FUNC_INFO, "missing thread");
if (!QCoreApplication::instance() || !QCoreApplication::instance()->thread()) { return false; }
return (QCoreApplication::instance()->thread() == toBeTested->thread());
return qApp && toBeTested->thread() == qApp->thread();
}
bool CThreadUtils::isApplicationThread(const QThread *toBeTested)
{
if (!toBeTested || !QCoreApplication::instance() || !QCoreApplication::instance()->thread()) { return false; }
return (QCoreApplication::instance()->thread() == toBeTested);
return qApp && toBeTested == qApp->thread();
}
bool CThreadUtils::isCurrentThreadApplicationThread()
{
if (!QCoreApplication::instance()) { return false; }
if (!QCoreApplication::instance()->thread()) { return false; }
return (QCoreApplication::instance()->thread() == QThread::currentThread());
return qApp && QThread::currentThread() == qApp->thread();
}
const QString &CThreadUtils::priorityToString(QThread::Priority priority)
QString CThreadUtils::currentThreadInfo()
{
static const QString idle("idle");
static const QString lowest("lowest");
static const QString low("low");
static const QString normal("normal");
static const QString high("high");
static const QString highest("highest");
static const QString time("time critical");
static const QString inherit("inherit");
switch (priority)
{
case QThread::IdlePriority: return idle;
case QThread::LowestPriority: return lowest;
case QThread::LowPriority: return low;
case QThread::NormalPriority: return normal;
case QThread::HighPriority: return high;
case QThread::HighestPriority: return highest;
case QThread::InheritPriority: return inherit;
case QThread::TimeCriticalPriority: return time;
default: break;
}
static const QString unknown("unknown");
return unknown;
}
const QString CThreadUtils::threadToString(const void *t)
{
return QStringLiteral("0x%1").arg(reinterpret_cast<long long>(t), 0, 16);
}
const QString CThreadUtils::threadInfo(const QThread *thread)
{
static const QString info("thread: %1 name: '%2' priority: '%3'");
if (!thread) { return QString("no thread"); }
return info.arg(threadToString(thread), thread->objectName(), priorityToString(thread->priority()));
}
const QString CThreadUtils::currentThreadInfo()
{
return threadInfo(QThread::currentThread());
std::ostringstream oss;
oss << std::this_thread::get_id();
const QThread *thread = QThread::currentThread();
const QString id = QString::fromStdString(oss.str());
return QStringLiteral("%1 (%2) prio %3").arg(id).arg(thread->objectName()).arg(thread->priority());
}
bool CThreadUtils::callInObjectThread(QObject *object, std::function<void()> callFunct)