From 3c2f025f6ce4ecbaef308f5e0f3dc933bd27166e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 18 Feb 2021 12:51:53 -0500
Subject: [PATCH] Add protected_*_adjust_fetch() which provides the modified
 value.

protected_*_adjust() only adjusts now.
---
 src/sbbs3/ftpsrvr.c    |  4 ++--
 src/sbbs3/mailsrvr.c   |  2 +-
 src/sbbs3/main.cpp     |  4 ++--
 src/sbbs3/websrvr.c    |  2 +-
 src/xpdev/threadwrap.h | 31 +++++++++++++++++++++++--------
 5 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/src/sbbs3/ftpsrvr.c b/src/sbbs3/ftpsrvr.c
index 0d46ed3d1f..fd88e100e5 100644
--- a/src/sbbs3/ftpsrvr.c
+++ b/src/sbbs3/ftpsrvr.c
@@ -225,7 +225,7 @@ static void thread_up(BOOL setuid)
 
 static int32_t thread_down(void)
 {
-	int32_t count = protected_uint32_adjust(&thread_count,-1);
+	int32_t count = protected_uint32_adjust_fetch(&thread_count,-1);
 	if(startup!=NULL && startup->thread_up!=NULL)
 		startup->thread_up(startup->cbdata,FALSE, FALSE);
 	return count;
@@ -5002,7 +5002,7 @@ static void ctrl_thread(void* arg)
 	ftp_close_socket(&tmp_sock,&sess,__LINE__);
 
 	{
-		int32_t	clients = protected_uint32_adjust(&active_clients, -1);
+		int32_t	clients = protected_uint32_adjust_fetch(&active_clients, -1);
 		int32_t	threads = thread_down();
 		update_clients();
 
diff --git a/src/sbbs3/mailsrvr.c b/src/sbbs3/mailsrvr.c
index caf3378492..e274aa07fb 100644
--- a/src/sbbs3/mailsrvr.c
+++ b/src/sbbs3/mailsrvr.c
@@ -267,7 +267,7 @@ static void thread_up(BOOL setuid)
 
 static int32_t thread_down(void)
 {
-	int32_t count = protected_uint32_adjust(&thread_count,-1);
+	int32_t count = protected_uint32_adjust_fetch(&thread_count,-1);
 	if(startup!=NULL && startup->thread_up!=NULL)
 		startup->thread_up(startup->cbdata,FALSE,FALSE);
 	return count;
diff --git a/src/sbbs3/main.cpp b/src/sbbs3/main.cpp
index 391118a59b..c1b8e2f0e0 100644
--- a/src/sbbs3/main.cpp
+++ b/src/sbbs3/main.cpp
@@ -57,7 +57,7 @@
 			lprintf(LOG_ERR, "%04d SSH Error %d destroying Cryptlib Session %d from line %d"
 				, sock, result, session, line);
 		else {
-			uint32_t remain = protected_uint32_adjust(&ssh_sessions, -1);
+			uint32_t remain = protected_uint32_adjust_fetch(&ssh_sessions, -1);
 			lprintf(LOG_DEBUG, "%04d SSH Cryptlib Session: %d destroyed from line %d (%u remain)"
 				, sock, session, line, remain);
 		}
@@ -4687,7 +4687,7 @@ void node_thread(void* arg)
 		/* crash here on Aug-4-2015:
 		node_thread_running already destroyed
 		bbs_thread() timed out waiting for 1 node thread(s) to terminate */
-		int32_t remain = protected_uint32_adjust(&node_threads_running, -1);
+		uint32_t remain = protected_uint32_adjust_fetch(&node_threads_running, -1);
 		lprintf(LOG_INFO,"Node %d thread terminated (%u node threads remain, %lu clients served)"
 			,sbbs->cfg.node_num, remain, served);
 	}
diff --git a/src/sbbs3/websrvr.c b/src/sbbs3/websrvr.c
index 807c38ba23..87d2803c15 100644
--- a/src/sbbs3/websrvr.c
+++ b/src/sbbs3/websrvr.c
@@ -6687,7 +6687,7 @@ void http_session_thread(void* arg)
 	RingBufDispose(&session.outbuf);
 	FREE_AND_NULL(session.subscan);
 
-	clients_remain=protected_uint32_adjust(&active_clients, -1);
+	clients_remain=protected_uint32_adjust_fetch(&active_clients, -1);
 	update_clients();
 	client_off(socket);
 
diff --git a/src/xpdev/threadwrap.h b/src/xpdev/threadwrap.h
index 9f2ed9944b..ee7c1a0cd9 100644
--- a/src/xpdev/threadwrap.h
+++ b/src/xpdev/threadwrap.h
@@ -168,10 +168,15 @@ typedef std::atomic<uint64_t> protected_uint64_t;
 #define protected_int64_init(pval, val) (std::atomic_store<int64_t>(pval, val), 0)
 #define protected_uint64_init(pval, val) (std::atomic_store<uint64_t>(pval, val), 0)
 
-#define protected_int32_adjust(pval, adj) (std::atomic_fetch_add<int32_t>(pval, adj) + adj)
-#define protected_uint32_adjust(pval, adj) (std::atomic_fetch_add<uint32_t>(pval, adj) + adj)
-#define protected_int64_adjust(pval, adj) (std::atomic_fetch_add<int64_t>(pval, adj) + adj)
-#define protected_uint64_adjust(pval, adj) (std::atomic_fetch_add<uint64_t>(pval, adj) + adj)
+#define protected_int32_adjust(pval, adj) std::atomic_fetch_add<int32_t>(pval, adj)
+#define protected_uint32_adjust(pval, adj) std::atomic_fetch_add<uint32_t>(pval, adj)
+#define protected_int64_adjust(pval, adj) std::atomic_fetch_add<int64_t>(pval, adj)
+#define protected_uint64_adjust(pval, adj) std::atomic_fetch_add<uint64_t>(pval, adj)
+
+#define protected_int32_adjust_fetch(pval, adj) (std::atomic_fetch_add<int32_t>(pval, adj) + adj)
+#define protected_uint32_adjust_fetch(pval, adj) (std::atomic_fetch_add<uint32_t>(pval, adj) + adj)
+#define protected_int64_adjust_fetch(pval, adj) (std::atomic_fetch_add<int64_t>(pval, adj) + adj)
+#define protected_uint64_adjust_fetch(pval, adj) (std::atomic_fetch_add<uint64_t>(pval, adj) + adj)
 
 #define protected_int32_value(val) std::atomic_load<int32_t>(&val)
 #define protected_uint32_value(val) std::atomic_load<uint32_t>(&val)
@@ -188,10 +193,15 @@ typedef _Atomic(uint64_t) protected_uint64_t;
 #define protected_int64_init(pval, val) (atomic_init(pval, val), 0)
 #define protected_uint64_init(pval, val) (atomic_init(pval, val), 0)
 
-#define protected_int32_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
-#define protected_uint32_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
-#define protected_int64_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
-#define protected_uint64_adjust(pval, adj) (atomic_fetch_add(pval, adj) + adj)
+#define protected_int32_adjust(pval, adj) atomic_fetch_add(pval, adj)
+#define protected_uint32_adjust(pval, adj) atomic_fetch_add(pval, adj)
+#define protected_int64_adjust(pval, adj) atomic_fetch_add(pval, adj)
+#define protected_uint64_adjust(pval, adj) atomic_fetch_add(pval, adj)
+
+#define protected_int32_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
+#define protected_uint32_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
+#define protected_int64_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
+#define protected_uint64_adjust_fetch(pval, adj) (atomic_fetch_add(pval, adj) + adj)
 
 #define protected_int32_value(val) atomic_load(&val)
 #define protected_uint32_value(val) atomic_load(&val)
@@ -236,6 +246,11 @@ typedef struct {
 #define protected_int64_value(i)		protected_int64_adjust(&i,0)
 #define protected_uint64_value(i)		protected_uint64_adjust(&i,0)
 
+#define protected_int32_adjust_fetch(a, b)	protected_int32_adjust(a, b)
+#define protected_uint32_adjust_fetch(a, b)	protected_uint32_adjust(a, b)
+#define protected_int64_adjust_fetch(a, b)	protected_int64_adjust(a, b)
+#define protected_uint64_adjust_fetch(a, b)	protected_uint64_adjust(a, b)
+
 /* Return 0 on success, non-zero on failure (see pthread_mutex_init): */
 DLLEXPORT int DLLCALL protected_int32_init(protected_int32_t*,	int32_t value);
 DLLEXPORT int DLLCALL protected_int64_init(protected_int64_t*,	int64_t value);
-- 
GitLab