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_TASK_H 17 #define PANDA_LIBPANDABASE_TASKMANAGER_TASK_H 18 19 #include "libpandabase/macros.h" 20 #include "libpandabase/globals.h" 21 #include <functional> 22 #include <ostream> 23 #include <array> 24 25 namespace panda::taskmanager { 26 27 /* 28 * TaskType - represents all types of components that can use TaskManager. 29 * UNKNOWN type - is a service type, it should be the last one in the list. 30 */ 31 enum class TaskType : uint8_t { GC, JIT, UNKNOWN }; 32 constexpr auto ALL_TASK_TYPES = std::array {TaskType::GC, TaskType::JIT}; 33 static_assert(ALL_TASK_TYPES.size() == static_cast<size_t>(TaskType::UNKNOWN)); 34 static_assert(std::is_same<decltype(ALL_TASK_TYPES)::value_type, TaskType>::value); 35 36 /* 37 * VMType - represents all types of VM that can use TaskManager. 38 * UNKNOWN type - is a service type, it should be the last one in the list. 39 */ 40 enum class VMType : uint8_t { DYNAMIC_VM, STATIC_VM, UNKNOWN }; 41 constexpr auto ALL_VM_TYPES = std::array {VMType::DYNAMIC_VM, VMType::STATIC_VM}; 42 static_assert(ALL_VM_TYPES.size() == static_cast<size_t>(VMType::UNKNOWN)); 43 static_assert(std::is_same<decltype(ALL_VM_TYPES)::value_type, VMType>::value); 44 45 /* 46 * TaskExecutionMode - represents all possible modes of tasks execution. 47 * UNKNOWN type - is a service type, it should be the last one in the list. 48 */ 49 enum class TaskExecutionMode : uint8_t { FOREGROUND, BACKGROUND, UNKNOWN }; 50 constexpr auto ALL_TASK_EXECUTION_MODES = std::array {TaskExecutionMode::FOREGROUND, TaskExecutionMode::BACKGROUND}; 51 static_assert(ALL_TASK_EXECUTION_MODES.size() == static_cast<size_t>(TaskExecutionMode::UNKNOWN)); 52 static_assert(std::is_same<decltype(ALL_TASK_EXECUTION_MODES)::value_type, TaskExecutionMode>::value); 53 54 /** 55 * @brief TaskProperties is class that consider all enums that are related to Task. It's used to parameterize task 56 * creation. 57 */ 58 class TaskProperties { 59 public: TaskProperties(TaskType taskType,VMType vmType,TaskExecutionMode executionMode)60 constexpr TaskProperties(TaskType taskType, VMType vmType, TaskExecutionMode executionMode) 61 : taskType_(taskType), vmType_(vmType), executionMode_(executionMode) 62 { 63 } 64 GetTaskType()65 constexpr TaskType GetTaskType() const 66 { 67 return taskType_; 68 } 69 GetVMType()70 constexpr VMType GetVMType() const 71 { 72 return vmType_; 73 } 74 GetTaskExecutionMode()75 constexpr TaskExecutionMode GetTaskExecutionMode() const 76 { 77 return executionMode_; 78 } 79 80 friend constexpr bool operator==(const TaskProperties &lv, const TaskProperties &rv) 81 { 82 return lv.taskType_ == rv.taskType_ && lv.vmType_ == rv.vmType_ && lv.executionMode_ == rv.executionMode_; 83 } 84 85 class Hash { 86 public: 87 constexpr Hash() = default; operator()88 constexpr uint32_t operator()(const TaskProperties &properties) const 89 { 90 return (static_cast<uint32_t>(properties.taskType_) << (2U * sizeof(uint8_t) * BITS_PER_BYTE)) | 91 (static_cast<uint32_t>(properties.vmType_) << (sizeof(uint8_t) * BITS_PER_BYTE)) | 92 (static_cast<uint32_t>(properties.executionMode_)); 93 } 94 }; 95 96 private: 97 TaskType taskType_; 98 VMType vmType_; 99 TaskExecutionMode executionMode_; 100 }; 101 102 class Task { 103 public: 104 NO_COPY_SEMANTIC(Task); 105 DEFAULT_MOVE_SEMANTIC(Task); 106 107 using RunnerCallback = std::function<void()>; 108 109 /** 110 * @brief Tasks are created through this method with the specified arguments. 111 * @param properties - properties of task, it contains TaskType, VMType and ExecutionMote. 112 * @param runner - body of task, that will be executed. 113 */ 114 [[nodiscard]] PANDA_PUBLIC_API static Task Create(TaskProperties properties, RunnerCallback runner); 115 116 /// @brief Returns properties of task 117 PANDA_PUBLIC_API TaskProperties GetTaskProperties() const; 118 119 /// @brief Executes body of task 120 PANDA_PUBLIC_API void RunTask(); 121 122 PANDA_PUBLIC_API ~Task() = default; 123 124 private: 125 Task(TaskProperties properties, RunnerCallback runner); 126 127 TaskProperties properties_; 128 RunnerCallback runner_; 129 }; 130 131 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, TaskType type); 132 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, VMType type); 133 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, TaskExecutionMode mode); 134 PANDA_PUBLIC_API std::ostream &operator<<(std::ostream &os, TaskProperties prop); 135 136 } // namespace panda::taskmanager 137 138 #endif // PANDA_LIBPANDABASE_TASKMANAGER_TASK_H 139