Statistics
| Branch: | Revision:

iof-bird-daemon / lib / event.c @ 67bd949a

History | View | Annotate | Download (1.17 KB)

1
/*
2
 *        BIRD Library -- Event Processing
3
 *
4
 *        (c) 1999 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include "nest/bird.h"
10
#include "lib/event.h"
11

    
12
event_list global_event_list;
13

    
14
inline void
15
ev_postpone(event *e)
16
{
17
  if (e->n.next)
18
    {
19
      rem_node(&e->n);
20
      e->n.next = NULL;
21
    }
22
}
23

    
24
static void
25
ev_dump(resource *r)
26
{
27
  event *e = (event *) r;
28

    
29
  debug("(code %p, data %p, %s)\n",
30
        e->hook,
31
        e->data,
32
        e->n.next ? "scheduled" : "inactive");
33
}
34

    
35
static struct resclass ev_class = {
36
  "Event",
37
  sizeof(event),
38
  (void (*)(resource *)) ev_postpone,
39
  ev_dump
40
};
41

    
42
event *
43
ev_new(pool *p)
44
{
45
  event *e = ralloc(p, &ev_class);
46

    
47
  e->hook = NULL;
48
  e->data = NULL;
49
  e->n.next = NULL;
50
  return e;
51
}
52

    
53
inline void
54
ev_run(event *e)
55
{
56
  e->hook(e->data);
57
  ev_postpone(e);
58
}
59

    
60
inline void
61
ev_enqueue(event_list *l, event *e)
62
{
63
  if (e->n.next)
64
    rem_node(&e->n);
65
  add_tail(l, &e->n);
66
}
67

    
68
void
69
ev_schedule(event *e)
70
{
71
  ev_enqueue(&global_event_list, e);
72
}
73

    
74
void
75
ev_run_list(event_list *l)
76
{
77
  for(;;)
78
    {
79
      node *n = HEAD(*l);
80
      event *e;
81
      if (!n->next)
82
        break;
83
      e = SKIP_BACK(event, n, n);
84
      ev_run(e);
85
    }
86
}
87