code: Migrate util::threadpool to new dynamic loader

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-05-14 06:50:27 +02:00 committed by Xaymar
parent 49f763aa44
commit 5ed2010015
4 changed files with 30 additions and 9 deletions

View file

@ -81,7 +81,6 @@
#include <stdexcept> #include <stdexcept>
#include "warning-enable.hpp" #include "warning-enable.hpp"
static std::shared_ptr<streamfx::util::threadpool::threadpool> _threadpool;
static std::shared_ptr<streamfx::gfx::opengl> _streamfx_gfx_opengl; static std::shared_ptr<streamfx::gfx::opengl> _streamfx_gfx_opengl;
static std::shared_ptr<streamfx::obs::source_tracker> _source_tracker; static std::shared_ptr<streamfx::obs::source_tracker> _source_tracker;
@ -142,9 +141,6 @@ MODULE_EXPORT bool obs_module_load(void)
// Initialize global configuration. // Initialize global configuration.
streamfx::configuration::initialize(); streamfx::configuration::initialize();
// Initialize global Thread Pool.
_threadpool = std::make_shared<streamfx::util::threadpool::threadpool>();
// Initialize Source Tracker // Initialize Source Tracker
_source_tracker = streamfx::obs::source_tracker::get(); _source_tracker = streamfx::obs::source_tracker::get();
@ -338,9 +334,6 @@ MODULE_EXPORT void obs_module_unload(void)
// Finalize Configuration // Finalize Configuration
streamfx::configuration::finalize(); streamfx::configuration::finalize();
// Finalize Thread Pool
_threadpool.reset();
// Run all finalizers. // Run all finalizers.
for (auto kv : streamfx::get_finalizers()) { for (auto kv : streamfx::get_finalizers()) {
for (auto init : kv.second) { for (auto init : kv.second) {
@ -364,7 +357,7 @@ MODULE_EXPORT void obs_module_unload(void)
std::shared_ptr<streamfx::util::threadpool::threadpool> streamfx::threadpool() std::shared_ptr<streamfx::util::threadpool::threadpool> streamfx::threadpool()
{ {
return _threadpool; return streamfx::util::threadpool::threadpool::instance();
} }
std::filesystem::path streamfx::data_file_path(std::string_view file) std::filesystem::path streamfx::data_file_path(std::string_view file)

View file

@ -6,7 +6,6 @@
#include "common.hpp" #include "common.hpp"
#include "warning-disable.hpp" #include "warning-disable.hpp"
#include <cstdint>
#include <functional> #include <functional>
#include "warning-enable.hpp" #include "warning-enable.hpp"

View file

@ -6,6 +6,7 @@
#include "util-threadpool.hpp" #include "util-threadpool.hpp"
#include "common.hpp" #include "common.hpp"
#include "plugin.hpp"
#include "util/util-logging.hpp" #include "util/util-logging.hpp"
#include "warning-disable.hpp" #include "warning-disable.hpp"
@ -235,3 +236,28 @@ void streamfx::util::threadpool::threadpool::work(std::shared_ptr<worker_info> w
} }
} }
} }
std::shared_ptr<streamfx::util::threadpool::threadpool> streamfx::util::threadpool::threadpool::instance()
{
static std::weak_ptr<streamfx::util::threadpool::threadpool> winst;
static std::mutex mtx;
std::unique_lock<decltype(mtx)> lock(mtx);
auto instance = winst.lock();
if (!instance) {
instance = std::shared_ptr<streamfx::util::threadpool::threadpool>(new streamfx::util::threadpool::threadpool());
winst = instance;
}
return instance;
};
static std::shared_ptr<streamfx::util::threadpool::threadpool> loader_instance;
static auto loader = streamfx::loader(
[]() { // Initalizer
loader_instance = streamfx::util::threadpool::threadpool::instance();
},
[]() { // Finalizer
loader_instance.reset();
},
streamfx::loader_priority::HIGHEST);

View file

@ -132,5 +132,8 @@ namespace streamfx::util::threadpool {
private: private:
void work(std::shared_ptr<worker_info>); void work(std::shared_ptr<worker_info>);
public /* Singleton */:
static std::shared_ptr<streamfx::util::threadpool::threadpool> instance();
}; };
} // namespace streamfx::util::threadpool } // namespace streamfx::util::threadpool