From ce76e2051ce668ff4623263215bf4b38cfca46ca Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 11 Jun 2020 21:52:00 +0200 Subject: [PATCH] Use atomic operations to check whether to write to the signal pipe. We need to do an atomic test-and-set operation to check whether we can avoid writing to the signal pipe. Use C11 atomics to do this in a portable way (hopefully). --- configure.ac | 2 ++ src/event.c | 8 ++++---- src/event.h | 2 +- src/have.h | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index cfef2a11..f0617f32 100644 --- a/configure.ac +++ b/configure.ac @@ -54,6 +54,8 @@ if test -d /sw/lib ; then LIBS="$LIBS -L/sw/lib" fi +AX_CHECK_COMPILE_FLAG([-std=c11], [CPPFLAGS="-std=c11"]) + dnl Compiler hardening flags dnl No -fstack-protector-all because it doesn't work on all platforms or architectures. diff --git a/src/event.c b/src/event.c index 5e9763e2..43e4e040 100644 --- a/src/event.c +++ b/src/event.c @@ -199,7 +199,7 @@ static void signalio_handler(event_loop_t *loop, void *data, int flags) { }); if(sig) { - sig->set = false; + atomic_flag_clear(&sig->set); sig->cb(loop, sig->data); } } @@ -224,12 +224,11 @@ static void pipe_exit(event_loop_t *loop) { } void signal_trigger(event_loop_t *loop, signal_t *sig) { - if(sig->set) { + if(atomic_flag_test_and_set(&sig->set)) { return; } uint8_t signum = sig->signum; - sig->set = true; write(loop->pipefd[1], &signum, 1); return; } @@ -240,9 +239,10 @@ void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, u sig->cb = cb; sig->data = data; sig->signum = signum; - sig->set = false; sig->node.data = sig; + atomic_flag_clear(&sig->set); + if(loop->pipefd[0] == -1) { pipe_init(loop); } diff --git a/src/event.h b/src/event.h index a87685da..ce6701ac 100644 --- a/src/event.h +++ b/src/event.h @@ -52,7 +52,7 @@ typedef struct timeout_t { typedef struct signal_t { struct splay_node_t node; int signum; - bool set; + volatile atomic_flag set; signal_cb_t cb; void *data; } signal_t; diff --git a/src/have.h b/src/have.h index f64e15d0..c0686a54 100644 --- a/src/have.h +++ b/src/have.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include -- 2.39.5