Seastar
High performance C++ framework for concurrent servers
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 
25 #include <seastar/util/backtrace.hh>
26 
27 #ifndef SEASTAR_MODULE
28 #include <memory>
29 #include <utility>
30 #endif
31 
32 namespace seastar {
33 
34 SEASTAR_MODULE_EXPORT
35 class task {
36 protected:
37  scheduling_group _sg;
38 private:
39 #ifdef SEASTAR_TASK_BACKTRACE
40  shared_backtrace _bt;
41 #endif
42 protected:
43  // Task destruction is performed by run_and_dispose() via a concrete type,
44  // so no need for a virtual destructor here. Derived classes that implement
45  // run_and_dispose() should be declared final to avoid losing concrete type
46  // information via inheritance.
47  ~task() = default;
48 
49  scheduling_group set_scheduling_group(scheduling_group new_sg) noexcept{
50  return std::exchange(_sg, new_sg);
51  }
52 public:
53  explicit task(scheduling_group sg = current_scheduling_group()) noexcept : _sg(sg) {}
54  virtual void run_and_dispose() noexcept = 0;
56  virtual task* waiting_task() noexcept = 0;
57  scheduling_group group() const { return _sg; }
58  shared_backtrace get_backtrace() const;
59 #ifdef SEASTAR_TASK_BACKTRACE
60  void make_backtrace() noexcept;
61 #else
62  void make_backtrace() noexcept {}
63 #endif
64 };
65 
66 inline
67 shared_backtrace task::get_backtrace() const {
68 #ifdef SEASTAR_TASK_BACKTRACE
69  return _bt;
70 #else
71  return {};
72 #endif
73 }
74 
75 SEASTAR_MODULE_EXPORT_BEGIN
76 
77 void schedule(task* t) noexcept;
78 void schedule_checked(task* t) noexcept;
79 void schedule_urgent(task* t) noexcept;
80 
81 SEASTAR_MODULE_EXPORT_END
82 }
Definition: shared_ptr.hh:270
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