• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 panda::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      * 3. true of queue was empty before last tasks adding, otherwise false
34      */
35     using NewTasksCallback = std::function<void(TaskProperties, size_t, bool)>;
36 
37     using AddTaskToWorkerFunc = std::function<void(Task &&)>;
38 
SchedulableTaskQueueInterface(TaskType taskType,VMType vmType,uint8_t priority)39     SchedulableTaskQueueInterface(TaskType taskType, VMType vmType, uint8_t priority)
40         : TaskQueueInterface(taskType, vmType, priority)
41     {
42     }
43     ~SchedulableTaskQueueInterface() override = default;
44     /**
45      * @brief Pops task from task queue. Operation is thread-safe. The method will wait new task if queue is empty and
46      * method WaitForQueueEmptyAndFinish has not been executed. Otherwise it will return std::nullopt.
47      */
48     [[nodiscard]] virtual std::optional<Task> PopTask() = 0;
49 
50     /**
51      * @brief Pops task from task queue with specified execution mode. Operation is thread-safe. The method will wait
52      * new task if queue with specified execution mode is empty and method WaitForQueueEmptyAndFinish has not been
53      * executed. Otherwise it will return std::nullopt.
54      * @param mode - execution mode of task that we want to pop.
55      */
56     [[nodiscard]] virtual std::optional<Task> PopTask(TaskExecutionMode mode) = 0;
57 
58     /**
59      * @brief Method pops several tasks to worker.
60      * @param add_task_func - Functor that will be used to add popped tasks to worker
61      * @param size - Count of tasks you want to pop. If it is greater then count of tasks that are stored in queue,
62      * method will not wait and will pop all stored tasks.
63      * @return count of task that was added to worker
64      */
65     size_t virtual PopTasksToWorker(AddTaskToWorkerFunc addTaskFunc, size_t size) = 0;
66 
67     /**
68      * @brief This method sets the callback. It will be called after adding new task in AddTask method.
69      * @param callback - function that get count of inputted tasks.
70      */
71     void virtual SetNewTasksCallback(NewTasksCallback callback) = 0;
72 
73     /// @brief Removes callback function.
74     void virtual UnsetNewTasksCallback() = 0;
75 };
76 
77 }  // namespace panda::taskmanager::internal
78 
79 #endif  // PANDA_LIBPANDABASE_TASKMANAGER_SCHEDULABLE_TASK_QUEUE_INTERFACE_H
80