Browse Source

Added initialized flag

Paul-Louis Ageneau 5 years ago
parent
commit
5b251af1d7
2 changed files with 9 additions and 3 deletions
  1. 1 0
      include/rtc/init.hpp
  2. 8 3
      src/init.cpp

+ 1 - 0
include/rtc/init.hpp

@@ -41,6 +41,7 @@ private:
 
 	static std::weak_ptr<void> Weak;
 	static std::shared_ptr<void> *Global;
+	static bool Initialized;
 	static std::mutex Mutex;
 };
 

+ 8 - 3
src/init.cpp

@@ -93,6 +93,7 @@ void doCleanup() {
 
 std::weak_ptr<void> Init::Weak;
 std::shared_ptr<void> *Init::Global = nullptr;
+bool Init::Initialized = false;
 std::mutex Init::Mutex;
 
 init_token Init::Token() { return Load(false); }
@@ -128,14 +129,18 @@ void Init::Cleanup() {
 
 Init::Init() {
 	// Mutex is locked by Token() here
-	doInit();
+	if (!std::exchange(Initialized, true)) {
+		doInit();
+	}
 }
 
 Init::~Init() {
 	// We need to lock Mutex ourselves
 	std::unique_lock lock(Mutex);
-	std::thread t([lock = std::move(lock)]() { doCleanup(); });
-	t.detach();
+	if (std::exchange(Initialized, false)) {
+		std::thread t([lock = std::move(lock)]() { doCleanup(); });
+		t.detach();
+	}
 }
 
 } // namespace rtc