mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
Issue #77 Simplify thread utils
This commit is contained in:
@@ -1008,7 +1008,7 @@ namespace BlackCore
|
||||
if (hr == S_OK || hr == S_FALSE) { m_winCoInitialized = true; }
|
||||
}
|
||||
#endif
|
||||
CLogMessage(this).info(u"Initialize AFV client in thread: %1") << CThreadUtils::threadInfo(this->thread());
|
||||
CLogMessage(this).info(u"Initialize AFV client in thread %1") << CThreadUtils::currentThreadInfo();
|
||||
}
|
||||
|
||||
void CAfvClient::cleanup()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -26,35 +26,22 @@ namespace BlackMisc
|
||||
//! No constructor
|
||||
CThreadUtils() = delete;
|
||||
|
||||
//! Is the current thread the QObject's thread?
|
||||
//! \remarks can be used as ASSERT check for threaded objects
|
||||
//! Is the current thread the object's thread?
|
||||
static bool isCurrentThreadObjectThread(const QObject *toBeTested);
|
||||
|
||||
//! Is the application thread the QObject's thread?
|
||||
//! \remarks can be used as ASSERT check for threaded objects
|
||||
//! Is the application thread the object's thread?
|
||||
static bool isApplicationThreadObjectThread(const QObject *toBeTested);
|
||||
|
||||
//! Is the application thread the QObject's thread?
|
||||
//! \remarks can be used as ASSERT check for threaded objects
|
||||
//! Is the application thread the object's thread?
|
||||
static bool isApplicationThread(const QThread *toBeTested);
|
||||
|
||||
//! Is the current thread the Application thread?
|
||||
//! \remarks can be used as ASSERT check for threaded objects
|
||||
//! Is the current thread the application thread?
|
||||
static bool isCurrentThreadApplicationThread();
|
||||
|
||||
//! Priority to string
|
||||
static const QString &priorityToString(QThread::Priority priority);
|
||||
//! Info about current thread, for debug messages
|
||||
static QString currentThreadInfo();
|
||||
|
||||
//! Thread to int string info
|
||||
static const QString threadToString(const void *t);
|
||||
|
||||
//! Info about current thread
|
||||
static const QString threadInfo(const QThread *thread);
|
||||
|
||||
//! Info about current thread
|
||||
static const QString currentThreadInfo();
|
||||
|
||||
//! Call in object's thread IF not already in object's thread
|
||||
//! Call in object's thread if not already in object's thread
|
||||
static bool callInObjectThread(QObject *object, std::function<void()> callFunct);
|
||||
};
|
||||
} // ns
|
||||
|
||||
Reference in New Issue
Block a user