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>
|
|
|
|
|
2022-06-24 17:53:34 +00:00
|
|
|
struct birdloop;
|
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;
|
2022-06-24 17:53:34 +00:00
|
|
|
struct event * _Atomic next;
|
2022-09-12 09:09:43 +00:00
|
|
|
struct event_list * _Atomic list;
|
1999-02-11 22:18:36 +00:00
|
|
|
} event;
|
|
|
|
|
2022-09-12 09:09:43 +00:00
|
|
|
typedef struct event_list {
|
|
|
|
event * _Atomic receiver; /* Event receive list */
|
|
|
|
event * _Atomic _executor; /* Event execute list */
|
|
|
|
const char *name;
|
|
|
|
struct birdloop *loop; /* The executor loop */
|
2021-06-19 18:50:18 +00:00
|
|
|
} event_list;
|
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 *);
|
2022-06-24 17:53:34 +00:00
|
|
|
void ev_init_list(event_list *, struct birdloop *loop, const char *name);
|
|
|
|
void ev_enqueue(event_list *, event *);
|
|
|
|
#define ev_send ev_enqueue
|
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 *);
|
2021-03-12 14:35:56 +00:00
|
|
|
int ev_run_list_limited(event_list *, uint);
|
2022-06-24 17:53:34 +00:00
|
|
|
#define ev_run_list(l) ev_run_list_limited((l), ~0)
|
2024-01-28 12:40:23 +00:00
|
|
|
#define ev_list_empty(l) !ev_run_list_limited((l), 0)
|
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))
|
|
|
|
|
2014-11-03 09:42:55 +00:00
|
|
|
static inline int
|
|
|
|
ev_active(event *e)
|
|
|
|
{
|
2022-09-12 09:09:43 +00:00
|
|
|
return atomic_load_explicit(&e->list, memory_order_acquire) != NULL;
|
2022-06-24 17:53:34 +00:00
|
|
|
}
|
2021-06-19 18:50:18 +00:00
|
|
|
|
2022-06-24 17:53:34 +00:00
|
|
|
static inline event_list *
|
|
|
|
ev_get_list(event *e)
|
|
|
|
{
|
2022-09-12 09:09:43 +00:00
|
|
|
return atomic_load_explicit(&e->list, memory_order_acquire);
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-23 11:06:58 +00:00
|
|
|
#define ev_new_send(loop, pool, hook, data) \
|
|
|
|
ev_send_loop((loop), ev_new_init((pool), (hook), (data)))
|
|
|
|
|
2024-06-13 08:42:18 +00:00
|
|
|
/* 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))
|
2014-11-03 09:42:55 +00:00
|
|
|
|
1999-02-11 22:18:36 +00:00
|
|
|
#endif
|