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 #pragma once
#include <cserver/engine/components.hpp> #include <utempl/constexpr_string.hpp>
#include <cserver/engine/coroutine.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
namespace cserver::engine::basic { namespace cserver::engine::basic {
@ -15,14 +14,6 @@ struct TaskProcessor {
pool{} { 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() { inline constexpr ~TaskProcessor() {
for(auto& thread : this->pool) { for(auto& thread : this->pool) {
thread.join(); thread.join();

View file

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