From 126d58d88f2a4b9c94fb05814c09e04d8d7843de Mon Sep 17 00:00:00 2001 From: Mamadou Babaei Date: Tue, 3 Jun 2025 07:14:37 +0200 Subject: [PATCH] thread-safe sgbackend initialization --- Handbook/src/appendix/changelog.md | 2 ++ .../Private/SGBackend/SGBackend.cpp | 20 +++++++++++++++---- .../Public/SGBackend/SGBackend.h | 9 +-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Handbook/src/appendix/changelog.md b/Handbook/src/appendix/changelog.md index e721838f..3cf8d41a 100644 --- a/Handbook/src/appendix/changelog.md +++ b/Handbook/src/appendix/changelog.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - SenseGlove libraries have migrated to `C++20` from `C++17`. - Revamped the `SGLog` logging utility class to use `TUniquePtr` instead of `std::unique_ptr`. - `SGLog` now relies on `TAtomic` for thread-safe initialization. +- `SGBackend` now relies on `TAtomic` for thread-safe initialization. +- `USGBackend::IsBackendInitialized()` is no longer inlined and the initialization flag has been moved to the private implementation of `USGBackend`. - Bumped the SenseGlove Unreal Engine Marketplace Packager `v0.6.0-4108c6f`. ### Fixed diff --git a/Source/SenseGloveBackend/Private/SGBackend/SGBackend.cpp b/Source/SenseGloveBackend/Private/SGBackend/SGBackend.cpp index f773e095..c5df5116 100644 --- a/Source/SenseGloveBackend/Private/SGBackend/SGBackend.cpp +++ b/Source/SenseGloveBackend/Private/SGBackend/SGBackend.cpp @@ -36,6 +36,7 @@ #include "SGBackend/SGBackend.h" #include "Engine/Engine.h" +#include "Templates/Atomic.h" #if PLATFORM_ANDROID #include "SGConnect/SGConnect.h" @@ -47,6 +48,12 @@ struct USGBackend::FImpl { + /************************ + * Member variables + ************************/ + + TAtomic bInitialized; + /************************ * Owner object ************************/ @@ -82,7 +89,11 @@ USGBackend* USGBackend::GetInstance() USGBackend::USGBackend() : Pimpl(TUniquePtr(new FImpl{this}, PimplDeleter)) { - bBackendInitialized = false; +} + +bool USGBackend::IsBackendInitialized() const +{ + return Pimpl->bInitialized; } bool USGBackend::InitializeBackend() @@ -130,7 +141,7 @@ bool USGBackend::InitializeBackend() FSGDeviceList::Dispose(); FSGDeviceList::Reinitialize(); - bBackendInitialized = true; + Pimpl->bInitialized = true; return true; } @@ -181,7 +192,7 @@ bool USGBackend::UninitializeBackend() } #endif /* PLATFORM_ANDROID */ - bBackendInitialized = false; + Pimpl->bInitialized = false; return true; } @@ -235,7 +246,8 @@ void USGBackend::Deinitialize() } USGBackend::FImpl::FImpl(USGBackend* InOwner) - : Owner(InOwner) + : bInitialized(false), + Owner(InOwner) { } diff --git a/Source/SenseGloveBackend/Public/SGBackend/SGBackend.h b/Source/SenseGloveBackend/Public/SGBackend/SGBackend.h index f918c15c..b165f2db 100644 --- a/Source/SenseGloveBackend/Public/SGBackend/SGBackend.h +++ b/Source/SenseGloveBackend/Public/SGBackend/SGBackend.h @@ -49,10 +49,6 @@ class SENSEGLOVEBACKEND_API USGBackend : public UEngineSubsystem { GENERATED_BODY() -private: - UPROPERTY(Transient) - bool bBackendInitialized; - public: static USGBackend* GetInstance(); @@ -71,10 +67,7 @@ private: USGBackend(); public: - FORCEINLINE bool IsBackendInitialized() const - { - return bBackendInitialized; - } + bool IsBackendInitialized() const; bool InitializeBackend(); bool UninitializeBackend();