/[svn]/ircd-hybrid-7.3/src/s_bsd_kqueue.c
ViewVC logotype

Annotation of /ircd-hybrid-7.3/src/s_bsd_kqueue.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1029 - (hide annotations)
Sun Nov 8 13:10:50 2009 UTC (13 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 4974 byte(s)
- branch off trunk to create 7.3 branch
1 adx 30 /*
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 knight 31 * $Id$
24 adx 30 */
25    
26     #include "stdinc.h"
27 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE
28 adx 30 #include <sys/event.h>
29     #include "fdlist.h"
30     #include "ircd.h"
31     #include "memory.h"
32     #include "s_bsd.h"
33     #include "s_log.h"
34    
35     #define KE_LENGTH 128
36    
37     /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */
38    
39     #ifndef EV_SET
40     #define EV_SET(kevp, a, b, c, d, e, f) do { \
41     (kevp)->ident = (a); \
42     (kevp)->filter = (b); \
43     (kevp)->flags = (c); \
44     (kevp)->fflags = (d); \
45     (kevp)->data = (e); \
46     (kevp)->udata = (f); \
47     } while(0)
48     #endif
49    
50     static fde_t kqfd;
51     static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */
52     static int kqoff; /* offset into the buffer */
53     void init_netio(void);
54    
55     /*
56     * init_netio
57     *
58     * This is a needed exported function which will be called to initialise
59     * the network loop code.
60     */
61     void
62     init_netio(void)
63     {
64     int fd;
65    
66     if ((fd = kqueue()) < 0)
67     {
68     ilog(L_CRIT, "init_netio: Couldn't open kqueue fd!");
69     exit(115); /* Whee! */
70     }
71    
72     fd_open(&kqfd, fd, 0, "kqueue() file descriptor");
73     }
74    
75     /*
76     * Write a single update to the kqueue list.
77     */
78     static void
79     kq_update_events(int fd, int filter, int what)
80     {
81     static struct timespec zero_timespec = {0, 0};
82     struct kevent *kep = kq_fdlist + kqoff;
83    
84     EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL);
85    
86 adx 842 if (++kqoff == KE_LENGTH)
87 adx 30 {
88     kevent(kqfd.fd, kq_fdlist, kqoff, NULL, 0, &zero_timespec);
89     kqoff = 0;
90     }
91     }
92    
93     /*
94     * comm_setselect
95     *
96     * This is a needed exported function which will be called to register
97     * and deregister interest in a pending IO state for a given FD.
98     */
99     void
100     comm_setselect(fde_t *F, unsigned int type, PF *handler,
101     void *client_data, time_t timeout)
102     {
103     int new_events, diff;
104    
105     if ((type & COMM_SELECT_READ))
106     {
107     F->read_handler = handler;
108     F->read_data = client_data;
109     }
110    
111     if ((type & COMM_SELECT_WRITE))
112     {
113     F->write_handler = handler;
114     F->write_data = client_data;
115     }
116    
117     new_events = (F->read_handler ? COMM_SELECT_READ : 0) |
118     (F->write_handler ? COMM_SELECT_WRITE : 0);
119    
120     if (timeout != 0)
121     F->timeout = CurrentTime + (timeout / 1000);
122    
123     diff = new_events ^ F->evcache;
124    
125     if ((diff & COMM_SELECT_READ))
126     kq_update_events(F->fd, EVFILT_READ,
127     (new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE);
128     if ((diff & COMM_SELECT_WRITE))
129     kq_update_events(F->fd, EVFILT_WRITE,
130     (new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE);
131    
132     F->evcache = new_events;
133     }
134    
135     /*
136     * comm_select
137     *
138     * Called to do the new-style IO, courtesy 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     int num, i;
147     static struct kevent ke[KE_LENGTH];
148     struct timespec poll_time;
149     PF *hdl;
150     fde_t *F;
151    
152     /*
153     * remember we are doing NANOseconds here, not micro/milli. God knows
154     * why jlemon used a timespec, but hey, he wrote the interface, not I
155     * -- Adrian
156     */
157     poll_time.tv_sec = 0;
158     poll_time.tv_nsec = SELECT_DELAY * 1000000;
159     num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time);
160     kqoff = 0;
161    
162     set_time();
163    
164     if (num < 0)
165     {
166     #ifdef HAVE_USLEEP
167     usleep(50000); /* avoid 99% CPU in comm_select */
168     #endif
169     return;
170     }
171    
172     for (i = 0; i < num; i++)
173     {
174     F = lookup_fd(ke[i].ident);
175     if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR))
176     continue;
177    
178     if (ke[i].filter == EVFILT_READ)
179     if ((hdl = F->read_handler) != NULL)
180     {
181     F->read_handler = NULL;
182     hdl(F, F->read_data);
183     if (!F->flags.open)
184     continue;
185     }
186    
187     if (ke[i].filter == EVFILT_WRITE)
188     if ((hdl = F->write_handler) != NULL)
189     {
190     F->write_handler = NULL;
191     hdl(F, F->write_data);
192     if (!F->flags.open)
193     continue;
194     }
195    
196     comm_setselect(F, 0, NULL, NULL, 0);
197     }
198     }
199 stu 908 #endif

Properties

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

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