Seastar
High performance C++ framework for concurrent servers
with_scheduling_group.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19/*
20 * Copyright (C) 2020 ScyllaDB.
21 */
22
23#pragma once
24
25#ifndef SEASTAR_MODULE
26#include <seastar/core/future.hh>
27#include <seastar/core/make_task.hh>
28#include <seastar/util/modules.hh>
29#include <tuple>
30#include <utility>
31#endif
32
33namespace seastar {
34
37
38namespace internal {
39
40template <typename Func>
41requires std::is_nothrow_move_constructible_v<Func>
42auto
43schedule_in_group(scheduling_group sg, Func func) noexcept {
44 static_assert(std::is_nothrow_move_constructible_v<Func>);
45 auto tsk = make_task(sg, std::move(func));
46 schedule_checked(tsk);
47 return tsk->get_future();
48}
49
50
51}
52
63SEASTAR_MODULE_EXPORT
64template <typename Func, typename... Args>
65requires std::is_nothrow_move_constructible_v<Func>
66inline
67auto
68with_scheduling_group(scheduling_group sg, Func func, Args&&... args) noexcept {
69 static_assert(std::is_nothrow_move_constructible_v<Func>);
70 using return_type = decltype(func(std::forward<Args>(args)...));
71 using futurator = futurize<return_type>;
72 if (sg.active()) {
73 return futurator::invoke(func, std::forward<Args>(args)...);
74 } else {
75 return internal::schedule_in_group(sg, [func = std::move(func), args = std::make_tuple(std::forward<Args>(args)...)] () mutable {
76 return futurator::apply(func, std::move(args));
77 });
78 }
79}
80
82
83} // namespace seastar
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:285
auto with_scheduling_group(scheduling_group sg, Func func, Args &&... args) noexcept
run a callable (with some arbitrary arguments) in a scheduling group
Definition: with_scheduling_group.hh:68
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Converts a type to a future type, if it isn't already.
Definition: future.hh:1853