1999-02-11 22:18:36 +00:00
|
|
|
/*
|
|
|
|
* BIRD Library -- Event Processing
|
|
|
|
*
|
|
|
|
* (c) 1999 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 12:19:12 +00:00
|
|
|
/**
|
|
|
|
* DOC: Events
|
|
|
|
*
|
|
|
|
* Events are there to keep track of deferred execution.
|
|
|
|
* Since BIRD is single-threaded, it requires long lasting tasks to be split to smaller
|
|
|
|
* parts, so that no module can monopolize the CPU. To split such a task, just create
|
|
|
|
* an &event resource, point it to the function you want to have called and call ev_schedule()
|
2000-06-07 13:25:53 +00:00
|
|
|
* to ask the core to run the event when nothing more important requires attention.
|
2000-06-05 12:19:12 +00:00
|
|
|
*
|
|
|
|
* You can also define your own event lists (the &event_list structure), enqueue your
|
|
|
|
* events in them and explicitly ask to run them.
|
|
|
|
*/
|
|
|
|
|
1999-02-11 22:18:36 +00:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/event.h"
|
|
|
|
|
|
|
|
event_list global_event_list;
|
2021-03-12 14:35:56 +00:00
|
|
|
event_list global_work_list;
|
1999-02-11 22:18:36 +00:00
|
|
|
|
|
|
|
inline void
|
|
|
|
ev_postpone(event *e)
|
|
|
|
{
|
2014-11-03 09:42:55 +00:00
|
|
|
if (ev_active(e))
|
1999-02-11 22:18:36 +00:00
|
|
|
{
|
|
|
|
rem_node(&e->n);
|
|
|
|
e->n.next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-11-14 19:43:35 +00:00
|
|
|
ev_dump(struct dump_request *dreq, resource *r)
|
1999-02-11 22:18:36 +00:00
|
|
|
{
|
|
|
|
event *e = (event *) r;
|
|
|
|
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("(code %p, data %p, %s)\n",
|
1999-02-11 22:18:36 +00:00
|
|
|
e->hook,
|
|
|
|
e->data,
|
|
|
|
e->n.next ? "scheduled" : "inactive");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct resclass ev_class = {
|
|
|
|
"Event",
|
1999-02-11 22:59:06 +00:00
|
|
|
sizeof(event),
|
1999-02-11 22:18:36 +00:00
|
|
|
(void (*)(resource *)) ev_postpone,
|
2010-02-21 13:34:53 +00:00
|
|
|
ev_dump,
|
2010-06-02 20:20:40 +00:00
|
|
|
NULL,
|
2010-02-21 13:34:53 +00:00
|
|
|
NULL
|
1999-02-11 22:18:36 +00:00
|
|
|
};
|
|
|
|
|
2000-06-05 12:19:12 +00:00
|
|
|
/**
|
|
|
|
* ev_new - create a new event
|
|
|
|
* @p: resource pool
|
|
|
|
*
|
|
|
|
* This function creates a new event resource. To use it,
|
|
|
|
* you need to fill the structure fields and call ev_schedule().
|
|
|
|
*/
|
1999-02-11 22:18:36 +00:00
|
|
|
event *
|
|
|
|
ev_new(pool *p)
|
|
|
|
{
|
|
|
|
event *e = ralloc(p, &ev_class);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2000-06-05 12:19:12 +00:00
|
|
|
/**
|
|
|
|
* ev_run - run an event
|
|
|
|
* @e: an event
|
|
|
|
*
|
|
|
|
* This function explicitly runs the event @e (calls its hook
|
|
|
|
* function) and removes it from an event list if it's linked to any.
|
|
|
|
*
|
|
|
|
* From the hook function, you can call ev_enqueue() or ev_schedule()
|
|
|
|
* to re-add the event.
|
|
|
|
*/
|
2000-04-27 22:28:49 +00:00
|
|
|
inline void
|
1999-02-11 22:18:36 +00:00
|
|
|
ev_run(event *e)
|
|
|
|
{
|
2000-04-27 22:28:49 +00:00
|
|
|
ev_postpone(e);
|
|
|
|
e->hook(e->data);
|
1999-02-11 22:18:36 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 12:19:12 +00:00
|
|
|
/**
|
|
|
|
* ev_enqueue - enqueue an event
|
|
|
|
* @l: an event list
|
|
|
|
* @e: an event
|
|
|
|
*
|
|
|
|
* ev_enqueue() stores the event @e to the specified event
|
|
|
|
* list @l which can be run by calling ev_run_list().
|
|
|
|
*/
|
1999-02-11 22:18:36 +00:00
|
|
|
inline void
|
|
|
|
ev_enqueue(event_list *l, event *e)
|
|
|
|
{
|
2000-04-27 22:28:49 +00:00
|
|
|
ev_postpone(e);
|
1999-02-11 22:18:36 +00:00
|
|
|
add_tail(l, &e->n);
|
|
|
|
}
|
|
|
|
|
2000-06-05 12:19:12 +00:00
|
|
|
/**
|
|
|
|
* ev_schedule - schedule an event
|
|
|
|
* @e: an event
|
|
|
|
*
|
|
|
|
* This function schedules an event by enqueueing it to a system-wide
|
|
|
|
* event list which is run by the platform dependent code whenever
|
|
|
|
* appropriate.
|
|
|
|
*/
|
1999-02-11 22:18:36 +00:00
|
|
|
void
|
|
|
|
ev_schedule(event *e)
|
|
|
|
{
|
|
|
|
ev_enqueue(&global_event_list, e);
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:35:56 +00:00
|
|
|
/**
|
|
|
|
* ev_schedule_work - schedule a work-event.
|
|
|
|
* @e: an event
|
|
|
|
*
|
|
|
|
* This function schedules an event by enqueueing it to a system-wide work-event
|
|
|
|
* list which is run by the platform dependent code whenever appropriate. This
|
|
|
|
* is designated for work-events instead of regular events. They are executed
|
|
|
|
* less often in order to not clog I/O loop.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ev_schedule_work(event *e)
|
|
|
|
{
|
|
|
|
if (!ev_active(e))
|
|
|
|
add_tail(&global_work_list, &e->n);
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:41:14 +00:00
|
|
|
void io_log_event(void *hook, void *data);
|
|
|
|
|
2000-06-05 12:19:12 +00:00
|
|
|
/**
|
|
|
|
* ev_run_list - run an event list
|
|
|
|
* @l: an event list
|
|
|
|
*
|
|
|
|
* This function calls ev_run() for all events enqueued in the list @l.
|
|
|
|
*/
|
1999-11-17 12:01:11 +00:00
|
|
|
int
|
1999-02-11 22:18:36 +00:00
|
|
|
ev_run_list(event_list *l)
|
|
|
|
{
|
2008-12-18 22:26:08 +00:00
|
|
|
node *n;
|
2000-04-27 22:28:49 +00:00
|
|
|
list tmp_list;
|
1999-10-29 12:08:49 +00:00
|
|
|
|
2000-04-27 22:28:49 +00:00
|
|
|
init_list(&tmp_list);
|
|
|
|
add_tail_list(&tmp_list, l);
|
|
|
|
init_list(l);
|
2008-12-18 22:26:08 +00:00
|
|
|
WALK_LIST_FIRST(n, tmp_list)
|
1999-02-11 22:18:36 +00:00
|
|
|
{
|
1999-10-29 12:08:49 +00:00
|
|
|
event *e = SKIP_BACK(event, n, n);
|
2015-03-02 08:41:14 +00:00
|
|
|
|
|
|
|
/* This is ugly hack, we want to log just events executed from the main I/O loop */
|
2021-03-12 14:35:56 +00:00
|
|
|
if ((l == &global_event_list) || (l == &global_work_list))
|
2015-03-02 08:41:14 +00:00
|
|
|
io_log_event(e->hook, e->data);
|
|
|
|
|
2000-01-16 17:39:16 +00:00
|
|
|
ev_run(e);
|
2022-03-02 09:35:21 +00:00
|
|
|
tmp_flush();
|
1999-02-11 22:18:36 +00:00
|
|
|
}
|
2021-03-12 14:35:56 +00:00
|
|
|
|
|
|
|
return !EMPTY_LIST(*l);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ev_run_list_limited(event_list *l, uint limit)
|
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
list tmp_list;
|
|
|
|
|
|
|
|
init_list(&tmp_list);
|
|
|
|
add_tail_list(&tmp_list, l);
|
|
|
|
init_list(l);
|
|
|
|
|
|
|
|
WALK_LIST_FIRST(n, tmp_list)
|
|
|
|
{
|
|
|
|
event *e = SKIP_BACK(event, n, n);
|
|
|
|
|
|
|
|
if (!limit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* This is ugly hack, we want to log just events executed from the main I/O loop */
|
|
|
|
if ((l == &global_event_list) || (l == &global_work_list))
|
|
|
|
io_log_event(e->hook, e->data);
|
|
|
|
|
|
|
|
ev_run(e);
|
2022-03-02 09:35:21 +00:00
|
|
|
tmp_flush();
|
2021-03-12 14:35:56 +00:00
|
|
|
limit--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EMPTY_LIST(tmp_list))
|
|
|
|
{
|
|
|
|
/* Attach new items after the unprocessed old items */
|
|
|
|
add_tail_list(&tmp_list, l);
|
|
|
|
init_list(l);
|
|
|
|
add_tail_list(l, &tmp_list);
|
|
|
|
}
|
|
|
|
|
2000-01-16 17:39:16 +00:00
|
|
|
return !EMPTY_LIST(*l);
|
1999-02-11 22:18:36 +00:00
|
|
|
}
|