• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 #define HST_LOG_TAG "Task"
16 #include "osal/task/task.h"
17 #include "osal/task/taskInner.h"
18 #include "osal/task/thread.h"
19 #include "osal/utils/util.h"
20 #include "cpp_ext/memory_ext.h"
21 #include "common/log.h"
22 
23 #include <mutex>
24 
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "PipelineTreadPool" };
27 }
28 
29 namespace OHOS {
30 namespace Media {
31 namespace {
32     constexpr int64_t ADJUST_US = 500;
33     constexpr int64_t US_PER_MS = 1000;
34 }
35 
ConvertPriorityType(TaskPriority priority)36 static ThreadPriority ConvertPriorityType(TaskPriority priority)
37 {
38     switch (priority) {
39         case TaskPriority::LOW:
40             return ThreadPriority::LOW;
41         case TaskPriority::NORMAL:
42             return ThreadPriority::NORMAL;
43         case TaskPriority::MIDDLE:
44             return ThreadPriority::MIDDLE;
45         case TaskPriority::HIGHEST:
46             return ThreadPriority::HIGHEST;
47         default:
48             return ThreadPriority::NORMAL;
49     }
50 }
51 
TaskTypeConvert(TaskType type)52 static std::string TaskTypeConvert(TaskType type)
53 {
54     static const std::map<TaskType, std::string> table = {
55         {TaskType::GLOBAL, "G"},
56         {TaskType::VIDEO, "V"},
57         {TaskType::AUDIO, "A"},
58         {TaskType::SUBTITLE, "T"},
59         {TaskType::SINGLETON, "S"},
60     };
61     auto it = table.find(type);
62     if (it != table.end()) {
63         return it->second;
64     }
65     return "NA";
66 }
67 
GetNowUs()68 static int64_t GetNowUs()
69 {
70     auto now = std::chrono::steady_clock::now();
71     return std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
72 }
73 
GetInstance()74 PipeLineThreadPool& PipeLineThreadPool::GetInstance()
75 {
76     static PipeLineThreadPool instance;
77     return instance;
78 }
79 
FindThread(const std::string & groupId,TaskType taskType,TaskPriority priority)80 std::shared_ptr<PipeLineThread> PipeLineThreadPool::FindThread(const std::string &groupId,
81     TaskType taskType, TaskPriority priority)
82 {
83     AutoLock lock(mutex_);
84     if (workerGroupMap.find(groupId) == workerGroupMap.end()) {
85         workerGroupMap[groupId] = std::make_shared<std::list<std::shared_ptr<PipeLineThread>>>();
86     }
87     std::shared_ptr<std::list<std::shared_ptr<PipeLineThread>>> threadList = workerGroupMap[groupId];
88     for (auto thread : *threadList.get()) {
89         if (thread->type_ == taskType) {
90             return thread;
91         }
92     }
93     std::shared_ptr<PipeLineThread> newThread = std::make_shared<PipeLineThread>(groupId, taskType, priority);
94     threadList->push_back(newThread);
95     return newThread;
96 }
97 
DestroyThread(const std::string & groupId)98 void PipeLineThreadPool::DestroyThread(const std::string &groupId)
99 {
100     MEDIA_LOG_I("DestroyThread groupId:" PUBLIC_LOG_S, groupId.c_str());
101     std::shared_ptr<std::list<std::shared_ptr<PipeLineThread>>> threadList;
102     {
103         AutoLock lock(mutex_);
104         if (workerGroupMap.find(groupId) == workerGroupMap.end()) {
105             MEDIA_LOG_E("DestroyThread groupId not exist");
106             return;
107         }
108         threadList = workerGroupMap[groupId];
109         workerGroupMap.erase(groupId);
110     }
111     for (auto thread : *threadList.get()) {
112         thread->Exit();
113     }
114 }
115 
PipeLineThread(std::string groupId,TaskType type,TaskPriority priority)116 PipeLineThread::PipeLineThread(std::string groupId, TaskType type, TaskPriority priority)
117     : groupId_(groupId), type_(type)
118 {
119     MEDIA_LOG_I("PipeLineThread groupId:" PUBLIC_LOG_S " type:%{public}d created call", groupId_.c_str(), type);
120     loop_ = CppExt::make_unique<Thread>(ConvertPriorityType(priority));
121     name_ = groupId_ + "_" + TaskTypeConvert(type);
122     loop_->SetName(name_);
123     threadExit_ = false;
124     if (loop_->CreateThread([this] { Run(); })) {
125         threadExit_ = false;
126     } else {
127         threadExit_ = true;
128         loop_ = nullptr;
129         MEDIA_LOG_E("PipeLineThread " PUBLIC_LOG_S " create failed", name_.c_str());
130     }
131 }
132 
UpdateThreadPriority(const uint32_t newPriority,const std::string & strBundleName)133 void PipeLineThread::UpdateThreadPriority(const uint32_t newPriority, const std::string &strBundleName)
134 {
135     FALSE_RETURN_W(!threadExit_.load() && loop_);
136     loop_->UpdateThreadPriority(newPriority, strBundleName);
137 }
138 
~PipeLineThread()139 PipeLineThread::~PipeLineThread()
140 {
141     Exit();
142 }
143 
Exit()144 void PipeLineThread::Exit()
145 {
146     {
147         AutoLock lock(mutex_);
148         FALSE_RETURN_W(!threadExit_.load() && loop_);
149 
150         MEDIA_LOG_I("PipeLineThread " PUBLIC_LOG_S " exit", name_.c_str());
151         threadExit_ = true;
152         syncCond_.NotifyAll();
153 
154         // trigger to quit thread in current running thread, must not wait,
155         // or else the current thread will be suspended and can not quit.
156         if (IsRunningInSelf()) {
157             return;
158         }
159     }
160     // loop_ destroy will wait thread join
161     loop_ = nullptr;
162 }
163 
Run()164 void PipeLineThread::Run()
165 {
166     MEDIA_LOG_I("PipeLineThread " PUBLIC_LOG_S " run enter", name_.c_str());
167     while (true) {
168         std::shared_ptr<TaskInner> nextTask;
169         {
170             AutoLock lock(mutex_);
171             if (threadExit_.load()) {
172                 break;
173             }
174             int64_t nextJobUs = INT64_MAX;
175             for (auto task: taskList_) {
176                 int64_t taskJobUs = task->NextJobUs();
177                 if (taskJobUs == -1) {
178                     continue;
179                 }
180                 if (taskJobUs < nextJobUs) {
181                     nextJobUs = taskJobUs;
182                     nextTask = task;
183                 }
184             }
185             if (nextTask == nullptr) {
186                 syncCond_.Wait(lock);
187                 continue;
188             }
189             int64_t nowUs = GetNowUs();
190             if (nextJobUs > (nowUs + ADJUST_US)) {
191                 syncCond_.WaitFor(lock, (nextJobUs - nowUs + ADJUST_US) / US_PER_MS);
192                 continue;
193             }
194         }
195         nextTask->HandleJob();
196     }
197 }
198 
AddTask(std::shared_ptr<TaskInner> task)199 void PipeLineThread::AddTask(std::shared_ptr<TaskInner> task)
200 {
201     AutoLock lock(mutex_);
202     taskList_.push_back(task);
203 }
204 
RemoveTask(std::shared_ptr<TaskInner> task)205 void PipeLineThread::RemoveTask(std::shared_ptr<TaskInner> task)
206 {
207     {
208         AutoLock lock(mutex_);
209         taskList_.remove(task);
210         FALSE_LOG_MSG(!taskList_.empty(),
211          "PipeLineThread " PUBLIC_LOG_S " remove all Task", name_.c_str());
212     }
213     if (type_ == TaskType::SINGLETON) {
214         PipeLineThreadPool::GetInstance().DestroyThread(groupId_);
215     }
216 }
217 
LockJobState()218 void PipeLineThread::LockJobState()
219 {
220     if (IsRunningInSelf()) {
221         return;
222     }
223     mutex_.lock();
224 }
225 
UnLockJobState(bool notifyChange)226 void PipeLineThread::UnLockJobState(bool notifyChange)
227 {
228     if (IsRunningInSelf()) {
229         return;
230     }
231     mutex_.unlock();
232     if (notifyChange) {
233         syncCond_.NotifyAll();
234     }
235 }
236 
IsRunningInSelf()237 bool PipeLineThread::IsRunningInSelf()
238 {
239     return loop_ ? loop_->IsRunningInSelf() : false;
240 }
241 } // namespace Media
242 } // namespace OHOS
243