summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMartin Mares <mj@ucw.cz>1999-02-11 22:18:36 +0000
committerMartin Mares <mj@ucw.cz>1999-02-11 22:18:36 +0000
commit3b15402fd4055cb5bd66cd9ac1106ceecff9f760 (patch)
treee5b1d33f760cdf3f3a90452051abae34ac6c6701 /lib
parente8f73195fa68b027fdcb89f239107c2d4902bd9a (diff)
Grrr, forgot to commit the event routines themselves :|
Diffstat (limited to 'lib')
-rw-r--r--lib/event.c87
-rw-r--r--lib/event.h33
2 files changed, 120 insertions, 0 deletions
diff --git a/lib/event.c b/lib/event.c
new file mode 100644
index 00000000..7f5454f8
--- /dev/null
+++ b/lib/event.c
@@ -0,0 +1,87 @@
+/*
+ * BIRD Library -- Event Processing
+ *
+ * (c) 1999 Martin Mares <mj@ucw.cz>
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#include "nest/bird.h"
+#include "lib/event.h"
+
+event_list global_event_list;
+
+inline void
+ev_postpone(event *e)
+{
+ if (e->n.next)
+ {
+ rem_node(&e->n);
+ e->n.next = NULL;
+ }
+}
+
+static void
+ev_dump(resource *r)
+{
+ event *e = (event *) r;
+
+ debug("(code %p, data %p, %s)\n",
+ e->hook,
+ e->data,
+ e->n.next ? "scheduled" : "inactive");
+}
+
+static struct resclass ev_class = {
+ "Event",
+ 0,
+ (void (*)(resource *)) ev_postpone,
+ ev_dump
+};
+
+event *
+ev_new(pool *p)
+{
+ event *e = ralloc(p, &ev_class);
+
+ e->hook = NULL;
+ e->data = NULL;
+ e->n.next = NULL;
+ return e;
+}
+
+inline void
+ev_run(event *e)
+{
+ e->hook(e->data);
+ ev_postpone(e);
+}
+
+inline void
+ev_enqueue(event_list *l, event *e)
+{
+ if (e->n.next)
+ rem_node(&e->n);
+ add_tail(l, &e->n);
+}
+
+void
+ev_schedule(event *e)
+{
+ ev_enqueue(&global_event_list, e);
+}
+
+void
+ev_run_list(event_list *l)
+{
+ for(;;)
+ {
+ node *n = HEAD(*l);
+ event *e;
+ if (!n->next)
+ break;
+ e = SKIP_BACK(event, n, n);
+ ev_run(e);
+ }
+}
+
diff --git a/lib/event.h b/lib/event.h
new file mode 100644
index 00000000..0b1ea842
--- /dev/null
+++ b/lib/event.h
@@ -0,0 +1,33 @@
+/*
+ * BIRD Library -- Event Processing
+ *
+ * (c) 1999 Martin Mares <mj@ucw.cz>
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#ifndef _BIRD_EVENT_H_
+#define _BIRD_EVENT_H_
+
+#include "lib/resource.h"
+
+typedef struct event {
+ resource r;
+ void (*hook)(void *);
+ void *data;
+ node n; /* Internal link */
+} event;
+
+typedef list event_list;
+
+extern event_list global_event_list;
+
+event *ev_new(pool *);
+void ev_run(event *);
+#define ev_init_list(el) init_list(el)
+void ev_enqueue(event_list *, event *);
+void ev_schedule(event *);
+void ev_postpone(event *);
+void ev_run_list(event_list *);
+
+#endif