Skip to content

Commit

Permalink
rename set_scheduler to bind & launch
Browse files Browse the repository at this point in the history
  • Loading branch information
petiaccja committed Nov 16, 2023
1 parent 5d11fe4 commit 1f62519
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
29 changes: 23 additions & 6 deletions include/async++/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,36 @@ class scheduler {


template <class T>
requires requires(T& t, scheduler& s) { t.set_scheduler(s); }
T& set_scheduler(T& t, scheduler& s) {
t.set_scheduler(s);
requires requires(T& t, scheduler& s) { t.bind(s); }
T& bind(T& t, scheduler& s) {
t.bind(s);
return t;
}


template <class T>
requires requires(T&& t, scheduler& s) { t.set_scheduler(s); }
T set_scheduler(T&& t, scheduler& s) {
t.set_scheduler(s);
requires requires(T&& t, scheduler& s) { t.bind(s); }
T bind(T&& t, scheduler& s) {
t.bind(s);
return std::move(t);
}


template <class T>
requires requires(T& t, scheduler& s) { bind(t, s); t.launch(); }
T& launch(T& t, scheduler& s) {
bind(t, s);
t.launch();
return t;
}


template <class T>
requires requires(T&& t, scheduler& s) { bind(t, s); t.launch(); }
T launch(T&& t, scheduler& s) {
bind(t, s);
t.launch();
return std::move(t);
}

} // namespace asyncpp
2 changes: 1 addition & 1 deletion include/async++/shared_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class shared_task {
return impl_shared_task::awaitable<T>(m_promise);
}

void set_scheduler(scheduler& scheduler) {
void bind(scheduler& scheduler) {
if (m_promise) {
m_promise->m_scheduler = &scheduler;
}
Expand Down
2 changes: 1 addition & 1 deletion include/async++/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class [[nodiscard]] task {
return impl_task::awaitable<T>(std::exchange(m_promise, nullptr));
}

void set_scheduler(scheduler& scheduler) {
void bind(scheduler& scheduler) {
if (m_promise) {
m_promise->m_scheduler = &scheduler;
}
Expand Down
8 changes: 4 additions & 4 deletions test/test_shared_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEST_CASE("Shared task: interleave sync", "[Shared task]") {
auto make_fixture = [&tester] {
auto f = std::make_shared<fixture>();
f->task = do_task(tester);
set_scheduler(f->task, f->sched);
bind(f->task, f->sched);
return f;
};

Expand Down Expand Up @@ -71,9 +71,9 @@ TEST_CASE("Shared task: interleaving co_await", "[Shared task]") {
auto make_fixture = [&tester] {
auto f = std::make_shared<fixture>();
auto worker_task = do_worker_task(tester);
set_scheduler(worker_task, f->worker_sched);
bind(worker_task, f->worker_sched);
f->main_task = do_main_task(std::move(worker_task));
f->main_task.set_scheduler(f->main_sched);
f->main_task.bind(f->main_sched);
return f;
};

Expand Down Expand Up @@ -114,7 +114,7 @@ TEST_CASE("Shared task: interleaving abandon", "[Shared task]") {
auto make_fixture = [&tester] {
auto f = std::make_shared<fixture>();
f->task = do_task(tester);
set_scheduler(f->task, f->sched);
bind(f->task, f->sched);
return f;
};

Expand Down
8 changes: 4 additions & 4 deletions test/test_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST_CASE("Task: interleaving sync", "[Task]") {
auto make_fixture = [&tester] {
auto f = std::make_shared<fixture>();
f->task = do_task(tester);
set_scheduler(f->task, f->sched);
bind(f->task, f->sched);
return f;
};

Expand Down Expand Up @@ -68,9 +68,9 @@ TEST_CASE("Task: interleaving co_await", "[Task]") {
auto make_fixture = [&tester] {
auto f = std::make_shared<fixture>();
auto worker_task = do_worker_task(tester);
set_scheduler(worker_task, f->worker_sched);
bind(worker_task, f->worker_sched);
f->main_task = do_main_task(std::move(worker_task));
f->main_task.set_scheduler(f->main_sched);
f->main_task.bind(f->main_sched);
return f;
};

Expand Down Expand Up @@ -111,7 +111,7 @@ TEST_CASE("Task: interleaving abandon", "[Task]") {
auto make_fixture = [&tester] {
auto f = std::make_shared<fixture>();
f->task = do_task(tester);
set_scheduler(f->task, f->sched);
bind(f->task, f->sched);
return f;
};

Expand Down
7 changes: 2 additions & 5 deletions test/test_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ TEST_CASE("Thread pool: perf test", "[Scheduler]") {
co_return 1;
}
std::array<task<int64_t>, branching> children;
std::ranges::generate(children, [&] { return set_scheduler(self(self, depth - 1), sched); });
std::ranges::generate(children, [&] { return launch(self(self, depth - 1), sched); });
int64_t sum = 0;
for (auto& tk : children) {
tk.launch();
}
for (auto& tk : children) {
sum += co_await tk;
}
Expand All @@ -41,7 +38,7 @@ TEST_CASE("Thread pool: perf test", "[Scheduler]") {

const auto count = int64_t(std::pow(branching, depth));
const auto start = std::chrono::high_resolution_clock::now();
const auto result = set_scheduler(coro(coro, depth), sched).get();
const auto result = bind(coro(coro, depth), sched).get();
const auto end = std::chrono::high_resolution_clock::now();
std::cout << "performance: " << 1e9 * double(count) / std::chrono::nanoseconds(end - start).count() << " / s" << std::endl;
REQUIRE(result == count);
Expand Down

0 comments on commit 1f62519

Please sign in to comment.