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 <memory>
26 #include <seastar/core/task.hh>
27 #include <seastar/core/future.hh>
28 #include <seastar/util/modules.hh>
29 #endif
30 
31 namespace seastar {
32 
33 SEASTAR_MODULE_EXPORT_BEGIN
34 
35 template <typename Func>
36 class lambda_task final : public task {
37  Func _func;
39  typename futurator::promise_type _result;
40 public:
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 
53 template <typename Func>
54 inline
55 lambda_task<Func>*
56 make_task(Func&& func) noexcept {
57  return new lambda_task<Func>(current_scheduling_group(), std::forward<Func>(func));
58 }
59 
60 template <typename Func>
61 inline
62 lambda_task<Func>*
63 make_task(scheduling_group sg, Func&& func) noexcept {
64  return new lambda_task<Func>(sg, std::forward<Func>(func));
65 }
66 
67 SEASTAR_MODULE_EXPORT_END
68 
69 }
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:286
Definition: task.hh:35
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:400
Converts a type to a future type, if it isn't already.
Definition: future.hh:1843
static type invoke(Func &&func, FuncArgs &&... args) noexcept