• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 bool 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 bool 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     /**
158      * Add file descriptor listener for a file descriptor.
159      *
160      * @param fileDescriptor File descriptor.
161      * @param events Events from file descriptor, such as input, output, error
162      * @param listener Listener callback.
163      * @return Return 'ERR_OK' on success.
164      */
165     ErrCode AddFileDescriptorListener(int32_t fileDescriptor, uint32_t events,
166         const std::shared_ptr<FileDescriptorListener> &listener, const std::string &taskName,
167         Priority priority = Priority::HIGH) override;
168 
169     /**
170      * Remove all file descriptor listeners for a specified owner.
171      *
172      * @param owner Owner of the event which is point to an instance of 'FileDescriptorListener'.
173      */
174     void RemoveFileDescriptorListener(const std::shared_ptr<EventHandler> &owner) override;
175 
176     /**
177      * Remove file descriptor listener for a file descriptor.
178      *
179      * @param fileDescriptor File descriptor.
180      */
181     void RemoveFileDescriptorListener(int32_t fileDescriptor) override;
182 
183     /**
184      * Prepare event queue, before calling {@link #GetEvent}.
185      * If {@link #Finish} is called, prepare event queue again, before calling {@link #GetEvent}.
186      */
187     void Prepare() override;
188 
189     /**
190      * Exit from blocking in {@link #GetEvent}, and mark the event queue finished.
191      * After calling {@link #Finish}, {@link #GetEvent} never returns any event, until {@link #Prepare} is called.
192      */
193     void Finish() override;
194 
195     void HandleFileDescriptorEvent(int32_t fileDescriptor, uint32_t events, const std::string &name,
196         Priority priority);
197 
198     /**
199      * Obtain the first event of the specified priority queue. This interface was disabled in ffrt queue.
200      *
201      * @param priority Specify priority.
202      * @return Return UINT64_MAX as invalid value.
203      */
204     inline uint64_t GetQueueFirstEventHandleTime(int32_t priority) override;
205 
206     /**
207      * set queue usable status.
208      *
209      * @param usable current usable.
210      */
211     void SetUsable(bool usable);
212 private:
213     LOCAL_API bool InsertEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW, bool syncWait = false,
214         EventInsertType insertType = EventInsertType::AT_END);
215     LOCAL_API bool SubmitEventAtEnd(InnerEvent::Pointer &event, Priority priority, bool syncWait,
216         const std::string &taskName, std::unique_lock<ffrt::mutex> &lock);
217     LOCAL_API bool SubmitEventAtFront(InnerEvent::Pointer &event, Priority priority, bool syncWait,
218         const std::string &taskName, std::unique_lock<ffrt::mutex> &lock);
219 
220     std::shared_ptr<ffrt::queue> ffrtQueue_ = nullptr;
221 
222     ffrt::mutex ffrtLock_;
223 };
224 }  // namespace AppExecFwk
225 }  // namespace OHOS
226 
227 #endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_FFRT_H
228