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.
|
|
|
|
*/
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
#undef LOCAL_DEBUG
|
|
|
|
|
1999-02-11 22:18:36 +00:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/event.h"
|
2021-06-19 18:50:18 +00:00
|
|
|
#include "lib/locking.h"
|
|
|
|
#include "lib/io-loop.h"
|
|
|
|
|
|
|
|
extern _Thread_local struct coroutine *this_coro;
|
1999-02-11 22:18:36 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
event_list *el = e->list;
|
|
|
|
if (!el)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASSERT_DIE(birdloop_inside(el->loop));
|
|
|
|
|
|
|
|
LOCK_DOMAIN(event, el->lock);
|
2014-11-03 09:42:55 +00:00
|
|
|
if (ev_active(e))
|
2021-06-19 18:50:18 +00:00
|
|
|
rem_node(&e->n);
|
|
|
|
UNLOCK_DOMAIN(event, el->lock);
|
1999-02-11 22:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
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
|
2021-06-19 18:50:18 +00:00
|
|
|
ev_send(event_list *l, event *e)
|
1999-02-11 22:18:36 +00:00
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
DBG("ev_send(%p, %p)\n", l, e);
|
|
|
|
ASSERT_DIE(e->hook);
|
|
|
|
ASSERT_DIE(!e->list || (e->list == l) || (e->list->loop == l->loop));
|
1999-02-11 22:18:36 +00:00
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
e->list = l;
|
1999-02-11 22:18:36 +00:00
|
|
|
|
2021-10-20 21:08:58 +00:00
|
|
|
struct event_cork *ec = e->cork;
|
|
|
|
|
|
|
|
uint ping = 0;
|
|
|
|
|
|
|
|
if (ec)
|
2021-06-19 18:50:18 +00:00
|
|
|
{
|
2021-10-20 21:08:58 +00:00
|
|
|
LOCK_DOMAIN(cork, ec->lock);
|
|
|
|
LOCK_DOMAIN(event, l->lock);
|
|
|
|
|
|
|
|
if (!enlisted(&e->n))
|
|
|
|
if (ec->count)
|
|
|
|
add_tail(&ec->events, &e->n);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
add_tail(&l->events, &e->n);
|
|
|
|
ping = 1;
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
2021-10-20 21:08:58 +00:00
|
|
|
UNLOCK_DOMAIN(cork, ec->lock);
|
2021-06-19 18:50:18 +00:00
|
|
|
}
|
2021-10-20 21:08:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOCK_DOMAIN(event, l->lock);
|
2021-06-19 18:50:18 +00:00
|
|
|
|
2021-10-20 21:08:58 +00:00
|
|
|
if (!enlisted(&e->n))
|
|
|
|
{
|
|
|
|
add_tail(&l->events, &e->n);
|
|
|
|
ping = 1;
|
|
|
|
}
|
2021-06-19 18:50:18 +00:00
|
|
|
|
2021-10-20 21:08:58 +00:00
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ping)
|
|
|
|
birdloop_ping(l->loop);
|
2021-03-12 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
const _Bool legacy = LEGACY_EVENT_LIST(l);
|
|
|
|
|
|
|
|
if (legacy)
|
|
|
|
ASSERT_THE_BIRD_LOCKED;
|
|
|
|
|
2008-12-18 22:26:08 +00:00
|
|
|
node *n;
|
1999-10-29 12:08:49 +00:00
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
list tmp_list;
|
2000-04-27 22:28:49 +00:00
|
|
|
init_list(&tmp_list);
|
2021-06-19 18:50:18 +00:00
|
|
|
|
|
|
|
/* Move the event list contents to a local list to avoid executing repeatedly added events */
|
|
|
|
LOCK_DOMAIN(event, l->lock);
|
|
|
|
add_tail_list(&tmp_list, &l->events);
|
|
|
|
init_list(&l->events);
|
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
|
|
|
|
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);
|
2021-12-06 18:03:00 +00:00
|
|
|
ASSERT_DIE(n->next->prev == n);
|
2015-03-02 08:41:14 +00:00
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
if (legacy)
|
|
|
|
{
|
|
|
|
/* The legacy way of event execution */
|
2015-03-02 08:41:14 +00:00
|
|
|
io_log_event(e->hook, e->data);
|
2021-06-19 18:50:18 +00:00
|
|
|
ev_postpone(e);
|
|
|
|
e->hook(e->data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// io_log_event(e->hook, e->data); /* TODO: add support for event logging in other io loops */
|
|
|
|
ASSERT_DIE(e->list == l);
|
|
|
|
LOCK_DOMAIN(event, l->lock);
|
|
|
|
rem_node(&e->n);
|
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
|
|
|
e->hook(e->data);
|
|
|
|
}
|
1999-02-11 22:18:36 +00:00
|
|
|
}
|
2021-03-12 14:35:56 +00:00
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
LOCK_DOMAIN(event, l->lock);
|
|
|
|
int repeat = ! EMPTY_LIST(l->events);
|
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
|
|
|
return repeat;
|
2021-03-12 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ev_run_list_limited(event_list *l, uint limit)
|
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
ASSERT_DIE(LEGACY_EVENT_LIST(l));
|
|
|
|
ASSERT_THE_BIRD_LOCKED;
|
|
|
|
|
2021-03-12 14:35:56 +00:00
|
|
|
node *n;
|
|
|
|
list tmp_list;
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
LOCK_DOMAIN(event, l->lock);
|
2021-03-12 14:35:56 +00:00
|
|
|
init_list(&tmp_list);
|
2021-06-19 18:50:18 +00:00
|
|
|
add_tail_list(&tmp_list, &l->events);
|
|
|
|
init_list(&l->events);
|
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
2021-03-12 14:35:56 +00:00
|
|
|
|
|
|
|
WALK_LIST_FIRST(n, tmp_list)
|
|
|
|
{
|
|
|
|
event *e = SKIP_BACK(event, n, n);
|
|
|
|
|
|
|
|
if (!limit)
|
|
|
|
break;
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
io_log_event(e->hook, e->data);
|
2021-03-12 14:35:56 +00:00
|
|
|
|
|
|
|
ev_run(e);
|
|
|
|
limit--;
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
LOCK_DOMAIN(event, l->lock);
|
2021-03-12 14:35:56 +00:00
|
|
|
if (!EMPTY_LIST(tmp_list))
|
|
|
|
{
|
|
|
|
/* Attach new items after the unprocessed old items */
|
2021-06-19 18:50:18 +00:00
|
|
|
add_tail_list(&tmp_list, &l->events);
|
|
|
|
init_list(&l->events);
|
|
|
|
add_tail_list(&l->events, &tmp_list);
|
2021-03-12 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
int repeat = ! EMPTY_LIST(l->events);
|
|
|
|
UNLOCK_DOMAIN(event, l->lock);
|
|
|
|
|
|
|
|
return repeat;
|
1999-02-11 22:18:36 +00:00
|
|
|
}
|
2021-10-20 21:08:58 +00:00
|
|
|
|
|
|
|
void ev_cork(struct event_cork *ec)
|
|
|
|
{
|
|
|
|
LOCK_DOMAIN(cork, ec->lock);
|
|
|
|
ec->count++;
|
|
|
|
UNLOCK_DOMAIN(cork, ec->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ev_uncork(struct event_cork *ec)
|
|
|
|
{
|
|
|
|
LOCK_DOMAIN(cork, ec->lock);
|
|
|
|
|
|
|
|
if (--ec->count)
|
|
|
|
{
|
|
|
|
UNLOCK_DOMAIN(cork, ec->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node *n;
|
|
|
|
WALK_LIST_FIRST(n, ec->events)
|
|
|
|
{
|
|
|
|
event *e = SKIP_BACK(event, n, n);
|
|
|
|
event_list *el = e->list;
|
|
|
|
|
|
|
|
rem_node(&e->n);
|
|
|
|
|
|
|
|
LOCK_DOMAIN(event, el->lock);
|
|
|
|
add_tail(&el->events, &e->n);
|
|
|
|
UNLOCK_DOMAIN(event, el->lock);
|
|
|
|
|
|
|
|
birdloop_ping(el->loop);
|
|
|
|
}
|
|
|
|
|
2022-02-01 08:45:50 +00:00
|
|
|
struct birdsock *sk;
|
|
|
|
WALK_LIST_FIRST2(sk, cork_node, ec->sockets)
|
|
|
|
{
|
|
|
|
// log(L_TRACE "Socket %p uncorked", sk);
|
|
|
|
rem_node(&sk->cork_node);
|
|
|
|
sk_ping(sk);
|
|
|
|
}
|
2021-10-20 21:08:58 +00:00
|
|
|
|
2022-02-01 08:45:50 +00:00
|
|
|
UNLOCK_DOMAIN(cork, ec->lock);
|
2021-10-20 21:08:58 +00:00
|
|
|
}
|