chiark / gitweb /
Fix a large number of bugs.
authormdw <mdw>
Fri, 22 Jun 2001 19:35:58 +0000 (19:35 +0000)
committermdw <mdw>
Fri, 22 Jun 2001 19:35:58 +0000 (19:35 +0000)
sel.c
sel.h

diff --git a/sel.c b/sel.c
index 62a61cfc9f5eafdb8858c88ff7eab50c6690f0ad..1e3cdaca40921c42e0663a634ad46c5f85d7a666 100644 (file)
--- a/sel.c
+++ b/sel.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sel.c,v 1.9 2001/02/03 19:07:08 mdw Exp $
+ * $Id: sel.c,v 1.10 2001/06/22 19:35:58 mdw Exp $
  *
  * I/O multiplexing support
  *
@@ -30,6 +30,9 @@
 /*----- 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.
  *
@@ -62,6 +65,7 @@
 
 /*----- 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@ --- *
@@ -123,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@ --- *
@@ -183,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@ --- *
@@ -204,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 --- */
   
@@ -236,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@ --- *
@@ -385,19 +413,32 @@ int sel_select(sel_state *s)
 
   /* --- Run through the timers --- */
 
-  if (s->timers && TV_CMP(&s->timers->tv, <=, &a.now)) {
-    sel_timer *t, *tt;
-    tt = s->timers;
-    for (t = tt; t && TV_CMP(&t->tv, <=, &a.now); t = t->next)
-      ;
+  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->next = 0;
       t->prev = (sel_timer *)&s->timers;
     }
     s->timers = t;
-    for (t = tt; t; t = tt) {
-      tt = t->next;
-      t->func(&a.now, t->p);
+    while (pthead) {
+      pt = pthead;
+      pthead = pt->next;
+      t = pt->t;
+      if (t) {
+       t->func(&a.now, t->p);
+       t->pend = 0;
+      }
+      DESTROY(pt);
     }
   }
 
@@ -412,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;
+       }
       }
     }
   }
diff --git a/sel.h b/sel.h
index 9749a7a90b8ad6d664e09c32c359cdf90ce681d2..338ea20255f44688a4f5d2a2715a169c1f1f09c0 100644 (file)
--- a/sel.h
+++ b/sel.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sel.h,v 1.7 1999/12/10 23:42:04 mdw Exp $
+ * $Id: sel.h,v 1.8 2001/06/22 19:35:58 mdw Exp $
  *
  * I/O multiplexing support
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: sel.h,v $
+ * Revision 1.8  2001/06/22 19:35:58  mdw
+ * Fix a large number of bugs.
+ *
  * Revision 1.7  1999/12/10 23:42:04  mdw
  * Change header file guard names.
  *
@@ -137,6 +140,7 @@ typedef struct sel_file {
   unsigned mode;                       /* Interesting event for file */
   void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/); /* Handler */
   void *p;                             /* Argument for the handler */
+  struct sel_pendfile *pend;           /* Pending file information */
 } sel_file;
 
 /* --- Waiting for a timeout --- */
@@ -147,6 +151,7 @@ typedef struct sel_timer {
   struct timeval tv;                   /* Real time when timer should go */
   void (*func)(struct timeval */*tv*/, void */*p*/); /* Handler function */
   void *p;                             /* Argument for the handler */
+  struct sel_pendtimer *pend;          /* Pending timer information */
 } sel_timer;
 
 /* --- A select argument block --- */