]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | #include <memory> | |
25 | #include <seastar/core/task.hh> | |
26 | #include <seastar/core/future.hh> | |
27 | ||
28 | namespace seastar { | |
29 | ||
30 | template <typename Func> | |
31 | class lambda_task final : public task { | |
32 | Func _func; | |
20effc67 | 33 | using futurator = futurize<std::invoke_result_t<Func>>; |
f67539c2 TL |
34 | typename futurator::promise_type _result; |
35 | public: | |
36 | lambda_task(scheduling_group sg, const Func& func) : task(sg), _func(func) {} | |
37 | lambda_task(scheduling_group sg, Func&& func) : task(sg), _func(std::move(func)) {} | |
38 | typename futurator::type get_future() noexcept { return _result.get_future(); } | |
39 | virtual void run_and_dispose() noexcept override { | |
40 | futurator::invoke(_func).forward_to(std::move(_result)); | |
41 | delete this; | |
42 | } | |
43 | virtual task* waiting_task() noexcept override { | |
44 | return _result.waiting_task(); | |
45 | } | |
46 | }; | |
47 | ||
48 | template <typename Func> | |
49 | inline | |
50 | lambda_task<Func>* | |
51 | make_task(Func&& func) noexcept { | |
52 | return new lambda_task<Func>(current_scheduling_group(), std::forward<Func>(func)); | |
53 | } | |
54 | ||
55 | template <typename Func> | |
56 | inline | |
57 | lambda_task<Func>* | |
58 | make_task(scheduling_group sg, Func&& func) noexcept { | |
59 | return new lambda_task<Func>(sg, std::forward<Func>(func)); | |
60 | } | |
61 | ||
62 | } |