• 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_TASK_RUNQUEUE_HPP
17 #define FFRT_TASK_RUNQUEUE_HPP
18 
19 
20 #include "core/task_ctx.h"
21 #include "c/executor_task.h"
22 
23 namespace ffrt {
24 template <typename Derived>
25 class RunQueue {
26 public:
27     virtual ~RunQueue() = default;
28 
EnQueue(TaskCtx * task)29     void EnQueue(TaskCtx* task)
30     {
31         static_cast<Derived*>(this)->EnQueueImpl(task);
32     }
33 
EnQueueNode(LinkedList * node)34     void EnQueueNode(LinkedList* node)
35     {
36         static_cast<Derived*>(this)->EnQueueNodeImpl(node);
37     }
38 
RmQueueNode(LinkedList * node)39     void RmQueueNode(LinkedList* node)
40     {
41         static_cast<Derived*>(this)->RmQueueNodeImpl(node);
42     }
43 
DeQueue()44     TaskCtx* DeQueue()
45     {
46         return static_cast<Derived*>(this)->DeQueueImpl();
47     }
48 
Empty()49     bool Empty()
50     {
51         return static_cast<Derived*>(this)->EmptyImpl();
52     }
53 
Size()54     int Size()
55     {
56         return static_cast<Derived*>(this)->SizeImpl();
57     }
58 };
59 
60 class FIFOQueue : public RunQueue<FIFOQueue> {
61     friend class RunQueue<FIFOQueue>;
62 
63 private:
EnQueueImpl(TaskCtx * task)64     void EnQueueImpl(TaskCtx* task)
65     {
66         auto entry = &task->fq_we;
67         list.PushBack(entry->node);
68         size++;
69     }
70 
DeQueueImpl()71     TaskCtx* DeQueueImpl()
72     {
73         if (list.Empty()) {
74             return nullptr;
75         }
76         auto node = list.PopFront();
77         ffrt_executor_task_t* w = (ffrt_executor_task_t *) ((char *)(node) - offsetof(ffrt_executor_task_t, wq));
78         if (w->type != 0) {
79             w->wq[0] = &w->wq;
80             w->wq[1] = &w->wq;
81             size--;
82             return (TaskCtx *)w;
83         }
84 
85         auto entry = node->ContainerOf(&WaitEntry::node);
86         TaskCtx* tsk = entry->task;
87 
88         size--;
89         return tsk;
90     }
91 
EnQueueNodeImpl(LinkedList * node)92     void EnQueueNodeImpl(LinkedList* node)
93     {
94         list.PushBack(*node);
95         size++;
96     }
97 
RmQueueNodeImpl(LinkedList * node)98     void RmQueueNodeImpl(LinkedList* node)
99     {
100         list.Delete(*node);
101         size--;
102     }
103 
EmptyImpl()104     bool EmptyImpl()
105     {
106         return list.Empty();
107     }
108 
SizeImpl()109     int SizeImpl()
110     {
111         return size;
112     }
113 
114     LinkedList list;
115     int size = 0;
116 };
117 } // namespace ffrt
118 
119 #endif
120