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 #include "tm/cpu_task.h" 20 #include "tm/uv_task.h" 21 #include <cassert> 22 23 namespace ffrt { 24 class FIFOQueue { 25 public: EnQueue(TaskBase * task)26 void EnQueue(TaskBase* task) 27 { 28 list.PushBack(task->node); 29 auto curSize = size.load(std::memory_order_relaxed); 30 size.store(curSize + 1, std::memory_order_relaxed); 31 } 32 EnQueueBatch(TaskBase * first,TaskBase * last,size_t cnt)33 void EnQueueBatch(TaskBase* first, TaskBase* last, size_t cnt) 34 { 35 list.PushBack(first->node, last->node); 36 size += static_cast<int>(cnt); 37 } 38 DeQueue()39 TaskBase* DeQueue() 40 { 41 if (list.Empty()) { 42 return nullptr; 43 } 44 auto node = list.PopFront(); 45 if (node == nullptr) { 46 return nullptr; 47 } 48 49 TaskBase* task = node->ContainerOf(&TaskBase::node); 50 auto curSize = size.load(std::memory_order_relaxed); 51 assert(curSize > 0); 52 size.store(curSize - 1, std::memory_order_relaxed); 53 return task; 54 } 55 Empty()56 bool Empty() 57 { 58 return list.Empty(); 59 } 60 Size()61 int Size() 62 { 63 return size.load(std::memory_order_relaxed); 64 } 65 66 private: 67 LinkedList list; 68 std::atomic<int> size = 0; 69 }; 70 } // namespace ffrt 71 72 #endif 73