From fbf63457c1d1927071b823143570a3104fce22f0 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 18 Apr 2020 02:39:04 +0200 Subject: [PATCH] Ref T786, make sure injectWeather is called in the correct thread --- src/plugins/simulator/fs9/fs9.pro | 2 +- src/plugins/simulator/fs9/simulatorfs9.cpp | 23 +++++++++++++++---- .../fsxcommon/simulatorfsxcommon.cpp | 17 ++++++++++++-- .../simulator/xplane/simulatorxplane.cpp | 22 ++++++++++++++---- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/plugins/simulator/fs9/fs9.pro b/src/plugins/simulator/fs9/fs9.pro index ba6a8a157..32f7ec6dd 100644 --- a/src/plugins/simulator/fs9/fs9.pro +++ b/src/plugins/simulator/fs9/fs9.pro @@ -6,7 +6,7 @@ TARGET = simulatorfs9 TEMPLATE = lib CONFIG += plugin shared -CONFIG += blackmisc blackcore blackgui +CONFIG += blackmisc blackcore blackgui blackconfig CONFIG += simulatorfscommon simulatorplugincommon DEPENDPATH += . $$SourceRoot/src diff --git a/src/plugins/simulator/fs9/simulatorfs9.cpp b/src/plugins/simulator/fs9/simulatorfs9.cpp index e2e96d3ed..457176ea1 100644 --- a/src/plugins/simulator/fs9/simulatorfs9.cpp +++ b/src/plugins/simulator/fs9/simulatorfs9.cpp @@ -19,6 +19,8 @@ #include "blackmisc/simulation/simulatorplugininfo.h" #include "blackmisc/logmessage.h" #include "blackmisc/propertyindexallclasses.h" +#include "blackmisc/verify.h" +#include "blackconfig/buildconfig.h" #include #include @@ -35,6 +37,7 @@ using namespace BlackMisc::Simulation::FsCommon; using namespace BlackMisc::Weather; using namespace BlackCore; using namespace BlackSimPlugin::FsCommon; +using namespace BlackConfig; namespace BlackSimPlugin { @@ -397,14 +400,13 @@ namespace BlackSimPlugin if (m_isWeatherActivated) { - const auto currentPosition = CCoordinateGeodetic { aircraftSituation.latitude(), aircraftSituation.longitude() }; if (CWeatherScenario::isRealWeatherScenario(m_weatherScenarioSettings.get())) { if (m_lastWeatherPosition.isNull() || - calculateGreatCircleDistance(m_lastWeatherPosition, currentPosition).value(CLengthUnit::mi()) > 20) + calculateGreatCircleDistance(m_lastWeatherPosition, aircraftSituation).value(CLengthUnit::mi()) > 20) { - m_lastWeatherPosition = currentPosition; - const auto weatherGrid = CWeatherGrid { { "GLOB", currentPosition } }; + m_lastWeatherPosition = aircraftSituation; + const auto weatherGrid = CWeatherGrid { { "GLOB", aircraftSituation } }; requestWeatherGrid(weatherGrid, { this, &CSimulatorFs9::injectWeatherGrid }); } } @@ -465,6 +467,19 @@ namespace BlackSimPlugin void CSimulatorFs9::injectWeatherGrid(const CWeatherGrid &weatherGrid) { + if (this->isShuttingDownOrDisconnected()) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + BLACK_VERIFY_X(!CBuildConfig::isLocalDeveloperDebugBuild(), Q_FUNC_INFO, "Wrong thread"); + QPointer myself(this); + QTimer::singleShot(0, this, [ = ] + { + if (!myself) { return; } + myself->injectWeatherGrid(weatherGrid); + }); + return; + } + if (!m_useFsuipc || !m_fsuipc) { return; } if (!m_fsuipc->isOpened()) { return; } if (weatherGrid.isEmpty()) diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index 5c6510801..27fa08a0b 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -685,7 +685,7 @@ namespace BlackSimPlugin aircraftSituation.setOnGround(dtb(simulatorOwnAircraft.simOnGround) ? CAircraftSituation::OnGround : CAircraftSituation::NotOnGround, CAircraftSituation::OutOnGroundOwnAircraft); const CAircraftLights lights(dtb(simulatorOwnAircraft.lightStrobe), dtb(simulatorOwnAircraft.lightLanding), dtb(simulatorOwnAircraft.lightTaxi), - dtb(simulatorOwnAircraft.lightBeacon), dtb(simulatorOwnAircraft.lightNav), dtb(simulatorOwnAircraft.lightLogo)); + dtb(simulatorOwnAircraft.lightBeacon), dtb(simulatorOwnAircraft.lightNav), dtb(simulatorOwnAircraft.lightLogo)); CAircraftEngineList engines; const QList helperList @@ -2393,8 +2393,21 @@ namespace BlackSimPlugin } } - void CSimulatorFsxCommon::injectWeatherGrid(const Weather::CWeatherGrid &weatherGrid) + void CSimulatorFsxCommon::injectWeatherGrid(const CWeatherGrid &weatherGrid) { + if (this->isShuttingDownOrDisconnected()) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + BLACK_VERIFY_X(!CBuildConfig::isLocalDeveloperDebugBuild(), Q_FUNC_INFO, "Wrong thread"); + QPointer myself(this); + QTimer::singleShot(0, this, [ = ] + { + if (!myself) { return; } + myself->injectWeatherGrid(weatherGrid); + }); + return; + } + // So far, there is only global weather auto glob = weatherGrid.frontOrDefault(); glob.setIdentifier("GLOB"); diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 277cfafad..c697a7754 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -364,14 +364,13 @@ namespace BlackSimPlugin if (m_isWeatherActivated) { - const auto currentPosition = CCoordinateGeodetic { situation.latitude(), situation.longitude() }; if (CWeatherScenario::isRealWeatherScenario(m_weatherScenarioSettings.get())) { if (m_lastWeatherPosition.isNull() || - calculateGreatCircleDistance(m_lastWeatherPosition, currentPosition).value(CLengthUnit::mi()) > 20) + calculateGreatCircleDistance(m_lastWeatherPosition, situation).value(CLengthUnit::mi()) > 20) { - m_lastWeatherPosition = currentPosition; - const auto weatherGrid = CWeatherGrid { { "GLOB", currentPosition } }; + m_lastWeatherPosition = situation; + const auto weatherGrid = CWeatherGrid { { "GLOB", situation } }; this->requestWeatherGrid(weatherGrid, { this, &CSimulatorXPlane::injectWeatherGrid }); } } @@ -896,9 +895,22 @@ namespace BlackSimPlugin return true; } - void CSimulatorXPlane::injectWeatherGrid(const Weather::CWeatherGrid &weatherGrid) + void CSimulatorXPlane::injectWeatherGrid(const CWeatherGrid &weatherGrid) { if (this->isShuttingDownOrDisconnected()) { return; } + if (!CThreadUtils::isCurrentThreadObjectThread(this)) + { + BLACK_VERIFY_X(!CBuildConfig::isLocalDeveloperDebugBuild(), Q_FUNC_INFO, "Wrong thread"); + QPointer myself(this); + QTimer::singleShot(0, this, [ = ] + { + if (!myself) { return; } + myself->injectWeatherGrid(weatherGrid); + }); + return; + } + + // XPlane weather off m_weatherProxy->setUseRealWeather(false); //! TODO: find the closest