/[svn]/ircd-hybrid/trunk/src/s_bsd_epoll.c
ViewVC logotype

Annotation of /ircd-hybrid/trunk/src/s_bsd_epoll.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9857 - (hide annotations)
Fri Jan 1 04:43:22 2021 UTC (2 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 4974 byte(s)
- Bump copyright years

1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 9857 * Copyright (c) 2005-2021 ircd-hybrid development team
5 adx 30 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file s_bsd_epoll.c
23     * \brief Linux epoll() compatible network routines.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_EPOLL
29 adx 30 #include "fdlist.h"
30     #include "ircd.h"
31     #include "s_bsd.h"
32 michael 1309 #include "log.h"
33 michael 8533 #include "memory.h"
34 adx 30 #include <sys/epoll.h>
35    
36 michael 8533 enum
37     {
38     INITIAL_NEVENT = 16,
39     MAXIMUM_NEVENT = 4096
40     };
41 adx 30
42 michael 8533 struct epollop
43     {
44     struct epoll_event *events;
45     int nevents;
46     int fd;
47     };
48 adx 30
49 michael 8533 static struct epollop *epollop;
50    
51    
52 adx 30 /*
53 michael 8414 * comm_select_init
54 adx 30 *
55     * This is a needed exported function which will be called to initialise
56     * the network loop code.
57     */
58     void
59 michael 8414 comm_select_init(void)
60 adx 30 {
61 michael 8533 int fd = epoll_create1(EPOLL_CLOEXEC);
62     if (fd < 0)
63 adx 30 {
64 michael 8414 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open epoll fd: %s",
65 michael 6560 strerror(errno));
66 michael 6553 exit(EXIT_FAILURE); /* Whee! */
67 adx 30 }
68    
69 michael 8658 fd_open(fd, false, "epoll file descriptor");
70 michael 8533
71     epollop = xcalloc(sizeof(*epollop));
72     epollop->fd = fd;
73     epollop->nevents = INITIAL_NEVENT;
74     epollop->events = xcalloc(epollop->nevents * sizeof(*epollop->events));
75 adx 30 }
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 michael 4461 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
85 michael 7330 void *client_data, uintmax_t timeout)
86 adx 30 {
87     int new_events, op;
88 michael 8529 struct epoll_event ep_event;
89 adx 30
90 michael 8717 assert(F);
91 michael 8658 assert(F->flags.open == true);
92 michael 8466
93 adx 30 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 michael 8431 if (timeout)
109 michael 2725 {
110 michael 8941 F->timeout = event_base->time.sec_monotonic + timeout;
111 michael 2725 F->timeout_handler = handler;
112     F->timeout_data = client_data;
113     }
114 adx 30
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 michael 8529 memset(&ep_event, 0, sizeof(ep_event));
125 adx 30 ep_event.events = F->evcache = new_events;
126 michael 8383 ep_event.data.ptr = F;
127 adx 30
128 michael 8533 if (epoll_ctl(epollop->fd, op, F->fd, &ep_event))
129 adx 30 {
130 michael 1247 ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno));
131 adx 30 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 michael 8431 int num;
148 michael 4461 void (*hdl)(fde_t *, void *);
149 adx 30
150 michael 8533 num = epoll_wait(epollop->fd, epollop->events, epollop->nevents, SELECT_DELAY);
151     assert(num <= epollop->nevents);
152 adx 30
153 michael 8900 event_time_set();
154 adx 30
155     if (num < 0)
156     {
157 michael 6542 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
158     nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
159 adx 30 return;
160     }
161    
162 michael 8431 for (int i = 0; i < num; ++i)
163 adx 30 {
164 michael 8533 fde_t *F = epollop->events[i].data.ptr;
165 michael 8339
166 michael 8658 if (F->flags.open == false)
167 adx 30 continue;
168    
169 michael 8533 if ((epollop->events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR)))
170 michael 2650 {
171 michael 5902 if ((hdl = F->read_handler))
172 adx 30 {
173     F->read_handler = NULL;
174     hdl(F, F->read_data);
175 michael 8339
176 michael 8658 if (F->flags.open == false)
177 michael 2650 continue;
178 adx 30 }
179 michael 2650 }
180 adx 30
181 michael 8533 if ((epollop->events[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR)))
182 michael 2650 {
183 michael 5902 if ((hdl = F->write_handler))
184 adx 30 {
185     F->write_handler = NULL;
186     hdl(F, F->write_data);
187 michael 8339
188 michael 8658 if (F->flags.open == false)
189 michael 2650 continue;
190 adx 30 }
191 michael 2650 }
192 adx 30
193     comm_setselect(F, 0, NULL, NULL, 0);
194     }
195 michael 8533
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 adx 30 }
209 stu 908 #endif

Properties

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

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