Browse Source

Simplified Init with a recursive mutex

Paul-Louis Ageneau 5 years ago
parent
commit
822b2e6558
2 changed files with 20 additions and 25 deletions
  1. 1 2
      include/rtc/init.hpp
  2. 19 23
      src/init.cpp

+ 1 - 2
include/rtc/init.hpp

@@ -37,12 +37,11 @@ public:
 
 private:
 	Init();
-	static init_token Load(bool preloading);
 
 	static std::weak_ptr<void> Weak;
 	static std::shared_ptr<void> *Global;
 	static bool Initialized;
-	static std::mutex Mutex;
+	static std::recursive_mutex Mutex;
 };
 
 inline void Preload() { Init::Preload(); }

+ 19 - 23
src/init.cpp

@@ -94,20 +94,12 @@ 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); }
-
-init_token Init::Load(bool preloading) {
-	std::lock_guard lock(Mutex);
-	if (auto token = Weak.lock()) {
-		if (preloading) {
-			// if preloading, set Global
-			delete Global;
-			Global = new shared_ptr<void>(token);
-		}
+std::recursive_mutex Init::Mutex;
+
+init_token Init::Token() {
+	std::unique_lock lock(Mutex);
+	if (auto token = Weak.lock())
 		return token;
-	}
 
 	delete Global;
 	Global = new shared_ptr<void>(new Init());
@@ -116,31 +108,35 @@ init_token Init::Load(bool preloading) {
 }
 
 void Init::Preload() {
-	init_token token = Load(true);
+	std::unique_lock lock(Mutex);
+	auto token = Token();
+	if (!Global)
+		Global = new shared_ptr<void>(token);
+
 	PLOG_DEBUG << "Preloading certificate";
 	make_certificate().wait();
 }
 
 void Init::Cleanup() {
-	std::lock_guard lock(Mutex);
+	std::unique_lock lock(Mutex);
 	delete Global;
 	Global = nullptr;
 }
 
 Init::Init() {
 	// Mutex is locked by Token() here
-	if (!std::exchange(Initialized, true)) {
+	if (!std::exchange(Initialized, true))
 		doInit();
-	}
 }
 
 Init::~Init() {
-	// We need to lock Mutex ourselves
-	std::unique_lock lock(Mutex);
-	if (std::exchange(Initialized, false)) {
-		std::thread t([lock = std::move(lock)]() { doCleanup(); });
-		t.detach();
-	}
+	std::thread t([]() {
+		// We need to lock Mutex ourselves
+		std::unique_lock lock(Mutex);
+		if (std::exchange(Initialized, false))
+			doCleanup();
+	});
+	t.detach();
 }
 
 } // namespace rtc