From f5fd70c54a9e7c6d67cb07efc56b2e1346800253 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Thu, 13 Jun 2024 10:42:18 +0200 Subject: [PATCH] Callback: bundling event with its target --- lib/event.h | 31 +++++++++++++++++++++++++++++++ lib/io-loop.h | 3 --- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/event.h b/lib/event.h index 1dde1e7d..a48c84a2 100644 --- a/lib/event.h +++ b/lib/event.h @@ -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 diff --git a/lib/io-loop.h b/lib/io-loop.h index 154b0774..5cad3033 100644 --- a/lib/io-loop.h +++ b/lib/io-loop.h @@ -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);