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 16 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_MANAGER_H 17 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_MANAGER_H 18 19 #include <memory> 20 #include <vector> 21 #include <map> 22 23 #include "conditions/icondition_listener.h" 24 #include "work_queue.h" 25 #include "work_status.h" 26 #include "ffrt.h" 27 28 namespace OHOS { 29 namespace WorkScheduler { 30 class WorkSchedulerService; 31 class WorkQueueManager : public std::enable_shared_from_this<WorkQueueManager> { 32 public: 33 explicit WorkQueueManager(const std::shared_ptr<WorkSchedulerService>& wss); 34 ~WorkQueueManager() = default; 35 /** 36 * @brief Init. 37 * 38 * @return True if success,else false. 39 */ 40 bool Init(); 41 /** 42 * @brief Add listener. 43 * 44 * @param type The type. 45 * @param listener The listener. 46 * @return True if success,else false. 47 */ 48 bool AddListener(WorkCondition::Type type, std::shared_ptr<IConditionListener> listener); 49 /** 50 * @brief Add work. 51 * 52 * @param workStatus The status of work. 53 * @return True if success,else false. 54 */ 55 bool AddWork(std::shared_ptr<WorkStatus> workStatus); 56 /** 57 * @brief Remove work. 58 * 59 * @param workStatus The status of work. 60 * @return True if success,else false. 61 */ 62 bool RemoveWork(std::shared_ptr<WorkStatus> workStatus); 63 /** 64 * @brief Cancel work. 65 * 66 * @param workStatus The status of work. 67 * @return True if success,else false. 68 */ 69 bool CancelWork(std::shared_ptr<WorkStatus> workStatus); 70 71 /** 72 * @brief The OnConditionChanged callback. 73 * 74 * @param conditionType The condition type. 75 * @param conditionVal The condition val. 76 */ 77 void OnConditionChanged(WorkCondition::Type conditionType, 78 std::shared_ptr<DetectorValue> conditionVal); 79 /** 80 * @brief Stop and clear works. 81 * 82 * @param workList The list of work. 83 * @return True if success,else false. 84 */ 85 bool StopAndClearWorks(std::list<std::shared_ptr<WorkStatus>> workList); 86 /** 87 * @brief Set time cycle. 88 * 89 * @param time The time. 90 */ 91 void SetTimeCycle(uint32_t time); 92 /** 93 * @brief Get the cycle of time. 94 * 95 * @return The cycle of time. 96 */ 97 uint32_t GetTimeCycle(); 98 /** 99 * @brief Set time retrigger. 100 * 101 * @param time The time. 102 */ 103 void SetTimeRetrigger(int32_t time); 104 /** 105 * @brief Get time retrigger. 106 * 107 * @param time The time. 108 */ 109 int32_t GetTimeRetrigger(); 110 /** 111 * @brief Dump. 112 * 113 * @param result The result. 114 */ 115 void Dump(std::string& result); 116 /** 117 * @brief Set min interval by dump. 118 */ 119 void SetMinIntervalByDump(int64_t interval); 120 private: 121 std::vector<std::shared_ptr<WorkStatus>> GetReayQueue(WorkCondition::Type conditionType, 122 std::shared_ptr<DetectorValue> conditionVal); 123 void PushWork(std::vector<std::shared_ptr<WorkStatus>> &works, std::vector<std::shared_ptr<WorkStatus>> &result); 124 void PrintWorkStatus(WorkCondition::Type conditionType); 125 void PrintAllWorkStatus(WorkCondition::Type conditionType); 126 127 private: 128 ffrt::mutex mutex_; 129 const std::weak_ptr<WorkSchedulerService> wss_; 130 std::map<WorkCondition::Type, std::shared_ptr<WorkQueue>> queueMap_; 131 std::map<WorkCondition::Type, std::shared_ptr<IConditionListener>> listenerMap_; 132 133 uint32_t timeCycle_; 134 }; 135 } // namespace WorkScheduler 136 } // namespace OHOS 137 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_MANAGER_H