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 MAPLE_PHASE_INCLUDE_PHASEDRIVER_H
17 #define MAPLE_PHASE_INCLUDE_PHASEDRIVER_H
18 #include "mir_module.h"
19 #include "mir_function.h"
20 #include "mpl_scheduler.h"
21 
22 namespace maple {
23 class PhaseDriverImpl : public MplTaskParam {
24 public:
25     PhaseDriverImpl() = default;
26     virtual ~PhaseDriverImpl() = default;
27 
GlobalInit()28     virtual void GlobalInit() {}
29 
LocalInit()30     virtual void LocalInit() {}
31 
ProcessRun(uint32, void *, void *)32     virtual void ProcessRun(uint32, void *, void *) {}
33 
ProcessFinish(uint32, void *, void *)34     virtual void ProcessFinish(uint32, void *, void *) {}
35 };
36 
37 class PhaseDriver : public MplScheduler {
38 public:
39     class Task : public MplTask {
40     public:
Task(void *currTarget, void *currParamEx = nullptr)41         Task(void *currTarget, void *currParamEx = nullptr) : target(currTarget), paramException(currParamEx) {}
42 
43         ~Task() = default;
44 
45     protected:
46         int RunImpl(MplTaskParam *param) override
47         {
48             CHECK_NULL_FATAL(param);
49             static_cast<PhaseDriverImpl *>(param)->ProcessRun(taskId, target, paramException);
50             return 0;
51         }
52 
53         int FinishImpl(MplTaskParam *param) override
54         {
55             CHECK_NULL_FATAL(param);
56             static_cast<PhaseDriverImpl *>(param)->ProcessFinish(taskId, target, paramException);
57             return 0;
58         }
59         void *target;
60         void *paramException;
61     };
62 
PhaseDriver(const std::string &phaseName)63     explicit PhaseDriver(const std::string &phaseName)
64         : MplScheduler(phaseName), module(nullptr), phaseImpl(nullptr), phaseName(phaseName) {}
65     virtual ~PhaseDriver() = default;
66 
67     virtual PhaseDriverImpl *NewPhase() = 0;
68     virtual void RegisterTasks() = 0;
69 
70 protected:
CallbackGetTaskRunParam() const71     virtual MplTaskParam *CallbackGetTaskRunParam() const
72     {
73         return phaseImplLocal;
74     }
75 
CallbackGetTaskFinishParam() const76     virtual MplTaskParam *CallbackGetTaskFinishParam() const
77     {
78         return phaseImplLocal;
79     }
80 
81     MIRModule *module;
82     PhaseDriverImpl *phaseImpl;
83     thread_local static PhaseDriverImpl *phaseImplLocal;
84     std::string phaseName;
85 };
86 }  // namespace maple
87 #endif  // MAPLE_PHASE_INCLUDE_PHASEDRIVER_H
88