From 9d43b15b4604a59cee321462e4532c0482a7c70e Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Thu, 7 May 2026 20:51:06 +0000 Subject: [PATCH] fix: use atomic operations for thread-safe state access Co-authored-by: aider (deepseek/deepseek-reasoner) --- src/main.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main.c b/src/main.c index 933defa..bf2898d 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include #include #include +#include typedef enum { STATE_IDLE, @@ -13,7 +14,7 @@ typedef enum { STATE_PAUSED } looper_state; -static volatile looper_state current_state = STATE_IDLE; +static atomic_int current_state = STATE_IDLE; static jack_port_t *input_port; static jack_port_t *output_port; @@ -46,18 +47,19 @@ static int process(jack_nframes_t nframes, void *arg) if ((ev.size >= 3) && ((ev.buffer[0] & 0xf0) == 0x90)) { unsigned char note = ev.buffer[1]; if (note == 1) { - switch (current_state) { + int cur_state = atomic_load(¤t_state); + switch (cur_state) { case STATE_IDLE: - current_state = STATE_RECORD; + atomic_store(¤t_state, STATE_RECORD); break; case STATE_RECORD: - current_state = STATE_LOOPING; + atomic_store(¤t_state, STATE_LOOPING); break; case STATE_LOOPING: - current_state = STATE_PAUSED; + atomic_store(¤t_state, STATE_PAUSED); break; case STATE_PAUSED: - current_state = STATE_LOOPING; + atomic_store(¤t_state, STATE_LOOPING); break; } } @@ -77,14 +79,16 @@ static int process(jack_nframes_t nframes, void *arg) unsigned char msg = cev.buffer[0]; // real-time messages: Start (0xFA), Stop (0xFC), Continue (0xFB) if (msg == 0xFA) { // Start transport -> begin recording if idle - if (current_state == STATE_IDLE) { - current_state = STATE_RECORD; + int s = atomic_load(¤t_state); + if (s == STATE_IDLE) { + atomic_store(¤t_state, STATE_RECORD); } } else if (msg == 0xFC) { // Stop transport -> return to IDLE - current_state = STATE_IDLE; + atomic_store(¤t_state, STATE_IDLE); } else if (msg == 0xFB) { // Continue transport -> resume looping (if paused) - if (current_state == STATE_PAUSED) { - current_state = STATE_LOOPING; + int s = atomic_load(¤t_state); + if (s == STATE_PAUSED) { + atomic_store(¤t_state, STATE_LOOPING); } } } @@ -104,7 +108,8 @@ static void jack_shutdown(void *arg) static void sigusr1_handler(int signo) { (void)signo; - int code = (int)current_state + 1; + int state = atomic_load(¤t_state); + int code = state + 1; _exit(code); }