/* -*-c-*-
*
- * $Id: sel.c,v 1.7 1999/12/11 11:12:17 mdw Exp $
+ * $Id: sel.c,v 1.13 2004/04/08 01:36:13 mdw Exp $
*
* I/O multiplexing support
*
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: sel.c,v $
- * Revision 1.7 1999/12/11 11:12:17 mdw
- * Fix comment formatting error.
- *
- * Revision 1.6 1999/09/26 14:28:11 mdw
- * (sel_select): Almost pointless efficiency tweak.
- *
- * Revision 1.5 1999/08/31 17:42:22 mdw
- * New function `sel_force' to force a descriptor to be `selected'.
- *
- * 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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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@ --- *
f->mode = mode;
f->func = func;
f->p = p;
+ f->pend = 0;
}
/* --- @sel_addfile@ --- *
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);
}
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@ --- *
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;
}
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@ --- *
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;
}
{
if (h->next)
h->next->prev = h->prev;
- h->prev->next = h->next;
+ *h->prev = h->next;
}
/* --- @sel_fdmerge@ --- *
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;
/* --- 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 --- *
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->pend = 0;
f->func(f->fd, i, f->p);
+ }
+ DESTROY(pf);
}
}
}