1 /* 2 * Copyright (c) 2021 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 UTILS_EVENT_DEMULTIPLEXER_H 17 #define UTILS_EVENT_DEMULTIPLEXER_H 18 19 #include <mutex> 20 #include <memory> 21 #include <cstdint> 22 #include <map> 23 #include <queue> 24 #include <tuple> 25 #include <sys/epoll.h> 26 #include <vector> 27 28 namespace OHOS { 29 namespace Utils { 30 31 class EventHandler; 32 33 class EventDemultiplexer { 34 public: 35 EventDemultiplexer(); 36 EventDemultiplexer(const EventDemultiplexer&) = delete; 37 EventDemultiplexer& operator=(const EventDemultiplexer&) = delete; 38 virtual ~EventDemultiplexer(); 39 40 uint32_t StartUp(); 41 void CleanUp(); 42 43 int Polling(int timeout, std::vector<epoll_event> &epollEvents); 44 45 uint32_t UpdateEventHandler(EventHandler* handler); 46 GetMaxEvents()47 int GetMaxEvents() { return maxEvents_; } 48 49 private: 50 uint32_t Update(int operation, EventHandler* handler); 51 static uint32_t Reactor2Epoll(uint32_t reactorEvent); 52 static uint32_t Epoll2Reactor(uint32_t epollEvents); 53 54 int epollFd_; 55 int maxEvents_; 56 std::recursive_mutex mutex_; 57 std::map<int, std::shared_ptr<EventHandler>> eventHandlers_; // guard by mutex_ 58 std::queue<std::tuple<int, int, int>> epollCtlErrQueue_; 59 }; 60 61 } 62 } 63 #endif /* UTILS_EVENT_DEMULTIPLEXER_H_ */ 64