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