From 6a7108a5bc03b6ff325161c962e4365f5c4f8c50 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Sun, 4 Feb 2024 04:08:44 -0500
Subject: [PATCH] Use pthread once for jsrt initializtion.

Does not fix the NetBSD issue, but should be done anyway.
---
 src/sbbs3/js_rtpool.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/sbbs3/js_rtpool.c b/src/sbbs3/js_rtpool.c
index 6de34fc2c4..da8587097c 100644
--- a/src/sbbs3/js_rtpool.c
+++ b/src/sbbs3/js_rtpool.c
@@ -25,18 +25,24 @@ static void trigger_thread(void *args)
 	}
 }
 
+static void 
+jsrt_init(void)
+{
+	initialized=TRUE;
+	pthread_mutex_init(&jsrt_mutex, NULL);
+	pthread_mutex_lock(&jsrt_mutex);
+	listInit(&rt_list, 0);
+	pthread_mutex_unlock(&jsrt_mutex);
+	_beginthread(trigger_thread, TRIGGER_THREAD_STACK_SIZE, NULL);
+}
+
+static pthread_once_t jsrt_once = PTHREAD_ONCE_INIT;
 JSRuntime * jsrt_GetNew(int maxbytes, unsigned long timeout, const char *filename, long line)
 {
 	JSRuntime *ret;
 
-	if(!initialized) {
-		initialized=TRUE;
-		pthread_mutex_init(&jsrt_mutex, NULL);
-		pthread_mutex_lock(&jsrt_mutex);
-		listInit(&rt_list, 0);
-		_beginthread(trigger_thread, TRIGGER_THREAD_STACK_SIZE, NULL);
-	} else
-		pthread_mutex_lock(&jsrt_mutex);
+	pthread_once(&jsrt_once, jsrt_init);
+	pthread_mutex_lock(&jsrt_mutex);
 	ret=JS_NewRuntime(maxbytes);
 	listPushNode(&rt_list, ret);
 	pthread_mutex_unlock(&jsrt_mutex);
-- 
GitLab