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 
LockMem()46     static void LockMem()
47     {
48         return Instance().lockMem_();
49     }
UnlockMem()50     static void UnlockMem()
51     {
52         return Instance().unlockMem_();
53     }
54 
55     static void RegistCb(TaskAllocCB<CPUEUTask>::Alloc &&alloc, TaskAllocCB<CPUEUTask>::Free &&free,
56         TaskAllocCB<CPUEUTask>::GetUnfreedMem &&getUnfreedMem = nullptr,
57         TaskAllocCB<CPUEUTask>::LockMem &&lockMem = nullptr,
58         TaskAllocCB<CPUEUTask>::UnlockMem &&unlockMem = nullptr)
59     {
60         Instance().alloc_ = std::move(alloc);
61         Instance().free_ = std::move(free);
62         Instance().getUnfreedMem_ = std::move(getUnfreedMem);
63         Instance().lockMem_ = std::move(lockMem);
64         Instance().unlockMem_ = std::move(unlockMem);
65     }
66 
67 private:
68     TaskAllocCB<CPUEUTask>::Free free_;
69     TaskAllocCB<CPUEUTask>::Alloc alloc_;
70     TaskAllocCB<CPUEUTask>::GetUnfreedMem getUnfreedMem_;
71     TaskAllocCB<CPUEUTask>::LockMem lockMem_;
72     TaskAllocCB<CPUEUTask>::UnlockMem unlockMem_;
73 };
74 
75 } // namespace ffrt
76 
77 #endif