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 26 namespace OHOS { 27 namespace Utils { 28 29 class EventHandler; 30 31 class EventDemultiplexer { 32 public: 33 EventDemultiplexer(); 34 EventDemultiplexer(const EventDemultiplexer&) = delete; 35 EventDemultiplexer& operator=(const EventDemultiplexer&) = delete; 36 virtual ~EventDemultiplexer(); 37 38 uint32_t StartUp(); 39 void CleanUp(); 40 41 int Polling(int timeout); 42 43 uint32_t UpdateEventHandler(EventHandler* handler); 44 45 private: 46 uint32_t Update(int operation, EventHandler* handler); 47 static uint32_t Reactor2Epoll(uint32_t reactorEvent); 48 static uint32_t Epoll2Reactor(uint32_t epollEvents); 49 50 int epollFd_; 51 int maxEvents_; 52 std::recursive_mutex mutex_; 53 std::map<int, std::shared_ptr<EventHandler>> eventHandlers_; // guard by mutex_ 54 std::queue<std::tuple<int, int, int>> epollCtlErrQueue_; 55 }; 56 57 } 58 } 59 #endif /* UTILS_EVENT_DEMULTIPLEXER_H_ */ 60