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 TASK_FACTORY_HPP 17 #define TASK_FACTORY_HPP 18 19 #include "tm/cpu_task.h" 20 #include "util/cb_func.h" 21 22 namespace ffrt { 23 24 class TaskFactory { 25 public: 26 static TaskFactory &Instance(); 27 Alloc()28 static CPUEUTask *Alloc() 29 { 30 return Instance().alloc_(); 31 } 32 Free(CPUEUTask * task)33 static void Free(CPUEUTask *task) 34 { 35 Instance().free_(task); 36 } 37 GetUnfreedMem()38 static std::vector<void *> GetUnfreedMem() 39 { 40 if (Instance().getUnfreedMem_ != nullptr) { 41 return Instance().getUnfreedMem_(); 42 } 43 return {}; 44 } 45 HasBeenFreed(CPUEUTask * task)46 static bool HasBeenFreed(CPUEUTask *task) 47 { 48 if (Instance().hasBeenFreed_ != nullptr) { 49 return Instance().hasBeenFreed_(task); 50 } 51 return true; 52 } 53 LockMem()54 static void LockMem() 55 { 56 return Instance().lockMem_(); 57 } UnlockMem()58 static void UnlockMem() 59 { 60 return Instance().unlockMem_(); 61 } 62 63 static void RegistCb(TaskAllocCB<CPUEUTask>::Alloc &&alloc, TaskAllocCB<CPUEUTask>::Free &&free, 64 TaskAllocCB<CPUEUTask>::GetUnfreedMem &&getUnfreedMem = nullptr, 65 TaskAllocCB<CPUEUTask>::HasBeenFreed &&hasBeenFreed = nullptr, 66 TaskAllocCB<CPUEUTask>::LockMem &&lockMem = nullptr, 67 TaskAllocCB<CPUEUTask>::UnlockMem &&unlockMem = nullptr) 68 { 69 Instance().alloc_ = std::move(alloc); 70 Instance().free_ = std::move(free); 71 Instance().getUnfreedMem_ = std::move(getUnfreedMem); 72 Instance().hasBeenFreed_ = std::move(hasBeenFreed); 73 Instance().lockMem_ = std::move(lockMem); 74 Instance().unlockMem_ = std::move(unlockMem); 75 } 76 77 private: 78 TaskAllocCB<CPUEUTask>::Free free_; 79 TaskAllocCB<CPUEUTask>::Alloc alloc_; 80 TaskAllocCB<CPUEUTask>::GetUnfreedMem getUnfreedMem_; 81 TaskAllocCB<CPUEUTask>::HasBeenFreed hasBeenFreed_; 82 TaskAllocCB<CPUEUTask>::LockMem lockMem_; 83 TaskAllocCB<CPUEUTask>::UnlockMem unlockMem_; 84 }; 85 86 } // namespace ffrt 87 88 #endif