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