]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Callback: bundling event with its target
authorMaria Matejka <mq@ucw.cz>
Thu, 13 Jun 2024 08:42:18 +0000 (10:42 +0200)
committerMaria Matejka <mq@ucw.cz>
Fri, 14 Jun 2024 21:16:07 +0000 (23:16 +0200)
lib/event.h
lib/io-loop.h

index 1dde1e7d0d64c7a0c179810bd19e1b1f394cf3bf..a48c84a2fcd2ff93f0d4726b1e4f2479289e8708 100644 (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
index 154b0774eec5ce75026029083518ec31bd157e9a..5cad30333d5f6ee8d1b8bfa0355d4c8173893d9c 100644 (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);