ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_devpoll.c
Revision: 8900
Committed: Sun Apr 21 20:09:18 2019 UTC (6 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 4766 byte(s)
Log Message:
- Add support for monotonic clocks and have some timers make use of it;  remove event_set_back_events()

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 8752 * Copyright (c) 2001-2019 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_devpoll.c
23     * \brief /dev/poll compatible network routines.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 stu 908 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_DEVPOLL
29 adx 30 #include <sys/ioctl.h>
30     /* HPUX uses devpoll.h and not sys/devpoll.h */
31     #ifdef HAVE_DEVPOLL_H
32     # include <devpoll.h>
33     #else
34     # ifdef HAVE_SYS_DEVPOLL_H
35     # include <sys/devpoll.h>
36     # else
37     # error "No devpoll.h found! Try ./configuring and letting the script choose for you."
38     # endif
39     #endif
40     #include "fdlist.h"
41     #include "ircd.h"
42     #include "s_bsd.h"
43 michael 1309 #include "log.h"
44 adx 30
45 michael 8339 static int devpoll_fd;
46 adx 30
47     /*
48 michael 8414 * comm_select_init
49 adx 30 *
50     * This is a needed exported function which will be called to initialise
51     * the network loop code.
52     */
53     void
54 michael 8414 comm_select_init(void)
55 adx 30 {
56 michael 8339 if ((devpoll_fd = open("/dev/poll", O_RDWR)) < 0)
57 adx 30 {
58 michael 8414 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open /dev/poll: %s",
59 michael 6560 strerror(errno));
60 michael 6553 exit(EXIT_FAILURE); /* Whee! */
61 adx 30 }
62    
63 michael 8658 fd_open(devpoll_fd, false, "/dev/poll file descriptor");
64 adx 30 }
65    
66     /*
67     * Write an update to the devpoll filter.
68     * See, we end up having to do a seperate (?) remove before we do an
69     * add of a new polltype, so we have to have this function seperate from
70     * the others.
71     */
72     static void
73     devpoll_write_update(int fd, int events)
74     {
75     struct pollfd pfd;
76    
77     /* Build the pollfd entry */
78     pfd.revents = 0;
79     pfd.fd = fd;
80     pfd.events = events;
81    
82     /* Write the thing to our poll fd */
83 michael 8339 if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd))
84 michael 1247 ilog(LOG_TYPE_IRCD, "devpoll_write_update: dpfd write failed %d: %s",
85 adx 30 errno, strerror(errno));
86     }
87    
88     /*
89     * comm_setselect
90     *
91     * This is a needed exported function which will be called to register
92     * and deregister interest in a pending IO state for a given FD.
93     */
94     void
95 michael 4461 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
96 michael 7330 void *client_data, uintmax_t timeout)
97 adx 30 {
98     int new_events;
99    
100 michael 8717 assert(F);
101     assert(F->flags.open == true);
102    
103 adx 30 if ((type & COMM_SELECT_READ))
104     {
105     F->read_handler = handler;
106     F->read_data = client_data;
107     }
108    
109     if ((type & COMM_SELECT_WRITE))
110     {
111     F->write_handler = handler;
112     F->write_data = client_data;
113     }
114    
115 adx 282 new_events = (F->read_handler ? POLLIN : 0) |
116     (F->write_handler ? POLLOUT : 0);
117 adx 30
118 michael 8431 if (timeout)
119 michael 2725 {
120 michael 8900 F->timeout = event_base->time.sec_monotonic + (timeout / 1000);
121 michael 2725 F->timeout_handler = handler;
122     F->timeout_data = client_data;
123     }
124 adx 30
125     if (new_events != F->evcache)
126     {
127     devpoll_write_update(F->fd, POLLREMOVE);
128     if ((F->evcache = new_events))
129     devpoll_write_update(F->fd, new_events);
130     }
131     }
132    
133     /*
134     * comm_select
135     *
136     * Called to do the new-style IO, courtesy of squid (like most of this
137     * new IO code). This routine handles the stuff we've hidden in
138     * comm_setselect and fd_table[] and calls callbacks for IO ready
139     * events.
140     */
141     void
142     comm_select(void)
143     {
144 michael 8431 int num;
145 adx 30 struct pollfd pollfds[128];
146     struct dvpoll dopoll;
147 michael 4461 void (*hdl)(fde_t *, void *);
148 adx 30
149     dopoll.dp_timeout = SELECT_DELAY;
150     dopoll.dp_nfds = 128;
151     dopoll.dp_fds = &pollfds[0];
152 michael 8339 num = ioctl(devpoll_fd, DP_POLL, &dopoll);
153 adx 30
154 michael 8900 event_time_set();
155 adx 30
156     if (num < 0)
157     {
158 michael 6542 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
159     nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
160 adx 30 return;
161     }
162    
163 michael 8431 for (int i = 0; i < num; ++i)
164 adx 30 {
165 michael 8339 fde_t *F = &fd_table[dopoll.dp_fds[i].fd];
166    
167 michael 8658 if (F->flags.open == false)
168 adx 30 continue;
169    
170 adx 282 if ((dopoll.dp_fds[i].revents & POLLIN))
171 michael 2650 {
172 michael 8431 if ((hdl = F->read_handler))
173 adx 30 {
174     F->read_handler = NULL;
175     hdl(F, F->read_data);
176 michael 8339
177 michael 8658 if (F->flags.open == false)
178 adx 242 continue;
179 adx 30 }
180 michael 2650 }
181 adx 30
182 adx 282 if ((dopoll.dp_fds[i].revents & POLLOUT))
183 michael 2650 {
184 michael 8431 if ((hdl = F->write_handler))
185 adx 30 {
186     F->write_handler = NULL;
187     hdl(F, F->write_data);
188 michael 8339
189 michael 8658 if (F->flags.open == false)
190 adx 242 continue;
191 adx 30 }
192 michael 2650 }
193 adx 30
194     comm_setselect(F, 0, NULL, NULL, 0);
195     }
196     }
197 stu 908 #endif

Properties

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