X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/1226c514d496f4552b43682c8ca3dde7d87f952a..f23d010aa281711352a1e8ea2b8d631742a41b82:/sel.c diff --git a/sel.c b/sel.c index de8cdef..c174b9a 100644 --- a/sel.c +++ b/sel.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: sel.c,v 1.4 1999/08/19 18:30:26 mdw Exp $ + * $Id: sel.c,v 1.13 2004/04/08 01:36:13 mdw Exp $ * * I/O multiplexing support * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,38 +15,21 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with mLib; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: sel.c,v $ - * Revision 1.4 1999/08/19 18:30:26 mdw - * Implement hooks for foreign select-using systems (currently not well - * tested). - * - * Revision 1.3 1999/05/21 22:13:59 mdw - * Use new `tv' macros. Fix ordering bug for timeout selectors. - * - * Revision 1.2 1999/05/15 10:33:32 mdw - * Fix copyright notices. - * - * Revision 1.1 1999/05/14 21:01:14 mdw - * Integrated `select' handling bits from the background resolver project. - * - */ - /*----- Header files ------------------------------------------------------*/ +#include #include #include #include @@ -56,8 +39,21 @@ #include #include "sel.h" +#include "sub.h" #include "tv.h" +/*----- Data structures ---------------------------------------------------*/ + +typedef struct sel_pendfile { + struct sel_pendfile *next; + sel_file *f; +} pfile; + +typedef struct sel_pendtimer { + struct sel_pendtimer *next; + sel_timer *t; +} ptimer; + /*----- Main code ---------------------------------------------------------*/ /* --- @sel_init@ --- * @@ -71,12 +67,15 @@ void sel_init(sel_state *s) { - s->files = 0; + int i; + + for (i = 0; i < SEL_MODES; i++) { + s->files[i] = 0; + FD_ZERO(&s->fd[i]); + } s->timers = 0; s->hooks = 0; - FD_ZERO(&s->fd[SEL_READ]); - FD_ZERO(&s->fd[SEL_WRITE]); - FD_ZERO(&s->fd[SEL_EXC]); + s->args = 0; } /* --- @sel_initfile@ --- * @@ -105,6 +104,7 @@ void sel_initfile(sel_state *s, sel_file *f, f->mode = mode; f->func = func; f->p = p; + f->pend = 0; } /* --- @sel_addfile@ --- * @@ -118,20 +118,38 @@ void sel_initfile(sel_state *s, sel_file *f, void sel_addfile(sel_file *f) { - sel_file **ff = &f->s->files; + sel_file **ff = &f->s->files[f->mode]; /* --- This little dance looks like line-noise, but it does the job --- */ while (*ff && (*ff)->fd > f->fd) ff = &(*ff)->next; f->next = *ff; - f->prev = (sel_file *)ff; + f->prev = ff; if (*ff) - (*ff)->prev = f; + (*ff)->prev = &f->next; *ff = f; FD_SET(f->fd, f->s->fd + f->mode); } +/* --- @sel_force@ --- * + * + * Arguments: @sel_file *f@ = pointer to file selector + * + * Returns: --- + * + * Use: Forces a file selector to be considered ready. This is only + * useful during a call to @sel_select@. Of particular use is + * forcing a write selector when there's something interesting + * ready for it. + */ + +void sel_force(sel_file *f) +{ + if (f->s->args) + FD_SET(f->fd, &f->s->args->fd[f->mode]); +} + /* --- @sel_rmfile@ --- * * * Arguments: @sel_file *f@ = pointer to a file block @@ -143,10 +161,14 @@ void sel_addfile(sel_file *f) void sel_rmfile(sel_file *f) { - f->prev->next = f->next; + *f->prev = f->next; if (f->next) f->next->prev = f->prev; FD_CLR(f->fd, f->s->fd + f->mode); + if (f->pend) { + f->pend->f = 0; + f->pend = 0; + } } /* --- @sel_addtimer@ --- * @@ -168,21 +190,23 @@ void sel_addtimer(sel_state *s, sel_timer *t, void *p) { sel_timer **tt = &s->timers; + { sel_timer *q; for (q = s->timers; q; q = q->next) assert(q != t); } /* --- Set up the timer block --- */ t->tv = *tv; t->func = func; t->p = p; + t->pend = 0; /* --- More line noise --- */ - + while (*tt && TV_CMP(&(*tt)->tv, <, tv)) tt = &(*tt)->next; t->next = *tt; - t->prev = (sel_timer *)tt; + t->prev = tt; if (*tt) - (*tt)->prev = t; + (*tt)->prev = &t->next; *tt = t; } @@ -197,9 +221,14 @@ void sel_addtimer(sel_state *s, sel_timer *t, void sel_rmtimer(sel_timer *t) { - t->prev->next = t->next; - if (t->next) - t->next->prev = t->prev; + if (t->pend) { + t->pend->t = 0; + t->pend = 0; + } else { + *t->prev = t->next; + if (t->next) + t->next->prev = t->prev; + } } /* --- @sel_addhook@ --- * @@ -222,9 +251,9 @@ void sel_addhook(sel_state *s, sel_hook *h, h->after = after; h->p = p; h->next = s->hooks; - h->prev = (sel_hook *)&s->hooks; + h->prev = &s->hooks; if (s->hooks) - s->hooks->prev = h; + s->hooks->prev = &h->next; s->hooks = h; } @@ -241,19 +270,19 @@ void sel_rmhook(sel_hook *h) { if (h->next) h->next->prev = h->prev; - h->prev->next = h->next; + *h->prev = h->next; } /* --- @sel_fdmerge@ --- * - * - * Arguments: @fd_set *dest@ = destination FD set - * @fd_set *fd@ = pointer to set to merge - * @int maxfd@ = highest numbered descriptor in @fd@ + 1 * - * Returns: Actual highest numbered descriptor. + * Arguments: @fd_set *dest@ = destination FD set + * @fd_set *fd@ = pointer to set to merge + * @int maxfd@ = highest numbered descriptor in @fd@ + 1 + * + * Returns: Actual highest numbered descriptor. * - * Use: Merges file descriptor sets, and returns an accurate @maxfd@ - * value. + * Use: Merges file descriptor sets, and returns an accurate @maxfd@ + * value. */ int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd) @@ -286,16 +315,30 @@ int sel_select(sel_state *s) /* --- Initialize the argument block --- */ - a.maxfd = s->files ? s->files->fd + 1 : 0; + { + int i; + a.maxfd = 0; + for (i = 0; i < SEL_MODES; i++) { + if (s->files[i] && s->files[i]->fd >= a.maxfd) + a.maxfd = s->files[i]->fd + 1; + } + } + memcpy(a.fd, s->fd, sizeof(a.fd)); if (s->timers || s->hooks) gettimeofday(&a.now, 0); if (!s->timers) a.tvp = 0; else { - TV_SUB(&a.tv, &s->timers->tv, &a.now); + if (TV_CMP(&s->timers->tv, >, &a.now)) + TV_SUB(&a.tv, &s->timers->tv, &a.now); + else { + a.tv.tv_sec = 0; + a.tv.tv_usec = 0; + } a.tvp = &a.tv; } + s->args = &a; /* --- Grind through the pre hooks --- */ @@ -310,11 +353,13 @@ int sel_select(sel_state *s) } /* --- Run the @select@ call --- */ - + if ((err = select(a.maxfd, &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC], - a.tvp)) < 0) + a.tvp)) < 0) { + s->args = 0; return (err); + } if (a.tvp) gettimeofday(&a.now, 0); @@ -333,29 +378,75 @@ int sel_select(sel_state *s) /* --- Run through the timers --- */ - { - sel_timer *t, *tt; - for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = tt) { - tt = t->next; - t->next = t->prev = t; - t->func(&a.now, t->p); + if (s->timers) { + ptimer *pthead, *pt, **ptt = &pthead; + sel_timer *t; + + for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = t->next) { + pt = CREATE(ptimer); + pt->t = t; + t->pend = pt; + *ptt = pt; + ptt = &pt->next; + } + *ptt = 0; + if (t) { + *t->prev = 0; + t->prev = &s->timers; } s->timers = t; - if (t) - t->prev = (sel_timer *)&s->timers; + while (pthead) { + pt = pthead; + pthead = pt->next; + t = pt->t; + if (t) { + t->pend = 0; + t->next = 0; + t->prev = &t->next; + t->func(&a.now, t->p); + } + DESTROY(pt); + } } - /* --- And finally run through the files --- */ + /* --- And finally run through the files --- * + * + * Do reads first. It's quite possible that a read might prompt a write, + * but the other way around is less likely. Fortunately, the modes are + * in the right order for this. + */ { - sel_file *f, *ff; - for (f = s->files; f; f = ff) { - ff = f->next; - if (FD_ISSET(f->fd, a.fd + f->mode)) - f->func(f->fd, f->mode, f->p); + int i; + + for (i = 0; i < SEL_MODES; i++) { + pfile *pfhead, *pf, **pff = &pfhead; + sel_file *f; + + for (f = s->files[i]; f; f = f->next) { + if (!FD_ISSET(f->fd, &a.fd[i])) + continue; + pf = CREATE(pfile); + pf->f = f; + f->pend = pf; + *pff = pf; + pff = &pf->next; + } + *pff = 0; + while (pfhead) { + pf = pfhead; + pfhead = pf->next; + f = pf->f; + if (f) { + f->pend = 0; + f->func(f->fd, i, f->p); + } + DESTROY(pf); + } } } + s->args = 0; return (0); }