diff --git a/NetworkServer.h b/NetworkServer.h index 7db5bc12..3f21609f 100644 --- a/NetworkServer.h +++ b/NetworkServer.h @@ -99,8 +99,8 @@ public: protected: std::string host; unsigned short port_num; - bool server_online; - bool server_listening; + std::atomic server_online; + std::atomic server_listening; std::vector& controllers; diff --git a/ResourceManager.cpp b/ResourceManager.cpp index e6a104fc..cca7d899 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -86,6 +86,7 @@ ResourceManager::ResourceManager() InitThread = nullptr; DetectDevicesThread = nullptr; dynamic_detectors_processed = false; + init_finished = false; SetupConfigurationDirectory(); @@ -1632,6 +1633,8 @@ void ResourceManager::InitThreadFunction() { cli_post_detection(); } + + init_finished = true; } void ResourceManager::UpdateDetectorSettings() @@ -1735,6 +1738,19 @@ void ResourceManager::UpdateDetectorSettings() } } +void ResourceManager::WaitForInitialization() +{ + /*-------------------------------------------------*\ + | A reliable sychronization of this kind is | + | impossible without the use of a `barrier` | + | implementation, which is only introduced in C++20 | + \*-------------------------------------------------*/ + while (!init_finished) + { + std::this_thread::sleep_for(1ms); + }; +} + void ResourceManager::WaitForDeviceDetection() { DetectDeviceMutex.lock(); diff --git a/ResourceManager.h b/ResourceManager.h index 970fcc5f..56e69c79 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -208,6 +208,7 @@ public: void StopDeviceDetection(); + void WaitForInitialization(); void WaitForDeviceDetection(); private: @@ -243,6 +244,10 @@ private: | Auto connection permitting flag | \*-------------------------------------------------------------------------------------*/ bool apply_post_options; + /*-------------------------------------------------------------------------------------*\ + | Initialization completion flag | + \*-------------------------------------------------------------------------------------*/ + std::atomic init_finished; /*-------------------------------------------------------------------------------------*\ | Profile Manager | diff --git a/main.cpp b/main.cpp index 36d2b31f..b72ef2da 100644 --- a/main.cpp +++ b/main.cpp @@ -253,7 +253,14 @@ int main(int argc, char* argv[]) { if(ret_flags & RET_FLAG_START_SERVER) { - if(!ResourceManager::get()->GetServer()->GetOnline()) + NetworkServer* server = ResourceManager::get()->GetServer(); + ResourceManager::get()->WaitForInitialization(); + + /*---------------------------------------------------------*\ + | The server is only started after detection finishes and | + | it takes some time to get the server online - we wait | + \*---------------------------------------------------------*/ + if(!server->GetOnline()) { #ifdef _MACOSX_X86_X64 CloseMacUSPCIODriver(); @@ -262,7 +269,7 @@ int main(int argc, char* argv[]) } else { - WaitWhileServerOnline(ResourceManager::get()->GetServer()); + WaitWhileServerOnline(server); #ifdef _MACOSX_X86_X64 CloseMacUSPCIODriver(); #endif