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
24namespace OHOS {
25namespace Utils {
26
27class EventHandler;
28
29class EventDemultiplexer {
30public:
31    EventDemultiplexer();
32    EventDemultiplexer(const EventDemultiplexer&) = delete;
33    EventDemultiplexer& operator=(const EventDemultiplexer&) = delete;
34    virtual ~EventDemultiplexer();
35
36    uint32_t StartUp();
37    void CleanUp();
38
39    int Polling(int timeout);
40
41    uint32_t UpdateEventHandler(EventHandler* handler);
42
43private:
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, std::shared_ptr<EventHandler>> eventHandlers_; // guard by mutex_
52};
53
54}
55}
56#endif /* UTILS_EVENT_DEMULTIPLEXER_H_ */
57