From 550c08e200fd6479e51d1afc7f625657a659d9db Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 28 Aug 2024 08:39:09 +0200 Subject: [PATCH] jscene: atomic queue access for interrupt-safety --- src/jscene.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/jscene.c b/src/jscene.c index 4517e33..38480a5 100644 --- a/src/jscene.c +++ b/src/jscene.c @@ -9,6 +9,7 @@ #include #include #include +#include /* Type identifier for jscene */ static int jscene_type_id = -1; @@ -89,25 +90,34 @@ void jscene_render(jscene *scene) jevent jscene_read_event(jscene *s) { - if(s->queue_first == s->queue_next) + cpu_atomic_start(); + + if(s->queue_first == s->queue_next) { + cpu_atomic_end(); return (jevent){ .source = NULL, .type = JSCENE_NONE }; + } jevent e = s->queue[s->queue_first]; s->queue_first = (s->queue_first + 1) % JSCENE_QUEUE_SIZE; + cpu_atomic_end(); return e; } void jscene_queue_event(jscene *s, jevent e) { + cpu_atomic_start(); + /* Prevent filling and overflowing the queue */ int next = (s->queue_next + 1) % JSCENE_QUEUE_SIZE; if(next == s->queue_first) { s->lost_events++; - return; + } + else { + s->queue[s->queue_next] = e; + s->queue_next = next; } - s->queue[s->queue_next] = e; - s->queue_next = next; + cpu_atomic_end(); } //---