]>
Commit | Line | Data |
---|---|---|
11fdf7f2 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/scheduling.hh> | |
26 | ||
27 | namespace seastar { | |
28 | ||
29 | class task { | |
30 | scheduling_group _sg; | |
9f95a23c TL |
31 | protected: |
32 | // Task destruction is performed by run_and_dispose() via a concrete type, | |
33 | // so no need for a virtual destructor here. Derived classes that implement | |
34 | // run_and_dispose() should be declared final to avoid losing concrete type | |
35 | // information via inheritance. | |
36 | ~task() = default; | |
11fdf7f2 TL |
37 | public: |
38 | explicit task(scheduling_group sg = current_scheduling_group()) : _sg(sg) {} | |
11fdf7f2 TL |
39 | virtual void run_and_dispose() noexcept = 0; |
40 | scheduling_group group() const { return _sg; } | |
41 | }; | |
42 | ||
9f95a23c TL |
43 | void schedule(task* t) noexcept; |
44 | void schedule_urgent(task* t) noexcept; | |
11fdf7f2 TL |
45 | |
46 | template <typename Func> | |
47 | class lambda_task final : public task { | |
48 | Func _func; | |
49 | public: | |
50 | lambda_task(scheduling_group sg, const Func& func) : task(sg), _func(func) {} | |
51 | lambda_task(scheduling_group sg, Func&& func) : task(sg), _func(std::move(func)) {} | |
52 | virtual void run_and_dispose() noexcept override { | |
53 | _func(); | |
54 | delete this; | |
55 | } | |
56 | }; | |
57 | ||
58 | template <typename Func> | |
59 | inline | |
9f95a23c TL |
60 | task* |
61 | make_task(Func&& func) noexcept { | |
62 | return new lambda_task<Func>(current_scheduling_group(), std::forward<Func>(func)); | |
11fdf7f2 TL |
63 | } |
64 | ||
65 | template <typename Func> | |
66 | inline | |
9f95a23c TL |
67 | task* |
68 | make_task(scheduling_group sg, Func&& func) noexcept { | |
69 | return new lambda_task<Func>(sg, std::forward<Func>(func)); | |
11fdf7f2 TL |
70 | } |
71 | ||
72 | } |