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 16 #ifndef HISTREAMER_FOUNDATION_OSAL_PIPELINETHREADPOOL_H 17 #define HISTREAMER_FOUNDATION_OSAL_PIPELINETHREADPOOL_H 18 19 #include <atomic> 20 #include <functional> 21 #include <string> 22 #include <list> 23 #include <map> 24 #include "osal/task/condition_variable.h" 25 #include "osal/task/mutex.h" 26 #include "osal/task/autolock.h" 27 #include "osal/task/thread.h" 28 29 namespace OHOS { 30 namespace Media { 31 32 class TaskInner; 33 34 class PipeLineThread { 35 public: 36 PipeLineThread(std::string groupId, TaskType type, TaskPriority priority); 37 ~PipeLineThread(); 38 void Run(); 39 void AddTask(std::shared_ptr<TaskInner> task); 40 void RemoveTask(std::shared_ptr<TaskInner> task); 41 void LockJobState(); 42 void UnLockJobState(bool notifyChange); 43 void Exit(); 44 bool IsRunningInSelf(); 45 void UpdateThreadPriority(const uint32_t newPriority, const std::string &strBundleName); 46 47 std::string groupId_; 48 std::string name_; 49 TaskType type_; 50 private: 51 std::list<std::shared_ptr<TaskInner>> taskList_; 52 std::unique_ptr<Thread> loop_; 53 FairMutex mutex_; 54 ConditionVariable syncCond_; 55 std::atomic<bool> threadExit_; 56 }; 57 58 class PipeLineThreadPool { 59 public: 60 static PipeLineThreadPool &GetInstance(); 61 std::shared_ptr<PipeLineThread> FindThread(const std::string &groupId, TaskType taskType, TaskPriority priority); 62 void DestroyThread(const std::string &groupId); 63 private: 64 PipeLineThreadPool() = default; 65 ~PipeLineThreadPool() = default; 66 std::map<std::string, std::shared_ptr<std::list<std::shared_ptr<PipeLineThread>>>> workerGroupMap; 67 Mutex mutex_; 68 }; 69 } // namespace Media 70 } // namespace OHOS 71 #endif // HISTREAMER_FOUNDATION_OSAL_PIPELINETHREADPOOL_H 72