From fddf4c99d714e47b780d66226495d5a6f653f827 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 6 Dec 2024 14:24:11 +0200 Subject: [PATCH] context in async factory --- include/spdlog/async.h | 14 +++++++------- include/spdlog/details/context.h | 1 - src/details/context.cpp | 5 ----- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/spdlog/async.h b/include/spdlog/async.h index db0dd98a..250f4cdc 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -21,6 +21,7 @@ #include "./async_logger.h" #include "./details/context.h" #include "./details/thread_pool.h" +#include "spdlog.h" namespace spdlog { @@ -35,15 +36,14 @@ template struct async_factory_impl { template static std::shared_ptr create(std::string logger_name, SinkArgs &&...args) { - auto ®istry_inst = details::context::instance(); - + auto context = spdlog::context(); // create global thread pool if not already exists - auto &mutex = registry_inst.tp_mutex(); + auto &mutex = context->tp_mutex(); std::lock_guard tp_lock(mutex); - auto tp = registry_inst.get_tp(); + auto tp = context->get_tp(); if (tp == nullptr) { tp = std::make_shared(details::default_async_q_size, 1U); - registry_inst.set_tp(tp); + context->set_tp(tp); } auto sink = std::make_shared(std::forward(args)...); @@ -71,7 +71,7 @@ inline void init_thread_pool(size_t q_size, std::function on_thread_start, std::function on_thread_stop) { auto tp = std::make_shared(q_size, thread_count, on_thread_start, on_thread_stop); - details::context::instance().set_tp(std::move(tp)); + spdlog::context()->set_tp(std::move(tp)); } inline void init_thread_pool(size_t q_size, size_t thread_count, std::function on_thread_start) { @@ -83,5 +83,5 @@ inline void init_thread_pool(size_t q_size, size_t thread_count) { } // get the global thread pool. -inline std::shared_ptr thread_pool() { return details::context::instance().get_tp(); } +inline std::shared_ptr thread_pool() { return spdlog::context()->get_tp(); } } // namespace spdlog diff --git a/include/spdlog/details/context.h b/include/spdlog/details/context.h index d6b1c7ef..ccf72e7f 100644 --- a/include/spdlog/details/context.h +++ b/include/spdlog/details/context.h @@ -23,7 +23,6 @@ class thread_pool; class SPDLOG_API context { public: - static context &instance(); context(); ~context(); context(const context &) = delete; diff --git a/src/details/context.cpp b/src/details/context.cpp index 5cd6a22b..83009353 100644 --- a/src/details/context.cpp +++ b/src/details/context.cpp @@ -69,10 +69,5 @@ void context::shutdown() { std::recursive_mutex &context::tp_mutex() { return tp_mutex_; } -context &context::instance() { - static context s_instance; - return s_instance; -} - } // namespace details } // namespace spdlog