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_UTIL_INCLUDE_MPLSCHEDULER_H 17 #define MAPLE_UTIL_INCLUDE_MPLSCHEDULER_H 18 19 #include <vector> 20 #include <set> 21 #include <thread> 22 #include <mutex> 23 #include <condition_variable> 24 #include <inttypes.h> 25 #include <pthread.h> 26 #include <inttypes.h> 27 #include <iostream> 28 #include <string> 29 #include "types_def.h" 30 31 namespace maple { 32 class MplTaskParam { 33 public: 34 MplTaskParam() = default; 35 virtual ~MplTaskParam() = default; 36 }; 37 38 class MplTask { 39 public: MplTask()40 MplTask() : taskId(0) {} 41 42 virtual ~MplTask() = default; 43 SetTaskId(uint32 id)44 void SetTaskId(uint32 id) 45 { 46 taskId = id; 47 } 48 GetTaskId() const49 uint32 GetTaskId() const 50 { 51 return taskId; 52 } 53 Run(MplTaskParam *param = nullptr)54 int Run(MplTaskParam *param = nullptr) 55 { 56 return RunImpl(param); 57 } 58 Finish(MplTaskParam *param = nullptr)59 int Finish(MplTaskParam *param = nullptr) 60 { 61 return FinishImpl(param); 62 } 63 64 protected: RunImpl(MplTaskParam *)65 virtual int RunImpl(MplTaskParam *) 66 { 67 return 0; 68 } 69 FinishImpl(MplTaskParam *)70 virtual int FinishImpl(MplTaskParam *) 71 { 72 return 0; 73 } 74 75 uint32 taskId; 76 }; 77 78 class MplSchedulerParam { 79 public: 80 MplSchedulerParam() = default; 81 ~MplSchedulerParam() = default; 82 }; 83 84 class MplScheduler { 85 public: MplScheduler(const std::string &name)86 explicit MplScheduler(const std::string &name) 87 : schedulerName(name), 88 taskIdForAdd(0), 89 taskIdToRun(0), 90 taskIdExpected(0), 91 numberTasks(0), 92 numberTasksFinish(0), 93 isSchedulerSeq(false), 94 dumpTime(false), 95 statusFinish(kThreadRun) 96 { 97 } 98 virtual ~MplScheduler() = default; 99 EncodeThreadMainEnvironment(uint32)100 virtual MplSchedulerParam *EncodeThreadMainEnvironment(uint32) 101 { 102 return nullptr; 103 } 104 DecodeThreadMainEnvironment(MplSchedulerParam *)105 virtual void DecodeThreadMainEnvironment(MplSchedulerParam *) {} 106 EncodeThreadFinishEnvironment()107 virtual MplSchedulerParam *EncodeThreadFinishEnvironment() 108 { 109 return nullptr; 110 } 111 DecodeThreadFinishEnvironment(MplSchedulerParam *)112 virtual void DecodeThreadFinishEnvironment(MplSchedulerParam *) {} 113 GlobalLock()114 void GlobalLock() 115 { 116 pthread_mutex_lock(&mutexGlobal); 117 } 118 GlobalUnlock()119 void GlobalUnlock() 120 { 121 pthread_mutex_unlock(&mutexGlobal); 122 } 123 124 protected: 125 std::string schedulerName; 126 std::vector<MplTask *> tbTasks; 127 std::set<uint32> tbTaskIdsToFinish; 128 uint32 taskIdForAdd; 129 uint32 taskIdToRun; 130 uint32 taskIdExpected; 131 uint32 numberTasks; 132 uint32 numberTasksFinish; 133 pthread_mutex_t mutexTaskIdsToRun; 134 pthread_mutex_t mutexTaskIdsToFinish; 135 pthread_mutex_t mutexTaskFinishProcess; 136 pthread_mutex_t mutexGlobal; 137 pthread_cond_t conditionFinishProcess; 138 bool isSchedulerSeq; 139 bool dumpTime; 140 141 enum ThreadStatus { kThreadStop, kThreadRun, kThreadPause }; 142 143 ThreadStatus statusFinish; 144 void ThreadFinishNoSequence(MplSchedulerParam *env); 145 void ThreadFinishSequence(MplSchedulerParam *env); 146 // Callback Function CallbackThreadMainStart()147 virtual void CallbackThreadMainStart() {} 148 CallbackThreadMainEnd()149 virtual void CallbackThreadMainEnd() {} 150 CallbackThreadFinishStart()151 virtual void CallbackThreadFinishStart() {} 152 CallbackThreadFinishEnd()153 virtual void CallbackThreadFinishEnd() {} 154 CallbackGetTaskRunParam() const155 virtual MplTaskParam *CallbackGetTaskRunParam() const 156 { 157 return nullptr; 158 } 159 CallbackGetTaskFinishParam() const160 virtual MplTaskParam *CallbackGetTaskFinishParam() const 161 { 162 return nullptr; 163 } 164 }; 165 } // namespace maple 166 #endif // MAPLE_UTIL_INCLUDE_MPLSCHEDULER_H 167