• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 UV_TASK_H_
17 #define UV_TASK_H_
18 
19 #include "task_base.h"
20 #include "c/executor_task.h"
21 #include "core/task_io.h"
22 #include "core/task_attr_private.h"
23 #include "tm/task_factory.h"
24 #ifdef USE_OHOS_QOS
25 #include "qos.h"
26 #else
27 #include "staging_qos/sched/qos.h"
28 #endif
29 
30 namespace ffrt {
31 class UVTask : public TaskBase {
32 public:
UVTask(ffrt_executor_task * uvWork,const task_attr_private * attr)33     UVTask(ffrt_executor_task* uvWork, const task_attr_private *attr)
34         : TaskBase(ffrt_uv_task, attr), uvWork(uvWork)
35     {
36         /*
37             uvWork是libuv传进来的ffrt_executor_task指针,其中的wq成员为双向链表节点,和LinkedList的内存布局一致,
38             曾经用于软化队列的节点和TaskBase类一样插入ReadyQueue。
39             目前由于已组合进入UVTask类型,不再作为链表节点,此处就可以使用uvWorker的wq来标记任务是否已经出队的状态。
40             libuv里面,在cancel时也会判断任务是否已经出队(uv__queue_empty):
41             q== q->next || q != q->next->prev;其中next对应wq[0],prev对应wq[1]
42             将wq入队,即可满足v__queue_empty返回false的需要
43         */
44         if (uvWork != nullptr) {
45             uvWorkList.PushBack(reinterpret_cast<LinkedList*>(&uvWork->wq));
46         } else {
47             FFRT_LOGE("executor_task is nullptr");
48         }
49     }
50     ffrt_executor_task* uvWork;
51 
Prepare()52     void Prepare() override {}
53 
54     void Ready() override;
55 
Pop()56     void Pop() override
57     {
58         SetStatus(TaskStatus::POPPED);
59     }
60 
61     void Execute() override;
62 
Block()63     BlockType Block() override
64     {
65         SetStatus(TaskStatus::THREAD_BLOCK);
66         return BlockType::BLOCK_THREAD;
67     }
68 
Wake()69     void Wake() override
70     {
71         SetStatus(TaskStatus::EXECUTING);
72     }
73 
Finish()74     void Finish() override {}
Cancel()75     void Cancel() override {}
76 
FreeMem()77     void FreeMem() override
78     {
79         TaskFactory<UVTask>::Free(this);
80     }
81 
SetQos(const QoS & newQos)82     void SetQos(const QoS& newQos) override
83     {
84         qos_ = newQos;
85     }
86 
GetLabel()87     std::string GetLabel() const override
88     {
89         return "uv-task";
90     }
91 
GetBlockType()92     BlockType GetBlockType() const override
93     {
94         return BlockType::BLOCK_THREAD;
95     }
96 
SetDequeued()97     inline void SetDequeued()
98     {
99         if (uvWork == nullptr) {
100             return;
101         }
102         /*
103             uvWork是libuv传进来的ffrt_executor_task指针,其中的wq成员为双向链表节点,和LinkedList的内存布局一致,
104             曾经用于软化队列的节点和TaskBase类一样插入ReadyQueue。
105             目前由于已组合进入UVTask类型,不再作为链表节点,此处就可以使用uvWorker的wq来标记任务是否已经出队的状态。
106             libuv里面,在cancel时也会判断任务是否已经出队(uv__queue_empty):
107             q== q->next || q != q->next->prev;其中next对应wq[0],prev对应wq[1]
108             将wq出队,即可满足v__queue_empty返回false的需要
109         */
110         LinkedList::RemoveCur(reinterpret_cast<LinkedList*>(&uvWork->wq));
111     }
112 
113     static void ExecuteImpl(UVTask* task, ffrt_executor_task_func func);
114 private:
115     LinkedList uvWorkList;
116 };
117 } /* namespace ffrt */
118 #endif
119