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 ------------------------------------------------------*/
35 #include <sys/types.h>
43 /*----- Data structures ---------------------------------------------------*/
45 typedef struct sel_pendfile {
46 struct sel_pendfile *next;
50 typedef struct sel_pendtimer {
51 struct sel_pendtimer *next;
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @sel_init@ --- *
59 * Arguments: @sel_state *s@ = pointer to a state block to initialize
63 * Use: Initializes a select state block.
66 void sel_init(sel_state *s)
70 for (i = 0; i < SEL_MODES; i++) {
79 /* --- @sel_initfile@ --- *
81 * Arguments: @sel_state *s@ = select state to attach to
82 * @sel_file *f@ = pointer to a file block to initialize
83 * @int fd@ = the file descriptor to listen to
84 * @unsigned mode@ = what to listen for
85 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
86 * @void *p@ = argument to pass to handler
90 * Use: Initializes a file block ready for use. The file block
91 * isn't added to the list of things to do until a call to
95 void sel_initfile(sel_state *s, sel_file *f,
96 int fd, unsigned mode,
97 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
108 /* --- @sel_addfile@ --- *
110 * Arguments: @sel_file *f@ = pointer to a file block
114 * Use: Adds a file block into the list of things to listen to.
117 void sel_addfile(sel_file *f)
119 sel_file **ff = &f->s->files[f->mode];
121 /* --- This little dance looks like line-noise, but it does the job --- */
123 while (*ff && (*ff)->fd > f->fd)
128 (*ff)->prev = &f->next;
130 FD_SET(f->fd, f->s->fd + f->mode);
133 /* --- @sel_force@ --- *
135 * Arguments: @sel_file *f@ = pointer to file selector
139 * Use: Forces a file selector to be considered ready. This is only
140 * useful during a call to @sel_select@. Of particular use is
141 * forcing a write selector when there's something interesting
145 void sel_force(sel_file *f)
148 FD_SET(f->fd, &f->s->args->fd[f->mode]);
151 /* --- @sel_rmfile@ --- *
153 * Arguments: @sel_file *f@ = pointer to a file block
157 * Use: Removes a file block from the list of things to listen to.
160 void sel_rmfile(sel_file *f)
164 f->next->prev = f->prev;
165 FD_CLR(f->fd, f->s->fd + f->mode);
172 /* --- @sel_addtimer@ --- *
174 * Arguments: @sel_state *s@ = pointer to a state block
175 * @sel_timer *t@ = pointer to a timer block
176 * @struct timeval *tv@ = pointer to time to activate
177 * @void (*func)(struct timeval *tv, void *p)@ = handler
178 * @void *p@ = argument for handler function
182 * Use: Registers and sets up a timer.
185 void sel_addtimer(sel_state *s, sel_timer *t,
187 void (*func)(struct timeval */*tv*/, void */*p*/),
190 sel_timer **tt = &s->timers;
191 { sel_timer *q; for (q = s->timers; q; q = q->next) assert(q != t); }
193 /* --- Set up the timer block --- */
200 /* --- More line noise --- */
202 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
207 (*tt)->prev = &t->next;
211 /* --- @sel_rmtimer@ --- *
213 * Arguments: @sel_timer *t@ = pointer to timer block
217 * Use: Removes a timer from the list of timers.
220 void sel_rmtimer(sel_timer *t)
228 t->next->prev = t->prev;
232 /* --- @sel_addhook@ --- *
234 * Arguments: @sel_state *s@ = pointer to state block
235 * @sel_hook *h@ = pointer to hook block
236 * @sel_hookfn before, after@ = hook functions
237 * @void *p@ = pointer argument to pass to hook functions
241 * Use: Registers hook functions to be called on each select call.
244 void sel_addhook(sel_state *s, sel_hook *h,
245 sel_hookfn before, sel_hookfn after,
254 s->hooks->prev = &h->next;
258 /* --- @sel_rmhook@ --- *
260 * Arguments: @sel_hook *h@ = pointer to hook block
264 * Use: Removes hook functions.
267 void sel_rmhook(sel_hook *h)
270 h->next->prev = h->prev;
274 /* --- @sel_fdmerge@ --- *
276 * Arguments: @fd_set *dest@ = destination FD set
277 * @fd_set *fd@ = pointer to set to merge
278 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
280 * Returns: Actual highest numbered descriptor.
282 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
286 int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd)
290 for (i = 0; i < maxfd; i++) {
291 if (FD_ISSET(i, fd)) {
300 /* --- @sel_select@ --- *
302 * Arguments: @sel_state *s@ = pointer to state block
304 * Returns: Zero if all OK, -1 on error.
306 * Use: Does a @select@ call (or equivalent @poll@).
309 int sel_select(sel_state *s)
314 /* --- Initialize the argument block --- */
319 for (i = 0; i < SEL_MODES; i++) {
320 if (s->files[i] && s->files[i]->fd >= a.maxfd)
321 a.maxfd = s->files[i]->fd + 1;
325 memcpy(a.fd, s->fd, sizeof(a.fd));
326 if (s->timers || s->hooks)
327 gettimeofday(&a.now, 0);
331 if (TV_CMP(&s->timers->tv, >, &a.now))
332 TV_SUB(&a.tv, &s->timers->tv, &a.now);
341 /* --- Grind through the pre hooks --- */
344 sel_hook *h = s->hooks;
349 hh->before(s, &a, hh->p);
353 /* --- Run the @select@ call --- */
355 if ((err = select(a.maxfd,
356 &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC],
363 gettimeofday(&a.now, 0);
365 /* --- Run through the hooks again --- */
368 sel_hook *h = s->hooks;
373 hh->after(s, &a, hh->p);
377 /* --- Run through the timers --- */
380 ptimer *pthead, *pt, **ptt = &pthead;
383 for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = t->next) {
393 t->prev = &s->timers;
404 t->func(&a.now, t->p);
410 /* --- And finally run through the files --- *
412 * Do reads first. It's quite possible that a read might prompt a write,
413 * but the other way around is less likely. Fortunately, the modes are
414 * in the right order for this.
420 for (i = 0; i < SEL_MODES; i++) {
421 pfile *pfhead, *pf, **pff = &pfhead;
424 for (f = s->files[i]; f; f = f->next) {
425 if (!FD_ISSET(f->fd, &a.fd[i]))
440 f->func(f->fd, i, f->p);
451 /*----- That's all, folks -------------------------------------------------*/