0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 01:31:55 +00:00

Callback: bundling event with its target

This commit is contained in:
Maria Matejka 2024-06-13 10:42:18 +02:00
parent 4ac55615e4
commit f5fd70c54a
2 changed files with 31 additions and 3 deletions

View File

@ -75,5 +75,36 @@ ev_new_init(pool *p, void (*hook)(void *), void *data)
#define ev_new_send(loop, pool, hook, data) \
ev_send_loop((loop), ev_new_init((pool), (hook), (data)))
/* Get birdloop's event list */
event_list *birdloop_event_list(struct birdloop *loop);
typedef struct callback {
event event;
void (*hook)(struct callback *);
struct birdloop *target;
} callback;
static inline void callback_run(void *_c)
{
struct callback *c = _c;
c->hook(c);
}
#define callback_init(_c, _h, _t) \
({ \
struct callback *_cc = (_c); \
*_cc = (struct callback) { \
.hook = _h, \
.event = (event) { \
.hook = callback_run, \
.data = _cc, \
}, \
.target = _t, \
}; \
})
#define callback_activate(_c) ev_send_loop(((_c)->target), &((_c)->event))
#define callback_is_active(_c) ev_active(&((_c)->event))
#define callback_cancel(_c) ev_postpone(&((_c)->event))
#endif

View File

@ -40,9 +40,6 @@ void birdloop_stop(struct birdloop *loop, void (*stopped)(void *data), void *dat
void birdloop_stop_self(struct birdloop *loop, void (*stopped)(void *data), void *data);
void birdloop_free(struct birdloop *loop);
/* Get birdloop's event list */
event_list *birdloop_event_list(struct birdloop *loop);
/* Run this event in this thread's priority event list */
void ev_send_this_thread(event *e);