ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_poll.c
Revision: 2650
Committed: Tue Dec 10 19:46:48 2013 UTC (11 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 4652 byte(s)
Log Message:
- s_bsd_select.c, s_bsd_devpoll.c, s_bsd_epoll.c,
  s_bsd_kqueue.c, s_bsd_poll.c: style corrections

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * s_bsd_poll.c: POSIX poll() compatible network routines.
4     *
5     * Originally by Adrian Chadd <adrian@creative.net.au>
6     * Copyright (C) 2002 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_POLL
28 adx 30 #include <sys/poll.h>
29     #include "fdlist.h"
30 michael 1021 #include "list.h"
31 michael 2627 #include "memory.h"
32 adx 30 #include "hook.h"
33     #include "ircd.h"
34     #include "s_bsd.h"
35 michael 1309 #include "log.h"
36 adx 30
37     /* I hate linux -- adrian */
38     #ifndef POLLRDNORM
39     #define POLLRDNORM POLLIN
40     #endif
41     #ifndef POLLWRNORM
42     #define POLLWRNORM POLLOUT
43     #endif
44    
45     static struct pollfd *pollfds;
46     static int pollmax = -1; /* highest FD number */
47    
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     pollfds = MyMalloc(sizeof(struct pollfd) * hard_fdlimit);
61    
62     for (fd = 0; fd < hard_fdlimit; fd++)
63     pollfds[fd].fd = -1;
64     }
65    
66     /*
67     * find a spare slot in the fd list. We can optimise this out later!
68     * -- adrian
69     */
70     static inline int
71     poll_findslot(void)
72     {
73     int i;
74    
75     for (i = 0; i < hard_fdlimit; i++)
76     if (pollfds[i].fd == -1)
77     {
78     /* MATCH!!#$*&$ */
79     return i;
80     }
81    
82     assert(1 == 0);
83     /* NOTREACHED */
84     return -1;
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;
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 ? POLLRDNORM : 0) |
112 michael 2650 (F->write_handler ? POLLWRNORM : 0);
113 adx 30
114     if (timeout != 0)
115     F->timeout = CurrentTime + (timeout / 1000);
116    
117     if (new_events != F->evcache)
118     {
119     if (new_events == 0)
120     {
121     pollfds[F->comm_index].fd = -1;
122     pollfds[F->comm_index].revents = 0;
123    
124     if (pollmax == F->comm_index)
125     while (pollmax >= 0 && pollfds[pollmax].fd == -1)
126 michael 2650 pollmax--;
127 adx 30 }
128     else
129     {
130     if (F->evcache == 0)
131     {
132     F->comm_index = poll_findslot();
133 michael 2650 if (F->comm_index > pollmax)
134     pollmax = F->comm_index;
135 adx 30
136 michael 2650 pollfds[F->comm_index].fd = F->fd;
137 adx 30 }
138 michael 2650
139 adx 30 pollfds[F->comm_index].events = new_events;
140     pollfds[F->comm_index].revents = 0;
141     }
142    
143     F->evcache = new_events;
144     }
145     }
146    
147     /*
148     * comm_select
149     *
150     * Called to do the new-style IO, courtesy of of squid (like most of this
151     * new IO code). This routine handles the stuff we've hidden in
152     * comm_setselect and fd_table[] and calls callbacks for IO ready
153     * events.
154     */
155     void
156     comm_select(void)
157     {
158     int num, ci, revents;
159     PF *hdl;
160     fde_t *F;
161    
162     /* XXX kill that +1 later ! -- adrian */
163     num = poll(pollfds, pollmax + 1, SELECT_DELAY);
164    
165     set_time();
166    
167     if (num < 0)
168     {
169     #ifdef HAVE_USLEEP
170     usleep(50000); /* avoid 99% CPU in comm_select */
171     #endif
172     return;
173     }
174    
175     for (ci = 0; ci <= pollmax && num > 0; ci++)
176     {
177     if ((revents = pollfds[ci].revents) == 0 || pollfds[ci].fd == -1)
178     continue;
179     num--;
180    
181     F = lookup_fd(pollfds[ci].fd);
182     if (F == NULL || !F->flags.open)
183     continue;
184    
185     if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR))
186 michael 2650 {
187 adx 30 if ((hdl = F->read_handler) != NULL)
188     {
189     F->read_handler = NULL;
190     hdl(F, F->read_data);
191 michael 2650 if (!F->flags.open)
192     continue;
193 adx 30 }
194 michael 2650 }
195 adx 30
196     if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR))
197 michael 2650 {
198 adx 30 if ((hdl = F->write_handler) != NULL)
199     {
200     F->write_handler = NULL;
201     hdl(F, F->write_data);
202 michael 2650 if (!F->flags.open)
203     continue;
204 adx 30 }
205 michael 2650 }
206 adx 30
207     comm_setselect(F, 0, NULL, NULL, 0);
208     }
209     }
210 stu 908 #endif

Properties

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