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