From b5e1d17f42ce5f35f8d0f47751f3491ace805e8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 18 Feb 2021 01:56:03 -0500
Subject: [PATCH] Use <atomic> types for C++ and <stdatomic.h> for C.

Also, fix things that incorrectly reach into the protected_*_t to
incorrectly access values.
---
 src/sbbs3/js_server.c  |  2 +-
 src/sbbs3/mailsrvr.c   |  2 +-
 src/sbbs3/main.cpp     |  2 +-
 src/sbbs3/sbbs.h       |  2 +-
 src/sbbs3/websrvr.c    |  2 +-
 src/xpdev/threadwrap.h | 12 ++++++++++++
 6 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/sbbs3/js_server.c b/src/sbbs3/js_server.c
index de81d138af..d5d2ddf80d 100644
--- a/src/sbbs3/js_server.c
+++ b/src/sbbs3/js_server.c
@@ -88,7 +88,7 @@ static JSBool js_server_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
 			break;
 		case SERVER_PROP_CLIENTS:
 			if(p->clients!=NULL)
-				*vp=UINT_TO_JSVAL(*p->clients);
+				*vp=UINT_TO_JSVAL(protected_uint32_val(*p->clients));
 			break;
 	}
 
diff --git a/src/sbbs3/mailsrvr.c b/src/sbbs3/mailsrvr.c
index a6acd01b24..cccf65eea1 100644
--- a/src/sbbs3/mailsrvr.c
+++ b/src/sbbs3/mailsrvr.c
@@ -6021,7 +6021,7 @@ void DLLCALL mail_server(void* arg)
 	ZERO_VAR(js_server_props);
 	SAFEPRINTF3(js_server_props.version,"%s %s%c",server_name, VERSION, REVISION);
 	js_server_props.version_detail=mail_ver();
-	js_server_props.clients=&active_clients.value;
+	js_server_props.clients=&active_clients;
 	js_server_props.options=&startup->options;
 	js_server_props.interfaces=&startup->interfaces;
 
diff --git a/src/sbbs3/main.cpp b/src/sbbs3/main.cpp
index 903155d0d5..19ec0a4120 100644
--- a/src/sbbs3/main.cpp
+++ b/src/sbbs3/main.cpp
@@ -5059,7 +5059,7 @@ void DLLCALL bbs_thread(void* arg)
 	ZERO_VAR(js_server_props);
 	SAFEPRINTF3(js_server_props.version,"%s %s%c",TELNET_SERVER,VERSION,REVISION);
 	js_server_props.version_detail=bbs_ver();
-	js_server_props.clients=&node_threads_running.value;
+	js_server_props.clients=&node_threads_running;
 	js_server_props.options=&startup->options;
 	js_server_props.interfaces=&startup->telnet_interfaces;
 
diff --git a/src/sbbs3/sbbs.h b/src/sbbs3/sbbs.h
index e6011ad200..aa495d10fd 100644
--- a/src/sbbs3/sbbs.h
+++ b/src/sbbs3/sbbs.h
@@ -1227,7 +1227,7 @@ extern "C" {
 		const char*	version_detail;
 		str_list_t*	interfaces;
 		uint32_t*	options;
-		uint32_t*	clients;
+		protected_uint32_t*	clients;
 	} js_server_props_t;
 
 	enum {
diff --git a/src/sbbs3/websrvr.c b/src/sbbs3/websrvr.c
index cc51d881d0..75f9ef7a83 100644
--- a/src/sbbs3/websrvr.c
+++ b/src/sbbs3/websrvr.c
@@ -6932,7 +6932,7 @@ void DLLCALL web_server(void* arg)
 	ZERO_VAR(js_server_props);
 	SAFEPRINTF3(js_server_props.version,"%s %s%c",server_name, VERSION, REVISION);
 	js_server_props.version_detail=web_ver();
-	js_server_props.clients=&active_clients.value;
+	js_server_props.clients=&active_clients;
 	js_server_props.options=&startup->options;
 	js_server_props.interfaces=&startup->interfaces;
 
diff --git a/src/xpdev/threadwrap.h b/src/xpdev/threadwrap.h
index 6814aa9fb0..9e94e1d1ec 100644
--- a/src/xpdev/threadwrap.h
+++ b/src/xpdev/threadwrap.h
@@ -41,8 +41,13 @@
 #include "wrapdll.h"	/* DLLEXPORT and DLLCALL */
 
 #ifdef __FreeBSD__
+#include <stdbool.h>
+#ifdef __cplusplus
+#include <atomic>
+#else
 #include <stdatomic.h>
 #endif
+#endif
 
 #if defined(__cplusplus)
 extern "C" {
@@ -153,10 +158,17 @@ DLLEXPORT int DLLCALL pthread_once(pthread_once_t *oc, void (*init)(void));
 /* mutexes.																*/
 /************************************************************************/
 #ifdef __FreeBSD__
+#ifdef __cplusplus
+typedef std::atomic<int32_t> protected_int32_t;
+typedef std::atomic<uint32_t> protected_uint32_t;
+typedef std::atomic<int64_t> protected_int64_t;
+typedef std::atomic<uint64_t> protected_uint64_t;
+#else
 typedef _Atomic(int32_t) protected_int32_t;
 typedef _Atomic(uint32_t) protected_uint32_t;
 typedef _Atomic(int64_t) protected_int64_t;
 typedef _Atomic(uint64_t) protected_uint64_t;
+#endif
 
 DLLEXPORT int DLLCALL protected_uint32_init(protected_uint32_t*,	uint32_t value);
 DLLEXPORT int DLLCALL protected_uint64_init(protected_uint64_t*,	uint64_t value);
-- 
GitLab