X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/932341d9970e779c2116e6c2b0a1087943712a96..0a2a19b4356ad08c670a00fee4ee72b08cfe328c:/sel.c diff --git a/sel.c b/sel.c index 55b5990..1e3cdac 100644 --- a/sel.c +++ b/sel.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sel.c,v 1.7 1999/12/11 11:12:17 mdw Exp $ + * $Id: sel.c,v 1.10 2001/06/22 19:35:58 mdw Exp $ * * I/O multiplexing support * @@ -30,6 +30,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: sel.c,v $ + * Revision 1.10 2001/06/22 19:35:58 mdw + * Fix a large number of bugs. + * + * Revision 1.9 2001/02/03 19:07:08 mdw + * Ensure that timers set to go off in the past don't case a problem. + * + * Revision 1.8 2000/03/23 20:42:08 mdw + * Rearrange timeout handling to avoid list corruptions. + * * Revision 1.7 1999/12/11 11:12:17 mdw * Fix comment formatting error. * @@ -56,6 +65,7 @@ /*----- Header files ------------------------------------------------------*/ +#include #include #include #include @@ -65,8 +75,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@ --- * @@ -117,6 +140,7 @@ void sel_initfile(sel_state *s, sel_file *f, f->mode = mode; f->func = func; f->p = p; + f->pend = 0; } /* --- @sel_addfile@ --- * @@ -177,6 +201,10 @@ void sel_rmfile(sel_file *f) 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@ --- * @@ -198,12 +226,14 @@ 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 --- */ @@ -230,6 +260,10 @@ 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; + } } /* --- @sel_addhook@ --- * @@ -331,7 +365,12 @@ int sel_select(sel_state *s) 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; @@ -374,16 +413,33 @@ 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; } - s->timers = t; - if (t) + *ptt = 0; + if (t) { + t->prev->next = 0; t->prev = (sel_timer *)&s->timers; + } + s->timers = t; + while (pthead) { + pt = pthead; + pthead = pt->next; + t = pt->t; + if (t) { + t->func(&a.now, t->p); + t->pend = 0; + } + DESTROY(pt); + } } /* --- And finally run through the files --- * @@ -397,11 +453,27 @@ int sel_select(sel_state *s) int i; for (i = 0; i < SEL_MODES; i++) { - sel_file *f, *ff; - for (f = s->files[i]; f; f = ff) { - ff = f->next; - if (FD_ISSET(f->fd, a.fd + 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->func(f->fd, i, f->p); + f->pend = 0; + } } } }