13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci#ifndef UTILS_EVENT_REACTOR_H 173f4cbf05Sopenharmony_ci#define UTILS_EVENT_REACTOR_H 183f4cbf05Sopenharmony_ci 193f4cbf05Sopenharmony_ci#include <cstdint> 203f4cbf05Sopenharmony_ci#include <atomic> 213f4cbf05Sopenharmony_ci#include <memory> 223f4cbf05Sopenharmony_ci#include <mutex> 233f4cbf05Sopenharmony_ci#include <vector> 243f4cbf05Sopenharmony_ci#include <set> 253f4cbf05Sopenharmony_ci#include <queue> 263f4cbf05Sopenharmony_ci#include "io_event_common.h" 273f4cbf05Sopenharmony_ci#include "errors.h" 283f4cbf05Sopenharmony_ci#include "io_event_handler.h" 293f4cbf05Sopenharmony_ci 303f4cbf05Sopenharmony_cinamespace OHOS { 313f4cbf05Sopenharmony_cinamespace Utils { 323f4cbf05Sopenharmony_ci 333f4cbf05Sopenharmony_ciclass IOEventEpoll; 343f4cbf05Sopenharmony_ci 353f4cbf05Sopenharmony_ciclass IOEventReactor { 363f4cbf05Sopenharmony_cipublic: 373f4cbf05Sopenharmony_ci static constexpr uint8_t FLAG_CHANGED = 0x01; 383f4cbf05Sopenharmony_ci static constexpr size_t INIT_FD_NUMS = 8; 393f4cbf05Sopenharmony_ci static constexpr int EXPANSION_COEFF = 2; 403f4cbf05Sopenharmony_ci 413f4cbf05Sopenharmony_ci IOEventReactor(); 423f4cbf05Sopenharmony_ci IOEventReactor(const IOEventReactor&) = delete; 433f4cbf05Sopenharmony_ci IOEventReactor& operator=(const IOEventReactor&) = delete; 443f4cbf05Sopenharmony_ci IOEventReactor(const IOEventReactor&&) = delete; 453f4cbf05Sopenharmony_ci IOEventReactor& operator=(const IOEventReactor&&) = delete; 463f4cbf05Sopenharmony_ci virtual ~IOEventReactor(); 473f4cbf05Sopenharmony_ci 483f4cbf05Sopenharmony_ci ErrCode SetUp(); 493f4cbf05Sopenharmony_ci ErrCode CleanUp(); 503f4cbf05Sopenharmony_ci ErrCode Clean(int fd); 513f4cbf05Sopenharmony_ci 523f4cbf05Sopenharmony_ci ErrCode AddHandler(IOEventHandler* target); 533f4cbf05Sopenharmony_ci ErrCode RemoveHandler(IOEventHandler* target); 543f4cbf05Sopenharmony_ci ErrCode UpdateHandler(IOEventHandler* target); 553f4cbf05Sopenharmony_ci ErrCode FindHandler(IOEventHandler* target); 563f4cbf05Sopenharmony_ci 573f4cbf05Sopenharmony_ci void Run(int timeout); 583f4cbf05Sopenharmony_ci 593f4cbf05Sopenharmony_ci inline void Terminate() 603f4cbf05Sopenharmony_ci { 613f4cbf05Sopenharmony_ci loopReady_ = false; 623f4cbf05Sopenharmony_ci } 633f4cbf05Sopenharmony_ci 643f4cbf05Sopenharmony_ci inline void EnableHandling() 653f4cbf05Sopenharmony_ci { 663f4cbf05Sopenharmony_ci enabled_ = true; 673f4cbf05Sopenharmony_ci } 683f4cbf05Sopenharmony_ci 693f4cbf05Sopenharmony_ci inline void DisableHandling() 703f4cbf05Sopenharmony_ci { 713f4cbf05Sopenharmony_ci enabled_ = false; 723f4cbf05Sopenharmony_ci } 733f4cbf05Sopenharmony_ciprivate: 743f4cbf05Sopenharmony_ci struct FdEvents { 753f4cbf05Sopenharmony_ci std::shared_ptr<IOEventHandler> head; 763f4cbf05Sopenharmony_ci EventId events; 773f4cbf05Sopenharmony_ci uint8_t flags; 783f4cbf05Sopenharmony_ci 793f4cbf05Sopenharmony_ci FdEvents() 803f4cbf05Sopenharmony_ci { 813f4cbf05Sopenharmony_ci head = std::make_shared<IOEventHandler>(); 823f4cbf05Sopenharmony_ci events = Events::EVENT_NONE; 833f4cbf05Sopenharmony_ci flags = 0u; 843f4cbf05Sopenharmony_ci } 853f4cbf05Sopenharmony_ci }; 863f4cbf05Sopenharmony_ci 873f4cbf05Sopenharmony_ci bool HasHandler(IOEventHandler* target); 883f4cbf05Sopenharmony_ci void InsertNodeFront(int fd, IOEventHandler* target); 893f4cbf05Sopenharmony_ci void RemoveNode(IOEventHandler* target); 903f4cbf05Sopenharmony_ci 913f4cbf05Sopenharmony_ci void HandleAll(const std::vector<std::pair<int, EventId>>&); 923f4cbf05Sopenharmony_ci void Execute(const std::vector<EventCallback>& tasks); 933f4cbf05Sopenharmony_ci 943f4cbf05Sopenharmony_ci ErrCode HandleEvents(int fd, EventId events); 953f4cbf05Sopenharmony_ci bool UpdateToDemultiplexer(int fd); 963f4cbf05Sopenharmony_ci 973f4cbf05Sopenharmony_ci bool DoClean(int fd); 983f4cbf05Sopenharmony_ci 993f4cbf05Sopenharmony_ci std::mutex mutex_; 1003f4cbf05Sopenharmony_ci std::atomic<bool> loopReady_; 1013f4cbf05Sopenharmony_ci std::atomic<bool> enabled_; 1023f4cbf05Sopenharmony_ci std::atomic<uint32_t> count_; 1033f4cbf05Sopenharmony_ci std::vector<struct FdEvents> ioHandlers_; 1043f4cbf05Sopenharmony_ci std::unique_ptr<IOEventEpoll> backend_; 1053f4cbf05Sopenharmony_ci}; 1063f4cbf05Sopenharmony_ci 1073f4cbf05Sopenharmony_ci} // namespace Utils 1083f4cbf05Sopenharmony_ci} // namespace OHOS 1093f4cbf05Sopenharmony_ci#endif