chiark / gitweb /
Integrated `select' handling bits from the background resolver project.
[mLib] / sel.c
diff --git a/sel.c b/sel.c
new file mode 100644 (file)
index 0000000..3f5493b
--- /dev/null
+++ b/sel.c
@@ -0,0 +1,250 @@
+/* -*-c-*-
+ *
+ * $Id: sel.c,v 1.1 1999/05/14 21:01:14 mdw Exp $
+ *
+ * I/O multiplexing support
+ *
+ * (c) 1999 Mark Wooding
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * 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.1  1999/05/14 21:01:14  mdw
+ * Integrated `select' handling bits from the background resolver project.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "sel.h"
+#include "tv.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @sel_init@ --- *
+ *
+ * Arguments:  @sel_state *s@ = pointer to a state block to initialize
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a select state block.
+ */
+
+void sel_init(sel_state *s)
+{
+  s->files = 0;
+  s->timers = 0;
+  FD_ZERO(&s->fd[SEL_READ]);
+  FD_ZERO(&s->fd[SEL_WRITE]);
+  FD_ZERO(&s->fd[SEL_EXC]);
+}
+
+/* --- @sel_initfile@ --- *
+ *
+ * Arguments:  @sel_state *s@ = select state to attach to
+ *             @sel_file *f@ = pointer to a file block to initialize
+ *             @int fd@ = the file descriptor to listen to
+ *             @unsigned mode@ = what to listen for
+ *             @void (*func)(int fd, unsigned mode, void *p)@ = handler
+ *             @void *p@ = argument to pass to handler
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a file block ready for use.  The file block
+ *             isn't added to the list of things to do until a call to
+ *             @sel_addfile@.
+ */
+
+void sel_initfile(sel_state *s, sel_file *f,
+                 int fd, unsigned mode,
+                 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
+                 void *p)
+{
+  f->s = s;
+  f->fd = fd;
+  f->mode = mode;
+  f->func = func;
+  f->p = p;
+}
+
+/* --- @sel_addfile@ --- *
+ *
+ * Arguments:  @sel_file *f@ = pointer to a file block
+ *
+ * Returns:    ---
+ *
+ * Use:                Adds a file block into the list of things to listen to.
+ */
+
+void sel_addfile(sel_file *f)
+{
+  sel_file **ff = &f->s->files;
+
+  /* --- 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;
+  if (*ff)
+    (*ff)->prev = f;
+  *ff = f;
+  FD_SET(f->fd, f->s->fd + f->mode);
+}
+
+/* --- @sel_rmfile@ --- *
+ *
+ * Arguments:  @sel_file *f@ = pointer to a file block
+ *
+ * Returns:    ---
+ *
+ * Use:                Removes a file block from the list of things to listen to.
+ */
+
+void sel_rmfile(sel_file *f)
+{
+  f->prev->next = f->next;
+  if (f->next)
+    f->next->prev = f->prev;
+  FD_CLR(f->fd, f->s->fd + f->mode);
+}
+
+/* --- @sel_addtimer@ --- *
+ *
+ * Arguments:  @sel_state *s@ = pointer to a state block
+ *             @sel_timer *t@ = pointer to a timer block
+ *             @struct timeval *tv@ = pointer to time to activate
+ *             @void (*func)(struct timeval *tv, void *p)@ = handler
+ *             @void *p@ = argument for handler function
+ *
+ * Returns:    ---
+ *
+ * Use:                Registers and sets up a timer.
+ */
+
+void sel_addtimer(sel_state *s, sel_timer *t,
+                 struct timeval *tv,
+                 void (*func)(struct timeval */*tv*/, void */*p*/),
+                 void *p)
+{
+  sel_timer **tt = &s->timers;
+
+  /* --- Set up the timer block --- */
+
+  t->tv = *tv;
+  t->func = func;
+  t->p = p;
+
+  /* --- More line noise --- */
+  
+  while (*tt && tv_cmp(&(*tt)->tv, tv) > 0)
+    tt = &(*tt)->next;
+  t->next = *tt;
+  t->prev = (sel_timer *)tt;
+  if (*tt)
+    (*tt)->prev = t;
+  *tt = t;
+}
+
+/* --- @sel_rmtimer@ --- *
+ *
+ * Arguments:  @sel_timer *t@ = pointer to timer block
+ *
+ * Returns:    ---
+ *
+ * Use:                Removes a timer from the list of timers.
+ */
+
+void sel_rmtimer(sel_timer *t)
+{
+  t->prev->next = t->next;
+  if (t->next)
+    t->next->prev = t->prev;
+}
+
+/* --- @sel_select@ --- *
+ *
+ * Arguments:  @sel_state *s@ = pointer to state block
+ *
+ * Returns:    Zero if all OK, -1 on error.
+ *
+ * Use:                Does a @select@ call (or equivalent @poll@).
+ */
+
+int sel_select(sel_state *s)
+{
+  fd_set fd[SEL_MODES];
+  struct timeval tv;
+  int err;
+
+  memcpy(fd, s->fd, sizeof(s->fd));
+  if (s->timers) {
+    struct timeval now;
+    gettimeofday(&now, 0);
+    tv_sub(&tv, &now, &s->timers->tv);
+    err = select(s->files ? s->files->fd + 1 : 0,
+                fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
+                &tv);
+    gettimeofday(&tv, 0);
+  } else
+    err = select(s->files ? s->files->fd + 1 : 0,
+                fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
+                0);
+
+  if (err < 0)
+    return (err);
+
+  {
+    sel_timer *t, *tt;
+    for (t = s->timers; t && tv_cmp(&t->tv, &tv) <= 0; t = tt) {
+      tt = t->next;
+      t->next = t->prev = t;
+      t->func(&tv, t->p);
+    }
+    s->timers = t;
+    if (t)
+      t->prev = (sel_timer *)&s->timers;
+  }
+
+  {
+    sel_file *f, *ff;
+    for (f = s->files; f; f = ff) {
+      ff = f->next;
+      if (FD_ISSET(f->fd, fd + f->mode))
+       f->func(f->fd, f->mode, f->p);
+    }
+  }
+
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/