1 /*
2  * Copyright (C) 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 TELEPHONY_EVENT_QUEUE_H
17 #define TELEPHONY_EVENT_QUEUE_H
18 
19 #include "event_handler.h"
20 #include "ffrt.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 class TelEventQueue {
25 public:
26     explicit TelEventQueue(const std::string &name);
27     ~TelEventQueue();
28     void Submit(AppExecFwk::InnerEvent::Pointer &event, AppExecFwk::EventQueue::Priority priority);
29     void RemoveEvent(uint32_t innerEventId);
30     bool HasInnerEvent(uint32_t innerEventId);
31     void RemoveAllEvents();
32     void ClearCurrentTask(bool isNeedEnd);
33 
34 private:
35     struct EventList {
36         std::list<AppExecFwk::InnerEvent::Pointer> events;
37     };
38 
39 private:
40     void InsertEventsInner(AppExecFwk::InnerEvent::Pointer &event, AppExecFwk::EventQueue::Priority priority);
41     bool HasEvent();
42     AppExecFwk::InnerEvent::Pointer PopEvent(int32_t queueId, bool &isNeedSubmit);
43     void SubmitInner(int32_t queueId);
44     bool IsEmpty();
45     uint32_t ToTelPriority(AppExecFwk::EventQueue::Priority priority);
46     uint32_t GetPriorityIndex();
47     AppExecFwk::InnerEvent::TimePoint GetHandleTime();
48     int32_t GetNextQueueId();
49     void SubmitToFFRT(int32_t queueId, AppExecFwk::InnerEvent::TimePoint handleTime, int64_t delayTime);
50     AppExecFwk::InnerEvent::TimePoint GetCurHandleTime();
51     void SetCurHandleTime(AppExecFwk::InnerEvent::TimePoint handleTime);
52 
53 private:
54     static const uint32_t EVENT_QUEUE_NUM = 3;
55     std::array<EventList, EVENT_QUEUE_NUM> eventLists_;
56     AppExecFwk::InnerEvent::TimePoint curHandleTime_ { AppExecFwk::InnerEvent::TimePoint::max() };
57     std::mutex eventCtx_;
58     std::mutex taskCtx_;
59     std::mutex memberCtx_;
60     ffrt::task_handle curTask_;
61     std::string name_;
62     std::atomic_int queueId_ { 0 };
63     std::shared_ptr<ffrt::queue> queue_ = nullptr;
64     AppExecFwk::InnerEvent::Pointer nullPointer_ = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
65 };
66 } // namespace Telephony
67 } // namespace OHOS
68 #endif // TELEPHONY_EVENT_QUEUE_H