3 * $Id: sel.c,v 1.5 1999/08/31 17:42:22 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.5 1999/08/31 17:42:22 mdw
34 * New function `sel_force' to force a descriptor to be `selected'.
36 * Revision 1.4 1999/08/19 18:30:26 mdw
37 * Implement hooks for foreign select-using systems (currently not well
40 * Revision 1.3 1999/05/21 22:13:59 mdw
41 * Use new `tv' macros. Fix ordering bug for timeout selectors.
43 * Revision 1.2 1999/05/15 10:33:32 mdw
44 * Fix copyright notices.
46 * Revision 1.1 1999/05/14 21:01:14 mdw
47 * Integrated `select' handling bits from the background resolver project.
51 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @sel_init@ --- *
68 * Arguments: @sel_state *s@ = pointer to a state block to initialize
72 * Use: Initializes a select state block.
75 void sel_init(sel_state *s)
79 for (i = 0; i < SEL_MODES; i++) {
88 /* --- @sel_initfile@ --- *
90 * Arguments: @sel_state *s@ = select state to attach to
91 * @sel_file *f@ = pointer to a file block to initialize
92 * @int fd@ = the file descriptor to listen to
93 * @unsigned mode@ = what to listen for
94 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
95 * @void *p@ = argument to pass to handler
99 * Use: Initializes a file block ready for use. The file block
100 * isn't added to the list of things to do until a call to
104 void sel_initfile(sel_state *s, sel_file *f,
105 int fd, unsigned mode,
106 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
116 /* --- @sel_addfile@ --- *
118 * Arguments: @sel_file *f@ = pointer to a file block
122 * Use: Adds a file block into the list of things to listen to.
125 void sel_addfile(sel_file *f)
127 sel_file **ff = &f->s->files[f->mode];
129 /* --- This little dance looks like line-noise, but it does the job --- */
131 while (*ff && (*ff)->fd > f->fd)
134 f->prev = (sel_file *)ff;
138 FD_SET(f->fd, f->s->fd + f->mode);
141 /* --- @sel_force@ --- *
143 * Arguments: @sel_file *f@ = pointer to file selector
147 * Use: Forces a file selector to be considered ready. This is only
148 * useful during a call to @sel_select@. Of particular use is
149 * forcing a write selector when there's something interesting
153 void sel_force(sel_file *f)
156 FD_SET(f->fd, &f->s->args->fd[f->mode]);
159 /* --- @sel_rmfile@ --- *
161 * Arguments: @sel_file *f@ = pointer to a file block
165 * Use: Removes a file block from the list of things to listen to.
168 void sel_rmfile(sel_file *f)
170 f->prev->next = f->next;
172 f->next->prev = f->prev;
173 FD_CLR(f->fd, f->s->fd + f->mode);
176 /* --- @sel_addtimer@ --- *
178 * Arguments: @sel_state *s@ = pointer to a state block
179 * @sel_timer *t@ = pointer to a timer block
180 * @struct timeval *tv@ = pointer to time to activate
181 * @void (*func)(struct timeval *tv, void *p)@ = handler
182 * @void *p@ = argument for handler function
186 * Use: Registers and sets up a timer.
189 void sel_addtimer(sel_state *s, sel_timer *t,
191 void (*func)(struct timeval */*tv*/, void */*p*/),
194 sel_timer **tt = &s->timers;
196 /* --- Set up the timer block --- */
202 /* --- More line noise --- */
204 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
207 t->prev = (sel_timer *)tt;
213 /* --- @sel_rmtimer@ --- *
215 * Arguments: @sel_timer *t@ = pointer to timer block
219 * Use: Removes a timer from the list of timers.
222 void sel_rmtimer(sel_timer *t)
224 t->prev->next = t->next;
226 t->next->prev = t->prev;
229 /* --- @sel_addhook@ --- *
231 * Arguments: @sel_state *s@ = pointer to state block
232 * @sel_hook *h@ = pointer to hook block
233 * @sel_hookfn before, after@ = hook functions
234 * @void *p@ = pointer argument to pass to hook functions
238 * Use: Registers hook functions to be called on each select call.
241 void sel_addhook(sel_state *s, sel_hook *h,
242 sel_hookfn before, sel_hookfn after,
249 h->prev = (sel_hook *)&s->hooks;
255 /* --- @sel_rmhook@ --- *
257 * Arguments: @sel_hook *h@ = pointer to hook block
261 * Use: Removes hook functions.
264 void sel_rmhook(sel_hook *h)
267 h->next->prev = h->prev;
268 h->prev->next = h->next;
271 /* --- @sel_fdmerge@ --- *
273 * Arguments: @fd_set *dest@ = destination FD set
274 * @fd_set *fd@ = pointer to set to merge
275 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
277 * Returns: Actual highest numbered descriptor.
279 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
283 int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd)
287 for (i = 0; i < maxfd; i++) {
288 if (FD_ISSET(i, fd)) {
297 /* --- @sel_select@ --- *
299 * Arguments: @sel_state *s@ = pointer to state block
301 * Returns: Zero if all OK, -1 on error.
303 * Use: Does a @select@ call (or equivalent @poll@).
306 int sel_select(sel_state *s)
311 /* --- Initialize the argument block --- */
316 for (i = 0; i < SEL_MODES; i++) {
317 if (s->files[i] && s->files[i]->fd >= a.maxfd)
318 a.maxfd = s->files[i]->fd + 1;
322 memcpy(a.fd, s->fd, sizeof(a.fd));
323 if (s->timers || s->hooks)
324 gettimeofday(&a.now, 0);
328 TV_SUB(&a.tv, &s->timers->tv, &a.now);
333 /* --- Grind through the pre hooks --- */
336 sel_hook *h = s->hooks;
341 hh->before(s, &a, hh->p);
345 /* --- Run the @select@ call --- */
347 if ((err = select(a.maxfd,
348 &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC],
355 gettimeofday(&a.now, 0);
357 /* --- Run through the hooks again --- */
360 sel_hook *h = s->hooks;
365 hh->after(s, &a, hh->p);
369 /* --- Run through the timers --- */
373 for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = tt) {
375 t->next = t->prev = t;
376 t->func(&a.now, t->p);
380 t->prev = (sel_timer *)&s->timers;
383 /* --- And finally run through the files --- *
385 * Do reads first. It's quite possible that a read might prompt a write,
386 * but the other way around is less likely. Fortunately, the modes are
387 * in the right order for this.
393 for (i = 0; i < SEL_MODES; i++) {
395 for (f = s->files[i]; f; f = ff) {
397 if (FD_ISSET(f->fd, a.fd + f->mode))
398 f->func(f->fd, f->mode, f->p);
407 /*----- That's all, folks -------------------------------------------------*/