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 EVENT_LOOP_EPOLL_H
17 #define EVENT_LOOP_EPOLL_H
18 
19 #include "event_loop_impl.h"
20 
21 #ifdef EVENT_LOOP_USE_EPOLL
22 #include <vector>
23 #include <sys/epoll.h>
24 
25 namespace DistributedDB {
26 class EventLoopEpoll : public EventLoopImpl {
27 public:
28     EventLoopEpoll();
29     ~EventLoopEpoll() override;
30 
31     int Initialize() override;
32 
33 private:
34     int Prepare(const std::set<EventImpl *> &polling) override;
35     int Poll(EventTime sleepTime) override;
36     int WakeUp() override;
37     int Exit(const std::set<EventImpl *> &polling) override;
38     int AddEvent(EventImpl *event) override;
39     int RemoveEvent(EventImpl *event) override;
40     int ModifyEvent(EventImpl *event, bool isAdd, EventsMask events) override;
41 
42     void EpollWokenUp();
43     uint32_t CalEpollEvents(EventsMask events) const;
44     EventsMask CalEventsMask(uint32_t epollEvents);
45     int EpollCtl(int operation, EventImpl *event, EventsMask events);
46 
47     DECLARE_OBJECT_TAG(EventLoopEpoll);
48 
49     static constexpr int EPOLL_INIT_REVENTS = 32;
50     EventFd wakeUpFd_;
51     EventFd epollFd_;
52     int pollFdCount_;
53     std::vector<epoll_event> revents_;
54 };
55 }
56 #endif // EVENT_LOOP_USE_EPOLL
57 #endif // EVENT_LOOP_EPOLL_H
58