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 Push. 40 * 41 * @param workStatusVector The work status vector. 42 */ 43 void Push(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector); 44 /** 45 * @brief Push. 46 * 47 * @param workStatus The status of work. 48 */ 49 void Push(std::shared_ptr<WorkStatus> workStatus); 50 /** 51 * @brief Get work to run by priority. 52 * 53 * @return The status of work. 54 */ 55 std::shared_ptr<WorkStatus> GetWorkToRunByPriority(); 56 /** 57 * @brief Remove. 58 * 59 * @param workStatus The status of work. 60 * @return True if success,else false. 61 */ 62 bool Remove(std::shared_ptr<WorkStatus> workStatus); 63 /** 64 * @brief Contains. 65 * 66 * @param workId The id of work. 67 * @return True if success,else false. 68 */ 69 bool Contains(std::shared_ptr<std::string> workId); 70 /** 71 * @brief Find. 72 * 73 * @param workId The id of work. 74 * @return The id of work. 75 */ 76 std::shared_ptr<WorkStatus> Find(std::string workId); 77 /** 78 * @brief Get size. 79 * 80 * @return The work list size. 81 */ 82 uint32_t GetSize(); 83 /** 84 * @brief Cancel work. 85 * 86 * @param workStatus The status of work. 87 * @return True if success,else false. 88 */ 89 bool CancelWork(std::shared_ptr<WorkStatus> workStatus); 90 /** 91 * @brief Get the list of work. 92 * 93 * @return The list of work. 94 */ 95 std::list<std::shared_ptr<WorkStatus>> GetWorkList(); 96 /** 97 * @brief Remove unready. 98 */ 99 void RemoveUnReady(); 100 /** 101 * @brief Get the count of running. 102 * 103 * @return The count of running. 104 */ 105 int32_t GetRunningCount(); 106 /** 107 * @brief Get work id str. 108 * 109 * @param result The result. 110 */ 111 void GetWorkIdStr(std::string& result); 112 /** 113 * @brief Dump. 114 * 115 * @param result The result. 116 */ 117 void Dump(std::string& result); 118 /** 119 * @brief Clear all. 120 */ 121 void ClearAll(); 122 /** 123 * @brief Set min interval by dump. 124 */ 125 void SetMinIntervalByDump(int64_t interval); 126 private: 127 std::list<std::shared_ptr<WorkStatus>> workList_; 128 }; 129 class WorkComp { 130 public: 131 /** 132 * @brief operator. 133 * 134 * @param w1 The w1. 135 * @param w2 The w2. 136 * @return True if success,else false. 137 */ 138 bool operator () (const std::shared_ptr<WorkStatus> w1, const std::shared_ptr<WorkStatus> w2); 139 }; 140 } // namespace WorkScheduler 141 } // namespace OHOS 142 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_H