1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_TASK_HANDLER_WRAP_H 17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_TASK_HANDLER_WRAP_H 18eace7efcSopenharmony_ci 19eace7efcSopenharmony_ci#include <string> 20eace7efcSopenharmony_ci#include <memory> 21eace7efcSopenharmony_ci#include <unordered_map> 22eace7efcSopenharmony_ci#include <functional> 23eace7efcSopenharmony_ci 24eace7efcSopenharmony_ci#include "task_utils_wrap.h" 25eace7efcSopenharmony_ci 26eace7efcSopenharmony_cinamespace ffrt { 27eace7efcSopenharmony_ciclass mutex; 28eace7efcSopenharmony_ci}; 29eace7efcSopenharmony_ci 30eace7efcSopenharmony_cinamespace OHOS { 31eace7efcSopenharmony_cinamespace AAFwk { 32eace7efcSopenharmony_ciclass TaskHandlerWrap; 33eace7efcSopenharmony_ciclass InnerTaskHandle; 34eace7efcSopenharmony_ciclass TaskHandle { 35eace7efcSopenharmony_cifriend class TaskHandlerWrap; 36eace7efcSopenharmony_cipublic: 37eace7efcSopenharmony_ci TaskHandle() = default; 38eace7efcSopenharmony_ci TaskHandle(std::shared_ptr<TaskHandlerWrap> handler, std::shared_ptr<InnerTaskHandle> InnerTaskHandle, 39eace7efcSopenharmony_ci TaskStatus status = TaskStatus::PENDING) : handler_(handler), innerTaskHandle_(InnerTaskHandle) 40eace7efcSopenharmony_ci { 41eace7efcSopenharmony_ci status_ = std::make_shared<TaskStatus>(status); 42eace7efcSopenharmony_ci } 43eace7efcSopenharmony_ci bool Cancel() const; 44eace7efcSopenharmony_ci void Sync() const; 45eace7efcSopenharmony_ci bool IsSame(const TaskHandle &other) const 46eace7efcSopenharmony_ci { 47eace7efcSopenharmony_ci return innerTaskHandle_ == other.innerTaskHandle_; 48eace7efcSopenharmony_ci } 49eace7efcSopenharmony_ci explicit operator bool() const 50eace7efcSopenharmony_ci { 51eace7efcSopenharmony_ci return status_ && innerTaskHandle_; 52eace7efcSopenharmony_ci } 53eace7efcSopenharmony_ciprivate: 54eace7efcSopenharmony_ci std::weak_ptr<TaskHandlerWrap> handler_; 55eace7efcSopenharmony_ci std::shared_ptr<InnerTaskHandle> innerTaskHandle_; 56eace7efcSopenharmony_ci std::shared_ptr<TaskStatus> status_; 57eace7efcSopenharmony_ci}; 58eace7efcSopenharmony_ci 59eace7efcSopenharmony_ciclass TaskHandlerWrap : public std::enable_shared_from_this<TaskHandlerWrap> { 60eace7efcSopenharmony_cifriend class TaskHandle; 61eace7efcSopenharmony_cipublic: 62eace7efcSopenharmony_ci static std::shared_ptr<TaskHandlerWrap> CreateQueueHandler(const std::string &queueName, 63eace7efcSopenharmony_ci TaskQoS queueQos = TaskQoS::DEFAULT); 64eace7efcSopenharmony_ci 65eace7efcSopenharmony_ci static std::shared_ptr<TaskHandlerWrap> CreateConcurrentQueueHandler(const std::string &queueName, 66eace7efcSopenharmony_ci int32_t concurrentNum, TaskQoS queueQos = TaskQoS::DEFAULT); 67eace7efcSopenharmony_ci 68eace7efcSopenharmony_ci static std::shared_ptr<TaskHandlerWrap> GetFfrtHandler(); 69eace7efcSopenharmony_ci 70eace7efcSopenharmony_ci TaskHandlerWrap(TaskHandlerWrap &) = delete; 71eace7efcSopenharmony_ci void operator=(TaskHandlerWrap &) = delete; 72eace7efcSopenharmony_ci virtual ~TaskHandlerWrap(); 73eace7efcSopenharmony_ci /** 74eace7efcSopenharmony_ci * Submit task to be scheduled and executed 75eace7efcSopenharmony_ci * @return TaskHandle, could be used later 76eace7efcSopenharmony_ci */ 77eace7efcSopenharmony_ci TaskHandle SubmitTask(const std::function<void()> &task); 78eace7efcSopenharmony_ci TaskHandle SubmitTask(const std::function<void()> &task, const std::string &name); 79eace7efcSopenharmony_ci TaskHandle SubmitTask(const std::function<void()> &task, int64_t delayMillis); 80eace7efcSopenharmony_ci TaskHandle SubmitTask(const std::function<void()> &task, TaskQoS taskQos); 81eace7efcSopenharmony_ci TaskHandle SubmitTask(const std::function<void()> &task, const std::string &name, 82eace7efcSopenharmony_ci int64_t delayMillis, bool forceSubmit = true); 83eace7efcSopenharmony_ci TaskHandle SubmitTask(const std::function<void()> &task, const TaskAttribute &taskAttr); 84eace7efcSopenharmony_ci // Task can't be canceled by name if submited with this mothed 85eace7efcSopenharmony_ci TaskHandle SubmitTaskJust(const std::function<void()> &task, const std::string &name, 86eace7efcSopenharmony_ci int64_t delayMillis); 87eace7efcSopenharmony_ci // This is only used for compatibility and could be be wrong if multi tasks with same name submited. 88eace7efcSopenharmony_ci // TaskHandle::Cancel is prefered. 89eace7efcSopenharmony_ci bool CancelTask(const std::string &name); 90eace7efcSopenharmony_ciprotected: 91eace7efcSopenharmony_ci TaskHandlerWrap(); 92eace7efcSopenharmony_ci virtual std::shared_ptr<InnerTaskHandle> SubmitTaskInner(std::function<void()> &&task, 93eace7efcSopenharmony_ci const TaskAttribute &taskAttr) = 0; 94eace7efcSopenharmony_ci virtual bool CancelTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle) = 0; 95eace7efcSopenharmony_ci virtual void WaitTaskInner(const std::shared_ptr<InnerTaskHandle> &taskHandle) = 0; 96eace7efcSopenharmony_ci bool RemoveTask(const std::string &name, const TaskHandle &taskHandle); 97eace7efcSopenharmony_ciprotected: 98eace7efcSopenharmony_ci // this is used only for compatibility 99eace7efcSopenharmony_ci std::unordered_map<std::string, TaskHandle> tasks_; 100eace7efcSopenharmony_ci std::unique_ptr<ffrt::mutex> tasksMutex_; 101eace7efcSopenharmony_ci}; 102eace7efcSopenharmony_ci 103eace7efcSopenharmony_ciclass AutoSyncTaskHandle { 104eace7efcSopenharmony_cipublic: 105eace7efcSopenharmony_ci explicit AutoSyncTaskHandle(const TaskHandle &handle) : handle_(handle) {} 106eace7efcSopenharmony_ci ~AutoSyncTaskHandle() 107eace7efcSopenharmony_ci { 108eace7efcSopenharmony_ci Sync(); 109eace7efcSopenharmony_ci } 110eace7efcSopenharmony_ci 111eace7efcSopenharmony_ci AutoSyncTaskHandle(AutoSyncTaskHandle&) = delete; 112eace7efcSopenharmony_ci void operator=(AutoSyncTaskHandle&) = delete; 113eace7efcSopenharmony_ci 114eace7efcSopenharmony_ci void Sync() 115eace7efcSopenharmony_ci { 116eace7efcSopenharmony_ci auto handle = handle_; 117eace7efcSopenharmony_ci handle_ = TaskHandle(); 118eace7efcSopenharmony_ci if (handle) { 119eace7efcSopenharmony_ci handle.Sync(); 120eace7efcSopenharmony_ci } 121eace7efcSopenharmony_ci } 122eace7efcSopenharmony_ciprivate: 123eace7efcSopenharmony_ci TaskHandle handle_; 124eace7efcSopenharmony_ci}; 125eace7efcSopenharmony_ci} // namespace AAFWK 126eace7efcSopenharmony_ci} // namespace OHOS 127eace7efcSopenharmony_ci#endif // OHOS_ABILITY_RUNTIME_TASK_HANDLER_WRAP_H