Statistics
| Branch: | Revision:

iof-bird-daemon / lib / event.c @ e81b440f

History | View | Annotate | Download (2.73 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
/**
10
 * DOC: Events
11
 *
12
 * Events are there to keep track of deferred execution.
13
 * Since BIRD is single-threaded, it requires long lasting tasks to be split to smaller
14
 * parts, so that no module can monopolize the CPU. To split such a task, just create
15
 * an &event resource, point it to the function you want to have called and call ev_schedule()
16
 * to ask the core to run the event when nothing more important requires attention.
17
 *
18
 * You can also define your own event lists (the &event_list structure), enqueue your
19
 * events in them and explicitly ask to run them.
20
 */
21

    
22
#include "nest/bird.h"
23
#include "lib/event.h"
24

    
25
event_list global_event_list;
26

    
27
inline void
28
ev_postpone(event *e)
29
{
30
  if (e->n.next)
31
    {
32
      rem_node(&e->n);
33
      e->n.next = NULL;
34
    }
35
}
36

    
37
static void
38
ev_dump(resource *r)
39
{
40
  event *e = (event *) r;
41

    
42
  debug("(code %p, data %p, %s)\n",
43
        e->hook,
44
        e->data,
45
        e->n.next ? "scheduled" : "inactive");
46
}
47

    
48
static struct resclass ev_class = {
49
  "Event",
50
  sizeof(event),
51
  (void (*)(resource *)) ev_postpone,
52
  ev_dump,
53
  NULL
54
};
55

    
56
/**
57
 * ev_new - create a new event
58
 * @p: resource pool
59
 *
60
 * This function creates a new event resource. To use it,
61
 * you need to fill the structure fields and call ev_schedule().
62
 */
63
event *
64
ev_new(pool *p)
65
{
66
  event *e = ralloc(p, &ev_class);
67
  return e;
68
}
69

    
70
/**
71
 * ev_run - run an event
72
 * @e: an event
73
 *
74
 * This function explicitly runs the event @e (calls its hook
75
 * function) and removes it from an event list if it's linked to any.
76
 *
77
 * From the hook function, you can call ev_enqueue() or ev_schedule()
78
 * to re-add the event.
79
 */
80
inline void
81
ev_run(event *e)
82
{
83
  ev_postpone(e);
84
  e->hook(e->data);
85
}
86

    
87
/**
88
 * ev_enqueue - enqueue an event
89
 * @l: an event list
90
 * @e: an event
91
 *
92
 * ev_enqueue() stores the event @e to the specified event
93
 * list @l which can be run by calling ev_run_list().
94
 */
95
inline void
96
ev_enqueue(event_list *l, event *e)
97
{
98
  ev_postpone(e);
99
  add_tail(l, &e->n);
100
}
101

    
102
/**
103
 * ev_schedule - schedule an event
104
 * @e: an event
105
 *
106
 * This function schedules an event by enqueueing it to a system-wide
107
 * event list which is run by the platform dependent code whenever
108
 * appropriate.
109
 */
110
void
111
ev_schedule(event *e)
112
{
113
  ev_enqueue(&global_event_list, e);
114
}
115

    
116
/**
117
 * ev_run_list - run an event list
118
 * @l: an event list
119
 *
120
 * This function calls ev_run() for all events enqueued in the list @l.
121
 */
122
int
123
ev_run_list(event_list *l)
124
{
125
  node *n;
126
  list tmp_list;
127

    
128
  init_list(&tmp_list);
129
  add_tail_list(&tmp_list, l);
130
  init_list(l);
131
  WALK_LIST_FIRST(n, tmp_list)
132
    {
133
      event *e = SKIP_BACK(event, n, n);
134
      ev_run(e);
135
    }
136
  return !EMPTY_LIST(*l);
137
}