• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 OH_VEF_TASK_MANAGER
17 #define OH_VEF_TASK_MANAGER
18 
19 #include <atomic>
20 #include "ffrt.h"
21 
22 namespace OHOS {
23 namespace Media {
24 using TaskHandle = ffrt::task_handle;
25 
26 enum class TaskManagerTimeOpt {
27     CONCURRENT,
28     SEQUENTIAL
29 };
30 
31 class TaskManager {
32 public:
33     TaskManager(std::string name, TaskManagerTimeOpt opt);
34     ~TaskManager();
35 
36 public:
37     void Submit(const std::function<void()>& func, std::string funcName = "");
38 
39     void Wait(std::vector<TaskHandle>& taskHandles);
40     void Wait();
41 
42     uint32_t GetTaskCount() const;
43 
44 private:
45     void Init();
46     void RunTask(const std::function<void()>& func, const std::string& funcName);
47 
48 private:
49     std::string taskName_;
50 
51     ffrt::mutex m_taskCntLock;
52     ffrt::condition_variable m_taskCv;
53     std::atomic<uint32_t> m_taskCnt{ 0 };
54 
55     TaskManagerTimeOpt m_timeOpt{ TaskManagerTimeOpt::CONCURRENT};
56     std::shared_ptr<ffrt::queue> m_taskQueue{ nullptr };
57 };
58 } // namespace Media
59 } // namespace OHOS
60 
61 #endif // OH_VEF_TASK_MANAGER