• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FFRT_CPU_TASK_H
17 #define FFRT_CPU_TASK_H
18 
19 
20 #include <string>
21 #include <functional>
22 #include <unordered_set>
23 #include <vector>
24 #include <mutex>
25 #include <atomic>
26 #include <string>
27 #include <set>
28 #include <list>
29 #include <memory>
30 #include "sched/task_state.h"
31 #include "sched/interval.h"
32 #include "eu/co_routine.h"
33 #include "core/task_attr_private.h"
34 #include "core/task_io.h"
35 #include "util/task_deleter.h"
36 
37 namespace ffrt {
38 struct VersionCtx;
39 class SCPUEUTask;
40 class TaskBase {
41 public:
42     uintptr_t reserved = 0;
43     uintptr_t type = 0;
44     WaitEntry fq_we; // used on fifo fast que
45     TaskBase();
46     const uint64_t gid; // global unique id in this process
47 };
48 
49 #ifdef FFRT_IO_TASK_SCHEDULER
50 class UserDefinedTask : public TaskBase {
51     ffrt_io_callable_t work;
52     ExecTaskStatus status;
53 };
54 #endif
55 
56 class CPUEUTask : public TaskBase, public TaskDeleter {
57 public:
58     CPUEUTask(const task_attr_private* attr, CPUEUTask* parent, const uint64_t& id, const QoS &qos);
59     WaitUntilEntry* wue;
60     bool wakeupTimeOut = false;
61     SkipStatus skipped = SkipStatus::SUBMITTED;
62     TaskStatus status = TaskStatus::PENDING;
63 
64     uint8_t func_storage[ffrt_auto_managed_function_storage_size]; // 函数闭包、指针或函数对象
65     CPUEUTask* parent = nullptr;
66     const uint64_t rank = 0x0;
67     CoRoutine* coRoutine = nullptr;
68     std::vector<std::string> traceTag;
69     std::mutex lock; // used in coroute
70 
71     TaskState state;
72 
73     /* The current number of child nodes does not represent the real number of child nodes,
74      * because the dynamic graph child nodes will grow to assist in the generation of id
75      */
76     std::atomic<uint64_t> childNum {0};
77 
78     std::string label; // used for debug
79 
80     QoS qos;
81     void SetQos(QoS& newQos);
82     uint64_t reserved[8];
83 
84     void freeMem() override;
85 
86     virtual void RecycleTask() = 0;
IsRoot()87     inline bool IsRoot()
88     {
89         if (parent == nullptr) {
90             return true;
91         }
92         return false;
93     }
94 
UpdateState(TaskState::State taskState)95     int UpdateState(TaskState::State taskState)
96     {
97         return TaskState::OnTransition(taskState, this);
98     }
99 
UpdateState(TaskState::State taskState,TaskState::Op && op)100     int UpdateState(TaskState::State taskState, TaskState::Op&& op)
101     {
102         return TaskState::OnTransition(taskState, this, std::move(op));
103     }
104 
SetTraceTag(const char * name)105     void SetTraceTag(const char* name)
106     {
107         traceTag.emplace_back(name);
108     }
109 
ClearTraceTag()110     void ClearTraceTag()
111     {
112         if (!traceTag.empty()) {
113             traceTag.pop_back();
114         }
115     }
116 
117 #ifdef FFRT_CO_BACKTRACE_OH_ENABLE
118     static void DumpTask(CPUEUTask* task, std::string& stackInfo, uint8_t flag = 0); /* 0:hilog others:hiview */
119 #endif
120 };
121 } /* namespace ffrt */
122 #endif
123