summaryrefslogtreecommitdiff
path: root/lib/event.h
diff options
context:
space:
mode:
authorMaria Matejka <mq@ucw.cz>2022-09-12 11:09:43 +0200
committerMaria Matejka <mq@ucw.cz>2022-09-18 16:33:51 +0200
commit0861d3a8a3af3935d474a12c6227ea2403375b4c (patch)
tree199d958d60c4cdb69b7dc5a1253d32dff70e4a6b /lib/event.h
parente3e15f36fa54414e497fab1a054c89c6b1cdd1df (diff)
Fixing several race-conditions in event code.
After a suggestion by Santiago, I added the direct list pointer into events and the events are now using this value to check whether the route is active or not. Also the whole trick with sentinel node unioned with event list is now gone. For debugging, there is also an internal circular buffer to store what has been recently happening in event code before e.g. a crash happened. By default, this debug is off and must be manually enabled in lib/event.c as it eats quite some time and space.
Diffstat (limited to 'lib/event.h')
-rw-r--r--lib/event.h30
1 files changed, 9 insertions, 21 deletions
diff --git a/lib/event.h b/lib/event.h
index 9773c3a9..0bef737a 100644
--- a/lib/event.h
+++ b/lib/event.h
@@ -11,6 +11,7 @@
#include "lib/resource.h"
#include "lib/locking.h"
+#include "lib/rcu.h"
#include <stdatomic.h>
@@ -21,17 +22,14 @@ typedef struct event {
void (*hook)(void *);
void *data;
struct event * _Atomic next;
+ struct event_list * _Atomic list;
} event;
-typedef union event_list {
- struct {
- event * _Atomic receiver; /* Event receive list */
- event * _Atomic _executor; /* Event execute list */
- const char *name;
- struct birdloop *loop; /* The executor loop */
- char _end[0];
- };
- event _sentinel; /* Sentinel node to actively detect list end */
+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 */
} event_list;
extern event_list global_event_list;
@@ -56,23 +54,13 @@ int ev_run_list_limited(event_list *, uint);
static inline int
ev_active(event *e)
{
- return atomic_load_explicit(&e->next, memory_order_relaxed) != NULL;
+ return atomic_load_explicit(&e->list, memory_order_acquire) != NULL;
}
static inline event_list *
ev_get_list(event *e)
{
- /* We are looking for the sentinel node at the list end.
- * After this, we have s->next == NULL */
- event *s = e;
- for (event *sn; sn = atomic_load_explicit(&s->next, memory_order_acquire); s = sn)
- ;
-
- /* No sentinel, no list. */
- if (s == e)
- return NULL;
- else
- return SKIP_BACK(event_list, _sentinel, s);
+ return atomic_load_explicit(&e->list, memory_order_acquire);
}
static inline event*