Skip to content

Commit

Permalink
refactor: make Executor header only
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Sep 12, 2024
1 parent bb0b0bd commit b47e09a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/ll/api/coro/CoroPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct CoroPromiseBase {
return std::forward<T>(awaitable);
}

constexpr ForwardAwaiter<NonNullExecutorRef> await_transform(CurrentExecutor) { return {exec.get()}; }
constexpr ForwardAwaiter<NonNullExecutorRef> await_transform(CurrentExecutor) { return {exec.value()}; }

constexpr YieldAwaiter await_transform(Yield) { return {exec}; }

Expand Down
16 changes: 0 additions & 16 deletions src/ll/api/coro/Executor.cpp

This file was deleted.

11 changes: 5 additions & 6 deletions src/ll/api/coro/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
#include "mc/common/wrapper/optional_ref.h"

namespace ll::coro {
class Executor : public std::enable_shared_from_this<Executor> {
struct Impl;
std::unique_ptr<Impl> 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<void()>) const = 0;

Expand Down
17 changes: 7 additions & 10 deletions src/ll/api/io/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ static void printLogError(std::string_view msg) noexcept try {
} catch (...) {}
}

static std::shared_ptr<thread::ThreadPoolExecutor> const& getLogPool() {
static std::shared_ptr<thread::ThreadPoolExecutor> p =
std::make_shared<thread::ThreadPoolExecutor>("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 {
Expand All @@ -38,11 +37,9 @@ struct Logger::Impl {

std::shared_ptr<std::vector<std::shared_ptr<SinkBase>>> sinks;

std::shared_ptr<thread::ThreadPoolExecutor> pool;
thread::ThreadPoolExecutor& pool;

Impl(std::string_view title, std::shared_ptr<thread::ThreadPoolExecutor> pool)
: title(title),
pool(std::move(pool)) {}
Impl(std::string_view title, thread::ThreadPoolExecutor& pool) : title(title), pool(pool) {}
};
Logger::~Logger() = default;

Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/ll/api/thread/InplaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +17,7 @@ std::shared_ptr<data::CancellableCallback> InplaceExecutor::executeAfter(std::fu
}

InplaceExecutor const& InplaceExecutor::getDefault() {
static std::shared_ptr<InplaceExecutor> p = std::make_shared<InplaceExecutor>("default");
return *p;
static InplaceExecutor ins("default_inplace");
return ins;
}
} // namespace ll::thread
2 changes: 1 addition & 1 deletion src/ll/api/thread/InplaceExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 5 additions & 6 deletions src/ll/api/thread/ServerThreadExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ll::thread {

struct ServerThreadExecutor::Impl {
struct ScheduledWork {
uint64 time;
uint64 time;
std::shared_ptr<data::CancellableCallback> callback;
};
struct SwCmp {
Expand Down Expand Up @@ -59,8 +59,8 @@ struct ServerThreadExecutor::Impl {
std::shared_ptr<SharedImpl> 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<Impl>(std::make_shared<Impl::SharedImpl>())) {
impl->worker = event::EventBus::getInstance().emplaceListener<event::LevelTickEvent>(
[name = getName(), maxOnceDuration, checkPack, weak = std::weak_ptr{impl->shared}](auto&&) {
Expand Down Expand Up @@ -104,8 +104,7 @@ ServerThreadExecutor::executeAfter(std::function<void()> f, Duration dur) const
}

ServerThreadExecutor const& ServerThreadExecutor::getDefault() {
static std::shared_ptr<ServerThreadExecutor> p =
std::make_shared<ServerThreadExecutor>("default", std::chrono::milliseconds{30}, 16);
return *p;
static ServerThreadExecutor ins("default_server_thread", std::chrono::milliseconds{30}, 16);
return ins;
}
} // namespace ll::thread
2 changes: 1 addition & 1 deletion src/ll/api/thread/ServerThreadExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ServerThreadExecutor final : public coro::Executor {
std::unique_ptr<Impl> 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;

Expand Down
11 changes: 5 additions & 6 deletions src/ll/api/thread/ThreadPoolExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace ll::thread {

struct ThreadPoolExecutor::Impl {
struct ScheduledWork {
Clock::time_point time;
Clock::time_point time;
std::shared_ptr<data::CancellableCallback> callback;
};
struct SwCmp {
Expand Down Expand Up @@ -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<Impl>(*this, nThreads)) {}

void ThreadPoolExecutor::resize(size_t nThreads) { impl = std::make_unique<Impl>(*this, nThreads); }
Expand All @@ -138,8 +138,7 @@ ThreadPoolExecutor::executeAfter(std::function<void()> f, Duration dur) const {
}
}
ThreadPoolExecutor const& ThreadPoolExecutor::getDefault() {
static std::shared_ptr<ThreadPoolExecutor> p =
std::make_shared<ThreadPoolExecutor>("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
2 changes: 1 addition & 1 deletion src/ll/api/thread/ThreadPoolExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ThreadPoolExecutor final : public coro::Executor {
std::unique_ptr<Impl> 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;

Expand Down

0 comments on commit b47e09a

Please sign in to comment.