1100ae2f9Sopenharmony_ci/*
2100ae2f9Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3100ae2f9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4100ae2f9Sopenharmony_ci * you may not use this file except in compliance with the License.
5100ae2f9Sopenharmony_ci * You may obtain a copy of the License at
6100ae2f9Sopenharmony_ci *
7100ae2f9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8100ae2f9Sopenharmony_ci *
9100ae2f9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10100ae2f9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11100ae2f9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12100ae2f9Sopenharmony_ci * See the License for the specific language governing permissions and
13100ae2f9Sopenharmony_ci * limitations under the License.
14100ae2f9Sopenharmony_ci */
15100ae2f9Sopenharmony_ci
16100ae2f9Sopenharmony_ci#ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_H
17100ae2f9Sopenharmony_ci#define BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_H
18100ae2f9Sopenharmony_ci
19100ae2f9Sopenharmony_ci#include <array>
20100ae2f9Sopenharmony_ci#include <functional>
21100ae2f9Sopenharmony_ci#include <list>
22100ae2f9Sopenharmony_ci#include <map>
23100ae2f9Sopenharmony_ci#include <mutex>
24100ae2f9Sopenharmony_ci
25100ae2f9Sopenharmony_ci#include "inner_event.h"
26100ae2f9Sopenharmony_ci#include "event_handler_errors.h"
27100ae2f9Sopenharmony_ci#include "file_descriptor_listener.h"
28100ae2f9Sopenharmony_ci#include "dumper.h"
29100ae2f9Sopenharmony_ci
30100ae2f9Sopenharmony_cinamespace OHOS {
31100ae2f9Sopenharmony_cinamespace AppExecFwk {
32100ae2f9Sopenharmony_ciclass IoWaiter;
33100ae2f9Sopenharmony_ciclass EventHandler;
34100ae2f9Sopenharmony_ciclass DeamonIoWaiter;
35100ae2f9Sopenharmony_cistruct PendingTaskInfo;
36100ae2f9Sopenharmony_ci
37100ae2f9Sopenharmony_cienum class EventInsertType: uint32_t {
38100ae2f9Sopenharmony_ci    // Insert event at end
39100ae2f9Sopenharmony_ci    AT_END = 0,
40100ae2f9Sopenharmony_ci    // Insert event at front
41100ae2f9Sopenharmony_ci    AT_FRONT
42100ae2f9Sopenharmony_ci};
43100ae2f9Sopenharmony_ci
44100ae2f9Sopenharmony_cienum class Observer {
45100ae2f9Sopenharmony_ci    ARKTS_GC,
46100ae2f9Sopenharmony_ci};
47100ae2f9Sopenharmony_ci
48100ae2f9Sopenharmony_cienum class EventRunnerStage {
49100ae2f9Sopenharmony_ci    // enter loop
50100ae2f9Sopenharmony_ci    STAGE_ENTRY_RUNNER = 1<<0,
51100ae2f9Sopenharmony_ci    // exit loop
52100ae2f9Sopenharmony_ci    STAGE_EXIT_RUNNER = 1<<1,
53100ae2f9Sopenharmony_ci    // waiting
54100ae2f9Sopenharmony_ci    STAGE_BEFORE_WAITING = 1<<2,
55100ae2f9Sopenharmony_ci    // recover form sleeping
56100ae2f9Sopenharmony_ci    STAGE_AFTER_WAITING = 1<<3,
57100ae2f9Sopenharmony_ci    // invaild key
58100ae2f9Sopenharmony_ci    STAGE_INVAILD = 0,
59100ae2f9Sopenharmony_ci};
60100ae2f9Sopenharmony_ci
61100ae2f9Sopenharmony_cistruct StageInfo {
62100ae2f9Sopenharmony_ci    // STAGE_BEFORE_WAITING, timesteap
63100ae2f9Sopenharmony_ci    int64_t timestamp;
64100ae2f9Sopenharmony_ci    // STAGE_BEFORE_WAITING, milliseconds
65100ae2f9Sopenharmony_ci    int32_t sleepTime;
66100ae2f9Sopenharmony_ci    // STAGE_AFTER_WAITING
67100ae2f9Sopenharmony_ci    int reason;
68100ae2f9Sopenharmony_ci};
69100ae2f9Sopenharmony_ci
70100ae2f9Sopenharmony_ciusing EventRunnerObserverCallBack = std::function<int(EventRunnerStage stage, const StageInfo* info)>;
71100ae2f9Sopenharmony_ci
72100ae2f9Sopenharmony_cistruct EventRunnerObserver {
73100ae2f9Sopenharmony_ci    Observer observer;
74100ae2f9Sopenharmony_ci    uint32_t stages;
75100ae2f9Sopenharmony_ci    EventRunnerObserverCallBack notifyCb;
76100ae2f9Sopenharmony_ci    void ClearObserver()
77100ae2f9Sopenharmony_ci    {
78100ae2f9Sopenharmony_ci        stages = static_cast<uint32_t>(EventRunnerStage::STAGE_INVAILD);
79100ae2f9Sopenharmony_ci        notifyCb = nullptr;
80100ae2f9Sopenharmony_ci    }
81100ae2f9Sopenharmony_ci};
82100ae2f9Sopenharmony_ci
83100ae2f9Sopenharmony_cistruct ObserverTrace {
84100ae2f9Sopenharmony_ci    std::string source;
85100ae2f9Sopenharmony_ci    std::string stage;
86100ae2f9Sopenharmony_ci    ObserverTrace() {};
87100ae2f9Sopenharmony_ci    ObserverTrace(std::string currentSource, std::string currentStage)
88100ae2f9Sopenharmony_ci        : source(currentSource), stage(currentStage) {}
89100ae2f9Sopenharmony_ci    std::string getTraceInfo()
90100ae2f9Sopenharmony_ci    {
91100ae2f9Sopenharmony_ci        std::string traceInfo;
92100ae2f9Sopenharmony_ci        traceInfo.append("Et-obs:");
93100ae2f9Sopenharmony_ci        if (stage.empty()) {
94100ae2f9Sopenharmony_ci            traceInfo.append(" ");
95100ae2f9Sopenharmony_ci        } else {
96100ae2f9Sopenharmony_ci            traceInfo.append(stage);
97100ae2f9Sopenharmony_ci        }
98100ae2f9Sopenharmony_ci        traceInfo.append(",");
99100ae2f9Sopenharmony_ci        if (!source.empty()) {
100100ae2f9Sopenharmony_ci            traceInfo.append(source);
101100ae2f9Sopenharmony_ci        }
102100ae2f9Sopenharmony_ci        return traceInfo;
103100ae2f9Sopenharmony_ci    }
104100ae2f9Sopenharmony_ci};
105100ae2f9Sopenharmony_ci
106100ae2f9Sopenharmony_ciclass EventQueue {
107100ae2f9Sopenharmony_cipublic:
108100ae2f9Sopenharmony_ci    // Priority for the events
109100ae2f9Sopenharmony_ci    enum class Priority : uint32_t {
110100ae2f9Sopenharmony_ci        // The highest priority queue, should be distributed until the tasks in the queue are completed.
111100ae2f9Sopenharmony_ci        VIP = 0,
112100ae2f9Sopenharmony_ci        // Event that should be distributed at once if possible.
113100ae2f9Sopenharmony_ci        IMMEDIATE,
114100ae2f9Sopenharmony_ci        // High priority event, sorted by handle time, should be distributed before low priority event.
115100ae2f9Sopenharmony_ci        HIGH,
116100ae2f9Sopenharmony_ci        // Normal event, sorted by handle time.
117100ae2f9Sopenharmony_ci        LOW,
118100ae2f9Sopenharmony_ci        // Event that should be distributed only if no other event right now.
119100ae2f9Sopenharmony_ci        IDLE,
120100ae2f9Sopenharmony_ci    };
121100ae2f9Sopenharmony_ci
122100ae2f9Sopenharmony_ci    EventQueue();
123100ae2f9Sopenharmony_ci    explicit EventQueue(const std::shared_ptr<IoWaiter> &ioWaiter);
124100ae2f9Sopenharmony_ci    virtual ~EventQueue();
125100ae2f9Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(EventQueue);
126100ae2f9Sopenharmony_ci
127100ae2f9Sopenharmony_ci    /**
128100ae2f9Sopenharmony_ci     * Insert an event into event queue with different priority.
129100ae2f9Sopenharmony_ci     * The events will be sorted by handle time.
130100ae2f9Sopenharmony_ci     *
131100ae2f9Sopenharmony_ci     * @param event Event instance which should be added into event queue.
132100ae2f9Sopenharmony_ci     * @param Priority Priority of the event
133100ae2f9Sopenharmony_ci     * @param insertType The type of insertint event to queue
134100ae2f9Sopenharmony_ci     *
135100ae2f9Sopenharmony_ci     * @see #Priority
136100ae2f9Sopenharmony_ci     */
137100ae2f9Sopenharmony_ci    virtual void Insert(InnerEvent::Pointer &event, Priority priority = Priority::LOW,
138100ae2f9Sopenharmony_ci        EventInsertType insertType = EventInsertType::AT_END) = 0;
139100ae2f9Sopenharmony_ci
140100ae2f9Sopenharmony_ci    /**
141100ae2f9Sopenharmony_ci     * Remove events if its owner is invalid, for base queue.
142100ae2f9Sopenharmony_ci     */
143100ae2f9Sopenharmony_ci    virtual void RemoveOrphan() {};
144100ae2f9Sopenharmony_ci
145100ae2f9Sopenharmony_ci    /**
146100ae2f9Sopenharmony_ci     * Remove events if its owner is invalid, for ffrt queue.
147100ae2f9Sopenharmony_ci     */
148100ae2f9Sopenharmony_ci    virtual void RemoveOrphanByHandlerId(const std::string& handlerId) { (void)handlerId; };
149100ae2f9Sopenharmony_ci
150100ae2f9Sopenharmony_ci    /**
151100ae2f9Sopenharmony_ci     * Remove all events.
152100ae2f9Sopenharmony_ci     */
153100ae2f9Sopenharmony_ci    virtual void RemoveAll() = 0;
154100ae2f9Sopenharmony_ci
155100ae2f9Sopenharmony_ci    /**
156100ae2f9Sopenharmony_ci     * Remove events with specified requirements.
157100ae2f9Sopenharmony_ci     *
158100ae2f9Sopenharmony_ci     * @param owner Owner of the event which is point to an instance of 'EventHandler'.
159100ae2f9Sopenharmony_ci     */
160100ae2f9Sopenharmony_ci    virtual void Remove(const std::shared_ptr<EventHandler> &owner) = 0;
161100ae2f9Sopenharmony_ci
162100ae2f9Sopenharmony_ci    /**
163100ae2f9Sopenharmony_ci     * Remove events with specified requirements.
164100ae2f9Sopenharmony_ci     *
165100ae2f9Sopenharmony_ci     * @param owner Owner of the event which is point to an instance of 'EventHandler'.
166100ae2f9Sopenharmony_ci     * @param innerEventId Remove events by event id.
167100ae2f9Sopenharmony_ci     */
168100ae2f9Sopenharmony_ci    virtual void Remove(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId) = 0;
169100ae2f9Sopenharmony_ci
170100ae2f9Sopenharmony_ci    /**
171100ae2f9Sopenharmony_ci     * Remove events with specified requirements.
172100ae2f9Sopenharmony_ci     *
173100ae2f9Sopenharmony_ci     * @param owner Owner of the event which is point to an instance of 'EventHandler'.
174100ae2f9Sopenharmony_ci     * @param innerEventId Remove events by event id.
175100ae2f9Sopenharmony_ci     * @param param Remove events by value of param.
176100ae2f9Sopenharmony_ci     */
177100ae2f9Sopenharmony_ci    virtual void Remove(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId, int64_t param) = 0;
178100ae2f9Sopenharmony_ci
179100ae2f9Sopenharmony_ci    /**
180100ae2f9Sopenharmony_ci     * Remove events with specified requirements.
181100ae2f9Sopenharmony_ci     *
182100ae2f9Sopenharmony_ci     * @param owner Owner of the event which is point to an instance of 'EventHandler'.
183100ae2f9Sopenharmony_ci     * @param name Remove events by name of the task.
184100ae2f9Sopenharmony_ci     */
185100ae2f9Sopenharmony_ci    virtual bool Remove(const std::shared_ptr<EventHandler> &owner, const std::string &name) = 0;
186100ae2f9Sopenharmony_ci
187100ae2f9Sopenharmony_ci    /**
188100ae2f9Sopenharmony_ci     * Add file descriptor listener for a file descriptor.
189100ae2f9Sopenharmony_ci     *
190100ae2f9Sopenharmony_ci     * @param fileDescriptor File descriptor.
191100ae2f9Sopenharmony_ci     * @param events Events from file descriptor, such as input, output, error
192100ae2f9Sopenharmony_ci     * @param listener Listener callback.
193100ae2f9Sopenharmony_ci     * @return Return 'ERR_OK' on success.
194100ae2f9Sopenharmony_ci     */
195100ae2f9Sopenharmony_ci    virtual ErrCode AddFileDescriptorListener(int32_t fileDescriptor, uint32_t events,
196100ae2f9Sopenharmony_ci        const std::shared_ptr<FileDescriptorListener> &listener, const std::string &taskName,
197100ae2f9Sopenharmony_ci        Priority priority = Priority::HIGH) = 0;
198100ae2f9Sopenharmony_ci
199100ae2f9Sopenharmony_ci    /**
200100ae2f9Sopenharmony_ci     * Remove all file descriptor listeners for a specified owner.
201100ae2f9Sopenharmony_ci     *
202100ae2f9Sopenharmony_ci     * @param owner Owner of the event which is point to an instance of 'FileDescriptorListener'.
203100ae2f9Sopenharmony_ci     */
204100ae2f9Sopenharmony_ci    virtual void RemoveFileDescriptorListener(const std::shared_ptr<EventHandler> &owner) = 0;
205100ae2f9Sopenharmony_ci
206100ae2f9Sopenharmony_ci    /**
207100ae2f9Sopenharmony_ci     * Remove file descriptor listener for a file descriptor.
208100ae2f9Sopenharmony_ci     *
209100ae2f9Sopenharmony_ci     * @param fileDescriptor File descriptor.
210100ae2f9Sopenharmony_ci     */
211100ae2f9Sopenharmony_ci    virtual void RemoveFileDescriptorListener(int32_t fileDescriptor) = 0;
212100ae2f9Sopenharmony_ci
213100ae2f9Sopenharmony_ci    /**
214100ae2f9Sopenharmony_ci     * Prepare event queue, before calling {@link #GetEvent}.
215100ae2f9Sopenharmony_ci     * If {@link #Finish} is called, prepare event queue again, before calling {@link #GetEvent}.
216100ae2f9Sopenharmony_ci     */
217100ae2f9Sopenharmony_ci    virtual void Prepare() = 0;
218100ae2f9Sopenharmony_ci
219100ae2f9Sopenharmony_ci    /**
220100ae2f9Sopenharmony_ci     * Exit from blocking in {@link #GetEvent}, and mark the event queue finished.
221100ae2f9Sopenharmony_ci     * After calling {@link #Finish}, {@link #GetEvent} never returns any event, until {@link #Prepare} is called.
222100ae2f9Sopenharmony_ci     */
223100ae2f9Sopenharmony_ci    virtual void Finish() = 0;
224100ae2f9Sopenharmony_ci
225100ae2f9Sopenharmony_ci    /**
226100ae2f9Sopenharmony_ci     * Get event from event queue one by one.
227100ae2f9Sopenharmony_ci     * Before calling this method, developers should call {@link #Prepare} first.
228100ae2f9Sopenharmony_ci     * If none should be handled right now, the thread will be blocked in this method.
229100ae2f9Sopenharmony_ci     * Call {@link #Finish} to exit from blocking.
230100ae2f9Sopenharmony_ci     *
231100ae2f9Sopenharmony_ci     * @return Returns nullptr if event queue is not prepared yet, or {@link #Finish} is called.
232100ae2f9Sopenharmony_ci     * Otherwise returns event instance.
233100ae2f9Sopenharmony_ci     */
234100ae2f9Sopenharmony_ci    virtual InnerEvent::Pointer GetEvent();
235100ae2f9Sopenharmony_ci
236100ae2f9Sopenharmony_ci    /**
237100ae2f9Sopenharmony_ci     * Get expired event from event queue one by one.
238100ae2f9Sopenharmony_ci     * Before calling this method, developers should call {@link #Prepare} first.
239100ae2f9Sopenharmony_ci     *
240100ae2f9Sopenharmony_ci     * @param nextExpiredTime Output the expired time for the next event.
241100ae2f9Sopenharmony_ci     * @return Returns nullptr if none in event queue is expired.
242100ae2f9Sopenharmony_ci     * Otherwise returns event instance.
243100ae2f9Sopenharmony_ci     */
244100ae2f9Sopenharmony_ci    virtual InnerEvent::Pointer GetExpiredEvent(InnerEvent::TimePoint &nextExpiredTime);
245100ae2f9Sopenharmony_ci
246100ae2f9Sopenharmony_ci    /**
247100ae2f9Sopenharmony_ci     * Prints out the internal information about an object in the specified format,
248100ae2f9Sopenharmony_ci     * helping you diagnose internal errors of the object.
249100ae2f9Sopenharmony_ci     *
250100ae2f9Sopenharmony_ci     * @param dumper The Dumper object you have implemented to process the output internal information.
251100ae2f9Sopenharmony_ci     */
252100ae2f9Sopenharmony_ci    virtual void Dump(Dumper &dumper) = 0;
253100ae2f9Sopenharmony_ci
254100ae2f9Sopenharmony_ci    /**
255100ae2f9Sopenharmony_ci     * Print out the internal information about an object in the specified format,
256100ae2f9Sopenharmony_ci     * helping you diagnose internal errors of the object.
257100ae2f9Sopenharmony_ci     *
258100ae2f9Sopenharmony_ci     * @param queueInfo queue Info.
259100ae2f9Sopenharmony_ci     */
260100ae2f9Sopenharmony_ci    virtual void DumpQueueInfo(std::string& queueInfo) = 0;
261100ae2f9Sopenharmony_ci
262100ae2f9Sopenharmony_ci    /**
263100ae2f9Sopenharmony_ci     * Checks whether the current EventHandler is idle.
264100ae2f9Sopenharmony_ci     *
265100ae2f9Sopenharmony_ci     * @return Returns true if all events have been processed; returns false otherwise.
266100ae2f9Sopenharmony_ci     */
267100ae2f9Sopenharmony_ci    virtual bool IsIdle() = 0;
268100ae2f9Sopenharmony_ci
269100ae2f9Sopenharmony_ci    /**
270100ae2f9Sopenharmony_ci     * Check whether this event queue is empty.
271100ae2f9Sopenharmony_ci     *
272100ae2f9Sopenharmony_ci     * @return If queue is empty return true otherwise return false.
273100ae2f9Sopenharmony_ci     */
274100ae2f9Sopenharmony_ci    virtual bool IsQueueEmpty() = 0;
275100ae2f9Sopenharmony_ci
276100ae2f9Sopenharmony_ci    /**
277100ae2f9Sopenharmony_ci     * Check whether an event with the given ID can be found among the events that have been sent but not processed.
278100ae2f9Sopenharmony_ci     *
279100ae2f9Sopenharmony_ci     * @param owner Owner of the event which is point to an instance of 'EventHandler'.
280100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
281100ae2f9Sopenharmony_ci     */
282100ae2f9Sopenharmony_ci    virtual bool HasInnerEvent(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId) = 0;
283100ae2f9Sopenharmony_ci
284100ae2f9Sopenharmony_ci    /**
285100ae2f9Sopenharmony_ci     * Check whether an event carrying the given param can be found among the events that have been sent but not
286100ae2f9Sopenharmony_ci     * processed.
287100ae2f9Sopenharmony_ci     *
288100ae2f9Sopenharmony_ci     * @param owner The owner of the event which is point to an instance of 'EventHandler'.
289100ae2f9Sopenharmony_ci     * @param param The basic parameter of the event.
290100ae2f9Sopenharmony_ci     */
291100ae2f9Sopenharmony_ci    virtual bool HasInnerEvent(const std::shared_ptr<EventHandler> &owner, int64_t param) = 0;
292100ae2f9Sopenharmony_ci
293100ae2f9Sopenharmony_ci    virtual void PushHistoryQueueBeforeDistribute(const InnerEvent::Pointer &event) { (void)event; }
294100ae2f9Sopenharmony_ci
295100ae2f9Sopenharmony_ci    virtual void PushHistoryQueueAfterDistribute() {}
296100ae2f9Sopenharmony_ci
297100ae2f9Sopenharmony_ci    virtual bool HasPreferEvent(int basePrio) = 0;
298100ae2f9Sopenharmony_ci
299100ae2f9Sopenharmony_ci    virtual std::string DumpCurrentQueueSize() = 0;
300100ae2f9Sopenharmony_ci
301100ae2f9Sopenharmony_ci    /**
302100ae2f9Sopenharmony_ci     * Check whether there are currenty file descriptors is need to be processed.
303100ae2f9Sopenharmony_ci     */
304100ae2f9Sopenharmony_ci    void CheckFileDescriptorEvent();
305100ae2f9Sopenharmony_ci
306100ae2f9Sopenharmony_ci    /**
307100ae2f9Sopenharmony_ci     * Set waiter mode, true for deamon io waiter
308100ae2f9Sopenharmony_ci     */
309100ae2f9Sopenharmony_ci    void SetIoWaiter(bool useDeamonIoWaiter)
310100ae2f9Sopenharmony_ci    {
311100ae2f9Sopenharmony_ci        useDeamonIoWaiter_ = useDeamonIoWaiter;
312100ae2f9Sopenharmony_ci    }
313100ae2f9Sopenharmony_ci
314100ae2f9Sopenharmony_ci    /**
315100ae2f9Sopenharmony_ci     * Get ffrt queue handler type, only for ffrt thread mode.
316100ae2f9Sopenharmony_ci     */
317100ae2f9Sopenharmony_ci    virtual void* GetFfrtQueue() { return nullptr; }
318100ae2f9Sopenharmony_ci
319100ae2f9Sopenharmony_ci    /**
320100ae2f9Sopenharmony_ci     * Insert task to ffrt queue, and wait to handled, only for ffrt thread mode.
321100ae2f9Sopenharmony_ci     */
322100ae2f9Sopenharmony_ci    virtual void InsertSyncEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW,
323100ae2f9Sopenharmony_ci        EventInsertType insertType = EventInsertType::AT_END)
324100ae2f9Sopenharmony_ci    {
325100ae2f9Sopenharmony_ci        (void)event;
326100ae2f9Sopenharmony_ci        (void)priority;
327100ae2f9Sopenharmony_ci        (void)insertType;
328100ae2f9Sopenharmony_ci    }
329100ae2f9Sopenharmony_ci
330100ae2f9Sopenharmony_ci    /**
331100ae2f9Sopenharmony_ci     * Get pending task info
332100ae2f9Sopenharmony_ci     */
333100ae2f9Sopenharmony_ci    virtual PendingTaskInfo QueryPendingTaskInfo(int32_t fileDescriptor) = 0;
334100ae2f9Sopenharmony_ci    /**
335100ae2f9Sopenharmony_ci     * add observer
336100ae2f9Sopenharmony_ci     *
337100ae2f9Sopenharmony_ci     * @param observer runner observer.
338100ae2f9Sopenharmony_ci     * @param stages The stages of observer
339100ae2f9Sopenharmony_ci     * @param callback observer callback.
340100ae2f9Sopenharmony_ci     */
341100ae2f9Sopenharmony_ci    void AddObserver(Observer observer, uint32_t stages, EventRunnerObserverCallBack callback)
342100ae2f9Sopenharmony_ci    {
343100ae2f9Sopenharmony_ci        observer_.observer = observer;
344100ae2f9Sopenharmony_ci        observer_.notifyCb = callback;
345100ae2f9Sopenharmony_ci        observer_.stages = stages;
346100ae2f9Sopenharmony_ci    }
347100ae2f9Sopenharmony_ci
348100ae2f9Sopenharmony_ci    /**
349100ae2f9Sopenharmony_ci     * Cancel And Wait
350100ae2f9Sopenharmony_ci     */
351100ae2f9Sopenharmony_ci    virtual void CancelAndWait() = 0;
352100ae2f9Sopenharmony_ciprotected:
353100ae2f9Sopenharmony_ci    void WaitUntilLocked(const InnerEvent::TimePoint &when, std::unique_lock<std::mutex> &lock);
354100ae2f9Sopenharmony_ci    std::mutex queueLock_;
355100ae2f9Sopenharmony_ci
356100ae2f9Sopenharmony_ci    std::atomic_bool usable_ {true};
357100ae2f9Sopenharmony_ci
358100ae2f9Sopenharmony_ci    bool isIdle_ {true};
359100ae2f9Sopenharmony_ci
360100ae2f9Sopenharmony_ci    // Mark if the event queue is finished.
361100ae2f9Sopenharmony_ci    bool finished_ {true};
362100ae2f9Sopenharmony_ci
363100ae2f9Sopenharmony_ci    // IO waiter used to block if no events while calling 'GetEvent'.
364100ae2f9Sopenharmony_ci    std::shared_ptr<IoWaiter> ioWaiter_;
365100ae2f9Sopenharmony_ci
366100ae2f9Sopenharmony_ci    // select different epoll
367100ae2f9Sopenharmony_ci    bool useDeamonIoWaiter_ = false;
368100ae2f9Sopenharmony_ci
369100ae2f9Sopenharmony_ci    // File descriptor listeners to handle IO events.
370100ae2f9Sopenharmony_ci    std::map<int32_t, std::shared_ptr<FileDescriptorListener>> listeners_;
371100ae2f9Sopenharmony_ci
372100ae2f9Sopenharmony_ci    EventRunnerObserver observer_ = {.stages = static_cast<uint32_t>(EventRunnerStage::STAGE_INVAILD),
373100ae2f9Sopenharmony_ci        .notifyCb = nullptr};
374100ae2f9Sopenharmony_ci};
375100ae2f9Sopenharmony_ci}  // namespace AppExecFwk
376100ae2f9Sopenharmony_ci}  // namespace OHOS
377100ae2f9Sopenharmony_ci
378100ae2f9Sopenharmony_ci#endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_H
379