mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-19 20:25:29 +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; }
|
if (hr == S_OK || hr == S_FALSE) { m_winCoInitialized = true; }
|
||||||
}
|
}
|
||||||
#endif
|
#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()
|
void CAfvClient::cleanup()
|
||||||
|
|||||||
@@ -14,80 +14,38 @@
|
|||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
#include <thread>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
bool CThreadUtils::isCurrentThreadObjectThread(const QObject *toBeTested)
|
bool CThreadUtils::isCurrentThreadObjectThread(const QObject *toBeTested)
|
||||||
{
|
{
|
||||||
Q_ASSERT_X(toBeTested, Q_FUNC_INFO, "missing QObject");
|
return QThread::currentThread() == toBeTested->thread();
|
||||||
Q_ASSERT_X(toBeTested->thread(), Q_FUNC_INFO, "missing thread");
|
|
||||||
return (QThread::currentThread() == toBeTested->thread());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CThreadUtils::isApplicationThreadObjectThread(const QObject *toBeTested)
|
bool CThreadUtils::isApplicationThreadObjectThread(const QObject *toBeTested)
|
||||||
{
|
{
|
||||||
Q_ASSERT_X(toBeTested, Q_FUNC_INFO, "missing QObject");
|
return qApp && toBeTested->thread() == qApp->thread();
|
||||||
Q_ASSERT_X(toBeTested->thread(), Q_FUNC_INFO, "missing thread");
|
|
||||||
if (!QCoreApplication::instance() || !QCoreApplication::instance()->thread()) { return false; }
|
|
||||||
return (QCoreApplication::instance()->thread() == toBeTested->thread());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CThreadUtils::isApplicationThread(const QThread *toBeTested)
|
bool CThreadUtils::isApplicationThread(const QThread *toBeTested)
|
||||||
{
|
{
|
||||||
if (!toBeTested || !QCoreApplication::instance() || !QCoreApplication::instance()->thread()) { return false; }
|
return qApp && toBeTested == qApp->thread();
|
||||||
return (QCoreApplication::instance()->thread() == toBeTested);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CThreadUtils::isCurrentThreadApplicationThread()
|
bool CThreadUtils::isCurrentThreadApplicationThread()
|
||||||
{
|
{
|
||||||
if (!QCoreApplication::instance()) { return false; }
|
return qApp && QThread::currentThread() == qApp->thread();
|
||||||
if (!QCoreApplication::instance()->thread()) { return false; }
|
|
||||||
return (QCoreApplication::instance()->thread() == QThread::currentThread());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &CThreadUtils::priorityToString(QThread::Priority priority)
|
QString CThreadUtils::currentThreadInfo()
|
||||||
{
|
{
|
||||||
static const QString idle("idle");
|
std::ostringstream oss;
|
||||||
static const QString lowest("lowest");
|
oss << std::this_thread::get_id();
|
||||||
static const QString low("low");
|
const QThread *thread = QThread::currentThread();
|
||||||
static const QString normal("normal");
|
const QString id = QString::fromStdString(oss.str());
|
||||||
static const QString high("high");
|
return QStringLiteral("%1 (%2) prio %3").arg(id).arg(thread->objectName()).arg(thread->priority());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CThreadUtils::callInObjectThread(QObject *object, std::function<void()> callFunct)
|
bool CThreadUtils::callInObjectThread(QObject *object, std::function<void()> callFunct)
|
||||||
|
|||||||
@@ -26,35 +26,22 @@ namespace BlackMisc
|
|||||||
//! No constructor
|
//! No constructor
|
||||||
CThreadUtils() = delete;
|
CThreadUtils() = delete;
|
||||||
|
|
||||||
//! Is the current thread the QObject's thread?
|
//! Is the current thread the object's thread?
|
||||||
//! \remarks can be used as ASSERT check for threaded objects
|
|
||||||
static bool isCurrentThreadObjectThread(const QObject *toBeTested);
|
static bool isCurrentThreadObjectThread(const QObject *toBeTested);
|
||||||
|
|
||||||
//! Is the application thread the QObject's thread?
|
//! Is the application thread the object's thread?
|
||||||
//! \remarks can be used as ASSERT check for threaded objects
|
|
||||||
static bool isApplicationThreadObjectThread(const QObject *toBeTested);
|
static bool isApplicationThreadObjectThread(const QObject *toBeTested);
|
||||||
|
|
||||||
//! Is the application thread the QObject's thread?
|
//! Is the application thread the object's thread?
|
||||||
//! \remarks can be used as ASSERT check for threaded objects
|
|
||||||
static bool isApplicationThread(const QThread *toBeTested);
|
static bool isApplicationThread(const QThread *toBeTested);
|
||||||
|
|
||||||
//! Is the current thread the Application thread?
|
//! Is the current thread the application thread?
|
||||||
//! \remarks can be used as ASSERT check for threaded objects
|
|
||||||
static bool isCurrentThreadApplicationThread();
|
static bool isCurrentThreadApplicationThread();
|
||||||
|
|
||||||
//! Priority to string
|
//! Info about current thread, for debug messages
|
||||||
static const QString &priorityToString(QThread::Priority priority);
|
static QString currentThreadInfo();
|
||||||
|
|
||||||
//! Thread to int string info
|
//! Call in object's thread if not already in object's thread
|
||||||
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
|
|
||||||
static bool callInObjectThread(QObject *object, std::function<void()> callFunct);
|
static bool callInObjectThread(QObject *object, std::function<void()> callFunct);
|
||||||
};
|
};
|
||||||
} // ns
|
} // ns
|
||||||
|
|||||||
Reference in New Issue
Block a user