• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H
17 #define PANDA_LIBPANDABASE_TASKMANAGER_TASK_H
18 
19 #include "libpandabase/macros.h"
20 #include "libpandabase/taskmanager/task_manager_common.h"
21 
22 #include <memory>
23 
24 namespace ark::taskmanager {
25 class TaskQueueInterface;
26 }  // namespace ark::taskmanager
27 
28 namespace ark::taskmanager::internal {
29 
30 /**
31  * @brief TaskLifeTimeAggregator structure used to save and log life time of task and execution time.
32  * It's methods doesn't works on release build.
33  */
34 class TaskLifeTimeAggregator {
35 public:
36     NO_COPY_SEMANTIC(TaskLifeTimeAggregator);
37     DEFAULT_MOVE_SEMANTIC(TaskLifeTimeAggregator);
38     PANDA_PUBLIC_API TaskLifeTimeAggregator() = default;
39     PANDA_PUBLIC_API ~TaskLifeTimeAggregator() = default;
40     /// @brief Saves time of task adding in queue.
41     PANDA_PUBLIC_API void GetAndStoreTimeOfTaskAddingToQueue();
42     /// @brief Saves start time of execution. Use this method before task execution start
43     PANDA_PUBLIC_API void GetAndStoreTimeOfTaskExecutionStart();
44     /// @brief Logs life time and execution time of task. Use this method after task execution end
45     PANDA_PUBLIC_API void GetTimeOfTaskExecutionFinishAndStoreTimeStats(TaskQueueInterface *queue);
46 
47 private:
48     // Time points for logs
49     uint64_t addingToQueueTime_ {0};
50     uint64_t startExecutionTime_ {0};
51 };
52 
53 class Task {
54 public:
55     using OnDestructionCallback = void (*)(TaskQueueInterface *);
56     using Ptr = std::unique_ptr<Task>;
57 
58     PANDA_PUBLIC_API ~Task();
59     NO_COPY_SEMANTIC(Task);
60     DEFAULT_MOVE_SEMANTIC(Task);
61 
62     /**
63      * @brief Tasks are created through this method with the specified arguments.
64      * @param properties - properties of task, it contains TaskType, VMType and ExecutionMote.
65      * @param runner - body of task, that will be executed.
66      */
67     [[nodiscard]] PANDA_PUBLIC_API static Ptr Create(RunnerCallback runner, TaskQueueInterface *queue,
68                                                      OnDestructionCallback callback);
69     /// @brief Executes body of task
70     PANDA_PUBLIC_API void RunTask();
71     /// @brief Makes task invalid, it should not be executed anymore.
72     PANDA_PUBLIC_API void MakeInvalid();
73     /// @brief Checks if task is invalid
74     PANDA_PUBLIC_API bool IsInvalid() const;
75 
76 private:
Task(RunnerCallback runner,TaskQueueInterface * queue,OnDestructionCallback callback)77     Task(RunnerCallback runner, TaskQueueInterface *queue, OnDestructionCallback callback)
78         : runner_(std::move(runner)), parentQueue_(queue), onDestructionCallback_(callback)
79     {
80         ASSERT(queue != nullptr);
81         ASSERT(callback != nullptr);
82         EventOnTaskAdding();
83     }
84 
85     PANDA_PUBLIC_API void EventOnTaskAdding();
86     PANDA_PUBLIC_API void EventOnStartExecution();
87     PANDA_PUBLIC_API void EventOnEndExecution();
88 
89     RunnerCallback runner_ {nullptr};
90     TaskQueueInterface *parentQueue_ {nullptr};
91     TaskLifeTimeAggregator lifeTimeStorage_;
92     OnDestructionCallback onDestructionCallback_ {nullptr};
93 };
94 
95 using TaskPtr = Task::Ptr;
96 
97 }  // namespace ark::taskmanager::internal
98 
99 #endif  // PANDA_LIBPANDABASE_TASKMANAGER_TASK_H
100