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>
35class lambda_task final : public task {
36 Func _func;
38 typename futurator::promise_type _result;
39public:
40 lambda_task(scheduling_group sg, const Func& func) : task(sg), _func(func) {}
41 lambda_task(scheduling_group sg, Func&& func) : task(sg), _func(std::move(func)) {}
42 typename futurator::type get_future() noexcept { return _result.get_future(); }
43 virtual void run_and_dispose() noexcept override {
44 futurator::invoke(_func).forward_to(std::move(_result));
45 delete this;
46 }
47 virtual task* waiting_task() noexcept override {
48 return _result.waiting_task();
49 }
50};
51
52template <typename Func>
53inline
54lambda_task<Func>*
55make_task(Func&& func) noexcept {
56 return new lambda_task<Func>(current_scheduling_group(), std::forward<Func>(func));
57}
58
59template <typename Func>
60inline
61lambda_task<Func>*
62make_task(scheduling_group sg, Func&& func) noexcept {
63 return new lambda_task<Func>(sg, std::forward<Func>(func));
64}
65
66SEASTAR_MODULE_EXPORT_END
67
68}
Definition: make_task.hh:35
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:47
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:285
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:397
Converts a type to a future type, if it isn't already.
Definition: future.hh:1853
static type invoke(Func &&func, FuncArgs &&... args) noexcept