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