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