Move basicTaskProcessor to default for parallel initialisation components in future

This commit is contained in:
sha512sum 2024-04-06 20:06:34 +00:00
parent 3426a2fce6
commit 81e0576461
2 changed files with 39 additions and 20 deletions

View file

@ -1,6 +1,5 @@
#pragma once
#include <cserver/engine/components.hpp>
#include <cserver/engine/coroutine.hpp>
#include <utempl/constexpr_string.hpp>
#include <boost/asio.hpp>
namespace cserver::engine::basic {
@ -15,14 +14,6 @@ struct TaskProcessor {
pool{} {
};
template <utempl::ConstexprString name, Options Options, typename T>
static consteval auto Adder(const T& context) {
constexpr std::size_t Count = T::kConfig.template Get<name>().template Get<"threadPoolSize">();
return context.TransformComponents(
[&](const ComponentConfig<name, TaskProcessor<>, Options>&) -> ComponentConfig<name, TaskProcessor<Count>, Options> {
return {};
});
};
inline constexpr ~TaskProcessor() {
for(auto& thread : this->pool) {
thread.join();

View file

@ -1,8 +1,12 @@
#pragma once
#include <cserver/engine/basic/task_processor.hpp>
#include <utempl/utils.hpp>
#include <thread>
namespace cserver {
inline constexpr utempl::ConstexprString kBasicTaskProcessorName = "basicTaskProcessor";
struct SeparatedComponent {};
template <typename... Ts>
struct Options : utempl::TypeList<Ts...> {};
@ -64,8 +68,10 @@ struct ServiceContext {
static constexpr auto kConfig = config;
static constexpr auto kList = utempl::TypeList<Ts...>{};
static constexpr auto kThreadsCount = config.template Get<"threads">();
engine::basic::TaskProcessor<kThreadsCount> taskProcessor;
utempl::Tuple<Ts...> storage;
inline constexpr ServiceContext() :
taskProcessor{utempl::Wrapper<utempl::ConstexprString{kBasicTaskProcessorName}>{}, *this},
storage{
[&]<auto... Is>(std::index_sequence<Is...>) -> utempl::Tuple<Ts...> {
return {[&]<auto I>(utempl::Wrapper<I>) {
@ -89,6 +95,10 @@ struct ServiceContext {
constexpr auto I = utempl::Find(names, name);
return Get<I>(this->storage);
};
template <>
inline constexpr auto FindComponent<kBasicTaskProcessorName>() -> auto& {
return this->taskProcessor;
};
template <typename T>
inline constexpr auto FindComponent() -> T& {
constexpr auto I = utempl::Find<std::remove_cvref_t<T>>(utempl::kTypeList<Ts...>);
@ -159,9 +169,13 @@ struct DependencyInfoInjector {
private:
template <utempl::ConstexprString name, utempl::ConstexprString... names, typename... TTs, Options... Options>
static consteval auto FindComponentTypeImpl(ComponentConfig<names, TTs, Options>...) {
if constexpr(name == kBasicTaskProcessorName) {
return [] -> engine::basic::TaskProcessor<config.template Get<"threads">()> {}();
} else {
constexpr auto I = utempl::Find(utempl::Tuple{std::string_view{names}...}, std::string_view{name});
return [] -> decltype(utempl::Get<I>(utempl::TypeList<TTs...>{})) {}();
};
};
public:
template <utempl::ConstexprString name>
using FindComponentType = decltype(FindComponentTypeImpl<name>(Ts{}...));
@ -187,6 +201,7 @@ public:
>
static constexpr auto FindComponent() -> FindComponentType<name>&;
template <
typename T,
typename...,
@ -208,9 +223,19 @@ public:
return utempl::Tuple{};
} else {
if constexpr(requires{GetDependencies<I + 1, names...>();}) {
return GetDependencies<I + 1, names..., Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{})>();
constexpr auto name = Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{});
if constexpr(name == kBasicTaskProcessorName) {
return GetDependencies<I + 1, names...>();
} else {
return utempl::Tuple{names..., Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{})};
return GetDependencies<I + 1, names..., name>();
};
} else {
constexpr auto name = Magic(loopholes::Getter<DependencyInfoKey<Current, I>{}>{});
if constexpr(name == kBasicTaskProcessorName) {
return utempl::Tuple{names...};
} else {
return utempl::Tuple{names..., name};
};
};
};
};
@ -339,13 +364,16 @@ struct ServiceContextBuilder {
};
template <utempl::ConstexprString name>
static consteval auto FindComponent() {
if constexpr(name == kBasicTaskProcessorName) {
return [] -> engine::basic::TaskProcessor<config.template Get<"threads">()> {}();
} else {
return []<typename... TTs, utempl::ConstexprString... names, Options... Options>
(const ServiceContextBuilder<config, ComponentConfig<names, TTs, Options>...>&)
-> decltype(utempl::Get<Find(utempl::Tuple{names...}, name)>(utempl::TypeList<TTs...>{})) {
std::unreachable();
}(ServiceContextBuilder<config, ComponentConfigs...>{});
};
};
static consteval auto Config() {
return config;
};