3 * $Id: sel.c,v 1.7 1999/12/11 11:12:17 mdw Exp $
5 * I/O multiplexing support
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.7 1999/12/11 11:12:17 mdw
34 * Fix comment formatting error.
36 * Revision 1.6 1999/09/26 14:28:11 mdw
37 * (sel_select): Almost pointless efficiency tweak.
39 * Revision 1.5 1999/08/31 17:42:22 mdw
40 * New function `sel_force' to force a descriptor to be `selected'.
42 * Revision 1.4 1999/08/19 18:30:26 mdw
43 * Implement hooks for foreign select-using systems (currently not well
46 * Revision 1.3 1999/05/21 22:13:59 mdw
47 * Use new `tv' macros. Fix ordering bug for timeout selectors.
49 * Revision 1.2 1999/05/15 10:33:32 mdw
50 * Fix copyright notices.
52 * Revision 1.1 1999/05/14 21:01:14 mdw
53 * Integrated `select' handling bits from the background resolver project.
57 /*----- Header files ------------------------------------------------------*/
63 #include <sys/types.h>
70 /*----- Main code ---------------------------------------------------------*/
72 /* --- @sel_init@ --- *
74 * Arguments: @sel_state *s@ = pointer to a state block to initialize
78 * Use: Initializes a select state block.
81 void sel_init(sel_state *s)
85 for (i = 0; i < SEL_MODES; i++) {
94 /* --- @sel_initfile@ --- *
96 * Arguments: @sel_state *s@ = select state to attach to
97 * @sel_file *f@ = pointer to a file block to initialize
98 * @int fd@ = the file descriptor to listen to
99 * @unsigned mode@ = what to listen for
100 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
101 * @void *p@ = argument to pass to handler
105 * Use: Initializes a file block ready for use. The file block
106 * isn't added to the list of things to do until a call to
110 void sel_initfile(sel_state *s, sel_file *f,
111 int fd, unsigned mode,
112 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
122 /* --- @sel_addfile@ --- *
124 * Arguments: @sel_file *f@ = pointer to a file block
128 * Use: Adds a file block into the list of things to listen to.
131 void sel_addfile(sel_file *f)
133 sel_file **ff = &f->s->files[f->mode];
135 /* --- This little dance looks like line-noise, but it does the job --- */
137 while (*ff && (*ff)->fd > f->fd)
140 f->prev = (sel_file *)ff;
144 FD_SET(f->fd, f->s->fd + f->mode);
147 /* --- @sel_force@ --- *
149 * Arguments: @sel_file *f@ = pointer to file selector
153 * Use: Forces a file selector to be considered ready. This is only
154 * useful during a call to @sel_select@. Of particular use is
155 * forcing a write selector when there's something interesting
159 void sel_force(sel_file *f)
162 FD_SET(f->fd, &f->s->args->fd[f->mode]);
165 /* --- @sel_rmfile@ --- *
167 * Arguments: @sel_file *f@ = pointer to a file block
171 * Use: Removes a file block from the list of things to listen to.
174 void sel_rmfile(sel_file *f)
176 f->prev->next = f->next;
178 f->next->prev = f->prev;
179 FD_CLR(f->fd, f->s->fd + f->mode);
182 /* --- @sel_addtimer@ --- *
184 * Arguments: @sel_state *s@ = pointer to a state block
185 * @sel_timer *t@ = pointer to a timer block
186 * @struct timeval *tv@ = pointer to time to activate
187 * @void (*func)(struct timeval *tv, void *p)@ = handler
188 * @void *p@ = argument for handler function
192 * Use: Registers and sets up a timer.
195 void sel_addtimer(sel_state *s, sel_timer *t,
197 void (*func)(struct timeval */*tv*/, void */*p*/),
200 sel_timer **tt = &s->timers;
202 /* --- Set up the timer block --- */
208 /* --- More line noise --- */
210 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
213 t->prev = (sel_timer *)tt;
219 /* --- @sel_rmtimer@ --- *
221 * Arguments: @sel_timer *t@ = pointer to timer block
225 * Use: Removes a timer from the list of timers.
228 void sel_rmtimer(sel_timer *t)
230 t->prev->next = t->next;
232 t->next->prev = t->prev;
235 /* --- @sel_addhook@ --- *
237 * Arguments: @sel_state *s@ = pointer to state block
238 * @sel_hook *h@ = pointer to hook block
239 * @sel_hookfn before, after@ = hook functions
240 * @void *p@ = pointer argument to pass to hook functions
244 * Use: Registers hook functions to be called on each select call.
247 void sel_addhook(sel_state *s, sel_hook *h,
248 sel_hookfn before, sel_hookfn after,
255 h->prev = (sel_hook *)&s->hooks;
261 /* --- @sel_rmhook@ --- *
263 * Arguments: @sel_hook *h@ = pointer to hook block
267 * Use: Removes hook functions.
270 void sel_rmhook(sel_hook *h)
273 h->next->prev = h->prev;
274 h->prev->next = h->next;
277 /* --- @sel_fdmerge@ --- *
279 * Arguments: @fd_set *dest@ = destination FD set
280 * @fd_set *fd@ = pointer to set to merge
281 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
283 * Returns: Actual highest numbered descriptor.
285 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
289 int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd)
293 for (i = 0; i < maxfd; i++) {
294 if (FD_ISSET(i, fd)) {
303 /* --- @sel_select@ --- *
305 * Arguments: @sel_state *s@ = pointer to state block
307 * Returns: Zero if all OK, -1 on error.
309 * Use: Does a @select@ call (or equivalent @poll@).
312 int sel_select(sel_state *s)
317 /* --- Initialize the argument block --- */
322 for (i = 0; i < SEL_MODES; i++) {
323 if (s->files[i] && s->files[i]->fd >= a.maxfd)
324 a.maxfd = s->files[i]->fd + 1;
328 memcpy(a.fd, s->fd, sizeof(a.fd));
329 if (s->timers || s->hooks)
330 gettimeofday(&a.now, 0);
334 TV_SUB(&a.tv, &s->timers->tv, &a.now);
339 /* --- Grind through the pre hooks --- */
342 sel_hook *h = s->hooks;
347 hh->before(s, &a, hh->p);
351 /* --- Run the @select@ call --- */
353 if ((err = select(a.maxfd,
354 &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC],
361 gettimeofday(&a.now, 0);
363 /* --- Run through the hooks again --- */
366 sel_hook *h = s->hooks;
371 hh->after(s, &a, hh->p);
375 /* --- Run through the timers --- */
379 for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = tt) {
381 t->next = t->prev = t;
382 t->func(&a.now, t->p);
386 t->prev = (sel_timer *)&s->timers;
389 /* --- And finally run through the files --- *
391 * Do reads first. It's quite possible that a read might prompt a write,
392 * but the other way around is less likely. Fortunately, the modes are
393 * in the right order for this.
399 for (i = 0; i < SEL_MODES; i++) {
401 for (f = s->files[i]; f; f = ff) {
403 if (FD_ISSET(f->fd, a.fd + i))
404 f->func(f->fd, i, f->p);
413 /*----- That's all, folks -------------------------------------------------*/