1 /* 2 * Copyright (c) 2022 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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_H 16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_H 17 18 #include <memory> 19 #include <list> 20 21 #include "work_status.h" 22 #include "detector_value.h" 23 #include "ffrt.h" 24 25 namespace OHOS { 26 namespace WorkScheduler { 27 class WorkQueue { 28 public: 29 explicit WorkQueue() = default; 30 ~WorkQueue() = default; 31 /** 32 * @brief The OnConditionChanged callback. 33 * 34 * @param type The type. 35 * @param conditionVal The condition val. 36 */ 37 std::vector<std::shared_ptr<WorkStatus>> OnConditionChanged( 38 WorkCondition::Type type, std::shared_ptr<DetectorValue> conditionVal); 39 /** 40 * @brief ParseCondition. 41 * 42 * @param type The type. 43 * @param conditionVal The condition val. 44 */ 45 std::shared_ptr<Condition> ParseCondition(WorkCondition::Type type, 46 std::shared_ptr<DetectorValue> conditionVal); 47 /** 48 * @brief Push. 49 * 50 * @param workStatusVector The work status vector. 51 */ 52 void Push(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector); 53 /** 54 * @brief Push. 55 * 56 * @param workStatus The status of work. 57 */ 58 void Push(std::shared_ptr<WorkStatus> workStatus); 59 /** 60 * @brief Get work to run by priority. 61 * 62 * @return The status of work. 63 */ 64 std::shared_ptr<WorkStatus> GetWorkToRunByPriority(); 65 /** 66 * @brief Remove. 67 * 68 * @param workStatus The status of work. 69 * @return True if success,else false. 70 */ 71 bool Remove(std::shared_ptr<WorkStatus> workStatus); 72 /** 73 * @brief Contains. 74 * 75 * @param workId The id of work. 76 * @return True if success,else false. 77 */ 78 bool Contains(std::shared_ptr<std::string> workId); 79 /** 80 * @brief Find. 81 * 82 * @param workId The id of work. 83 * @return The id of work. 84 */ 85 std::shared_ptr<WorkStatus> Find(std::string workId); 86 /** 87 * @brief Find SA. 88 * 89 * @param saId The id of sa. 90 * @return The SA of saId. 91 */ 92 std::shared_ptr<WorkStatus> FindSA(int32_t saId); 93 /** 94 * @brief Get size. 95 * 96 * @return The work list size. 97 */ 98 uint32_t GetSize(); 99 /** 100 * @brief Cancel work. 101 * 102 * @param workStatus The status of work. 103 * @return True if success,else false. 104 */ 105 bool CancelWork(std::shared_ptr<WorkStatus> workStatus); 106 /** 107 * @brief Get the list of work. 108 * 109 * @return The list of work. 110 */ 111 std::list<std::shared_ptr<WorkStatus>> GetWorkList(); 112 /** 113 * @brief Remove unready. 114 */ 115 void RemoveUnReady(); 116 /** 117 * @brief Get the count of running. 118 * 119 * @return The count of running. 120 */ 121 int32_t GetRunningCount(); 122 /** 123 * @brief Get work info of running tasks. 124 * 125 * @return get all running works. 126 */ 127 std::vector<WorkInfo> GetRunningWorks(); 128 /** 129 * @brief Get All DeepIdle Works. 130 * 131 * @return All DeepIdle Works; 132 */ 133 std::list<std::shared_ptr<WorkStatus>> GetDeepIdleWorks(); 134 /** 135 * @brief Get work id str. 136 * 137 * @param result The result. 138 */ 139 void GetWorkIdStr(std::string& result); 140 /** 141 * @brief Dump. 142 * 143 * @param result The result. 144 */ 145 void Dump(std::string& result); 146 /** 147 * @brief Clear all. 148 */ 149 void ClearAll(); 150 /** 151 * @brief Set min interval by dump. 152 */ 153 void SetMinIntervalByDump(int64_t interval); 154 bool Find(const int32_t userId, const std::string &bundleName); 155 private: 156 ffrt::recursive_mutex workListMutex_; 157 std::list<std::shared_ptr<WorkStatus>> workList_; 158 }; 159 class WorkComp { 160 public: 161 /** 162 * @brief operator. 163 * 164 * @param w1 The w1. 165 * @param w2 The w2. 166 * @return True if success,else false. 167 */ 168 bool operator () (const std::shared_ptr<WorkStatus> w1, const std::shared_ptr<WorkStatus> w2); 169 }; 170 } // namespace WorkScheduler 171 } // namespace OHOS 172 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_H