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 #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;
33  using futurator = futurize<std::result_of_t<Func()>>;
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 }
seastar::futurize::invoke
static type invoke(Func &&func, FuncArgs &&... args) noexcept
seastar::task::waiting_task
virtual task * waiting_task() noexcept=0
Returns the next task which is waiting for this task to complete execution, or nullptr.
seastar
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
seastar::current_scheduling_group
scheduling_group current_scheduling_group() noexcept
Returns the current scheduling group.
Definition: scheduling.hh:339
seastar::lambda_task::waiting_task
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:43
seastar::futurize
Converts a type to a future type, if it isn't already.
Definition: future.hh:1956
seastar::task
Definition: task.hh:30
seastar::lambda_task
Definition: make_task.hh:31
seastar::scheduling_group
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:251