• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 LIBPANDABASE_TASKMANAGER_UTILS_TASK_SELECTOR_H
17 #define LIBPANDABASE_TASKMANAGER_UTILS_TASK_SELECTOR_H
18 
19 #include "libpandabase/taskmanager/schedulable_task_queue_interface.h"
20 #include <vector>
21 #include <map>
22 
23 namespace ark::taskmanager::internal {
24 
25 class TaskSelector {
26     /// PQPair is pair of priority and queue id. It's used in heap.
27     using PQPair = std::pair<size_t, TaskQueueId>;
28 
29 public:
30     explicit TaskSelector(const std::map<TaskQueueId, internal::SchedulableTaskQueueInterface *> &taskQueues);
31 
32     /// @brief inits selection after adding all queues
33     void Init();
34 
35     /**
36      * @brief Method next TaskQueue to pop tasks from it
37      * @returns TaskQueueId to pop if it's exists, else returns INVALID_TASKQUEUE_ID
38      */
39     TaskQueueId SelectQueue();
40 
41 private:
42     /// taskQueues_ is ref on map from id to pointer on queue
43     const std::map<TaskQueueId, internal::SchedulableTaskQueueInterface *> &taskQueues_;
44 
45     /**
46      * priorityQueues is a heap with PQPair elements type.
47      * It's used for priority searching of queues.
48      */
49     std::vector<PQPair> priorityQueue_;
50 };
51 
52 }  // namespace ark::taskmanager::internal
53 
54 #endif  // LIBPANDABASE_TASKMANAGER_UTILS_TASK_SELECTOR_H
55