1 /*
2  * Copyright (c) 2023 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 <vector>
22 #include <set>
23 #include "io_event_common.h"
24 #include "errors.h"
25 
26 namespace OHOS {
27 namespace Utils {
28 
29 class IOEventHandler;
30 
31 class IOEventEpoll {
32 public:
33     using REventId = uint32_t;
34     using EPEventId = uint32_t;
35 
36     static constexpr int EPOLL_MAX_EVENTS_INIT = 8;
37     static constexpr int EXPANSION_COEFF = 2;
38 
39     IOEventEpoll();
40     IOEventEpoll(const IOEventEpoll&) = delete;
41     IOEventEpoll& operator=(const IOEventEpoll&) = delete;
42     virtual ~IOEventEpoll();
43 
44     virtual ErrCode SetUp();
45     virtual void CleanUp();
46 
47     virtual ErrCode Polling(int timeout, std::vector<std::pair<int, REventId>>&);
48 
49     virtual ErrCode ModifyEvents(int fd, REventId events);
50 
51 private:
52     EPEventId Reactor2Epoll(REventId reactorEvent);
53     REventId Epoll2Reactor(EPEventId epollEvents);
54     bool OperateEpoll(int op, int fd, EPEventId epollEvents);
55 
56     int epollFd_;
57     int maxEvents_;
58     std::set<int> interestFds_;
59 };
60 
61 } // Utils
62 } // OHOS
63 #endif /* UTILS_EVENT_DEMULTIPLEXER_H_ */
64