Issue #77 Refactor to inline functions

This commit is contained in:
Mat Sutcliffe
2020-08-24 00:50:28 +01:00
parent ac454ebf3a
commit 92edbb90c2
9 changed files with 73 additions and 85 deletions

View File

@@ -220,7 +220,7 @@ namespace BlackCore
void CAirspaceAnalyzer::analyzeAirspace() void CAirspaceAnalyzer::analyzeAirspace()
{ {
Q_ASSERT_X(!CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "Expect to run in background thread"); 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; bool restricted, enabled;
int maxAircraft; int maxAircraft;

View File

@@ -1809,7 +1809,7 @@ namespace BlackCore
if (!this->isNetworkAccessible()) { return nullptr; } if (!this->isNetworkAccessible()) { return nullptr; }
QWriteLocker locker(&m_accessManagerLock); 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)) if (!CThreadUtils::isCurrentThreadObjectThread(m_accessManager))
{ {
this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest); this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest);

View File

@@ -195,10 +195,10 @@ namespace BlackCore
const CVoiceSetup vs = m_voiceSettings.getThreadLocal(); const CVoiceSetup vs = m_voiceSettings.getThreadLocal();
m_voiceClient->updateVoiceServerUrl(vs.getAfvVoiceServerUrl()); 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 m_voiceClient->start(); // thread
Q_ASSERT_X(m_voiceClient->owner() == this, Q_FUNC_INFO, "Wrong owner"); 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::outputVolumePeakVU, this, &CContextAudioBase::outputVolumePeakVU, Qt::QueuedConnection);
// connect(m_voiceClient, &CAfvClient::inputVolumePeakVU, this, &CContextAudioBase::inputVolumePeakVU, Qt::QueuedConnection); // connect(m_voiceClient, &CAfvClient::inputVolumePeakVU, this, &CContextAudioBase::inputVolumePeakVU, Qt::QueuedConnection);

View File

@@ -225,7 +225,15 @@ namespace BlackCore
void CFSDClient::connectToServer() 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; } if (m_socket.isOpen()) { return; }
Q_ASSERT(!m_clientName.isEmpty()); Q_ASSERT(!m_clientName.isEmpty());
Q_ASSERT((m_versionMajor + m_versionMinor) > 0); Q_ASSERT((m_versionMajor + m_versionMinor) > 0);
@@ -257,8 +265,15 @@ namespace BlackCore
void CFSDClient::disconnectFromServer() void CFSDClient::disconnectFromServer()
{ {
if (this->isDisconnected()) { return; } if (!CThreadUtils::isCurrentThreadObjectThread(this))
if (CThreadUtils::callInObjectThread(this, [ = ] { if (sApp && !sApp->isShuttingDown()) { this->disconnectFromServer(); }})) { return; } {
QMetaObject::invokeMethod(this, [ = ]
{
if (sApp && !sApp->isShuttingDown()) { disconnectFromServer(); }
});
return;
}
this->stopPositionTimers(); this->stopPositionTimers();
this->updateConnectionStatus(CConnectionStatus::Disconnecting); this->updateConnectionStatus(CConnectionStatus::Disconnecting);
@@ -426,7 +441,14 @@ namespace BlackCore
void CFSDClient::sendClientQuery(ClientQueryType queryType, const CCallsign &receiver, const QStringList &queryData) void CFSDClient::sendClientQuery(ClientQueryType queryType, const CCallsign &receiver, const QStringList &queryData)
{ {
if (queryType == ClientQueryType::Unknown) { return; } 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(); const QString reveiverCallsign = receiver.getFsdCallsignString();
if (queryType == ClientQueryType::IsValidATC) if (queryType == ClientQueryType::IsValidATC)
@@ -492,7 +514,14 @@ namespace BlackCore
void CFSDClient::sendTextMessages(const CTextMessageList &messages) void CFSDClient::sendTextMessages(const CTextMessageList &messages)
{ {
if (messages.isEmpty()) { return; } 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 CTextMessageList privateMessages = messages.getPrivateMessages().markedAsSent();
const QString ownCallsign = getOwnCallsignAsString(); const QString ownCallsign = getOwnCallsignAsString();
@@ -536,7 +565,14 @@ namespace BlackCore
void CFSDClient::sendTextMessage(TextMessageGroups receiverGroup, const QString &message) void CFSDClient::sendTextMessage(TextMessageGroups receiverGroup, const QString &message)
{ {
if (message.isEmpty()) { return; } 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; QString receiver;
if (receiverGroup == TextMessageGroups::AllClients) { receiver = '*'; } if (receiverGroup == TextMessageGroups::AllClients) { receiver = '*'; }
@@ -578,7 +614,14 @@ namespace BlackCore
void CFSDClient::sendFlightPlan(const CFlightPlan &flightPlan) 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 // Removed with T353 although it is standard
// const QString route = QString(flightPlan.getRoute()).replace(" ", "."); // const QString route = QString(flightPlan.getRoute()).replace(" ", ".");
@@ -628,7 +671,15 @@ namespace BlackCore
void CFSDClient::sendPlaneInfoRequest(const CCallsign &receiver) 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()); const PlaneInfoRequest planeInfoRequest(getOwnCallsignAsString(), receiver.toQString());
sendQueudedMessage(planeInfoRequest); sendQueudedMessage(planeInfoRequest);
increaseStatisticsValue(QStringLiteral("sendPlaneInfoRequest")); increaseStatisticsValue(QStringLiteral("sendPlaneInfoRequest"));
@@ -636,7 +687,15 @@ namespace BlackCore
void CFSDClient::sendPlaneInfoRequestFsinn(const CCallsign &callsign) 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(); const bool connected = isConnected();
BLACK_VERIFY_X(connected, Q_FUNC_INFO, "Can't send to server when disconnected"); BLACK_VERIFY_X(connected, Q_FUNC_INFO, "Can't send to server when disconnected");
if (!connected) { return; } if (!connected) { return; }

View File

@@ -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;
}
}

View File

@@ -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 <QDateTime>
namespace BlackMisc
{
//! Get the application start time in UTC.
BLACKMISC_EXPORT const QDateTime &getApplicationStartTimeUtc();
}
#endif

View File

@@ -7,7 +7,6 @@
*/ */
#include "blackmisc/filelogger.h" #include "blackmisc/filelogger.h"
#include "blackmisc/appstarttime.h"
#include "blackmisc/loghandler.h" #include "blackmisc/loghandler.h"
#include "blackmisc/directoryutils.h" #include "blackmisc/directoryutils.h"
#include "blackconfig/buildconfig.h" #include "blackconfig/buildconfig.h"
@@ -40,7 +39,7 @@ namespace BlackMisc
{ {
static const QString fileName = applicationName() % static const QString fileName = applicationName() %
QLatin1String("_") % QLatin1String("_") %
getApplicationStartTimeUtc().toString(QStringLiteral("yyMMddhhmmss")) % QDateTime::currentDateTimeUtc().toString(QStringLiteral("yyMMddhhmmss")) %
QLatin1String("_") % QLatin1String("_") %
QString::number(QCoreApplication::applicationPid()) % QString::number(QCoreApplication::applicationPid()) %
QLatin1String(".log"); QLatin1String(".log");

View File

@@ -24,16 +24,6 @@ namespace BlackMisc
return QThread::currentThread() == toBeTested->thread(); 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() bool CThreadUtils::isCurrentThreadApplicationThread()
{ {
return qApp && QThread::currentThread() == qApp->thread(); return qApp && QThread::currentThread() == qApp->thread();
@@ -47,13 +37,4 @@ namespace BlackMisc
const QString id = QString::fromStdString(oss.str()); const QString id = QString::fromStdString(oss.str());
return QStringLiteral("%1 (%2) prio %3").arg(id).arg(thread->objectName()).arg(thread->priority()); return QStringLiteral("%1 (%2) prio %3").arg(id).arg(thread->objectName()).arg(thread->priority());
} }
bool CThreadUtils::callInObjectThread(QObject *object, std::function<void()> callFunct)
{
if (!object) { return false; }
if (CThreadUtils::isCurrentThreadObjectThread(object)) { return false; }
QMetaObject::invokeMethod(object, callFunct);
return true;
}
} // ns } // ns

View File

@@ -29,20 +29,11 @@ namespace BlackMisc
//! Is the current thread the object's thread? //! Is the current thread the object's thread?
static bool isCurrentThreadObjectThread(const QObject *toBeTested); 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? //! Is the current thread the application thread?
static bool isCurrentThreadApplicationThread(); static bool isCurrentThreadApplicationThread();
//! Info about current thread, for debug messages //! Info about current thread, for debug messages
static QString currentThreadInfo(); static QString currentThreadInfo();
//! Call in object's thread if not already in object's thread
static bool callInObjectThread(QObject *object, std::function<void()> callFunct);
}; };
} // ns } // ns