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_SPEC_TASK_DISPATCHER_H 16 #define OHOS_APP_DISPATCHER_SPEC_TASK_DISPATCHER_H 17 18 #include <string> 19 #include "base_task_dispatcher.h" 20 #include "task.h" 21 #include "sync_task.h" 22 #include "revocable.h" 23 #include "runnable.h" 24 #include "task/task_dispatcher_handler.h" 25 #include "event_runner.h" 26 #include "spec_dispatcher_config.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 class EventRunner; 31 class SpecTaskDispatcher : public BaseTaskDispatcher, public std::enable_shared_from_this<SpecTaskDispatcher> { 32 public: 33 /** 34 * constructor for special task dispatchers 35 * 36 * @param config which is the config of this dispatcher 37 * @param runner event runner 38 * 39 */ 40 SpecTaskDispatcher(std::shared_ptr<SpecDispatcherConfig> config, std::shared_ptr<EventRunner> runner); ~SpecTaskDispatcher()41 ~SpecTaskDispatcher(){}; 42 43 /** 44 * Called when post a task to the TaskDispatcher with waiting Attention: Call 45 * this function of Specific dispatcher on the corresponding thread will lock. 46 * 47 * @param runnable is the job to execute 48 * 49 */ 50 ErrCode SyncDispatch(const std::shared_ptr<Runnable> &runnable); 51 52 /** 53 * Called when post a task to the TaskDispatcher without waiting 54 * 55 * @param runnable is the job to execute 56 * @return an interface for revoke the task if it hasn't been invoked. 57 * 58 */ 59 std::shared_ptr<Revocable> AsyncDispatch(const std::shared_ptr<Runnable> &runnable); 60 61 /** 62 * Called when post a task group to the TaskDispatcher and without waiting 63 * 64 * @param runnable is the job to execute 65 * @param delayMs indicate the delay time in milliseconds to execute 66 * @return an interface for revoke the task if it hasn't been invoked. 67 * 68 */ 69 std::shared_ptr<Revocable> DelayDispatch(const std::shared_ptr<Runnable> &runnable, long delayMs); 70 71 private: 72 static std::string DISPATCHER_TAG; 73 74 static std::string ASYNC_DISPATCHER_TAG; 75 76 static std::string SYNC_DISPATCHER_TAG; 77 78 static std::string DELAY_DISPATCHER_TAG; 79 80 std::shared_ptr<TaskDispatcherHandler> handler_; 81 }; 82 } // namespace AppExecFwk 83 } // namespace OHOS 84 #endif 85