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