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 FOUNDATION_APPEXECFWK_OHOS_PARALLEL_TASK_DISPATCHER_BASE_H 16 #define FOUNDATION_APPEXECFWK_OHOS_PARALLEL_TASK_DISPATCHER_BASE_H 17 18 #include <assert.h> 19 #include <list> 20 #include <iostream> 21 #include <string> 22 #include <vector> 23 24 #include "base_task_dispatcher.h" 25 #include "group.h" 26 #include "revocable.h" 27 #include "runnable.h" 28 #include "sync_task.h" 29 #include "task.h" 30 #include "task_priority.h" 31 #include "task_executor.h" 32 #include "task_execute_interceptor.h" 33 34 namespace OHOS { 35 namespace AppExecFwk { 36 /** 37 * Base implementation for parallel TaskDispatcher 38 */ 39 class ParallelTaskDispatcherBase : public BaseTaskDispatcher, 40 public std::enable_shared_from_this<ParallelTaskDispatcherBase> { 41 public: 42 ParallelTaskDispatcherBase( 43 TaskPriority taskPriority, std::shared_ptr<TaskExecutor> &executor, const std::string &dispatcherName); 44 45 virtual ~ParallelTaskDispatcherBase() = default; 46 /** 47 * Called when post a task to the TaskDispatcher with waiting Attention: Call 48 * this function of Specific dispatcher on the corresponding thread will lock. 49 * 50 * @param runnable is the job to execute 51 * 52 */ 53 ErrCode SyncDispatch(const std::shared_ptr<Runnable> &runnable) override; 54 55 /** 56 * Called when post a task to the TaskDispatcher without waiting 57 * 58 * @param runnable is the job to execute 59 * @return an interface for revoke the task if it hasn't been invoked. 60 * 61 */ 62 std::shared_ptr<Revocable> AsyncDispatch(const std::shared_ptr<Runnable> &runnable) override; 63 64 /** 65 * Called when post a task group to the TaskDispatcher and without waiting 66 * 67 * @param runnable is the job to execute 68 * @param delayMs indicate the delay time to execute 69 * @return an interface for revoke the task if it hasn't been invoked. 70 * 71 */ 72 std::shared_ptr<Revocable> DelayDispatch(const std::shared_ptr<Runnable> &runnable, long delayMs) override; 73 74 /** 75 * Called when post a task to the TaskDispatcher and relates it to a group 76 * without waiting. 77 * 78 * @param group related by task. 79 * @param runnable is the job to execute. 80 * @return an interface for revoke the task if it hasn't been invoked. 81 * 82 */ 83 std::shared_ptr<Revocable> AsyncGroupDispatch( 84 const std::shared_ptr<Group> &group, const std::shared_ptr<Runnable> &runnable) override; 85 86 protected: 87 virtual std::shared_ptr<TaskExecuteInterceptor> GetInterceptor(); 88 89 std::shared_ptr<TaskExecutor> executor_ = nullptr; 90 91 private: 92 static std::string DISPATCHER_TAG; 93 static std::string ASYNC_DISPATCHER_TAG; 94 static std::string SYNC_DISPATCHER_TAG; 95 static std::string DELAY_DISPATCHER_TAG; 96 static std::string ASYNC_GROUP_DISPATCHER_TAG; 97 98 ErrCode InterceptedExecute(std::shared_ptr<Task> &task); 99 100 class MyTaskListener : public TaskListener { 101 private: 102 std::function<void()> callback_; 103 104 public: OnChanged(const TaskStage & stage)105 void OnChanged(const TaskStage &stage) 106 { 107 if (stage.IsDone()) { 108 HILOG_INFO("ParallelTaskDispatcherBase task done."); 109 callback_(); 110 } 111 } 112 // set callback function Callback(const std::function<void ()> & callbackFunction)113 void Callback(const std::function<void()> &callbackFunction) 114 { 115 callback_ = std::move(callbackFunction); 116 } 117 }; 118 }; 119 120 } // namespace AppExecFwk 121 } // namespace OHOS 122 #endif 123