chiark / gitweb /
Build: Put build utilities in the config/ subdirectory.
[mLib] / sel.c
diff --git a/sel.c b/sel.c
index 1e3cdaca40921c42e0663a634ad46c5f85d7a666..c174b9a457dc14673bcab4d4da954bacc8f5b67c 100644 (file)
--- a/sel.c
+++ b/sel.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: sel.c,v 1.10 2001/06/22 19:35:58 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.
  *
  * 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.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.
- *
- * 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>
@@ -161,9 +125,9 @@ void sel_addfile(sel_file *f)
   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);
 }
@@ -197,7 +161,7 @@ void sel_force(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);
@@ -236,13 +200,13 @@ void sel_addtimer(sel_state *s, sel_timer *t,
   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;
 }
 
@@ -257,12 +221,13 @@ 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;
   }
 }
 
@@ -286,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;
 }
 
@@ -305,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
+ * 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.
+ * 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)
@@ -388,7 +353,7 @@ 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) {
@@ -426,8 +391,8 @@ int sel_select(sel_state *s)
     }
     *ptt = 0;
     if (t) {
-      t->prev->next = 0;
-      t->prev = (sel_timer *)&s->timers;
+      *t->prev = 0;
+      t->prev = &s->timers;
     }
     s->timers = t;
     while (pthead) {
@@ -435,8 +400,10 @@ int sel_select(sel_state *s)
       pthead = pt->next;
       t = pt->t;
       if (t) {
-       t->func(&a.now, t->p);
        t->pend = 0;
+       t->next = 0;
+       t->prev = &t->next;
+       t->func(&a.now, t->p);
       }
       DESTROY(pt);
     }
@@ -471,9 +438,10 @@ int sel_select(sel_state *s)
        pfhead = pf->next;
        f = pf->f;
        if (f) {
-         f->func(f->fd, i, f->p);
          f->pend = 0;
+         f->func(f->fd, i, f->p);
        }
+       DESTROY(pf);
       }
     }
   }