diff --git a/src/ll/api/coro/CoroPromise.h b/src/ll/api/coro/CoroPromise.h index 92b18cf807..0465d3f23c 100644 --- a/src/ll/api/coro/CoroPromise.h +++ b/src/ll/api/coro/CoroPromise.h @@ -39,7 +39,7 @@ struct CoroPromiseBase { return std::forward(awaitable); } - constexpr ForwardAwaiter await_transform(CurrentExecutor) { return {exec.get()}; } + constexpr ForwardAwaiter await_transform(CurrentExecutor) { return {exec.value()}; } constexpr YieldAwaiter await_transform(Yield) { return {exec}; } diff --git a/src/ll/api/coro/Executor.cpp b/src/ll/api/coro/Executor.cpp deleted file mode 100644 index 6a27feaaf5..0000000000 --- a/src/ll/api/coro/Executor.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "ll/api/coro/Executor.h" -#include "ll/api/coro/CoroTask.h" - -namespace ll::coro { - -struct Executor::Impl { - std::string name; -}; - -Executor::~Executor() = default; - -Executor::Executor(std::string_view name) : impl(std::make_unique(std::string{name})) {} - -std::string const& Executor::getName() const { return impl->name; } - -} // namespace ll::coro diff --git a/src/ll/api/coro/Executor.h b/src/ll/api/coro/Executor.h index 8f79ebf5c0..a3e54b03e0 100644 --- a/src/ll/api/coro/Executor.h +++ b/src/ll/api/coro/Executor.h @@ -14,19 +14,18 @@ #include "mc/common/wrapper/optional_ref.h" namespace ll::coro { -class Executor : public std::enable_shared_from_this { - struct Impl; - std::unique_ptr impl; +class Executor { + std::string name; public: using Clock = std::chrono::steady_clock; using Duration = Clock::duration; - LLNDAPI Executor(std::string_view name); + Executor(std::string name) : name(std::move(name)) {} - LLNDAPI std::string const& getName() const; + std::string const& getName() const { return name; } - LLAPI virtual ~Executor(); + virtual ~Executor() = default; virtual void execute(std::function) const = 0; diff --git a/src/ll/api/io/Logger.cpp b/src/ll/api/io/Logger.cpp index 28f5623b50..222388b7da 100644 --- a/src/ll/api/io/Logger.cpp +++ b/src/ll/api/io/Logger.cpp @@ -26,10 +26,9 @@ static void printLogError(std::string_view msg) noexcept try { } catch (...) {} } -static std::shared_ptr const& getLogPool() { - static std::shared_ptr p = - std::make_shared("logger", 1); // logger need keep some order - return p; +static thread::ThreadPoolExecutor& getLogPool() { + static thread::ThreadPoolExecutor ins("logger", 1); // logger need keep some order + return ins; } struct Logger::Impl { @@ -38,11 +37,9 @@ struct Logger::Impl { std::shared_ptr>> sinks; - std::shared_ptr pool; + thread::ThreadPoolExecutor& pool; - Impl(std::string_view title, std::shared_ptr pool) - : title(title), - pool(std::move(pool)) {} + Impl(std::string_view title, thread::ThreadPoolExecutor& pool) : title(title), pool(pool) {} }; Logger::~Logger() = default; @@ -65,8 +62,8 @@ void Logger::printStr(LogLevel level, std::string&& msg) const noexcept try { if (level > impl->level) { return; } - impl->pool->execute([sinks = impl->sinks, - msg = LogMessage{std::move(msg), impl->title, level, sys_utils::getLocalTime()}] { + impl->pool.execute([sinks = impl->sinks, + msg = LogMessage{std::move(msg), impl->title, level, sys_utils::getLocalTime()}] { try { for (auto& sink : *sinks) { sink->append(msg); diff --git a/src/ll/api/thread/InplaceExecutor.cpp b/src/ll/api/thread/InplaceExecutor.cpp index 73cb1426f1..98529d4ee5 100644 --- a/src/ll/api/thread/InplaceExecutor.cpp +++ b/src/ll/api/thread/InplaceExecutor.cpp @@ -4,7 +4,7 @@ namespace ll::thread { -InplaceExecutor::InplaceExecutor(std::string_view name) : Executor(name) {} +InplaceExecutor::InplaceExecutor(std::string name) : Executor(std::move(name)) {} InplaceExecutor::~InplaceExecutor() = default; @@ -17,7 +17,7 @@ std::shared_ptr InplaceExecutor::executeAfter(std::fu } InplaceExecutor const& InplaceExecutor::getDefault() { - static std::shared_ptr p = std::make_shared("default"); - return *p; + static InplaceExecutor ins("default_inplace"); + return ins; } } // namespace ll::thread diff --git a/src/ll/api/thread/InplaceExecutor.h b/src/ll/api/thread/InplaceExecutor.h index 6627efb9df..39f8cd9e66 100644 --- a/src/ll/api/thread/InplaceExecutor.h +++ b/src/ll/api/thread/InplaceExecutor.h @@ -5,7 +5,7 @@ namespace ll::thread { class InplaceExecutor final : public coro::Executor { public: - LLAPI InplaceExecutor(std::string_view name); + LLAPI InplaceExecutor(std::string name); LLAPI ~InplaceExecutor() override; diff --git a/src/ll/api/thread/ServerThreadExecutor.cpp b/src/ll/api/thread/ServerThreadExecutor.cpp index c2d8e090de..0f648a6fc4 100644 --- a/src/ll/api/thread/ServerThreadExecutor.cpp +++ b/src/ll/api/thread/ServerThreadExecutor.cpp @@ -16,7 +16,7 @@ namespace ll::thread { struct ServerThreadExecutor::Impl { struct ScheduledWork { - uint64 time; + uint64 time; std::shared_ptr callback; }; struct SwCmp { @@ -59,8 +59,8 @@ struct ServerThreadExecutor::Impl { std::shared_ptr shared; event::ListenerPtr worker; }; -ServerThreadExecutor::ServerThreadExecutor(std::string_view name, Duration maxOnceDuration, size_t checkPack) -: Executor(name), +ServerThreadExecutor::ServerThreadExecutor(std::string name, Duration maxOnceDuration, size_t checkPack) +: Executor(std::move(name)), impl(std::make_unique(std::make_shared())) { impl->worker = event::EventBus::getInstance().emplaceListener( [name = getName(), maxOnceDuration, checkPack, weak = std::weak_ptr{impl->shared}](auto&&) { @@ -104,8 +104,7 @@ ServerThreadExecutor::executeAfter(std::function f, Duration dur) const } ServerThreadExecutor const& ServerThreadExecutor::getDefault() { - static std::shared_ptr p = - std::make_shared("default", std::chrono::milliseconds{30}, 16); - return *p; + static ServerThreadExecutor ins("default_server_thread", std::chrono::milliseconds{30}, 16); + return ins; } } // namespace ll::thread diff --git a/src/ll/api/thread/ServerThreadExecutor.h b/src/ll/api/thread/ServerThreadExecutor.h index 5ac46b9e67..73286e247d 100644 --- a/src/ll/api/thread/ServerThreadExecutor.h +++ b/src/ll/api/thread/ServerThreadExecutor.h @@ -8,7 +8,7 @@ class ServerThreadExecutor final : public coro::Executor { std::unique_ptr impl; public: - LLAPI ServerThreadExecutor(std::string_view name, Duration maxOnceDuration, size_t checkPack); + LLAPI ServerThreadExecutor(std::string name, Duration maxOnceDuration, size_t checkPack); LLAPI ~ServerThreadExecutor() override; diff --git a/src/ll/api/thread/ThreadPoolExecutor.cpp b/src/ll/api/thread/ThreadPoolExecutor.cpp index 393d6bff68..b7376d3786 100644 --- a/src/ll/api/thread/ThreadPoolExecutor.cpp +++ b/src/ll/api/thread/ThreadPoolExecutor.cpp @@ -17,7 +17,7 @@ namespace ll::thread { struct ThreadPoolExecutor::Impl { struct ScheduledWork { - Clock::time_point time; + Clock::time_point time; std::shared_ptr callback; }; struct SwCmp { @@ -112,8 +112,8 @@ struct ThreadPoolExecutor::Impl { }; ThreadPoolExecutor::~ThreadPoolExecutor() = default; -ThreadPoolExecutor::ThreadPoolExecutor(std::string_view name, size_t nThreads) -: Executor(name), +ThreadPoolExecutor::ThreadPoolExecutor(std::string name, size_t nThreads) +: Executor(std::move(name)), impl(std::make_unique(*this, nThreads)) {} void ThreadPoolExecutor::resize(size_t nThreads) { impl = std::make_unique(*this, nThreads); } @@ -138,8 +138,7 @@ ThreadPoolExecutor::executeAfter(std::function f, Duration dur) const { } } ThreadPoolExecutor const& ThreadPoolExecutor::getDefault() { - static std::shared_ptr p = - std::make_shared("default", std::max((int)std::thread::hardware_concurrency() - 2, 2)); - return *p; + static ThreadPoolExecutor ins("default_thread_pool", std::max((int)std::thread::hardware_concurrency() - 2, 2)); + return ins; } } // namespace ll::thread diff --git a/src/ll/api/thread/ThreadPoolExecutor.h b/src/ll/api/thread/ThreadPoolExecutor.h index a132aabfad..581b6a2903 100644 --- a/src/ll/api/thread/ThreadPoolExecutor.h +++ b/src/ll/api/thread/ThreadPoolExecutor.h @@ -8,7 +8,7 @@ class ThreadPoolExecutor final : public coro::Executor { std::unique_ptr impl; public: - LLAPI explicit ThreadPoolExecutor(std::string_view name, size_t nThreads = 1); + LLAPI explicit ThreadPoolExecutor(std::string name, size_t nThreads = 1); LLAPI ~ThreadPoolExecutor() override;