• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef TASK_DEPENDENCY_MANAGER_H
16 #define TASK_DEPENDENCY_MANAGER_H
17 
18 #include <chrono>
19 #include <functional>
20 #include <future>
21 #include <map>
22 #include <mutex>
23 
24 #include "base/memory/ace_type.h"
25 
26 namespace OHOS::Ace {
27 class TaskDependencyManager : public AceType {
28     DECLARE_ACE_TYPE(TaskDependencyManager, AceType);
29 public:
30     enum class ErrorCode {
31         TASK_ERROR_KEY_CONFLICT = 0,
32         TASK_ERROR_KEY_ILLEGAL,
33         TASK_ERROR_UNRECORDED_DEPENDENCY
34     };
35     static RefPtr<TaskDependencyManager> GetInstance();
36 
37     // Do not invoke in subthread
38     virtual void PostTaskToBg(std::function<void()>&& task, const std::string& taskName,
39         std::function<void(ErrorCode, std::function<void()>&&,
40         const std::string&)>&& errorHandler = [] (ErrorCode code,
41             std::function<void()>&& task,
42             const std::string& taskName) {
43                 task();
44         });
45     virtual void Wait(const std::string& dependencyName,
46         std::function<void(ErrorCode, const std::string&)>&& errorHandler = [] (
47             ErrorCode code, const std::string& dependencyName) {
48                 return;
49         });
50     // Do not invoke in subthread
51     virtual void Sync();
52 private:
53     virtual void Delete(const std::string& taskName);
54     static RefPtr<TaskDependencyManager> instance_;
55     std::map<std::string, std::shared_ptr<std::future<void>>> futures_;
56     std::mutex lock_;
57 };
58 } //OHOS::Ace
59 
60 #endif //TASK_DEPENDENCY_MANAGER_H
61