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 #include <errno.h>
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include "le_epoll.h"
20 #include "le_idle.h"
21 #include "le_timer.h"
22
23 #define MAX_TIMEOUT_MILLISECONDS (20 * 1000) // 20s
24
IsValid_(const EventEpoll * loop)25 static int IsValid_(const EventEpoll *loop)
26 {
27 return loop->epollFd >= 0;
28 }
29
GetEpollEvent_(int fd,int op,struct epoll_event * event)30 static void GetEpollEvent_(int fd, int op, struct epoll_event *event)
31 {
32 event->data.fd = fd;
33 if (LE_TEST_FLAGS(op, EVENT_READ)) {
34 event->events |= EPOLLIN;
35 }
36 if (LE_TEST_FLAGS(op, EVENT_WRITE)) {
37 event->events |= EPOLLOUT;
38 }
39 }
40
Close_(const EventLoop * loop)41 static LE_STATUS Close_(const EventLoop *loop)
42 {
43 LE_CHECK(loop != NULL, return LE_FAILURE, "Invalid loop");
44 EventEpoll *epoll = (EventEpoll *)loop;
45 LE_LOGV("Close_ epollFd %d", epoll->epollFd);
46 close(epoll->epollFd);
47 free(epoll);
48 return LE_SUCCESS;
49 }
50
AddEvent_(const EventLoop * loop,const BaseTask * task,int op)51 static LE_STATUS AddEvent_(const EventLoop *loop, const BaseTask *task, int op)
52 {
53 LE_CHECK(loop != NULL, return LE_FAILURE, "Invalid loop");
54 EventEpoll *epoll = (EventEpoll *)loop;
55
56 int ret = LE_FAILURE;
57 struct epoll_event event = {};
58 int fd = GetSocketFd((const TaskHandle)task);
59 GetEpollEvent_(fd, op, &event);
60 if (IsValid_(epoll) && fd >= 0) {
61 ret = epoll_ctl(epoll->epollFd, EPOLL_CTL_ADD, fd, &event);
62 }
63 LE_CHECK(ret == 0, return LE_FAILURE, "Failed to add epoll_ctl %d ret %d", fd, errno);
64 return LE_SUCCESS;
65 }
66
ModEvent_(const EventLoop * loop,const BaseTask * task,int op)67 static LE_STATUS ModEvent_(const EventLoop *loop, const BaseTask *task, int op)
68 {
69 LE_CHECK(loop != NULL, return LE_FAILURE, "Invalid loop");
70 EventEpoll *epoll = (EventEpoll *)loop;
71
72 int ret = LE_FAILURE;
73 struct epoll_event event = {};
74 int fd = GetSocketFd((const TaskHandle)task);
75 GetEpollEvent_(fd, op, &event);
76 if (IsValid_(epoll) && fd >= 0) {
77 ret = epoll_ctl(epoll->epollFd, EPOLL_CTL_MOD, fd, &event);
78 }
79 LE_CHECK(ret == 0, return LE_FAILURE, "Failed to mod epoll_ctl %d ret %d", fd, errno);
80 return LE_SUCCESS;
81 }
82
DelEvent_(const EventLoop * loop,int fd,int op)83 static LE_STATUS DelEvent_(const EventLoop *loop, int fd, int op)
84 {
85 LE_CHECK(loop != NULL, return LE_FAILURE, "Invalid loop");
86 EventEpoll *epoll = (EventEpoll *)loop;
87
88 int ret = LE_FAILURE;
89 struct epoll_event event = {};
90 GetEpollEvent_(fd, op, &event);
91 if (IsValid_(epoll) && fd >= 0) {
92 ret = epoll_ctl(epoll->epollFd, EPOLL_CTL_DEL, fd, &event);
93 }
94 LE_CHECK(ret == 0, return LE_FAILURE, "Failed to del epoll_ctl %d ret %d", fd, errno);
95 return LE_SUCCESS;
96 }
97
RunLoop_(const EventLoop * loop)98 static LE_STATUS RunLoop_(const EventLoop *loop)
99 {
100 LE_CHECK(loop != NULL, return LE_FAILURE, "Invalid loop");
101
102 EventEpoll *epoll = (EventEpoll *)loop;
103 if (!IsValid_(epoll)) {
104 return LE_FAILURE;
105 }
106
107 while (1) {
108 LE_RunIdle((LoopHandle)&(epoll->loop));
109
110 uint64_t minTimePeriod = GetMinTimeoutPeriod(loop);
111 int timeout = 0;
112 if (minTimePeriod == 0) {
113 timeout = -1;
114 } else if (GetCurrentTimespec(0) >= minTimePeriod) {
115 timeout = 0;
116 } else {
117 timeout = (int)(minTimePeriod - GetCurrentTimespec(0));
118 }
119 if (timeout < 0 || timeout > MAX_TIMEOUT_MILLISECONDS) {
120 LE_LOGW("timeout:%d", timeout);
121 }
122
123 int number = epoll_wait(epoll->epollFd, epoll->waitEvents, loop->maxevents, timeout);
124 if (number > 1) {
125 LE_LOGI("RunLoop_ epoll_wait with number %d", number);
126 }
127 for (int index = 0; index < number; index++) {
128 if ((epoll->waitEvents[index].events & EPOLLIN) == EPOLLIN) {
129 ProcessEvent(loop, epoll->waitEvents[index].data.fd, EVENT_READ);
130 }
131 if ((epoll->waitEvents[index].events & EPOLLOUT) == EPOLLOUT) {
132 ProcessEvent(loop, epoll->waitEvents[index].data.fd, EVENT_WRITE);
133 }
134 if (epoll->waitEvents[index].events & (EPOLLERR | EPOLLHUP)) {
135 LE_LOGW("RunLoop_ fd:%d, error:%d", epoll->waitEvents[index].data.fd, errno);
136 ProcessEvent(loop, epoll->waitEvents[index].data.fd, EVENT_ERROR);
137 }
138 }
139 if (number > 1) {
140 LE_LOGI("RunLoop_ epoll_wait finish");
141 }
142 CheckTimeoutOfTimer((EventLoop *)loop, GetCurrentTimespec(0));
143
144 if (loop->stop) {
145 break;
146 }
147 }
148 return LE_SUCCESS;
149 }
150
CreateEpollLoop(EventLoop ** loop,uint32_t maxevents,uint32_t timeout)151 LE_STATUS CreateEpollLoop(EventLoop **loop, uint32_t maxevents, uint32_t timeout)
152 {
153 LE_CHECK(loop != NULL, return LE_FAILURE, "Invalid loop");
154 EventEpoll *epoll = (EventEpoll *)malloc(sizeof(EventEpoll) + sizeof(struct epoll_event) * (maxevents));
155 LE_CHECK(epoll != NULL, return LE_FAILURE, "Failed to alloc memory for epoll");
156 epoll->epollFd = epoll_create(maxevents);
157 LE_CHECK(epoll->epollFd >= 0, free(epoll);
158 return LE_FAILURE, "Failed to create epoll");
159 if (fcntl(epoll->epollFd, F_SETFD, FD_CLOEXEC) == -1) {
160 LE_LOGE("fcntl setfd close_on_exec failed, errno: %d", errno);
161 }
162 *loop = (EventLoop *)epoll;
163 epoll->loop.maxevents = maxevents;
164 epoll->loop.timeout = timeout;
165 epoll->loop.close = Close_;
166 epoll->loop.runLoop = RunLoop_;
167 epoll->loop.delEvent = DelEvent_;
168 epoll->loop.addEvent = AddEvent_;
169 epoll->loop.modEvent = ModEvent_;
170 return LE_SUCCESS;
171 }
172