3 * I/O multiplexing support
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include <sys/types.h>
41 /*----- Data structures ---------------------------------------------------*/
43 typedef struct sel_pendfile {
44 struct sel_pendfile *next;
48 typedef struct sel_pendtimer {
49 struct sel_pendtimer *next;
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @sel_init@ --- *
57 * Arguments: @sel_state *s@ = pointer to a state block to initialize
61 * Use: Initializes a select state block.
64 void sel_init(sel_state *s)
68 for (i = 0; i < SEL_MODES; i++) {
77 /* --- @sel_initfile@ --- *
79 * Arguments: @sel_state *s@ = select state to attach to
80 * @sel_file *f@ = pointer to a file block to initialize
81 * @int fd@ = the file descriptor to listen to
82 * @unsigned mode@ = what to listen for
83 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
84 * @void *p@ = argument to pass to handler
88 * Use: Initializes a file block ready for use. The file block
89 * isn't added to the list of things to do until a call to
93 void sel_initfile(sel_state *s, sel_file *f,
94 int fd, unsigned mode,
95 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
106 /* --- @sel_addfile@ --- *
108 * Arguments: @sel_file *f@ = pointer to a file block
112 * Use: Adds a file block into the list of things to listen to.
115 void sel_addfile(sel_file *f)
117 sel_file **ff = &f->s->files[f->mode];
119 /* --- This little dance looks like line-noise, but it does the job --- */
121 while (*ff && (*ff)->fd > f->fd)
126 (*ff)->prev = &f->next;
128 FD_SET(f->fd, f->s->fd + f->mode);
131 /* --- @sel_force@ --- *
133 * Arguments: @sel_file *f@ = pointer to file selector
137 * Use: Forces a file selector to be considered ready. This is only
138 * useful during a call to @sel_select@. Of particular use is
139 * forcing a write selector when there's something interesting
143 void sel_force(sel_file *f)
146 FD_SET(f->fd, &f->s->args->fd[f->mode]);
149 /* --- @sel_rmfile@ --- *
151 * Arguments: @sel_file *f@ = pointer to a file block
155 * Use: Removes a file block from the list of things to listen to.
158 void sel_rmfile(sel_file *f)
162 f->next->prev = f->prev;
163 FD_CLR(f->fd, f->s->fd + f->mode);
170 /* --- @sel_addtimer@ --- *
172 * Arguments: @sel_state *s@ = pointer to a state block
173 * @sel_timer *t@ = pointer to a timer block
174 * @struct timeval *tv@ = pointer to time to activate
175 * @void (*func)(struct timeval *tv, void *p)@ = handler
176 * @void *p@ = argument for handler function
180 * Use: Registers and sets up a timer.
183 void sel_addtimer(sel_state *s, sel_timer *t,
185 void (*func)(struct timeval */*tv*/, void */*p*/),
188 sel_timer **tt = &s->timers;
189 { sel_timer *q; for (q = s->timers; q; q = q->next) assert(q != t); }
191 /* --- Set up the timer block --- */
198 /* --- More line noise --- */
200 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
205 (*tt)->prev = &t->next;
209 /* --- @sel_rmtimer@ --- *
211 * Arguments: @sel_timer *t@ = pointer to timer block
215 * Use: Removes a timer from the list of timers.
218 void sel_rmtimer(sel_timer *t)
226 t->next->prev = t->prev;
230 /* --- @sel_addhook@ --- *
232 * Arguments: @sel_state *s@ = pointer to state block
233 * @sel_hook *h@ = pointer to hook block
234 * @sel_hookfn before, after@ = hook functions
235 * @void *p@ = pointer argument to pass to hook functions
239 * Use: Registers hook functions to be called on each select call.
242 void sel_addhook(sel_state *s, sel_hook *h,
243 sel_hookfn before, sel_hookfn after,
252 s->hooks->prev = &h->next;
256 /* --- @sel_rmhook@ --- *
258 * Arguments: @sel_hook *h@ = pointer to hook block
262 * Use: Removes hook functions.
265 void sel_rmhook(sel_hook *h)
268 h->next->prev = h->prev;
272 /* --- @sel_fdmerge@ --- *
274 * Arguments: @fd_set *dest@ = destination FD set
275 * @fd_set *fd@ = pointer to set to merge
276 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
278 * Returns: Actual highest numbered descriptor.
280 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
284 int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd)
288 for (i = 0; i < maxfd; i++) {
289 if (FD_ISSET(i, fd)) {
298 /* --- @sel_select@ --- *
300 * Arguments: @sel_state *s@ = pointer to state block
302 * Returns: Zero if all OK, -1 on error.
304 * Use: Does a @select@ call (or equivalent @poll@).
307 int sel_select(sel_state *s)
312 /* --- Initialize the argument block --- */
317 for (i = 0; i < SEL_MODES; i++) {
318 if (s->files[i] && s->files[i]->fd >= a.maxfd)
319 a.maxfd = s->files[i]->fd + 1;
323 memcpy(a.fd, s->fd, sizeof(a.fd));
324 if (s->timers || s->hooks)
325 gettimeofday(&a.now, 0);
329 if (TV_CMP(&s->timers->tv, >, &a.now))
330 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 --- */
378 ptimer *pthead, *pt, **ptt = &pthead;
381 for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = t->next) {
391 t->prev = &s->timers;
402 t->func(&a.now, t->p);
408 /* --- And finally run through the files --- *
410 * Do reads first. It's quite possible that a read might prompt a write,
411 * but the other way around is less likely. Fortunately, the modes are
412 * in the right order for this.
418 for (i = 0; i < SEL_MODES; i++) {
419 pfile *pfhead, *pf, **pff = &pfhead;
422 for (f = s->files[i]; f; f = f->next) {
423 if (!FD_ISSET(f->fd, &a.fd[i]))
438 f->func(f->fd, i, f->p);
449 /*----- That's all, folks -------------------------------------------------*/