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 
46     std::string groupId_;
47     std::string name_;
48     TaskType type_;
49 private:
50     std::list<std::shared_ptr<TaskInner>> taskList_;
51     std::unique_ptr<Thread> loop_;
52     FairMutex mutex_;
53     ConditionVariable syncCond_;
54     std::atomic<bool> threadExit_;
55 };
56 
57 class PipeLineThreadPool {
58 public:
59     static PipeLineThreadPool &GetInstance();
60     std::shared_ptr<PipeLineThread> FindThread(const std::string &groupId, TaskType taskType, TaskPriority priority);
61     void DestroyThread(const std::string &groupId);
62 private:
63     PipeLineThreadPool() = default;
64     ~PipeLineThreadPool() = default;
65     std::map<std::string, std::shared_ptr<std::list<std::shared_ptr<PipeLineThread>>>> workerGroupMap;
66     Mutex mutex_;
67 };
68 } // namespace Media
69 } // namespace OHOS
70 #endif // HISTREAMER_FOUNDATION_OSAL_PIPELINETHREADPOOL_H
71