1 /*
2  * Copyright (c) 2023 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 OHOS_ABILITY_RUNTIME_TASK_HANDLER_WRAP_H
17 #define OHOS_ABILITY_RUNTIME_TASK_HANDLER_WRAP_H
18 
19 #include <string>
20 #include <memory>
21 #include <unordered_map>
22 #include <functional>
23 #include <atomic>
24 
25 #include "task_utils_wrap.h"
26 
27 namespace ffrt {
28 class mutex;
29 };
30 
31 namespace OHOS {
32 namespace AAFwk {
33 class TaskHandlerWrap;
34 class InnerTaskHandle;
35 class TaskHandle {
36 friend class TaskHandlerWrap;
37 public:
38     TaskHandle() = default;
39     TaskHandle(std::shared_ptr<TaskHandlerWrap> handler, std::shared_ptr<InnerTaskHandle> InnerTaskHandle,
40         TaskStatus status = TaskStatus::PENDING) : handler_(handler), innerTaskHandle_(InnerTaskHandle)
41     {
42         status_ = std::make_shared<TaskStatus>(status);
43     }
44     bool Cancel() const;
45     void Sync() const;
IsSame(const TaskHandle & other)46     bool IsSame(const TaskHandle &other) const
47     {
48         return innerTaskHandle_ == other.innerTaskHandle_;
49     }
50     explicit operator bool() const
51     {
52         return status_ && innerTaskHandle_;
53     }
GetTaskId()54     int32_t GetTaskId() const
55     {
56         return taskId_;
57     }
PrintTaskLog()58     bool PrintTaskLog() const
59     {
60         return printTaskLog_;
61     }
62 private:
63     std::weak_ptr<TaskHandlerWrap> handler_;
64     std::shared_ptr<InnerTaskHandle> innerTaskHandle_;
65     std::shared_ptr<TaskStatus> status_;
66 
67     int32_t taskId_ = 0;
68     bool printTaskLog_ = false;
69 };
70 
71 class TaskHandlerWrap : public std::enable_shared_from_this<TaskHandlerWrap> {
72 friend class TaskHandle;
73 public:
74     static std::shared_ptr<TaskHandlerWrap> CreateQueueHandler(const std::string &queueName,
75         TaskQoS queueQos = TaskQoS::DEFAULT);
76     static std::shared_ptr<TaskHandlerWrap> GetFfrtHandler();
77 
78     TaskHandlerWrap(TaskHandlerWrap &) = delete;
79     void operator=(TaskHandlerWrap &) = delete;
80     virtual ~TaskHandlerWrap();
81     /**
82      * Submit task to be scheduled and executed
83      * @return TaskHandle, could be used later
84      */
85     TaskHandle SubmitTask(const std::function<void()> &task);
86     TaskHandle SubmitTask(const std::function<void()> &task, const std::string &name);
87     TaskHandle SubmitTask(const std::function<void()> &task, int64_t delayMillis);
88     TaskHandle SubmitTask(const std::function<void()> &task, TaskQoS taskQos);
89     TaskHandle SubmitTask(const std::function<void()> &task, const std::string &name,
90         int64_t delayMillis, bool forceSubmit = true);
91     TaskHandle SubmitTask(const std::function<void()> &task, const TaskAttribute &taskAttr);
92     // Task can't be canceled by name if submitted with this method
93     TaskHandle SubmitTaskJust(const std::function<void()> &task, const std::string &name,
94         int64_t delayMillis);
95     // This is only used for compatibility and could be be wrong if multi tasks with same name submitted.
96     // TaskHandle::Cancel is preferred.
97     bool CancelTask(const std::string &name);
SetPrintTaskLog(bool printTaskLog)98     void SetPrintTaskLog(bool printTaskLog)
99     {
100         printTaskLog_ = printTaskLog;
101     }
102 protected:
103     TaskHandlerWrap(const std::string &queueName);
104     virtual std::shared_ptr<InnerTaskHandle> SubmitTaskInner(std::function<void()> &&task,
105         const TaskAttribute &taskAttr) = 0;
106     virtual bool CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle) = 0;
107     virtual void WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle) = 0;
GetTaskCount()108     virtual uint64_t GetTaskCount()
109     {
110         return 0;
111     }
112     bool RemoveTask(const std::string &name, const TaskHandle &taskHandle);
113 protected:
114     static std::atomic_int32_t g_taskId;
115 
116     // this is used only for compatibility
117     std::unordered_map<std::string, TaskHandle> tasks_;
118     std::unique_ptr<ffrt::mutex> tasksMutex_;
119 
120     bool printTaskLog_ = false;
121     std::string queueName_;
122 };
123 
124 class AutoSyncTaskHandle {
125 public:
AutoSyncTaskHandle(const TaskHandle & handle)126     explicit AutoSyncTaskHandle(const TaskHandle &handle) : handle_(handle) {}
~AutoSyncTaskHandle()127     ~AutoSyncTaskHandle()
128     {
129         Sync();
130     }
131 
132     AutoSyncTaskHandle(AutoSyncTaskHandle&) = delete;
133     void operator=(AutoSyncTaskHandle&) = delete;
134 
Sync()135     void Sync()
136     {
137         auto handle = handle_;
138         handle_ = TaskHandle();
139         if (handle) {
140             handle.Sync();
141         }
142     }
143 private:
144     TaskHandle handle_;
145 };
146 }  // namespace AAFWK
147 }  // namespace OHOS
148 #endif // OHOS_ABILITY_RUNTIME_TASK_HANDLER_WRAP_H