Browse Source

Do not make ThreadPool tasks borrow init tokens

Paul-Louis Ageneau 2 years ago
parent
commit
36a0c035da
4 changed files with 12 additions and 1 deletions
  1. 3 0
      src/impl/certificate.hpp
  2. 1 0
      src/impl/init.cpp
  3. 6 0
      src/impl/threadpool.cpp
  4. 2 1
      src/impl/threadpool.hpp

+ 3 - 0
src/impl/certificate.hpp

@@ -21,6 +21,7 @@
 
 #include "common.hpp"
 #include "configuration.hpp" // for CertificateType
+#include "init.hpp"
 #include "tls.hpp"
 
 #include <future>
@@ -46,6 +47,8 @@ public:
 	string fingerprint() const;
 
 private:
+	const init_token mInitToken = Init::Instance().token();
+
 #if USE_GNUTLS
 	Certificate(shared_ptr<gnutls_certificate_credentials_t> creds);
 	const shared_ptr<gnutls_certificate_credentials_t> mCredentials;

+ 1 - 0
src/impl/init.cpp

@@ -158,6 +158,7 @@ void Init::doCleanup() {
 	PLOG_DEBUG << "Global cleanup";
 
 	ThreadPool::Instance().join();
+	ThreadPool::Instance().clear();
 #if RTC_ENABLE_WEBSOCKET
 	PollService::Instance().join();
 #endif

+ 6 - 0
src/impl/threadpool.cpp

@@ -57,6 +57,12 @@ void ThreadPool::join() {
 	mJoining = false;
 }
 
+void ThreadPool::clear() {
+	std::unique_lock lock(mMutex);
+	while (!mTasks.empty())
+		mTasks.pop();
+}
+
 void ThreadPool::run() {
 	++mBusyWorkers;
 	scope_guard guard([&]() { --mBusyWorkers; });

+ 2 - 1
src/impl/threadpool.hpp

@@ -54,6 +54,7 @@ public:
 	int count() const;
 	void spawn(int count = 1);
 	void join();
+	void clear();
 	void run();
 	bool runOne();
 
@@ -115,7 +116,7 @@ auto ThreadPool::schedule(clock::time_point time, F &&f, Args &&...args)
 	});
 	std::future<R> result = task->get_future();
 
-	mTasks.push({time, [task = std::move(task), token = Init::Instance().token()]() { return (*task)(); }});
+	mTasks.push({time, [task = std::move(task)]() { return (*task)(); }});
 	mTasksCondition.notify_one();
 	return result;
 }