/[svn]/ircd-hybrid/branches/8.2.x/src/s_bsd_epoll.c
ViewVC logotype

Contents of /ircd-hybrid/branches/8.2.x/src/s_bsd_epoll.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8901 - (show annotations)
Sun Apr 21 20:10:44 2019 UTC (3 years, 3 months ago) by michael
File MIME type: text/x-chdr
File size: 4983 byte(s)
- Add support for monotonic clocks and have some timers make use of it;  remove event_set_back_events()

1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2005-2019 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file s_bsd_epoll.c
23 * \brief Linux epoll() compatible network routines.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_EPOLL
29 #include "fdlist.h"
30 #include "ircd.h"
31 #include "s_bsd.h"
32 #include "log.h"
33 #include "memory.h"
34 #include <sys/epoll.h>
35
36 enum
37 {
38 INITIAL_NEVENT = 16,
39 MAXIMUM_NEVENT = 4096
40 };
41
42 struct epollop
43 {
44 struct epoll_event *events;
45 int nevents;
46 int fd;
47 };
48
49 static struct epollop *epollop;
50
51
52 /*
53 * comm_select_init
54 *
55 * This is a needed exported function which will be called to initialise
56 * the network loop code.
57 */
58 void
59 comm_select_init(void)
60 {
61 int fd = epoll_create1(EPOLL_CLOEXEC);
62 if (fd < 0)
63 {
64 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open epoll fd: %s",
65 strerror(errno));
66 exit(EXIT_FAILURE); /* Whee! */
67 }
68
69 fd_open(fd, false, "epoll file descriptor");
70
71 epollop = xcalloc(sizeof(*epollop));
72 epollop->fd = fd;
73 epollop->nevents = INITIAL_NEVENT;
74 epollop->events = xcalloc(epollop->nevents * sizeof(*epollop->events));
75 }
76
77 /*
78 * comm_setselect
79 *
80 * This is a needed exported function which will be called to register
81 * and deregister interest in a pending IO state for a given FD.
82 */
83 void
84 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
85 void *client_data, uintmax_t timeout)
86 {
87 int new_events, op;
88 struct epoll_event ep_event;
89
90 assert(F);
91 assert(F->flags.open == true);
92
93 if ((type & COMM_SELECT_READ))
94 {
95 F->read_handler = handler;
96 F->read_data = client_data;
97 }
98
99 if ((type & COMM_SELECT_WRITE))
100 {
101 F->write_handler = handler;
102 F->write_data = client_data;
103 }
104
105 new_events = (F->read_handler ? EPOLLIN : 0) |
106 (F->write_handler ? EPOLLOUT : 0);
107
108 if (timeout)
109 {
110 F->timeout = event_base->time.sec_monotonic + (timeout / 1000);
111 F->timeout_handler = handler;
112 F->timeout_data = client_data;
113 }
114
115 if (new_events != F->evcache)
116 {
117 if (new_events == 0)
118 op = EPOLL_CTL_DEL;
119 else if (F->evcache == 0)
120 op = EPOLL_CTL_ADD;
121 else
122 op = EPOLL_CTL_MOD;
123
124 memset(&ep_event, 0, sizeof(ep_event));
125 ep_event.events = F->evcache = new_events;
126 ep_event.data.ptr = F;
127
128 if (epoll_ctl(epollop->fd, op, F->fd, &ep_event))
129 {
130 ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno));
131 abort();
132 }
133 }
134 }
135
136 /*
137 * comm_select()
138 *
139 * Called to do the new-style IO, courtesy of of squid (like most of this
140 * new IO code). This routine handles the stuff we've hidden in
141 * comm_setselect and fd_table[] and calls callbacks for IO ready
142 * events.
143 */
144 void
145 comm_select(void)
146 {
147 int num;
148 void (*hdl)(fde_t *, void *);
149
150 num = epoll_wait(epollop->fd, epollop->events, epollop->nevents, SELECT_DELAY);
151 assert(num <= epollop->nevents);
152
153 event_time_set();
154
155 if (num < 0)
156 {
157 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
158 nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
159 return;
160 }
161
162 for (int i = 0; i < num; ++i)
163 {
164 fde_t *F = epollop->events[i].data.ptr;
165
166 if (F->flags.open == false)
167 continue;
168
169 if ((epollop->events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR)))
170 {
171 if ((hdl = F->read_handler))
172 {
173 F->read_handler = NULL;
174 hdl(F, F->read_data);
175
176 if (F->flags.open == false)
177 continue;
178 }
179 }
180
181 if ((epollop->events[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR)))
182 {
183 if ((hdl = F->write_handler))
184 {
185 F->write_handler = NULL;
186 hdl(F, F->write_data);
187
188 if (F->flags.open == false)
189 continue;
190 }
191 }
192
193 comm_setselect(F, 0, NULL, NULL, 0);
194 }
195
196 if (num == epollop->nevents && epollop->nevents < MAXIMUM_NEVENT)
197 {
198 /*
199 * We used all of the event space this time. We should be
200 * ready for more events next time.
201 */
202 int new_nevents = epollop->nevents * 2;
203 struct epoll_event *new_events = xrealloc(epollop->events, new_nevents * sizeof(*epollop->events));
204
205 epollop->events = new_events;
206 epollop->nevents = new_nevents;
207 }
208 }
209 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28