From b16518ed4e156be37adc516eac603272f956298a Mon Sep 17 00:00:00 2001
From: Guus Sliepen <guus@meshlink.io>
Date: Tue, 7 Jul 2020 10:26:21 +0200
Subject: [PATCH] Check for the presence of stdatomic.h before using it.

---
 configure.ac | 2 +-
 src/adns.c   | 1 -
 src/event.c  | 8 ++++++++
 src/event.h  | 2 ++
 src/have.h   | 5 ++++-
 5 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1047dc1b..f9a93259 100644
--- a/configure.ac
+++ b/configure.ac
@@ -123,7 +123,7 @@ AM_CONDITIONAL(INSTALL_TESTS, test "$install_tests" = true)
 dnl Checks for header files.
 dnl We do this in multiple stages, because unlike Linux all the other operating systems really suck and don't include their own dependencies.
 
-AC_CHECK_HEADERS([syslog.h sys/file.h sys/param.h sys/resource.h sys/socket.h sys/time.h sys/un.h sys/wait.h netdb.h arpa/inet.h dirent.h curses.h ifaddrs.h])
+AC_CHECK_HEADERS([syslog.h sys/file.h sys/param.h sys/resource.h sys/socket.h sys/time.h sys/un.h sys/wait.h netdb.h arpa/inet.h dirent.h curses.h ifaddrs.h stdatomic.h])
 
 dnl Checks for typedefs, structures, and compiler characteristics.
 MeshLink_ATTRIBUTE(__malloc__)
diff --git a/src/adns.c b/src/adns.c
index cae8234f..758c0808 100644
--- a/src/adns.c
+++ b/src/adns.c
@@ -20,7 +20,6 @@
 #include "system.h"
 
 #include <pthread.h>
-#include <stdatomic.h>
 
 #include "adns.h"
 #include "devtools.h"
diff --git a/src/event.c b/src/event.c
index 8ceab362..a7c1f25a 100644
--- a/src/event.c
+++ b/src/event.c
@@ -199,7 +199,9 @@ static void signalio_handler(event_loop_t *loop, void *data, int flags) {
 	});
 
 	if(sig) {
+#ifdef HAVE_STDATOMIC_H
 		atomic_flag_clear(&sig->set);
+#endif
 		sig->cb(loop, sig->data);
 	}
 }
@@ -224,10 +226,14 @@ static void pipe_exit(event_loop_t *loop) {
 }
 
 void signal_trigger(event_loop_t *loop, signal_t *sig) {
+#ifdef HAVE_STDATOMIC_H
+
 	if(atomic_flag_test_and_set(&sig->set)) {
 		return;
 	}
 
+#endif
+
 	uint8_t signum = sig->signum;
 	write(loop->pipefd[1], &signum, 1);
 	return;
@@ -241,7 +247,9 @@ void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, u
 	sig->signum = signum;
 	sig->node.data = sig;
 
+#ifdef HAVE_STDATOMIC_H
 	atomic_flag_clear(&sig->set);
+#endif
 
 	if(loop->pipefd[0] == -1) {
 		pipe_init(loop);
diff --git a/src/event.h b/src/event.h
index ce6701ac..bec32045 100644
--- a/src/event.h
+++ b/src/event.h
@@ -52,7 +52,9 @@ typedef struct timeout_t {
 typedef struct signal_t {
 	struct splay_node_t node;
 	int signum;
+#ifdef HAVE_STDATOMIC_H
 	volatile atomic_flag set;
+#endif
 	signal_cb_t cb;
 	void *data;
 } signal_t;
diff --git a/src/have.h b/src/have.h
index c0686a54..e7da3167 100644
--- a/src/have.h
+++ b/src/have.h
@@ -32,7 +32,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
-#include <stdatomic.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <inttypes.h>
@@ -45,6 +44,10 @@
 #include <limits.h>
 #include <assert.h>
 
+#ifdef HAVE_STDATOMIC_H
+#include <stdatomic.h>
+#endif
+
 #ifdef HAVE_MINGW
 #include <w32api.h>
 #include <winsock2.h>
-- 
2.39.5