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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_EVENT_H_
|
|
|
|
#define _BIRD_EVENT_H_
|
|
|
|
|
|
|
|
#include "lib/resource.h"
|
2021-06-19 18:50:18 +00:00
|
|
|
#include "lib/locking.h"
|
|
|
|
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
|
|
|
DEFINE_DOMAIN(event);
|
2021-10-20 21:08:58 +00:00
|
|
|
DEFINE_DOMAIN(cork);
|
1999-02-11 22:18:36 +00:00
|
|
|
|
|
|
|
typedef struct event {
|
|
|
|
resource r;
|
2000-04-27 22:28:49 +00:00
|
|
|
void (*hook)(void *);
|
1999-02-11 22:18:36 +00:00
|
|
|
void *data;
|
|
|
|
node n; /* Internal link */
|
2021-06-19 18:50:18 +00:00
|
|
|
struct event_list *list; /* List where this event is put in */
|
2021-10-20 21:08:58 +00:00
|
|
|
struct event_cork *cork; /* Event execution limiter */
|
|
|
|
node cork_node;
|
1999-02-11 22:18:36 +00:00
|
|
|
} event;
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
typedef struct event_list {
|
|
|
|
list events;
|
|
|
|
pool *pool;
|
|
|
|
struct birdloop *loop;
|
|
|
|
DOMAIN(event) lock;
|
|
|
|
} event_list;
|
1999-02-11 22:18:36 +00:00
|
|
|
|
2021-10-20 21:08:58 +00:00
|
|
|
struct event_cork {
|
|
|
|
DOMAIN(cork) lock;
|
|
|
|
u32 count;
|
|
|
|
list events;
|
2022-02-01 08:45:50 +00:00
|
|
|
list sockets;
|
2021-10-20 21:08:58 +00:00
|
|
|
};
|
|
|
|
|
1999-02-11 22:18:36 +00:00
|
|
|
extern event_list global_event_list;
|
2021-03-12 14:35:56 +00:00
|
|
|
extern event_list global_work_list;
|
1999-02-11 22:18:36 +00:00
|
|
|
|
|
|
|
event *ev_new(pool *);
|
2000-04-27 22:28:49 +00:00
|
|
|
void ev_run(event *);
|
2021-06-19 18:50:18 +00:00
|
|
|
|
|
|
|
static inline void ev_init_list(event_list *el, struct birdloop *loop, const char *name)
|
|
|
|
{
|
|
|
|
init_list(&el->events);
|
|
|
|
el->loop = loop;
|
|
|
|
el->lock = DOMAIN_NEW(event, name);
|
|
|
|
}
|
|
|
|
|
2021-10-20 21:08:58 +00:00
|
|
|
static inline void ev_init_cork(struct event_cork *ec, const char *name)
|
|
|
|
{
|
|
|
|
init_list(&ec->events);
|
2022-02-01 08:45:50 +00:00
|
|
|
init_list(&ec->sockets);
|
2021-10-20 21:08:58 +00:00
|
|
|
ec->lock = DOMAIN_NEW(cork, name);
|
|
|
|
ec->count = 0;
|
|
|
|
};
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
void ev_send(event_list *, event *);
|
2022-02-02 10:20:53 +00:00
|
|
|
#define ev_send_self(e) ({ ASSERT_DIE((e)->list); ev_send((e)->list, (e)); })
|
2021-06-19 18:50:18 +00:00
|
|
|
#define ev_send_loop(l, e) ev_send(birdloop_event_list((l)), (e))
|
|
|
|
|
|
|
|
#define ev_schedule(e) ({ ASSERT_THE_BIRD_LOCKED; if (!ev_active((e))) ev_send(&global_event_list, (e)); })
|
|
|
|
#define ev_schedule_work(e) ({ ASSERT_THE_BIRD_LOCKED; if (!ev_active((e))) ev_send(&global_work_list, (e)); })
|
|
|
|
|
1999-02-11 22:18:36 +00:00
|
|
|
void ev_postpone(event *);
|
1999-11-17 12:01:11 +00:00
|
|
|
int ev_run_list(event_list *);
|
2021-03-12 14:35:56 +00:00
|
|
|
int ev_run_list_limited(event_list *, uint);
|
1999-02-11 22:18:36 +00:00
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
#define LEGACY_EVENT_LIST(l) (((l) == &global_event_list) || ((l) == &global_work_list))
|
|
|
|
|
2021-10-20 21:08:58 +00:00
|
|
|
void ev_cork(struct event_cork *);
|
|
|
|
void ev_uncork(struct event_cork *);
|
|
|
|
|
|
|
|
static inline u32 ev_corked(struct event_cork *ec)
|
|
|
|
{
|
|
|
|
if (!ec)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LOCK_DOMAIN(cork, ec->lock);
|
|
|
|
u32 out = ec->count;
|
|
|
|
UNLOCK_DOMAIN(cork, ec->lock);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:50:18 +00:00
|
|
|
_Bool birdloop_inside(struct birdloop *loop);
|
|
|
|
|
2014-11-03 09:42:55 +00:00
|
|
|
static inline int
|
|
|
|
ev_active(event *e)
|
|
|
|
{
|
2021-06-19 18:50:18 +00:00
|
|
|
if (e->list == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ASSERT_DIE(birdloop_inside(e->list->loop));
|
|
|
|
return enlisted(&e->n);
|
2014-11-03 09:42:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 13:55:23 +00:00
|
|
|
static inline event*
|
|
|
|
ev_new_init(pool *p, void (*hook)(void *), void *data)
|
|
|
|
{
|
|
|
|
event *e = ev_new(p);
|
|
|
|
e->hook = hook;
|
|
|
|
e->data = data;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2014-11-03 09:42:55 +00:00
|
|
|
|
1999-02-11 22:18:36 +00:00
|
|
|
#endif
|