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_TASK_RUNQUEUE_HPP
17 #define FFRT_TASK_RUNQUEUE_HPP
18 
19 
20 #include "c/executor_task.h"
21 #include "tm/cpu_task.h"
22 
23 namespace ffrt {
24 class RunQueue {
25 public:
26     virtual ~RunQueue() = default;
27     virtual void EnQueue(CPUEUTask* task) = 0;
28     virtual CPUEUTask* DeQueue() = 0;
29     virtual void EnQueueNode(LinkedList* node) = 0;
30     virtual void RmQueueNode(LinkedList* node) = 0;
31     virtual bool Empty() = 0;
32     virtual int Size() = 0;
33     virtual void SetQos(QoS &newQos) = 0;
34 
35 protected:
36     LinkedList list;
37     int size = 0;
38 };
39 
40 class FIFOQueue : public RunQueue {
41 public:
EnQueue(CPUEUTask * task)42     void EnQueue(CPUEUTask* task) override
43     {
44         auto entry = &task->fq_we;
45         list.PushBack(entry->node);
46         size++;
47     }
48 
DeQueue()49     CPUEUTask* DeQueue() override
50     {
51         if (list.Empty()) {
52             return nullptr;
53         }
54         auto node = list.PopFront();
55         if (node == nullptr) {
56             return nullptr;
57         }
58         ffrt_executor_task_t* w = reinterpret_cast<ffrt_executor_task_t *>(reinterpret_cast<char *>(node) -
59             offsetof(ffrt_executor_task_t, wq));
60         if (w->type != ffrt_normal_task && w->type != ffrt_queue_task) {
61             w->wq[0] = &w->wq;
62             w->wq[1] = &w->wq;
63             size--;
64             return reinterpret_cast<CPUEUTask *>(w);
65         }
66 
67         auto entry = node->ContainerOf(&WaitEntry::node);
68         CPUEUTask* tsk = entry->task;
69 
70         size--;
71         return tsk;
72     }
73 
EnQueueNode(LinkedList * node)74     void EnQueueNode(LinkedList* node) override
75     {
76         list.PushBack(*node);
77         size++;
78     }
79 
RmQueueNode(LinkedList * node)80     void RmQueueNode(LinkedList* node) override
81     {
82         list.Delete(*node);
83         size--;
84     }
85 
Empty()86     bool Empty() override
87     {
88         return list.Empty();
89     }
90 
Size()91     int Size() override
92     {
93         return size;
94     }
SetQos(QoS & newQos)95     void SetQos(QoS &newQos) override {}
96 };
97 } // namespace ffrt
98 
99 #endif
100