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 EnQueue(CPUEUTask * task)28 void EnQueue(CPUEUTask* task) 29 { 30 EnQueueImpl(task); 31 } 32 EnQueueNode(LinkedList * node)33 void EnQueueNode(LinkedList* node) 34 { 35 EnQueueNodeImpl(node); 36 } 37 RmQueueNode(LinkedList * node)38 void RmQueueNode(LinkedList* node) 39 { 40 RmQueueNodeImpl(node); 41 } 42 DeQueue()43 CPUEUTask* DeQueue() 44 { 45 return DeQueueImpl(); 46 } 47 Empty()48 bool Empty() 49 { 50 return EmptyImpl(); 51 } 52 Size()53 int Size() 54 { 55 return SizeImpl(); 56 } 57 58 protected: 59 LinkedList list; 60 int size = 0; 61 62 private: 63 virtual void EnQueueImpl(CPUEUTask* task) = 0; 64 virtual CPUEUTask* DeQueueImpl() = 0; 65 virtual void EnQueueNodeImpl(LinkedList* node) = 0; 66 virtual void RmQueueNodeImpl(LinkedList* node) = 0; 67 virtual bool EmptyImpl() = 0; 68 virtual int SizeImpl() = 0; 69 }; 70 71 class FIFOQueue : public RunQueue { 72 private: EnQueueImpl(CPUEUTask * task)73 void EnQueueImpl(CPUEUTask* task) override 74 { 75 auto entry = &task->fq_we; 76 list.PushBack(entry->node); 77 size++; 78 } 79 DeQueueImpl()80 CPUEUTask* DeQueueImpl() override 81 { 82 if (list.Empty()) { 83 return nullptr; 84 } 85 auto node = list.PopFront(); 86 if (node == nullptr) { 87 return nullptr; 88 } 89 90 ffrt_executor_task_t* w = reinterpret_cast<ffrt_executor_task_t *>(reinterpret_cast<char *>(node) - 91 offsetof(ffrt_executor_task_t, wq)); 92 if (w->type != 0) { 93 w->wq[0] = &w->wq; 94 w->wq[1] = &w->wq; 95 size--; 96 return reinterpret_cast<CPUEUTask *>(w); 97 } 98 99 auto entry = node->ContainerOf(&WaitEntry::node); 100 CPUEUTask* tsk = entry->task; 101 102 size--; 103 return tsk; 104 } 105 EnQueueNodeImpl(LinkedList * node)106 void EnQueueNodeImpl(LinkedList* node) override 107 { 108 list.PushBack(*node); 109 size++; 110 } 111 RmQueueNodeImpl(LinkedList * node)112 void RmQueueNodeImpl(LinkedList* node) override 113 { 114 list.Delete(*node); 115 size--; 116 } 117 EmptyImpl()118 bool EmptyImpl() override 119 { 120 return list.Empty(); 121 } 122 SizeImpl()123 int SizeImpl() override 124 { 125 return size; 126 } 127 }; 128 } // namespace ffrt 129 130 #endif 131