1 /* 2 * Copyright (c) 2021-2023 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 BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_FFRT_H 17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_FFRT_H 18 19 #include "ffrt.h" 20 #include "event_queue.h" 21 #include "event_handler.h" 22 23 #define LOCAL_API __attribute__((visibility ("hidden"))) 24 namespace OHOS { 25 namespace AppExecFwk { 26 class EventHandler; 27 28 class EventQueueFFRT : public EventQueue { 29 public: 30 31 EventQueueFFRT(); 32 explicit EventQueueFFRT(const std::shared_ptr<IoWaiter> &ioWaiter); 33 ~EventQueueFFRT(); 34 DISALLOW_COPY_AND_MOVE(EventQueueFFRT); 35 36 /** 37 * Insert an event into event queue with different priority. 38 * The events will be sorted by handle time. 39 * 40 * @param event Event instance which should be added into event queue. 41 * @param Priority Priority of the event 42 * @param insertType The type of insertint event to queue 43 * 44 * @see #Priority 45 */ 46 LOCAL_API void Insert(InnerEvent::Pointer &event, Priority priority = Priority::LOW, 47 EventInsertType insertType = EventInsertType::AT_END) override; 48 49 /** 50 * Remove events if its owner is invalid. 51 */ 52 LOCAL_API void RemoveOrphanByHandlerId(const std::string& handlerId) override; 53 54 /** 55 * Remove all events. 56 */ 57 LOCAL_API void RemoveAll() override; 58 59 /** 60 * Remove events with specified requirements. 61 * 62 * @param owner Owner of the event which is point to an instance of 'EventHandler'. 63 */ 64 LOCAL_API void Remove(const std::shared_ptr<EventHandler> &owner) override; 65 66 /** 67 * Remove events with specified requirements. 68 * 69 * @param owner Owner of the event which is point to an instance of 'EventHandler'. 70 * @param innerEventId Remove events by event id. 71 */ 72 LOCAL_API void Remove(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId) override; 73 74 /** 75 * Remove events with specified requirements. 76 * 77 * @param owner Owner of the event which is point to an instance of 'EventHandler'. 78 * @param innerEventId Remove events by event id. 79 * @param param Remove events by value of param. 80 */ 81 LOCAL_API void Remove(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId, int64_t param) override; 82 83 /** 84 * Remove events with specified requirements. 85 * 86 * @param owner Owner of the event which is point to an instance of 'EventHandler'. 87 * @param name Remove events by name of the task. 88 */ 89 LOCAL_API bool Remove(const std::shared_ptr<EventHandler> &owner, const std::string &name) override; 90 91 /** 92 * Prints out the internal information about an object in the specified format, 93 * helping you diagnose internal errors of the object. 94 * 95 * @param dumper The Dumper object you have implemented to process the output internal information. 96 */ 97 LOCAL_API void Dump(Dumper &dumper) override; 98 99 /** 100 * Print out the internal information about an object in the specified format, 101 * helping you diagnose internal errors of the object. 102 * 103 * @param queueInfo queue Info. 104 */ 105 LOCAL_API void DumpQueueInfo(std::string& queueInfo) override; 106 107 /** 108 * Checks whether the current EventHandler is idle. 109 * 110 * @return Returns true if all events have been processed; returns false otherwise. 111 */ 112 LOCAL_API bool IsIdle() override; 113 114 /** 115 * Check whether this event queue is empty. 116 * 117 * @return If queue is empty return true otherwise return false. 118 */ 119 LOCAL_API bool IsQueueEmpty() override; 120 121 /** 122 * Check whether an event with the given ID can be found among the events that have been sent but not processed. 123 * 124 * @param owner Owner of the event which is point to an instance of 'EventHandler'. 125 * @param innerEventId The id of the event. 126 */ 127 LOCAL_API bool HasInnerEvent(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId) override; 128 129 /** 130 * Check whether an event carrying the given param can be found among the events that have been sent but not 131 * processed. 132 * 133 * @param owner The owner of the event which is point to an instance of 'EventHandler'. 134 * @param param The basic parameter of the event. 135 */ 136 LOCAL_API bool HasInnerEvent(const std::shared_ptr<EventHandler> &owner, int64_t param) override; 137 138 LOCAL_API bool HasPreferEvent(int basePrio) override; 139 140 LOCAL_API std::string DumpCurrentQueueSize() override; 141 142 LOCAL_API void* GetFfrtQueue() override; 143 144 /** 145 * Insert task to ffrt queue, and wait to handled, only for ffrt thread mode. 146 */ 147 LOCAL_API void InsertSyncEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW, 148 EventInsertType insertType = EventInsertType::AT_END) override; 149 150 LOCAL_API PendingTaskInfo QueryPendingTaskInfo(int32_t fileDescriptor) override; 151 152 /** 153 * Cancel And Wait. 154 */ 155 LOCAL_API void CancelAndWait() override; 156 157 private: 158 LOCAL_API void InsertEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW, bool syncWait = false, 159 EventInsertType insertType = EventInsertType::AT_END); 160 LOCAL_API void SubmitEventAtEnd(InnerEvent::Pointer &event, Priority priority, bool syncWait, 161 const std::string &taskName, std::unique_lock<std::mutex> &lock); 162 LOCAL_API void SubmitEventAtFront(InnerEvent::Pointer &event, Priority priority, bool syncWait, 163 const std::string &taskName, std::unique_lock<std::mutex> &lock); 164 165 std::shared_ptr<ffrt::queue> ffrtQueue_ = nullptr; 166 }; 167 } // namespace AppExecFwk 168 } // namespace OHOS 169 170 #endif // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_FFRT_H 171