Skip to content

Commit

Permalink
sharded.hh: migrate to concepts
Browse files Browse the repository at this point in the history
Migrates static_assert calls on invoke variants to idiomatic C++20 concept requirements. Requirements are duplicated on proxy functions for improved readability, loose coupling and faster failures.

Closes scylladb#2460
  • Loading branch information
tomershafir authored and xemul committed Oct 4, 2024
1 parent 3e07190 commit a30ec0c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions include/seastar/core/sharded.hh
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public:
/// \return Future that becomes ready once all calls have completed
template <typename Func, typename... Args>
requires std::invocable<Func, Service&, internal::sharded_unwrap_t<Args>...>
&& std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, internal::sharded_unwrap_t<Args>...>>, future<>>
future<> invoke_on_all(smp_submit_to_options options, Func func, Args... args) noexcept;

/// Invoke a function on all instances of `Service`.
Expand All @@ -300,6 +301,7 @@ public:
/// \ref smp::submit_to() called behind the scenes.
template <typename Func, typename... Args>
requires std::invocable<Func, Service&, internal::sharded_unwrap_t<Args>...>
&& std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, internal::sharded_unwrap_t<Args>...>>, future<>>
future<> invoke_on_all(Func func, Args... args) noexcept {
try {
return invoke_on_all(smp_submit_to_options{}, std::move(func), std::move(args)...);
Expand All @@ -320,6 +322,7 @@ public:
/// processed the message.
template <typename Func, typename... Args>
requires std::invocable<Func, Service&, Args...>
&& std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, Args...>>, future<>>
future<> invoke_on_others(smp_submit_to_options options, Func func, Args... args) noexcept;

/// Invoke a callable on all instances of \c Service except the instance
Expand All @@ -335,6 +338,7 @@ public:
/// \ref smp::submit_to() called behind the scenes.
template <typename Func, typename... Args>
requires std::invocable<Func, Service&, Args...>
&& std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, Args...>>, future<>>
future<> invoke_on_others(Func func, Args... args) noexcept {
try {
return invoke_on_others(smp_submit_to_options{}, std::move(func), std::move(args)...);
Expand Down Expand Up @@ -796,11 +800,10 @@ sharded<Service>::invoke_on_all(smp_submit_to_options options, std::function<fut
template <typename Service>
template <typename Func, typename... Args>
requires std::invocable<Func, Service&, internal::sharded_unwrap_t<Args>...>
&& std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, internal::sharded_unwrap_t<Args>...>>, future<>>
inline
future<>
sharded<Service>::invoke_on_all(smp_submit_to_options options, Func func, Args... args) noexcept {
static_assert(std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, internal::sharded_unwrap_t<Args>...>>, future<>>,
"invoke_on_all()'s func must return void or future<>");
try {
return invoke_on_all(options, invoke_on_multiple_func_type([func = std::move(func), args = std::tuple(std::move(args)...)] (Service& service) mutable {
return std::apply([&service, &func] (Args&&... args) mutable {
Expand All @@ -815,11 +818,10 @@ sharded<Service>::invoke_on_all(smp_submit_to_options options, Func func, Args..
template <typename Service>
template <typename Func, typename... Args>
requires std::invocable<Func, Service&, Args...>
&& std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, Args...>>, future<>>
inline
future<>
sharded<Service>::invoke_on_others(smp_submit_to_options options, Func func, Args... args) noexcept {
static_assert(std::is_same_v<futurize_t<std::invoke_result_t<Func, Service&, Args...>>, future<>>,
"invoke_on_others()'s func must return void or future<>");
try {
return invoke_on_all(options, [orig = this_shard_id(), func = std::move(func), args = std::tuple(std::move(args)...)] (Service& s) mutable -> future<> {
return this_shard_id() == orig ? make_ready_future<>() : futurize_apply(func, std::tuple_cat(std::forward_as_tuple(s), args));;
Expand Down

0 comments on commit a30ec0c

Please sign in to comment.