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 "dfx/perf/ffrt_perf.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 FFRT_PERF_TASK_NUM(qos, RQSize()); 41 return task; 42 } 43 WakeupTask(CPUEUTask * task)44 bool WakeupTask(CPUEUTask* task) 45 { 46 bool ret = false; 47 { 48 que->EnQueue(task); 49 ret = true; 50 } 51 FFRT_PERF_TASK_NUM(qos, RQSize()); 52 return ret; 53 } 54 WakeupNode(LinkedList * node)55 bool WakeupNode(LinkedList* node) 56 { 57 bool ret = false; 58 { 59 que->EnQueueNode(node); 60 ret = true; 61 } 62 FFRT_PERF_TASK_NUM(qos, RQSize()); 63 return ret; 64 } 65 RemoveNode(LinkedList * node)66 bool RemoveNode(LinkedList* node) 67 { 68 FFRT_PERF_TASK_NUM(qos, RQSize()); 69 bool ret = false; 70 { 71 que->RmQueueNode(node); 72 ret = true; 73 } 74 return ret; 75 } 76 RQEmpty()77 bool RQEmpty() 78 { 79 return que->Empty(); 80 } 81 RQSize()82 int RQSize() 83 { 84 return que->Size(); 85 } 86 SetQos(QoS & q)87 void SetQos(QoS &q) 88 { 89 que->SetQos(q); 90 } 91 92 int qos {0}; 93 private: 94 RunQueue *que; 95 }; 96 97 class SchedulerFactory { 98 public: 99 using AllocCB = std::function<TaskScheduler *()>; 100 using RecycleCB = std::function<void (TaskScheduler *)>; 101 Instance()102 static SchedulerFactory &Instance() 103 { 104 static SchedulerFactory fac; 105 return fac; 106 } 107 Alloc()108 static TaskScheduler *Alloc() 109 { 110 return Instance().alloc_(); 111 } 112 Recycle(TaskScheduler * schd)113 static void Recycle(TaskScheduler *schd) 114 { 115 Instance().recycle_(schd); 116 } 117 RegistCb(const AllocCB & alloc,const RecycleCB & recycle)118 static void RegistCb(const AllocCB &alloc, const RecycleCB &recycle) 119 { 120 Instance().alloc_ = alloc; 121 Instance().recycle_ = recycle; 122 } 123 124 private: 125 AllocCB alloc_; 126 RecycleCB recycle_; 127 }; 128 } // namespace ffrt 129 130 #endif 131