/[svn]/ircd-hybrid/branches/8.2.x/src/s_bsd_kqueue.c
ViewVC logotype

Contents of /ircd-hybrid/branches/8.2.x/src/s_bsd_kqueue.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8901 - (show annotations)
Sun Apr 21 20:10:44 2019 UTC (3 years, 3 months ago) by michael
File MIME type: text/x-chdr
File size: 4887 byte(s)
- Add support for monotonic clocks and have some timers make use of it;  remove event_set_back_events()

1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2000-2019 ircd-hybrid development team
5 *
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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file s_bsd_kqueue.c
23 * \brief kqueue() compatible network routines.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE
29 #include <sys/event.h>
30 #include "fdlist.h"
31 #include "ircd.h"
32 #include "s_bsd.h"
33 #include "log.h"
34
35 enum { KE_LENGTH = 128 };
36
37 static int kqueue_fd;
38 static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */
39 static int kqoff; /* offset into the buffer */
40
41
42 /*
43 * comm_select_init
44 *
45 * This is a needed exported function which will be called to initialise
46 * the network loop code.
47 */
48 void
49 comm_select_init(void)
50 {
51 if ((kqueue_fd = kqueue()) < 0)
52 {
53 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open kqueue fd: %s",
54 strerror(errno));
55 exit(EXIT_FAILURE); /* Whee! */
56 }
57
58 fd_open(kqueue_fd, false, "kqueue() file descriptor");
59 }
60
61 /*
62 * Write a single update to the kqueue list.
63 */
64 static void
65 kq_update_events(fde_t *F, int filter, int what)
66 {
67 const struct timespec zero_timespec = { .tv_sec = 0, .tv_nsec = 0 };
68 struct kevent *kep = kq_fdlist + kqoff;
69
70 EV_SET(kep, (uintptr_t) F->fd, (short) filter, what, 0, 0, F);
71
72 if (++kqoff == KE_LENGTH)
73 {
74 int i;
75
76 for (i = 0; i < kqoff; ++i)
77 kevent(kqueue_fd, &kq_fdlist[i], 1, NULL, 0, &zero_timespec);
78 kqoff = 0;
79 }
80 }
81
82 /*
83 * comm_setselect
84 *
85 * This is a needed exported function which will be called to register
86 * and deregister interest in a pending IO state for a given FD.
87 */
88 void
89 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
90 void *client_data, uintmax_t timeout)
91 {
92 int new_events, diff;
93
94 assert(F);
95 assert(F->flags.open == true);
96
97 if ((type & COMM_SELECT_READ))
98 {
99 F->read_handler = handler;
100 F->read_data = client_data;
101 }
102
103 if ((type & COMM_SELECT_WRITE))
104 {
105 F->write_handler = handler;
106 F->write_data = client_data;
107 }
108
109 new_events = (F->read_handler ? COMM_SELECT_READ : 0) |
110 (F->write_handler ? COMM_SELECT_WRITE : 0);
111
112 if (timeout)
113 {
114 F->timeout = event_base->time.sec_monotonic + (timeout / 1000);
115 F->timeout_handler = handler;
116 F->timeout_data = client_data;
117 }
118
119 diff = new_events ^ F->evcache;
120
121 if ((diff & COMM_SELECT_READ))
122 kq_update_events(F, EVFILT_READ, (new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE);
123 if ((diff & COMM_SELECT_WRITE))
124 kq_update_events(F, EVFILT_WRITE, (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;
141 static struct kevent ke[KE_LENGTH];
142 struct timespec poll_time;
143 void (*hdl)(fde_t *, void *);
144
145 /*
146 * remember we are doing NANOseconds here, not micro/milli. God knows
147 * why jlemon used a timespec, but hey, he wrote the interface, not I
148 * -- Adrian
149 */
150 poll_time.tv_sec = 0;
151 poll_time.tv_nsec = SELECT_DELAY * 1000000;
152 num = kevent(kqueue_fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time);
153 kqoff = 0;
154
155 event_time_set();
156
157 if (num < 0)
158 {
159 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
160 nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
161 return;
162 }
163
164 for (int i = 0; i < num; ++i)
165 {
166 fde_t *F = ke[i].udata;
167
168 if (F->flags.open == false || (ke[i].flags & EV_ERROR))
169 continue;
170
171 if (ke[i].filter == EVFILT_READ)
172 {
173 if ((hdl = F->read_handler))
174 {
175 F->read_handler = NULL;
176 hdl(F, F->read_data);
177
178 if (F->flags.open == false)
179 continue;
180 }
181 }
182
183 if (ke[i].filter == EVFILT_WRITE)
184 {
185 if ((hdl = F->write_handler))
186 {
187 F->write_handler = NULL;
188 hdl(F, F->write_data);
189
190 if (F->flags.open == false)
191 continue;
192 }
193 }
194
195 comm_setselect(F, 0, NULL, NULL, 0);
196 }
197 }
198 #endif

Properties

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

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