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_SERIAL_TASK_DISPATCHER_H 16 #define OHOS_APP_DISPATCHER_SERIAL_TASK_DISPATCHER_H 17 18 #include <string> 19 20 #include "base_task_dispatcher.h" 21 #include "task.h" 22 #include "sync_task.h" 23 #include "task_executor.h" 24 #include "task_priority.h" 25 #include "revocable.h" 26 #include "task_listener.h" 27 #include "concurrent_queue.h" 28 #include "runnable.h" 29 30 namespace OHOS { 31 namespace AppExecFwk { 32 /** 33 * Dispatcher for serial thread model. 34 */ 35 class SerialTaskDispatcher : public BaseTaskDispatcher, public std::enable_shared_from_this<SerialTaskDispatcher> { 36 public: 37 SerialTaskDispatcher( 38 const std::string &dispatcherName, const TaskPriority priority, const std::shared_ptr<TaskExecutor> &executor); ~SerialTaskDispatcher()39 ~SerialTaskDispatcher(){}; 40 41 /** 42 * Gets waiting tasks count of SerialTaskDispatcher. 43 * 44 * @return The waiting tasks count of SerialTaskDispatcher. 45 * 46 */ 47 int GetWorkingTasksSize(); 48 49 /** 50 * Gets name of SerialTaskDispatcher. 51 * 52 * @return The name of SerialTaskDispatcher. 53 * 54 */ 55 std::string GetDispatcherName(); 56 57 /** 58 * Called when post a task to the TaskDispatcher with waiting Attention: Call 59 * this function of Specific dispatcher on the corresponding thread will lock. 60 * 61 * @param runnable is the job to execute 62 * 63 */ 64 ErrCode SyncDispatch(const std::shared_ptr<Runnable> &runnable); 65 66 /** 67 * Called when post a task to the TaskDispatcher without waiting 68 * 69 * @param runnable is the job to execute 70 * @return an interface for revoke the task if it hasn't been invoked. 71 * 72 */ 73 std::shared_ptr<Revocable> AsyncDispatch(const std::shared_ptr<Runnable> &runnable); 74 75 /** 76 * Called when post a task group to the TaskDispatcher and without waiting 77 * 78 * @param runnable is the job to execute 79 * @param delayMs indicate the delay time to execute 80 * @return an interface for revoke the task if it hasn't been invoked. 81 * 82 */ 83 std::shared_ptr<Revocable> DelayDispatch(const std::shared_ptr<Runnable> &runnable, long delayMs); 84 85 private: 86 ErrCode OnNewTaskIn(std::shared_ptr<Task> &task); 87 88 ErrCode Prepare(std::shared_ptr<Task> &task); 89 90 /** 91 * Callback for task when finish. 92 * 93 */ 94 void OnTaskDone(); 95 96 bool Schedule(); 97 98 /** 99 * Do task in turn, until the task queue is empty. 100 * 101 * @param isExhausted is an inaccurate judge indicate that the workingTasks is empty. If true, do double-check. 102 * @return true if has work remain to do else false. 103 * 104 */ 105 bool DoNext(bool isExhausted); 106 107 void DoWork(std::shared_ptr<Task> &task); 108 109 private: 110 static std::string DISPATCHER_TAG; 111 static std::string ASYNC_DISPATCHER_TAG; 112 static std::string SYNC_DISPATCHER_TAG; 113 static std::string DELAY_DISPATCHER_TAG; 114 115 std::atomic<bool> running_; 116 ConcurrentQueue<std::shared_ptr<Task>> workingTasks_; 117 118 std::shared_ptr<TaskExecutor> executor_; 119 std::mutex mutex_; 120 }; 121 } // namespace AppExecFwk 122 } // namespace OHOS 123 124 #endif 125