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_SCHEDULER_HPP 17 #define FFRT_TASK_SCHEDULER_HPP 18 #include "core/entity.h" 19 #include "sched/task_runqueue.h" 20 #include "eu/worker_thread.h" 21 #include "sync/sync.h" 22 #include "ffrt_trace.h" 23 #include "tm/cpu_task.h" 24 #include "tm/task_factory.h" 25 26 namespace ffrt { 27 class TaskScheduler { 28 public: TaskScheduler(RunQueue * q)29 TaskScheduler(RunQueue* q) : que(q) {} ~TaskScheduler()30 ~TaskScheduler() 31 { 32 if (que != nullptr) { 33 delete que; 34 } 35 } 36 PickNextTask()37 CPUEUTask* PickNextTask() 38 { 39 CPUEUTask* task = que->DeQueue(); 40 return task; 41 } 42 WakeupTask(CPUEUTask * task)43 bool WakeupTask(CPUEUTask* task) 44 { 45 bool ret = false; 46 { 47 que->EnQueue(task); 48 ret = true; 49 } 50 return ret; 51 } 52 WakeupNode(LinkedList * node)53 bool WakeupNode(LinkedList* node) 54 { 55 bool ret = false; 56 { 57 que->EnQueueNode(node); 58 ret = true; 59 } 60 return ret; 61 } 62 RemoveNode(LinkedList * node)63 bool RemoveNode(LinkedList* node) 64 { 65 bool ret = false; 66 { 67 que->RmQueueNode(node); 68 ret = true; 69 } 70 return ret; 71 } 72 RQEmpty()73 bool RQEmpty() 74 { 75 return que->Empty(); 76 } 77 RQSize()78 int RQSize() 79 { 80 return que->Size(); 81 } 82 SetQos(QoS & q)83 void SetQos(QoS &q) 84 { 85 que->SetQos(q); 86 } 87 88 int qos {0}; 89 private: 90 RunQueue *que; 91 }; 92 93 class SchedulerFactory { 94 public: 95 using AllocCB = std::function<TaskScheduler *()>; 96 using RecycleCB = std::function<void (TaskScheduler *)>; 97 Instance()98 static SchedulerFactory &Instance() 99 { 100 static SchedulerFactory fac; 101 return fac; 102 } 103 Alloc()104 static TaskScheduler *Alloc() 105 { 106 return Instance().alloc_(); 107 } 108 Recycle(TaskScheduler * schd)109 static void Recycle(TaskScheduler *schd) 110 { 111 Instance().recycle_(schd); 112 } 113 RegistCb(const AllocCB & alloc,const RecycleCB & recycle)114 static void RegistCb(const AllocCB &alloc, const RecycleCB &recycle) 115 { 116 Instance().alloc_ = alloc; 117 Instance().recycle_ = recycle; 118 } 119 120 private: 121 AllocCB alloc_; 122 RecycleCB recycle_; 123 }; 124 } // namespace ffrt 125 126 #endif 127