chiark / gitweb /
Universal hashing.
[mLib] / sel.c
CommitLineData
97f65b00 1/* -*-c-*-
2 *
35036742 3 * $Id: sel.c,v 1.12 2003/05/18 15:10:29 mdw Exp $
97f65b00 4 *
5 * I/O multiplexing support
6 *
a3ddb778 7 * (c) 1999 Straylight/Edgeware
97f65b00 8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: sel.c,v $
35036742 33 * Revision 1.12 2003/05/18 15:10:29 mdw
34 * Remove memory leak.
35 *
4790f418 36 * Revision 1.11 2003/05/17 10:34:04 mdw
37 * Tidying and bugfixing.
38 *
0a2a19b4 39 * Revision 1.10 2001/06/22 19:35:58 mdw
40 * Fix a large number of bugs.
41 *
c1f23e99 42 * Revision 1.9 2001/02/03 19:07:08 mdw
43 * Ensure that timers set to go off in the past don't case a problem.
44 *
38472b8e 45 * Revision 1.8 2000/03/23 20:42:08 mdw
46 * Rearrange timeout handling to avoid list corruptions.
47 *
932341d9 48 * Revision 1.7 1999/12/11 11:12:17 mdw
49 * Fix comment formatting error.
50 *
5e1ded7d 51 * Revision 1.6 1999/09/26 14:28:11 mdw
52 * (sel_select): Almost pointless efficiency tweak.
53 *
1101f87a 54 * Revision 1.5 1999/08/31 17:42:22 mdw
55 * New function `sel_force' to force a descriptor to be `selected'.
56 *
1226c514 57 * Revision 1.4 1999/08/19 18:30:26 mdw
58 * Implement hooks for foreign select-using systems (currently not well
59 * tested).
60 *
c05c6d83 61 * Revision 1.3 1999/05/21 22:13:59 mdw
62 * Use new `tv' macros. Fix ordering bug for timeout selectors.
63 *
a3ddb778 64 * Revision 1.2 1999/05/15 10:33:32 mdw
65 * Fix copyright notices.
66 *
97f65b00 67 * Revision 1.1 1999/05/14 21:01:14 mdw
68 * Integrated `select' handling bits from the background resolver project.
69 *
70 */
71
72/*----- Header files ------------------------------------------------------*/
73
0a2a19b4 74#include <assert.h>
97f65b00 75#include <stdio.h>
76#include <stdlib.h>
77#include <string.h>
78
79#include <sys/types.h>
80#include <sys/time.h>
81#include <unistd.h>
82
83#include "sel.h"
0a2a19b4 84#include "sub.h"
97f65b00 85#include "tv.h"
86
0a2a19b4 87/*----- Data structures ---------------------------------------------------*/
88
89typedef struct sel_pendfile {
90 struct sel_pendfile *next;
91 sel_file *f;
92} pfile;
93
94typedef struct sel_pendtimer {
95 struct sel_pendtimer *next;
96 sel_timer *t;
97} ptimer;
98
97f65b00 99/*----- Main code ---------------------------------------------------------*/
100
101/* --- @sel_init@ --- *
102 *
103 * Arguments: @sel_state *s@ = pointer to a state block to initialize
104 *
105 * Returns: ---
106 *
107 * Use: Initializes a select state block.
108 */
109
110void sel_init(sel_state *s)
111{
1101f87a 112 int i;
113
114 for (i = 0; i < SEL_MODES; i++) {
115 s->files[i] = 0;
116 FD_ZERO(&s->fd[i]);
117 }
97f65b00 118 s->timers = 0;
1226c514 119 s->hooks = 0;
1101f87a 120 s->args = 0;
97f65b00 121}
122
123/* --- @sel_initfile@ --- *
124 *
125 * Arguments: @sel_state *s@ = select state to attach to
126 * @sel_file *f@ = pointer to a file block to initialize
127 * @int fd@ = the file descriptor to listen to
128 * @unsigned mode@ = what to listen for
129 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
130 * @void *p@ = argument to pass to handler
131 *
132 * Returns: ---
133 *
134 * Use: Initializes a file block ready for use. The file block
135 * isn't added to the list of things to do until a call to
136 * @sel_addfile@.
137 */
138
139void sel_initfile(sel_state *s, sel_file *f,
140 int fd, unsigned mode,
141 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
142 void *p)
143{
144 f->s = s;
145 f->fd = fd;
146 f->mode = mode;
147 f->func = func;
148 f->p = p;
0a2a19b4 149 f->pend = 0;
97f65b00 150}
151
152/* --- @sel_addfile@ --- *
153 *
154 * Arguments: @sel_file *f@ = pointer to a file block
155 *
156 * Returns: ---
157 *
158 * Use: Adds a file block into the list of things to listen to.
159 */
160
161void sel_addfile(sel_file *f)
162{
1101f87a 163 sel_file **ff = &f->s->files[f->mode];
97f65b00 164
165 /* --- This little dance looks like line-noise, but it does the job --- */
166
167 while (*ff && (*ff)->fd > f->fd)
168 ff = &(*ff)->next;
169 f->next = *ff;
4790f418 170 f->prev = ff;
97f65b00 171 if (*ff)
4790f418 172 (*ff)->prev = &f->next;
97f65b00 173 *ff = f;
174 FD_SET(f->fd, f->s->fd + f->mode);
175}
176
1101f87a 177/* --- @sel_force@ --- *
178 *
179 * Arguments: @sel_file *f@ = pointer to file selector
180 *
181 * Returns: ---
182 *
183 * Use: Forces a file selector to be considered ready. This is only
184 * useful during a call to @sel_select@. Of particular use is
185 * forcing a write selector when there's something interesting
186 * ready for it.
187 */
188
189void sel_force(sel_file *f)
190{
191 if (f->s->args)
192 FD_SET(f->fd, &f->s->args->fd[f->mode]);
193}
194
97f65b00 195/* --- @sel_rmfile@ --- *
196 *
197 * Arguments: @sel_file *f@ = pointer to a file block
198 *
199 * Returns: ---
200 *
201 * Use: Removes a file block from the list of things to listen to.
202 */
203
204void sel_rmfile(sel_file *f)
205{
4790f418 206 *f->prev = f->next;
97f65b00 207 if (f->next)
208 f->next->prev = f->prev;
209 FD_CLR(f->fd, f->s->fd + f->mode);
0a2a19b4 210 if (f->pend) {
211 f->pend->f = 0;
212 f->pend = 0;
213 }
97f65b00 214}
215
216/* --- @sel_addtimer@ --- *
217 *
218 * Arguments: @sel_state *s@ = pointer to a state block
219 * @sel_timer *t@ = pointer to a timer block
220 * @struct timeval *tv@ = pointer to time to activate
221 * @void (*func)(struct timeval *tv, void *p)@ = handler
222 * @void *p@ = argument for handler function
223 *
224 * Returns: ---
225 *
226 * Use: Registers and sets up a timer.
227 */
228
229void sel_addtimer(sel_state *s, sel_timer *t,
230 struct timeval *tv,
231 void (*func)(struct timeval */*tv*/, void */*p*/),
232 void *p)
233{
234 sel_timer **tt = &s->timers;
0a2a19b4 235 { sel_timer *q; for (q = s->timers; q; q = q->next) assert(q != t); }
97f65b00 236
237 /* --- Set up the timer block --- */
238
239 t->tv = *tv;
240 t->func = func;
241 t->p = p;
0a2a19b4 242 t->pend = 0;
97f65b00 243
244 /* --- More line noise --- */
245
c05c6d83 246 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
97f65b00 247 tt = &(*tt)->next;
248 t->next = *tt;
4790f418 249 t->prev = tt;
97f65b00 250 if (*tt)
4790f418 251 (*tt)->prev = &t->next;
97f65b00 252 *tt = t;
253}
254
255/* --- @sel_rmtimer@ --- *
256 *
257 * Arguments: @sel_timer *t@ = pointer to timer block
258 *
259 * Returns: ---
260 *
261 * Use: Removes a timer from the list of timers.
262 */
263
264void sel_rmtimer(sel_timer *t)
265{
0a2a19b4 266 if (t->pend) {
267 t->pend->t = 0;
268 t->pend = 0;
4790f418 269 } else {
270 *t->prev = t->next;
271 if (t->next)
272 t->next->prev = t->prev;
0a2a19b4 273 }
97f65b00 274}
275
1226c514 276/* --- @sel_addhook@ --- *
277 *
278 * Arguments: @sel_state *s@ = pointer to state block
279 * @sel_hook *h@ = pointer to hook block
280 * @sel_hookfn before, after@ = hook functions
281 * @void *p@ = pointer argument to pass to hook functions
282 *
283 * Returns: ---
284 *
285 * Use: Registers hook functions to be called on each select call.
286 */
287
288void sel_addhook(sel_state *s, sel_hook *h,
289 sel_hookfn before, sel_hookfn after,
290 void *p)
291{
292 h->before = before;
293 h->after = after;
294 h->p = p;
295 h->next = s->hooks;
4790f418 296 h->prev = &s->hooks;
1226c514 297 if (s->hooks)
4790f418 298 s->hooks->prev = &h->next;
1226c514 299 s->hooks = h;
300}
301
302/* --- @sel_rmhook@ --- *
303 *
304 * Arguments: @sel_hook *h@ = pointer to hook block
305 *
306 * Returns: ---
307 *
308 * Use: Removes hook functions.
309 */
310
311void sel_rmhook(sel_hook *h)
312{
313 if (h->next)
314 h->next->prev = h->prev;
4790f418 315 *h->prev = h->next;
1226c514 316}
317
318/* --- @sel_fdmerge@ --- *
932341d9 319 *
1226c514 320 * Arguments: @fd_set *dest@ = destination FD set
321 * @fd_set *fd@ = pointer to set to merge
322 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
323 *
324 * Returns: Actual highest numbered descriptor.
325 *
326 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
327 * value.
328 */
329
330int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd)
331{
332 int i, m = -1;
333
334 for (i = 0; i < maxfd; i++) {
335 if (FD_ISSET(i, fd)) {
336 FD_SET(i, dest);
337 m = i;
338 }
339 }
340
341 return (m + 1);
342}
343
97f65b00 344/* --- @sel_select@ --- *
345 *
346 * Arguments: @sel_state *s@ = pointer to state block
347 *
348 * Returns: Zero if all OK, -1 on error.
349 *
350 * Use: Does a @select@ call (or equivalent @poll@).
351 */
352
353int sel_select(sel_state *s)
354{
1226c514 355 sel_args a;
97f65b00 356 int err;
357
1226c514 358 /* --- Initialize the argument block --- */
359
1101f87a 360 {
361 int i;
362 a.maxfd = 0;
363 for (i = 0; i < SEL_MODES; i++) {
364 if (s->files[i] && s->files[i]->fd >= a.maxfd)
365 a.maxfd = s->files[i]->fd + 1;
366 }
367 }
368
1226c514 369 memcpy(a.fd, s->fd, sizeof(a.fd));
370 if (s->timers || s->hooks)
371 gettimeofday(&a.now, 0);
372 if (!s->timers)
373 a.tvp = 0;
374 else {
c1f23e99 375 if (TV_CMP(&s->timers->tv, >, &a.now))
376 TV_SUB(&a.tv, &s->timers->tv, &a.now);
377 else {
378 a.tv.tv_sec = 0;
379 a.tv.tv_usec = 0;
380 }
1226c514 381 a.tvp = &a.tv;
382 }
1101f87a 383 s->args = &a;
1226c514 384
385 /* --- Grind through the pre hooks --- */
386
387 {
388 sel_hook *h = s->hooks;
389 while (h) {
390 sel_hook *hh = h;
391 h = h->next;
392 if (hh->before)
393 hh->before(s, &a, hh->p);
394 }
395 }
396
397 /* --- Run the @select@ call --- */
398
399 if ((err = select(a.maxfd,
400 &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC],
1101f87a 401 a.tvp)) < 0) {
402 s->args = 0;
97f65b00 403 return (err);
1101f87a 404 }
97f65b00 405
1226c514 406 if (a.tvp)
407 gettimeofday(&a.now, 0);
408
409 /* --- Run through the hooks again --- */
410
411 {
412 sel_hook *h = s->hooks;
413 while (h) {
414 sel_hook *hh = h;
415 h = h->next;
416 if (hh->after)
417 hh->after(s, &a, hh->p);
418 }
419 }
420
421 /* --- Run through the timers --- */
422
0a2a19b4 423 if (s->timers) {
424 ptimer *pthead, *pt, **ptt = &pthead;
425 sel_timer *t;
426
427 for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = t->next) {
428 pt = CREATE(ptimer);
429 pt->t = t;
430 t->pend = pt;
431 *ptt = pt;
432 ptt = &pt->next;
433 }
434 *ptt = 0;
38472b8e 435 if (t) {
4790f418 436 *t->prev = 0;
437 t->prev = &s->timers;
38472b8e 438 }
439 s->timers = t;
0a2a19b4 440 while (pthead) {
441 pt = pthead;
442 pthead = pt->next;
443 t = pt->t;
444 if (t) {
0a2a19b4 445 t->pend = 0;
4790f418 446 t->next = 0;
447 t->prev = &t->next;
448 t->func(&a.now, t->p);
0a2a19b4 449 }
450 DESTROY(pt);
97f65b00 451 }
97f65b00 452 }
453
1101f87a 454 /* --- And finally run through the files --- *
455 *
456 * Do reads first. It's quite possible that a read might prompt a write,
457 * but the other way around is less likely. Fortunately, the modes are
458 * in the right order for this.
459 */
1226c514 460
97f65b00 461 {
1101f87a 462 int i;
463
464 for (i = 0; i < SEL_MODES; i++) {
0a2a19b4 465 pfile *pfhead, *pf, **pff = &pfhead;
466 sel_file *f;
467
468 for (f = s->files[i]; f; f = f->next) {
469 if (!FD_ISSET(f->fd, &a.fd[i]))
470 continue;
471 pf = CREATE(pfile);
472 pf->f = f;
473 f->pend = pf;
474 *pff = pf;
475 pff = &pf->next;
476 }
477 *pff = 0;
478 while (pfhead) {
479 pf = pfhead;
480 pfhead = pf->next;
481 f = pf->f;
482 if (f) {
0a2a19b4 483 f->pend = 0;
4790f418 484 f->func(f->fd, i, f->p);
0a2a19b4 485 }
35036742 486 DESTROY(pf);
1101f87a 487 }
97f65b00 488 }
489 }
490
1101f87a 491 s->args = 0;
97f65b00 492 return (0);
493}
494
495/*----- That's all, folks -------------------------------------------------*/