ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/s_bsd_epoll.c
Revision: 8657
Committed: Mon Nov 12 19:02:38 2018 UTC (5 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 4945 byte(s)
Log Message:
- Make use of the bool data type in some more places

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 8280 * Copyright (c) 2005-2018 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 4564 * 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 8534 #include "memory.h"
34 adx 30 #include <sys/epoll.h>
35    
36 michael 8534 enum
37     {
38     INITIAL_NEVENT = 16,
39     MAXIMUM_NEVENT = 4096
40     };
41 adx 30
42 michael 8534 struct epollop
43     {
44     struct epoll_event *events;
45     int nevents;
46     int fd;
47     };
48 adx 30
49 michael 8534 static struct epollop *epollop;
50    
51    
52 adx 30 /*
53 michael 8413 * 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 8413 comm_select_init(void)
60 adx 30 {
61 michael 8534 int fd = epoll_create1(EPOLL_CLOEXEC);
62     if (fd < 0)
63 adx 30 {
64 michael 8413 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open epoll fd: %s",
65 michael 6559 strerror(errno));
66 michael 6554 exit(EXIT_FAILURE); /* Whee! */
67 adx 30 }
68    
69 michael 8657 fd_open(fd, false, "epoll file descriptor");
70 michael 8534
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 4462 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
85 michael 7329 void *client_data, uintmax_t timeout)
86 adx 30 {
87     int new_events, op;
88 michael 8530 struct epoll_event ep_event;
89 adx 30
90 michael 8657 assert(F->flags.open == true);
91 michael 8465
92 adx 30 if ((type & COMM_SELECT_READ))
93     {
94     F->read_handler = handler;
95     F->read_data = client_data;
96     }
97    
98     if ((type & COMM_SELECT_WRITE))
99     {
100     F->write_handler = handler;
101     F->write_data = client_data;
102     }
103    
104     new_events = (F->read_handler ? EPOLLIN : 0) |
105     (F->write_handler ? EPOLLOUT : 0);
106    
107 michael 8430 if (timeout)
108 michael 2725 {
109 adx 30 F->timeout = CurrentTime + (timeout / 1000);
110 michael 2725 F->timeout_handler = handler;
111     F->timeout_data = client_data;
112     }
113 adx 30
114     if (new_events != F->evcache)
115     {
116     if (new_events == 0)
117     op = EPOLL_CTL_DEL;
118     else if (F->evcache == 0)
119     op = EPOLL_CTL_ADD;
120     else
121     op = EPOLL_CTL_MOD;
122    
123 michael 8530 memset(&ep_event, 0, sizeof(ep_event));
124 adx 30 ep_event.events = F->evcache = new_events;
125 michael 8382 ep_event.data.ptr = F;
126 adx 30
127 michael 8534 if (epoll_ctl(epollop->fd, op, F->fd, &ep_event))
128 adx 30 {
129 michael 1247 ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno));
130 adx 30 abort();
131     }
132     }
133     }
134    
135     /*
136     * comm_select()
137     *
138     * Called to do the new-style IO, courtesy of of squid (like most of this
139     * new IO code). This routine handles the stuff we've hidden in
140     * comm_setselect and fd_table[] and calls callbacks for IO ready
141     * events.
142     */
143     void
144     comm_select(void)
145     {
146 michael 8430 int num;
147 michael 4462 void (*hdl)(fde_t *, void *);
148 adx 30
149 michael 8534 num = epoll_wait(epollop->fd, epollop->events, epollop->nevents, SELECT_DELAY);
150     assert(num <= epollop->nevents);
151 adx 30
152     set_time();
153    
154     if (num < 0)
155     {
156 michael 6541 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
157     nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
158 adx 30 return;
159     }
160    
161 michael 8430 for (int i = 0; i < num; ++i)
162 adx 30 {
163 michael 8534 fde_t *F = epollop->events[i].data.ptr;
164 michael 8338
165 michael 8657 if (F->flags.open == false)
166 adx 30 continue;
167    
168 michael 8534 if ((epollop->events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR)))
169 michael 2650 {
170 michael 5901 if ((hdl = F->read_handler))
171 adx 30 {
172     F->read_handler = NULL;
173     hdl(F, F->read_data);
174 michael 8338
175 michael 8657 if (F->flags.open == false)
176 michael 2650 continue;
177 adx 30 }
178 michael 2650 }
179 adx 30
180 michael 8534 if ((epollop->events[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR)))
181 michael 2650 {
182 michael 5901 if ((hdl = F->write_handler))
183 adx 30 {
184     F->write_handler = NULL;
185     hdl(F, F->write_data);
186 michael 8338
187 michael 8657 if (F->flags.open == false)
188 michael 2650 continue;
189 adx 30 }
190 michael 2650 }
191 adx 30
192     comm_setselect(F, 0, NULL, NULL, 0);
193     }
194 michael 8534
195     if (num == epollop->nevents && epollop->nevents < MAXIMUM_NEVENT)
196     {
197     /*
198     * We used all of the event space this time. We should be
199     * ready for more events next time.
200     */
201     int new_nevents = epollop->nevents * 2;
202     struct epoll_event *new_events = xrealloc(epollop->events, new_nevents * sizeof(*epollop->events));
203    
204     epollop->events = new_events;
205     epollop->nevents = new_nevents;
206     }
207 adx 30 }
208 stu 908 #endif

Properties

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