event.c 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 *	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",
37
  sizeof(event),
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  (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;
}

53
inline int
54 55
ev_run(event *e)
{
56 57
  int keep = e->hook(e->data);
  if (!keep)
58
    ev_postpone(e);
59
  return keep;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

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);
}

76
int
77 78
ev_run_list(event_list *l)
{
79 80 81
  node *n, *p;

  WALK_LIST_DELSAFE(n, p, *l)
82
    {
83
      event *e = SKIP_BACK(event, n, n);
84
      ev_run(e);
85
    }
86
  return !EMPTY_LIST(*l);
87
}