1 /* 2 * Copyright (c) 2021-2022 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 OHOS_APP_DISPATCHER_BASE_TASK_DISPATCHER_H 16 #define OHOS_APP_DISPATCHER_BASE_TASK_DISPATCHER_H 17 18 #include <atomic> 19 #include <memory> 20 #include "revocable.h" 21 #include "runnable.h" 22 #include "task.h" 23 #include "group.h" 24 #include "group_impl.h" 25 #include "task_dispatcher.h" 26 #include "task_execute_interceptor.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 /** 31 * Base implementation for interface of TaskDispatcher. 32 */ 33 class BaseTaskDispatcher : public TaskDispatcher { 34 public: 35 BaseTaskDispatcher(const std::string &dispatcherName, const TaskPriority priority); ~BaseTaskDispatcher()36 virtual ~BaseTaskDispatcher(){}; 37 38 /** 39 * Called when post a task to the TaskDispatcher with waiting Attention: Call 40 * this function of Specific dispatcher on the corresponding thread will lock. 41 * @param runnable is the job to execute. 42 */ 43 virtual ErrCode SyncDispatch(const std::shared_ptr<Runnable> &runnable) override = 0; 44 45 /** 46 * Called when post a task to the TaskDispatcher without waiting. 47 * 48 * @param runnable is the job to execute. 49 * @return an interface for revoke the task if it hasn't been invoked. 50 */ 51 52 virtual std::shared_ptr<Revocable> AsyncDispatch(const std::shared_ptr<Runnable> &runnable) override = 0; 53 54 /** 55 * Called when post a task group to the TaskDispatcher and without waiting. 56 * @param runnable is the job to execute. 57 * @param delayMs indicate the delay time to execute. 58 * @return an interface for revoke the task if it hasn't been invoked. 59 */ 60 virtual std::shared_ptr<Revocable> DelayDispatch( 61 const std::shared_ptr<Runnable> &runnable, long delayMs) override = 0; 62 63 /** 64 * Set a barrier and meanwhile a sync task that execute after all tasks finished. 65 * @param runnable is the job to execute after barrier. 66 */ 67 ErrCode SyncDispatchBarrier(const std::shared_ptr<Runnable> &runnable) override; 68 69 /** 70 * Set a barrier and meanwhile an async task that execute after all tasks finished. 71 * @param runnable is the job to execute after jobs in group. 72 */ 73 ErrCode AsyncDispatchBarrier(const std::shared_ptr<Runnable> &runnable) override; 74 /** 75 * Create a group. 76 * @return a new created group. 77 */ 78 std::shared_ptr<Group> CreateDispatchGroup() override; 79 80 /** 81 * Called when post a task to the TaskDispatcher and relates it to a group 82 * without waiting. 83 * @param group related by task. 84 * @param runnable is the job to execute. 85 * @return an interface for revoke the task if it hasn't been invoked. 86 */ 87 virtual std::shared_ptr<Revocable> AsyncGroupDispatch( 88 const std::shared_ptr<Group> &group, const std::shared_ptr<Runnable> &runnable) override; 89 90 /** 91 * Synchronously waiting all tasks in the group to be done. 92 * @param group contains a serial of jobs. 93 * @param timeout is the max waiting time for jobs in group execute, in ms. 94 * @return true if all jobs in group has finished or false if timeout occurs. 95 * 96 */ 97 bool GroupDispatchWait(const std::shared_ptr<Group> &group, long timeout) override; 98 99 /** 100 * Asynchronously waiting all tasks in the group to be done. |task| will be run after waiting. 101 * 102 * @param group contains a serial of jobs. 103 * @param runnable executes after all jobs in group finished. 104 * 105 */ 106 ErrCode GroupDispatchNotify( 107 const std::shared_ptr<Group> &group, const std::shared_ptr<Runnable> &runnable) override; 108 109 /** 110 * Called to dispatch |task| |iterations| times and wait. 111 * 112 * @param task is the job to execute multi times. 113 * @param iterations indicates times the task should be executed. 114 * 115 */ 116 ErrCode ApplyDispatch(const std::shared_ptr<IteratableTask<long>> &task, long iterations) override; 117 118 /** 119 * Gets the priority. 120 * @return The priority. 121 */ 122 TaskPriority GetPriority() const; 123 124 /** 125 * Create span and set HiTraceId for task, and then tracePoint information 126 * @param task of which the information to tracePoint 127 * @param isAsyncTask whether the task is async 128 * @param dispatcherName the name of dispatcher which post the task 129 * @return valid HiTraceId if set successfully 130 * 131 */ 132 void TracePointBeforePost(std::shared_ptr<Task> &task, bool isAsyncTask, const std::string &dispatcherName) const; 133 134 /** 135 * TracePoint information after post task 136 * 137 * @param task of which the information to tracePoint 138 * @param isAsyncTask whether the task is async 139 * @param dispatcherName the name of dispatcher which post the task 140 * 141 */ 142 void TracePointAfterPost(std::shared_ptr<Task> &task, bool isAsyncTask, const std::string &dispatcherName) const; 143 144 protected: 145 /** 146 * Name of dispatcher. 147 * 148 */ 149 std::string dispatcherName_; 150 151 TaskPriority taskPriority_; 152 153 protected: 154 /** 155 * Check for the |task| argument. 156 * 157 * @param task The task to check 158 * 159 */ 160 ErrCode Check(const std::shared_ptr<Runnable> &task) const; 161 162 /** 163 * Check for the |group| argument. 164 * 165 * @param group The group to check. 166 * @return GroupImpl 167 * 168 */ 169 std::shared_ptr<GroupImpl> CastToGroupImpl(const std::shared_ptr<Group> &group); 170 171 /** 172 * Gets the task interceptor. 173 * Subclasses override this function to change the interceptor. 174 * 175 * @return The TaskExecuteInterceptor. 176 * 177 */ 178 std::shared_ptr<TaskExecuteInterceptor> GetInterceptor(); 179 180 private: 181 static std::atomic<int> SEQUENCE_; 182 }; 183 } // namespace AppExecFwk 184 } // namespace OHOS 185 186 #endif 187