From 92edbb90c2f8c5bd2d1fc6301f9e1453802e5136 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Mon, 24 Aug 2020 00:50:28 +0100 Subject: [PATCH] Issue #77 Refactor to inline functions --- src/blackcore/airspaceanalyzer.cpp | 2 +- src/blackcore/application.cpp | 2 +- src/blackcore/context/contextaudio.cpp | 4 +- src/blackcore/fsd/fsdclient.cpp | 77 +++++++++++++++++++++++--- src/blackmisc/appstarttime.cpp | 18 ------ src/blackmisc/appstarttime.h | 24 -------- src/blackmisc/filelogger.cpp | 3 +- src/blackmisc/threadutils.cpp | 19 ------- src/blackmisc/threadutils.h | 9 --- 9 files changed, 73 insertions(+), 85 deletions(-) delete mode 100644 src/blackmisc/appstarttime.cpp delete mode 100644 src/blackmisc/appstarttime.h diff --git a/src/blackcore/airspaceanalyzer.cpp b/src/blackcore/airspaceanalyzer.cpp index 0bdffedb9..f50d4bb75 100644 --- a/src/blackcore/airspaceanalyzer.cpp +++ b/src/blackcore/airspaceanalyzer.cpp @@ -220,7 +220,7 @@ namespace BlackCore void CAirspaceAnalyzer::analyzeAirspace() { Q_ASSERT_X(!CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "Expect to run in background thread"); - Q_ASSERT_X(!CThreadUtils::isApplicationThreadObjectThread(this), Q_FUNC_INFO, "Expect to run in background thread affinity"); + Q_ASSERT_X(thread() != qApp->thread(), Q_FUNC_INFO, "Expect to run in background thread affinity"); bool restricted, enabled; int maxAircraft; diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 0e7520bd5..7281246cc 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -1809,7 +1809,7 @@ namespace BlackCore if (!this->isNetworkAccessible()) { return nullptr; } QWriteLocker locker(&m_accessManagerLock); - Q_ASSERT_X(CThreadUtils::isApplicationThreadObjectThread(m_accessManager), Q_FUNC_INFO, "Network manager supposed to be in main thread"); + Q_ASSERT_X(m_accessManager->thread() == qApp->thread(), Q_FUNC_INFO, "Network manager supposed to be in main thread"); if (!CThreadUtils::isCurrentThreadObjectThread(m_accessManager)) { this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest); diff --git a/src/blackcore/context/contextaudio.cpp b/src/blackcore/context/contextaudio.cpp index 96d2301b0..134c54e69 100644 --- a/src/blackcore/context/contextaudio.cpp +++ b/src/blackcore/context/contextaudio.cpp @@ -195,10 +195,10 @@ namespace BlackCore const CVoiceSetup vs = m_voiceSettings.getThreadLocal(); m_voiceClient->updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); - Q_ASSERT_X(CThreadUtils::isApplicationThread(m_voiceClient->thread()), Q_FUNC_INFO, "Should be in main thread"); + Q_ASSERT_X(m_voiceClient->thread() == qApp->thread(), Q_FUNC_INFO, "Should be in main thread"); m_voiceClient->start(); // thread Q_ASSERT_X(m_voiceClient->owner() == this, Q_FUNC_INFO, "Wrong owner"); - Q_ASSERT_X(!CThreadUtils::isApplicationThread(m_voiceClient->thread()), Q_FUNC_INFO, "Must NOT be in main thread"); + Q_ASSERT_X(m_voiceClient->thread() != qApp->thread(), Q_FUNC_INFO, "Must NOT be in main thread"); // connect(m_voiceClient, &CAfvClient::outputVolumePeakVU, this, &CContextAudioBase::outputVolumePeakVU, Qt::QueuedConnection); // connect(m_voiceClient, &CAfvClient::inputVolumePeakVU, this, &CContextAudioBase::inputVolumePeakVU, Qt::QueuedConnection); diff --git a/src/blackcore/fsd/fsdclient.cpp b/src/blackcore/fsd/fsdclient.cpp index 47fda6fa6..1b9e74094 100644 --- a/src/blackcore/fsd/fsdclient.cpp +++ b/src/blackcore/fsd/fsdclient.cpp @@ -225,7 +225,15 @@ namespace BlackCore void CFSDClient::connectToServer() { - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->connectToServer(); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { connectToServer(); } + }); + return; + } + if (m_socket.isOpen()) { return; } Q_ASSERT(!m_clientName.isEmpty()); Q_ASSERT((m_versionMajor + m_versionMinor) > 0); @@ -257,8 +265,15 @@ namespace BlackCore void CFSDClient::disconnectFromServer() { - if (this->isDisconnected()) { return; } - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->disconnectFromServer(); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { disconnectFromServer(); } + }); + return; + } + this->stopPositionTimers(); this->updateConnectionStatus(CConnectionStatus::Disconnecting); @@ -426,7 +441,14 @@ namespace BlackCore void CFSDClient::sendClientQuery(ClientQueryType queryType, const CCallsign &receiver, const QStringList &queryData) { if (queryType == ClientQueryType::Unknown) { return; } - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->sendClientQuery(queryType, receiver, queryData); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { sendClientQuery(queryType, receiver, queryData); } + }); + return; + } const QString reveiverCallsign = receiver.getFsdCallsignString(); if (queryType == ClientQueryType::IsValidATC) @@ -492,7 +514,14 @@ namespace BlackCore void CFSDClient::sendTextMessages(const CTextMessageList &messages) { if (messages.isEmpty()) { return; } - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->sendTextMessages(messages); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { sendTextMessages(messages); } + }); + return; + } const CTextMessageList privateMessages = messages.getPrivateMessages().markedAsSent(); const QString ownCallsign = getOwnCallsignAsString(); @@ -536,7 +565,14 @@ namespace BlackCore void CFSDClient::sendTextMessage(TextMessageGroups receiverGroup, const QString &message) { if (message.isEmpty()) { return; } - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->sendTextMessage(receiverGroup, message); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { sendTextMessage(receiverGroup, message); } + }); + return; + } QString receiver; if (receiverGroup == TextMessageGroups::AllClients) { receiver = '*'; } @@ -578,7 +614,14 @@ namespace BlackCore void CFSDClient::sendFlightPlan(const CFlightPlan &flightPlan) { - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->sendFlightPlan(flightPlan); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { sendFlightPlan(flightPlan); } + }); + return; + } // Removed with T353 although it is standard // const QString route = QString(flightPlan.getRoute()).replace(" ", "."); @@ -628,7 +671,15 @@ namespace BlackCore void CFSDClient::sendPlaneInfoRequest(const CCallsign &receiver) { - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->sendPlaneInfoRequest(receiver); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { sendPlaneInfoRequest(receiver); } + }); + return; + } + const PlaneInfoRequest planeInfoRequest(getOwnCallsignAsString(), receiver.toQString()); sendQueudedMessage(planeInfoRequest); increaseStatisticsValue(QStringLiteral("sendPlaneInfoRequest")); @@ -636,7 +687,15 @@ namespace BlackCore void CFSDClient::sendPlaneInfoRequestFsinn(const CCallsign &callsign) { - if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->sendPlaneInfoRequestFsinn(callsign); }})) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + QMetaObject::invokeMethod(this, [ = ] + { + if (sApp && !sApp->isShuttingDown()) { sendPlaneInfoRequestFsinn(callsign); } + }); + return; + } + const bool connected = isConnected(); BLACK_VERIFY_X(connected, Q_FUNC_INFO, "Can't send to server when disconnected"); if (!connected) { return; } diff --git a/src/blackmisc/appstarttime.cpp b/src/blackmisc/appstarttime.cpp deleted file mode 100644 index 2748d4cc7..000000000 --- a/src/blackmisc/appstarttime.cpp +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (C) 2019 - * swift project Community / Contributors - * - * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level - * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, - * or distributed except according to the terms contained in the LICENSE file. - */ - -#include "blackmisc/appstarttime.h" - -namespace BlackMisc -{ - const QDateTime &getApplicationStartTimeUtc() - { - static const QDateTime gApplicationStartTimeUtc = QDateTime::currentDateTimeUtc(); - return gApplicationStartTimeUtc; - } -} diff --git a/src/blackmisc/appstarttime.h b/src/blackmisc/appstarttime.h deleted file mode 100644 index acbb421f3..000000000 --- a/src/blackmisc/appstarttime.h +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright (C) 2019 - * swift project Community / Contributors - * - * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level - * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, - * or distributed except according to the terms contained in the LICENSE file. - */ - -//! \file - -#ifndef BLACKMISC_APPSTARTTIME_H -#define BLACKMISC_APPSTARTTIME_H - -#include "blackmisc/blackmiscexport.h" - -#include - -namespace BlackMisc -{ - //! Get the application start time in UTC. - BLACKMISC_EXPORT const QDateTime &getApplicationStartTimeUtc(); -} - -#endif diff --git a/src/blackmisc/filelogger.cpp b/src/blackmisc/filelogger.cpp index 760e44277..44ba880bb 100644 --- a/src/blackmisc/filelogger.cpp +++ b/src/blackmisc/filelogger.cpp @@ -7,7 +7,6 @@ */ #include "blackmisc/filelogger.h" -#include "blackmisc/appstarttime.h" #include "blackmisc/loghandler.h" #include "blackmisc/directoryutils.h" #include "blackconfig/buildconfig.h" @@ -40,7 +39,7 @@ namespace BlackMisc { static const QString fileName = applicationName() % QLatin1String("_") % - getApplicationStartTimeUtc().toString(QStringLiteral("yyMMddhhmmss")) % + QDateTime::currentDateTimeUtc().toString(QStringLiteral("yyMMddhhmmss")) % QLatin1String("_") % QString::number(QCoreApplication::applicationPid()) % QLatin1String(".log"); diff --git a/src/blackmisc/threadutils.cpp b/src/blackmisc/threadutils.cpp index 0aa7b1615..4642e9212 100644 --- a/src/blackmisc/threadutils.cpp +++ b/src/blackmisc/threadutils.cpp @@ -24,16 +24,6 @@ namespace BlackMisc return QThread::currentThread() == toBeTested->thread(); } - bool CThreadUtils::isApplicationThreadObjectThread(const QObject *toBeTested) - { - return qApp && toBeTested->thread() == qApp->thread(); - } - - bool CThreadUtils::isApplicationThread(const QThread *toBeTested) - { - return qApp && toBeTested == qApp->thread(); - } - bool CThreadUtils::isCurrentThreadApplicationThread() { return qApp && QThread::currentThread() == qApp->thread(); @@ -47,13 +37,4 @@ namespace BlackMisc 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 callFunct) - { - if (!object) { return false; } - if (CThreadUtils::isCurrentThreadObjectThread(object)) { return false; } - - QMetaObject::invokeMethod(object, callFunct); - return true; - } } // ns diff --git a/src/blackmisc/threadutils.h b/src/blackmisc/threadutils.h index 64dbf7baa..1c8415574 100644 --- a/src/blackmisc/threadutils.h +++ b/src/blackmisc/threadutils.h @@ -29,20 +29,11 @@ namespace BlackMisc //! Is the current thread the object's thread? static bool isCurrentThreadObjectThread(const QObject *toBeTested); - //! Is the application thread the object's thread? - static bool isApplicationThreadObjectThread(const QObject *toBeTested); - - //! Is the application thread the object's thread? - static bool isApplicationThread(const QThread *toBeTested); - //! Is the current thread the application thread? static bool isCurrentThreadApplicationThread(); //! Info about current thread, for debug messages static QString currentThreadInfo(); - - //! Call in object's thread if not already in object's thread - static bool callInObjectThread(QObject *object, std::function callFunct); }; } // ns