1 /*
2 * Copyright (c) 2023-2025 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_TASK_QUEUE_INTERFACE_H
17 #define PANDA_LIBPANDABASE_TASKMANAGER_TASK_QUEUE_INTERFACE_H
18
19 #include <atomic>
20
21 #include "libpandabase/taskmanager/task_manager_common.h"
22 #include "libpandabase/taskmanager/utils/wait_list.h"
23
24 namespace ark::taskmanager {
25
26 /**
27 * @brief TaskQueueInteface is an interface of push-thread-safe queue for tasks. Queues can be registered in
28 * TaskScheduler and used to execute tasks on workers. Also, queues can notify other threads when a new task is pushed.
29 */
30 class TaskQueueInterface {
31 public:
32 NO_COPY_SEMANTIC(TaskQueueInterface);
33 NO_MOVE_SEMANTIC(TaskQueueInterface);
34
priority_(priority)35 PANDA_PUBLIC_API explicit TaskQueueInterface(QueuePriority priority = DEFAULT_QUEUE_PRIORITY) : priority_(priority)
36 {
37 TASK_MANAGER_CHECK_PRIORITY_VALUE(priority);
38 }
39 PANDA_PUBLIC_API virtual ~TaskQueueInterface() = default;
40
41 PANDA_PUBLIC_API virtual size_t AddForegroundTask(RunnerCallback runner) = 0;
42 PANDA_PUBLIC_API virtual size_t AddBackgroundTask(RunnerCallback runner) = 0;
43
44 PANDA_PUBLIC_API virtual WaiterId AddForegroundTaskInWaitList(RunnerCallback runtime, uint64_t timeToWait) = 0;
45 PANDA_PUBLIC_API virtual WaiterId AddBackgroundTaskInWaitList(RunnerCallback runtime, uint64_t timeToWait) = 0;
46
47 PANDA_PUBLIC_API virtual WaiterId AddForegroundTaskInWaitList(RunnerCallback runtime) = 0;
48 PANDA_PUBLIC_API virtual WaiterId AddBackgroundTaskInWaitList(RunnerCallback runtime) = 0;
49
50 PANDA_PUBLIC_API virtual void SignalWaitList(WaiterId id) = 0;
51
52 [[nodiscard]] PANDA_PUBLIC_API virtual bool IsEmpty() const = 0;
53 [[nodiscard]] PANDA_PUBLIC_API virtual bool HasForegroundTasks() const = 0;
54 [[nodiscard]] PANDA_PUBLIC_API virtual bool HasBackgroundTasks() const = 0;
55
56 [[nodiscard]] PANDA_PUBLIC_API virtual size_t Size() const = 0;
57 [[nodiscard]] PANDA_PUBLIC_API virtual size_t CountOfForegroundTasks() const = 0;
58 [[nodiscard]] PANDA_PUBLIC_API virtual size_t CountOfBackgroundTasks() const = 0;
59
60 PANDA_PUBLIC_API size_t virtual ExecuteTask() = 0;
61 PANDA_PUBLIC_API size_t virtual ExecuteForegroundTask() = 0;
62 PANDA_PUBLIC_API size_t virtual ExecuteBackgroundTask() = 0;
63
64 PANDA_PUBLIC_API void virtual WaitTasks() = 0;
65 PANDA_PUBLIC_API void virtual WaitForegroundTasks() = 0;
66 PANDA_PUBLIC_API void virtual WaitBackgroundTasks() = 0;
67
68 void SetPriority(QueuePriority priority);
69 QueuePriority GetPriority() const;
70
71 void Register(QueueId id);
72 QueueId GetQueueId() const;
73
74 private:
75 QueueId id_ = INVALID_ID;
76 std::atomic<QueuePriority> priority_;
77 };
78
GetPriority()79 inline QueuePriority TaskQueueInterface::GetPriority() const
80 {
81 // Atomic with relaxed order reason: no order dependency with another variables
82 return priority_.load(std::memory_order_relaxed);
83 }
84
SetPriority(QueuePriority priority)85 inline void TaskQueueInterface::SetPriority(QueuePriority priority)
86 {
87 TASK_MANAGER_CHECK_PRIORITY_VALUE(priority);
88 // Atomic with relaxed order reason: no order dependency with another variables
89 priority_.store(priority, std::memory_order_relaxed);
90 }
91
GetQueueId()92 inline QueueId TaskQueueInterface::GetQueueId() const
93 {
94 return id_;
95 }
96
Register(QueueId id)97 inline void TaskQueueInterface::Register(QueueId id)
98 {
99 ASSERT_PRINT(id_ == INVALID_ID, "Second registration");
100 id_ = id;
101 }
102
103 } // namespace ark::taskmanager
104
105 #endif // PANDA_LIBPANDABASE_TASKMANAGER_TASK_QUEUE_INTERFACE_H
106