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 class EventStats { 39 public: 40 EventStats() = default; 41 ~EventStats() = default; 42 void CalculationInsertQueueEvents(); 43 void CalculationSubmitToFFRTEvents(); 44 void CalculationExecutedEvents(); 45 void CalculationRemovedEvents(int count); 46 void PrintEventStats(std::string &name); 47 private: 48 std::atomic_uint64_t totalHandledEvents { 0 }; 49 std::atomic_uint64_t currentQueueEvents { 0 }; 50 std::atomic_uint64_t submitedToFFRTEvents { 0 }; 51 std::atomic_uint64_t executedEvents { 0 }; 52 std::atomic_uint64_t removedEvents { 0 }; 53 AppExecFwk::InnerEvent::TimePoint lastPrintTime_ { AppExecFwk::InnerEvent::Clock::now() }; 54 }; 55 private: 56 void InsertEventsInner(AppExecFwk::InnerEvent::Pointer &event, AppExecFwk::EventQueue::Priority priority); 57 bool HasEvent(); 58 AppExecFwk::InnerEvent::Pointer PopEvent(int32_t queueId, bool &isNeedSubmit); 59 void SubmitInner(int32_t queueId); 60 bool IsEmpty(); 61 uint32_t ToTelPriority(AppExecFwk::EventQueue::Priority priority); 62 uint32_t GetPriorityIndex(); 63 AppExecFwk::InnerEvent::TimePoint GetHandleTime(); 64 int32_t GetNextQueueId(); 65 void SubmitToFFRT(int32_t queueId, AppExecFwk::InnerEvent::TimePoint handleTime, int64_t delayTime); 66 AppExecFwk::InnerEvent::TimePoint GetCurHandleTime(); 67 void SetCurHandleTime(AppExecFwk::InnerEvent::TimePoint handleTime); 68 69 private: 70 static const uint32_t EVENT_QUEUE_NUM = 3; 71 std::array<EventList, EVENT_QUEUE_NUM> eventLists_; 72 AppExecFwk::InnerEvent::TimePoint curHandleTime_ { AppExecFwk::InnerEvent::TimePoint::max() }; 73 std::mutex eventCtx_; 74 std::mutex taskCtx_; 75 std::mutex memberCtx_; 76 ffrt::task_handle curTask_; 77 std::string name_; 78 std::atomic_int queueId_ { 0 }; 79 std::shared_ptr<ffrt::queue> queue_ = nullptr; 80 AppExecFwk::InnerEvent::Pointer nullPointer_ = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr); 81 EventStats eventStats_; 82 }; 83 } // namespace Telephony 84 } // namespace OHOS 85 #endif // TELEPHONY_EVENT_QUEUE_H