Seastar
High performance C++ framework for concurrent servers
make_task.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 * Copyright (C) 2015 Cloudius Systems, Ltd.
20 */
21
22#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <seastar/core/task.hh>
26#include <seastar/core/future.hh>
27#include <seastar/util/modules.hh>
28#endif
29
30namespace seastar {
31
32SEASTAR_MODULE_EXPORT_BEGIN
33
34template <typename Func>
35requires std::invocable<Func>
36class lambda_task final : public task {
37 Func _func;
39 typename futurator::promise_type _result;
40public:
41 lambda_task(scheduling_group sg, const Func& func) : task(sg), _func(func) {}
42 lambda_task(scheduling_group sg, Func&& func) : task(sg), _func(std::move(func)) {}
43 typename futurator::type get_future() noexcept { return _result.get_future(); }
44 virtual void run_and_dispose() noexcept override {
45 futurator::invoke(_func).forward_to(std::move(_result));
46 delete this;
47 }
48 virtual task* waiting_task() noexcept override {
49 return _result.waiting_task();
50 }
51};
52
53template <typename Func>
54requires std::invocable<Func>
55inline
56lambda_task<Func>*
57make_task(Func&& func) noexcept {
58 return new lambda_task<Func>(current_scheduling_group(), std::forward<Func>(func));
59}
60
61template <typename Func>
62requires std::invocable<Func>
63inline
64lambda_task<Func>*
65make_task(scheduling_group sg, Func&& func) noexcept {
66 return new lambda_task<Func>(sg, std::forward<Func>(func));
67}
68
69SEASTAR_MODULE_EXPORT_END
70
71}
Definition: make_task.hh:36
virtual task * waiting_task() noexcept override
Returns the next task which is waiting for this task to complete execution, or nullptr.
Definition: make_task.hh:48
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:369
Definition: task.hh:34
virtual task * waiting_task() noexcept=0
Returns the next task which is waiting for this task to complete execution, or nullptr.
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
scheduling_group current_scheduling_group() noexcept
Returns the current scheduling group.
Definition: scheduling.hh:481
Converts a type to a future type, if it isn't already.
Definition: future.hh:1830
static type invoke(Func &&func, FuncArgs &&... args) noexcept