/[svn]/ircd-hybrid/libio/comm/kqueue.c
ViewVC logotype

Contents of /ircd-hybrid/libio/comm/kqueue.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 841 - (show annotations)
Fri Feb 9 21:24:15 2007 UTC (13 years, 7 months ago) by bear
File MIME type: text/x-chdr
File size: 4817 byte(s)
Fix off-by-one. This should get rid of adx's whowashash overwriteage.

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * s_bsd_kqueue.c: FreeBSD kqueue compatible network routines.
4 *
5 * Originally by Adrian Chadd <adrian@creative.net.au>
6 * Copyright (C) 2005 Hybrid Development Team
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 * $Id$
24 */
25
26 #include "stdinc.h"
27 #include <sys/event.h>
28
29 #define KE_LENGTH 128
30
31 /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */
32
33 #ifndef EV_SET
34 #define EV_SET(kevp, a, b, c, d, e, f) do { \
35 (kevp)->ident = (a); \
36 (kevp)->filter = (b); \
37 (kevp)->flags = (c); \
38 (kevp)->fflags = (d); \
39 (kevp)->data = (e); \
40 (kevp)->udata = (f); \
41 } while(0)
42 #endif
43
44 static fde_t kqfd;
45 static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */
46 static int kqoff; /* offset into the buffer */
47 void init_netio(void);
48
49 /*
50 * init_netio
51 *
52 * This is a needed exported function which will be called to initialise
53 * the network loop code.
54 */
55 void
56 init_netio(void)
57 {
58 int fd;
59
60 if ((fd = kqueue()) < 0)
61 {
62 ilog(L_CRIT, "init_netio: Couldn't open kqueue fd!");
63 exit(115); /* Whee! */
64 }
65
66 fd_open(&kqfd, fd, 0, "kqueue() file descriptor");
67 }
68
69 /*
70 * Write a single update to the kqueue list.
71 */
72 static void
73 kq_update_events(int fd, int filter, int what)
74 {
75 static struct timespec zero_timespec = {0, 0};
76 struct kevent *kep = kq_fdlist + kqoff;
77
78 EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL);
79
80 if (++kqoff == KE_LENGTH)
81 {
82 kevent(kqfd.fd, kq_fdlist, kqoff, NULL, 0, &zero_timespec);
83 kqoff = 0;
84 }
85 }
86
87 /*
88 * comm_setselect
89 *
90 * This is a needed exported function which will be called to register
91 * and deregister interest in a pending IO state for a given FD.
92 */
93 void
94 comm_setselect(fde_t *F, unsigned int type, PF *handler,
95 void *client_data, time_t timeout)
96 {
97 int new_events, diff;
98
99 if ((type & COMM_SELECT_READ))
100 {
101 F->read_handler = handler;
102 F->read_data = client_data;
103 }
104
105 if ((type & COMM_SELECT_WRITE))
106 {
107 F->write_handler = handler;
108 F->write_data = client_data;
109 }
110
111 new_events = (F->read_handler ? COMM_SELECT_READ : 0) |
112 (F->write_handler ? COMM_SELECT_WRITE : 0);
113
114 if (timeout != 0)
115 F->timeout = CurrentTime + (timeout / 1000);
116
117 diff = new_events ^ F->evcache;
118
119 if ((diff & COMM_SELECT_READ))
120 kq_update_events(F->fd, EVFILT_READ,
121 (new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE);
122 if ((diff & COMM_SELECT_WRITE))
123 kq_update_events(F->fd, EVFILT_WRITE,
124 (new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE);
125
126 F->evcache = new_events;
127 }
128
129 /*
130 * comm_select
131 *
132 * Called to do the new-style IO, courtesy of squid (like most of this
133 * new IO code). This routine handles the stuff we've hidden in
134 * comm_setselect and fd_table[] and calls callbacks for IO ready
135 * events.
136 */
137 void
138 comm_select(void)
139 {
140 int num, i;
141 static struct kevent ke[KE_LENGTH];
142 struct timespec poll_time;
143 PF *hdl;
144 fde_t *F;
145
146 /*
147 * remember we are doing NANOseconds here, not micro/milli. God knows
148 * why jlemon used a timespec, but hey, he wrote the interface, not I
149 * -- Adrian
150 */
151 poll_time.tv_sec = 0;
152 poll_time.tv_nsec = SELECT_DELAY * 1000000;
153 num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time);
154 kqoff = 0;
155
156 set_time();
157
158 if (num < 0)
159 {
160 #ifdef HAVE_USLEEP
161 usleep(50000); /* avoid 99% CPU in comm_select */
162 #endif
163 return;
164 }
165
166 for (i = 0; i < num; i++)
167 {
168 F = lookup_fd(ke[i].ident);
169 if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR))
170 continue;
171
172 if (ke[i].filter == EVFILT_READ)
173 if ((hdl = F->read_handler) != NULL)
174 {
175 F->read_handler = NULL;
176 hdl(F, F->read_data);
177 if (!F->flags.open)
178 continue;
179 }
180
181 if (ke[i].filter == EVFILT_WRITE)
182 if ((hdl = F->write_handler) != NULL)
183 {
184 F->write_handler = NULL;
185 hdl(F, F->write_data);
186 if (!F->flags.open)
187 continue;
188 }
189
190 comm_setselect(F, 0, NULL, NULL, 0);
191 }
192 }

Properties

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

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