3 * $Id: sel.c,v 1.4 1999/08/19 18:30:26 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.4 1999/08/19 18:30:26 mdw
34 * Implement hooks for foreign select-using systems (currently not well
37 * Revision 1.3 1999/05/21 22:13:59 mdw
38 * Use new `tv' macros. Fix ordering bug for timeout selectors.
40 * Revision 1.2 1999/05/15 10:33:32 mdw
41 * Fix copyright notices.
43 * Revision 1.1 1999/05/14 21:01:14 mdw
44 * Integrated `select' handling bits from the background resolver project.
48 /*----- Header files ------------------------------------------------------*/
54 #include <sys/types.h>
61 /*----- Main code ---------------------------------------------------------*/
63 /* --- @sel_init@ --- *
65 * Arguments: @sel_state *s@ = pointer to a state block to initialize
69 * Use: Initializes a select state block.
72 void sel_init(sel_state *s)
77 FD_ZERO(&s->fd[SEL_READ]);
78 FD_ZERO(&s->fd[SEL_WRITE]);
79 FD_ZERO(&s->fd[SEL_EXC]);
82 /* --- @sel_initfile@ --- *
84 * Arguments: @sel_state *s@ = select state to attach to
85 * @sel_file *f@ = pointer to a file block to initialize
86 * @int fd@ = the file descriptor to listen to
87 * @unsigned mode@ = what to listen for
88 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
89 * @void *p@ = argument to pass to handler
93 * Use: Initializes a file block ready for use. The file block
94 * isn't added to the list of things to do until a call to
98 void sel_initfile(sel_state *s, sel_file *f,
99 int fd, unsigned mode,
100 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
110 /* --- @sel_addfile@ --- *
112 * Arguments: @sel_file *f@ = pointer to a file block
116 * Use: Adds a file block into the list of things to listen to.
119 void sel_addfile(sel_file *f)
121 sel_file **ff = &f->s->files;
123 /* --- This little dance looks like line-noise, but it does the job --- */
125 while (*ff && (*ff)->fd > f->fd)
128 f->prev = (sel_file *)ff;
132 FD_SET(f->fd, f->s->fd + f->mode);
135 /* --- @sel_rmfile@ --- *
137 * Arguments: @sel_file *f@ = pointer to a file block
141 * Use: Removes a file block from the list of things to listen to.
144 void sel_rmfile(sel_file *f)
146 f->prev->next = f->next;
148 f->next->prev = f->prev;
149 FD_CLR(f->fd, f->s->fd + f->mode);
152 /* --- @sel_addtimer@ --- *
154 * Arguments: @sel_state *s@ = pointer to a state block
155 * @sel_timer *t@ = pointer to a timer block
156 * @struct timeval *tv@ = pointer to time to activate
157 * @void (*func)(struct timeval *tv, void *p)@ = handler
158 * @void *p@ = argument for handler function
162 * Use: Registers and sets up a timer.
165 void sel_addtimer(sel_state *s, sel_timer *t,
167 void (*func)(struct timeval */*tv*/, void */*p*/),
170 sel_timer **tt = &s->timers;
172 /* --- Set up the timer block --- */
178 /* --- More line noise --- */
180 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
183 t->prev = (sel_timer *)tt;
189 /* --- @sel_rmtimer@ --- *
191 * Arguments: @sel_timer *t@ = pointer to timer block
195 * Use: Removes a timer from the list of timers.
198 void sel_rmtimer(sel_timer *t)
200 t->prev->next = t->next;
202 t->next->prev = t->prev;
205 /* --- @sel_addhook@ --- *
207 * Arguments: @sel_state *s@ = pointer to state block
208 * @sel_hook *h@ = pointer to hook block
209 * @sel_hookfn before, after@ = hook functions
210 * @void *p@ = pointer argument to pass to hook functions
214 * Use: Registers hook functions to be called on each select call.
217 void sel_addhook(sel_state *s, sel_hook *h,
218 sel_hookfn before, sel_hookfn after,
225 h->prev = (sel_hook *)&s->hooks;
231 /* --- @sel_rmhook@ --- *
233 * Arguments: @sel_hook *h@ = pointer to hook block
237 * Use: Removes hook functions.
240 void sel_rmhook(sel_hook *h)
243 h->next->prev = h->prev;
244 h->prev->next = h->next;
247 /* --- @sel_fdmerge@ --- *
249 * Arguments: @fd_set *dest@ = destination FD set
250 * @fd_set *fd@ = pointer to set to merge
251 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
253 * Returns: Actual highest numbered descriptor.
255 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
259 int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd)
263 for (i = 0; i < maxfd; i++) {
264 if (FD_ISSET(i, fd)) {
273 /* --- @sel_select@ --- *
275 * Arguments: @sel_state *s@ = pointer to state block
277 * Returns: Zero if all OK, -1 on error.
279 * Use: Does a @select@ call (or equivalent @poll@).
282 int sel_select(sel_state *s)
287 /* --- Initialize the argument block --- */
289 a.maxfd = s->files ? s->files->fd + 1 : 0;
290 memcpy(a.fd, s->fd, sizeof(a.fd));
291 if (s->timers || s->hooks)
292 gettimeofday(&a.now, 0);
296 TV_SUB(&a.tv, &s->timers->tv, &a.now);
300 /* --- Grind through the pre hooks --- */
303 sel_hook *h = s->hooks;
308 hh->before(s, &a, hh->p);
312 /* --- Run the @select@ call --- */
314 if ((err = select(a.maxfd,
315 &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC],
320 gettimeofday(&a.now, 0);
322 /* --- Run through the hooks again --- */
325 sel_hook *h = s->hooks;
330 hh->after(s, &a, hh->p);
334 /* --- Run through the timers --- */
338 for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = tt) {
340 t->next = t->prev = t;
341 t->func(&a.now, t->p);
345 t->prev = (sel_timer *)&s->timers;
348 /* --- And finally run through the files --- */
352 for (f = s->files; f; f = ff) {
354 if (FD_ISSET(f->fd, a.fd + f->mode))
355 f->func(f->fd, f->mode, f->p);
362 /*----- That's all, folks -------------------------------------------------*/