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 Get size. 88 * 89 * @return The work list size. 90 */ 91 uint32_t GetSize(); 92 /** 93 * @brief Cancel work. 94 * 95 * @param workStatus The status of work. 96 * @return True if success,else false. 97 */ 98 bool CancelWork(std::shared_ptr<WorkStatus> workStatus); 99 /** 100 * @brief Get the list of work. 101 * 102 * @return The list of work. 103 */ 104 std::list<std::shared_ptr<WorkStatus>> GetWorkList(); 105 /** 106 * @brief Remove unready. 107 */ 108 void RemoveUnReady(); 109 /** 110 * @brief Get the count of running. 111 * 112 * @return The count of running. 113 */ 114 int32_t GetRunningCount(); 115 /** 116 * @brief Get work info of running tasks. 117 * 118 * @return get all running works. 119 */ 120 std::list<std::shared_ptr<WorkInfo>> GetRunningWorks(); 121 /** 122 * @brief Get All DeepIdle Works. 123 * 124 * @return All DeepIdle Works; 125 */ 126 std::list<std::shared_ptr<WorkStatus>> GetDeepIdleWorks(); 127 /** 128 * @brief Get work id str. 129 * 130 * @param result The result. 131 */ 132 void GetWorkIdStr(std::string& result); 133 /** 134 * @brief Dump. 135 * 136 * @param result The result. 137 */ 138 void Dump(std::string& result); 139 /** 140 * @brief Clear all. 141 */ 142 void ClearAll(); 143 /** 144 * @brief Set min interval by dump. 145 */ 146 void SetMinIntervalByDump(int64_t interval); 147 bool Find(const int32_t userId, const std::string &bundleName); 148 private: 149 ffrt::recursive_mutex workListMutex_; 150 std::list<std::shared_ptr<WorkStatus>> workList_; 151 }; 152 class WorkComp { 153 public: 154 /** 155 * @brief operator. 156 * 157 * @param w1 The w1. 158 * @param w2 The w2. 159 * @return True if success,else false. 160 */ 161 bool operator () (const std::shared_ptr<WorkStatus> w1, const std::shared_ptr<WorkStatus> w2); 162 }; 163 } // namespace WorkScheduler 164 } // namespace OHOS 165 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_H