1 /* 2 * Copyright (c) 2023-2024 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 PANDA_LIBPANDABASE_TASKMANAGER_SCHEDULABLE_TASK_QUEUE_INTERFACE_H 17 #define PANDA_LIBPANDABASE_TASKMANAGER_SCHEDULABLE_TASK_QUEUE_INTERFACE_H 18 19 #include "libpandabase/taskmanager/task_queue_interface.h" 20 #include <optional> 21 22 namespace ark::taskmanager::internal { 23 24 class SchedulableTaskQueueInterface : public TaskQueueInterface { 25 public: 26 NO_COPY_SEMANTIC(SchedulableTaskQueueInterface); 27 NO_MOVE_SEMANTIC(SchedulableTaskQueueInterface); 28 29 /** 30 * NewTasksCallback instance should be called after tasks adding. It should have next arguments: 31 * 1. property of tasks you added 32 * 2. count of tasks you added 33 */ 34 using NewTasksCallback = std::function<void(TaskProperties, size_t)>; 35 using SignalWorkersCallback = std::function<void()>; 36 37 using AddTaskToWorkerFunc = std::function<void(Task &&)>; 38 using AddTaskToHelperFunc = std::function<void(Task &&)>; 39 40 using GetSplittingFactorFunc = std::function<size_t()>; 41 SchedulableTaskQueueInterface(TaskType taskType,VMType vmType,uint8_t priority)42 SchedulableTaskQueueInterface(TaskType taskType, VMType vmType, uint8_t priority) 43 : TaskQueueInterface(taskType, vmType, priority) 44 { 45 } 46 ~SchedulableTaskQueueInterface() override = default; 47 48 /** 49 * @brief The method adds a task to the queue without execution the new task callback. This method should only be 50 * used with tasks that have already triggered this callback. 51 * @param task: instance of Task 52 */ 53 void virtual AddTaskWithoutNewTaskCallbackExecution(Task &&task) = 0; 54 55 /** 56 * @brief Pops task from task queue. Operation is thread-safe. The method will wait new task if queue is empty and 57 * method WaitForQueueEmptyAndFinish has not been executed. Otherwise it will return std::nullopt. 58 */ 59 [[nodiscard]] virtual std::optional<Task> PopTask() = 0; 60 61 /** 62 * @brief Pops task from task queue with specified execution mode. Operation is thread-safe. The method will wait 63 * new task if queue with specified execution mode is empty and method WaitForQueueEmptyAndFinish has not been 64 * executed. Otherwise it will return std::nullopt. 65 * @param mode - execution mode of task that we want to pop. 66 */ 67 [[nodiscard]] virtual std::optional<Task> PopTask(TaskExecutionMode mode) = 0; 68 69 /** 70 * @brief Method pops several tasks to worker. 71 * @param addTaskFunc - Functor that will be used to add popped tasks to worker 72 * @param size - Count of tasks you want to pop. If it is greater then count of tasks that are stored in queue, 73 * method will not wait and will pop all stored tasks. 74 * @return count of task that was added to worker 75 */ 76 size_t virtual PopTasksToWorker(const AddTaskToWorkerFunc &addTaskFunc, size_t size) = 0; 77 78 /** 79 * @brief Method pops several tasks to helper thread. Helper thread in TaskScheduler is the thread that uses 80 * HelpWorkersWithTasks method. 81 * @param addTaskFunc - Functor that will be used to add popped tasks to helper 82 * @param size - Count of tasks you want to pop. If it is greater then count of tasks that are stored in queue, 83 * method will not wait and will pop all stored tasks. 84 * @param mode - Execution mode of task you wast to pop 85 * @return count of task that was added to helper 86 */ 87 size_t virtual PopTasksToHelperThread(const AddTaskToHelperFunc &addTaskFunc, size_t size, 88 TaskExecutionMode mode) = 0; 89 90 /** 91 * @brief This method saves the @arg callback. 92 * @param newTaskCallback - function that get count of inputted tasks and uses in AddTask method. 93 * @param signalWorkers - function that should signal workers to return to work if it's needed 94 */ 95 void virtual SetCallbacks(NewTasksCallback newTaskCallback, SignalWorkersCallback signalWorkersCallback) = 0; 96 97 /// @brief Removes callback function. 98 void virtual UnsetCallbacks() = 0; 99 }; 100 101 } // namespace ark::taskmanager::internal 102 103 #endif // PANDA_LIBPANDABASE_TASKMANAGER_SCHEDULABLE_TASK_QUEUE_INTERFACE_H 104