Skip to content

Commit

Permalink
update: runtime builder
Browse files Browse the repository at this point in the history
  • Loading branch information
8sileus committed May 8, 2024
1 parent d628309 commit 50534f9
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 109 deletions.
3 changes: 1 addition & 2 deletions tests/channel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ auto test(std::size_t n) -> Task<void> {

auto main() -> int {
SET_LOG_LEVEL(LogLevel::Trace);
auto runtime = zedio::runtime::Builder<>::options().set_num_workers(4).build();
runtime.block_on(test(1000));
zedio::runtime::MultiThreadBuilder::options().set_num_workers(4).build().block_on(test(1000));
return 0;
}
3 changes: 1 addition & 2 deletions tests/condition_variable_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ auto test(std::size_t n) -> Task<void> {

auto main() -> int {
SET_LOG_LEVEL(LogLevel::Trace);
auto runtime = zedio::runtime::Builder<>::options().set_num_workers(4).build();
runtime.block_on(test(10000));
zedio::runtime::MultiThreadBuilder::options().set_num_workers(4).build().block_on(test(10000));
return 0;
}
4 changes: 1 addition & 3 deletions tests/file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ auto test() -> Task<void> {
}

auto main() -> int {
// auto runtime = zedio::runtime::Builder<>::options().set_num_workers(1).build();
auto runtime = zedio::runtime::Builder<zedio::runtime::Kind::CurrentThread>::default_create();
runtime.block_on(test());
zedio::runtime::MultiThreadBuilder::default_create().block_on(test());
return 0;
}
3 changes: 2 additions & 1 deletion tests/io_buf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ auto test() -> Task<void> {
}

auto main() -> int {
zedio::runtime::Builder<>::default_create().block_on(test());
// zedio::runtime::CurrentThreadBuilder::default_create().block_on(test());
zedio::runtime::MultiThreadBuilder::default_create().block_on(test());
return 0;
}
2 changes: 1 addition & 1 deletion tests/latch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ auto test(std::size_t n) -> Task<void> {

auto main() -> int {
SET_LOG_LEVEL(LogLevel::Trace);
zedio::runtime::Builder<>::default_create().block_on(test(100000));
zedio::runtime::MultiThreadBuilder::default_create().block_on(test(100000));
return 0;
}
2 changes: 1 addition & 1 deletion tests/mutex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ auto test(std::size_t n) -> Task<void> {

auto main() -> int {
SET_LOG_LEVEL(LogLevel::Trace);
zedio::runtime::Builder<>::default_create().block_on(test(100000));
zedio::runtime::MultiThreadBuilder::default_create().block_on(test(100000));
return 0;
}
135 changes: 64 additions & 71 deletions zedio/runtime/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,84 @@

namespace zedio::runtime {

enum class Kind {
CurrentThread,
MultiThread,
};
namespace detail {

template <typename T>
class Runtime;
template <typename H>
class Runtime;

template <Kind KIND = Kind::MultiThread>
class Builder {
private:
Builder() {
auto _ = set_thread_basename("ZEDIO-WORKER");
}
template <typename B, typename H>
class Builder {
private:
Builder() {
auto _ = set_thread_basename("ZEDIO-WORKER");
}

public:
[[nodiscard]]
auto set_num_workers(std::size_t num_worker_threads) -> Builder & {
config_.num_workers_ = num_worker_threads;
return *this;
}
public:
[[nodiscard]]
auto set_global_queue_interval(uint32_t interval) -> Builder & {
config_.global_queue_interval_ = interval;
return *this;
}

[[nodiscard]]
auto set_global_queue_interval(uint32_t interval) -> Builder & {
config_.global_queue_interval_ = interval;
return *this;
}
[[nodiscard]]
auto set_io_interval(uint32_t interval) -> Builder & {
config_.io_interval_ = interval;
return *this;
}

[[nodiscard]]
auto set_io_interval(uint32_t interval) -> Builder & {
config_.io_interval_ = interval;
return *this;
}
[[nodiscard]]
auto set_submit_interval(std::size_t interval) {
config_.submit_interval_ = interval;
return *this;
}

[[nodiscard]]
auto set_submit_interval(std::size_t interval) {
config_.submit_interval_ = interval;
return *this;
}
[[nodiscard]]
auto set_thread_name_fn(std::function<std::string(std::size_t)> &&func) -> Builder & {
build_thread_name_func_ = std::move(func);
return *this;
}

[[nodiscard]]
auto set_thread_name_fn(std::function<std::string(std::size_t)> &&func) -> Builder & {
build_thread_name_func_ = std::move(func);
return *this;
}
[[nodiscard]]
auto set_thread_basename(std::string_view basename) -> Builder & {
build_thread_name_func_ = [basename = basename](std::size_t index) -> std::string {
return std::format("{}-{}", basename, index);
};
return *this;
}

[[nodiscard]]
auto set_thread_basename(std::string_view basename) -> Builder & {
build_thread_name_func_ = [basename = basename](std::size_t index) -> std::string {
return std::format("{}-{}", basename, index);
};
return *this;
}
[[nodiscard]]
auto build() {
return Runtime<H>{std::move(config_), std::move(build_thread_name_func_)};
}

[[nodiscard]]
auto build() {
if constexpr (KIND == Kind::CurrentThread) {
return Runtime<current_thread::Handle>{std::move(config_),
std::move(build_thread_name_func_)};
} else {
return Runtime<multi_thread::Handle>{
std::move(config_),
std::move(build_thread_name_func_),
};
public:
[[nodiscard]]
static auto options() -> B {
return B{};
}
}

public:
[[nodiscard]]
static auto options() -> Builder {
return Builder{};
}
[[nodiscard]]
static auto default_create() {
return B{}.build();
}

[[nodiscard]]
static auto default_create() {
return Builder{}.build();
}
protected:
detail::Config config_{};
std::function<std::string(std::size_t)> build_thread_name_func_{};
};

private:
detail::Config config_{};
std::function<std::string(std::size_t)> build_thread_name_func_{};
};
} // namespace detail

using CurrentThreadBuilder = Builder<Kind::CurrentThread>;
class CurrentThreadBuilder : public detail::Builder<CurrentThreadBuilder, current_thread::Handle> {
};

using MultiThreadBuilder = Builder<Kind::MultiThread>;
class MultiThreadBuilder : public detail::Builder<MultiThreadBuilder, current_thread::Handle> {
public:
[[nodiscard]]
auto set_num_workers(std::size_t num_worker_threads) -> MultiThreadBuilder & {
config_.num_workers_ = num_worker_threads;
return *this;
}
};

} // namespace zedio::runtime
52 changes: 24 additions & 28 deletions zedio/runtime/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "zedio/runtime/builder.hpp"
#include "zedio/time/sleep.hpp"

namespace zedio::runtime {
namespace zedio::runtime::detail {

template <typename Handle>
class Runtime {
Expand Down Expand Up @@ -46,40 +46,36 @@ class Runtime {
Handle handle_;
};

namespace detail {

static inline auto is_current_thread() -> bool {
return current_thread::t_worker != nullptr;
}
static inline auto is_current_thread() -> bool {
return current_thread::t_worker != nullptr;
}

static inline void schedule_local(std::coroutine_handle<> handle) {
if (is_current_thread()) {
current_thread::schedule_local(handle);
} else {
multi_thread::schedule_local(handle);
}
static inline void schedule_local(std::coroutine_handle<> handle) {
if (is_current_thread()) {
current_thread::schedule_local(handle);
} else {
multi_thread::schedule_local(handle);
}
}

static inline void schedule_remote(std::coroutine_handle<> handle) {
if (is_current_thread()) {
current_thread::schedule_remote(handle);
} else {
multi_thread::schedule_remote(handle);
}
static inline void schedule_remote(std::coroutine_handle<> handle) {
if (is_current_thread()) {
current_thread::schedule_remote(handle);
} else {
multi_thread::schedule_remote(handle);
}
}

static inline void schedule_remote_batch(std::list<std::coroutine_handle<>> &&handles,
std::size_t n) {
if (is_current_thread()) {
current_thread::schedule_remote_batch(std::move(handles), n);
} else {
multi_thread::schedule_remote_batch(std::move(handles), n);
}
static inline void schedule_remote_batch(std::list<std::coroutine_handle<>> &&handles,
std::size_t n) {
if (is_current_thread()) {
current_thread::schedule_remote_batch(std::move(handles), n);
} else {
multi_thread::schedule_remote_batch(std::move(handles), n);
}
}

} // namespace detail

} // namespace zedio::runtime
} // namespace zedio::runtime::detail

namespace zedio {

Expand Down

0 comments on commit 50534f9

Please sign in to comment.