From 1dbbd0fc78889750d8afce52449c2d7fbb9e0fa3 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Wed, 24 Oct 2007 06:55:43 +0000
Subject: [PATCH] Use pthread_attr_destroy() to avoid stepping on a different
 threads stack size.

---
 src/xpdev/threadwrap.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/src/xpdev/threadwrap.c b/src/xpdev/threadwrap.c
index e356b690dc..13b43e980c 100644
--- a/src/xpdev/threadwrap.c
+++ b/src/xpdev/threadwrap.c
@@ -59,23 +59,19 @@ ulong _beginthread(void( *start_address )( void * )
 		,unsigned stack_size, void *arglist)
 {
 	pthread_t	thread;
-	static pthread_attr_t attr;
-	static size_t		default_stack;
-	static int			attr_initialized=0;
-
-	if(!attr_initialized) {
-		pthread_attr_init(&attr);     /* initialize attribute structure */
-		/* set thread attributes to PTHREAD_CREATE_DETACHED which will ensure
-		   that thread resources are freed on exit() */
-		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-		pthread_attr_getstacksize(&attr, &default_stack);
-		attr_initialized=1;
-	}
+	pthread_attr_t attr;
+	size_t		default_stack;
+
+	pthread_attr_init(&attr);     /* initialize attribute structure */
+	/* set thread attributes to PTHREAD_CREATE_DETACHED which will ensure
+	   that thread resources are freed on exit() */
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 
 	/* Default stack size in BSD is too small for JS stuff */
 	/* Force to at least 256k */
 #define XPDEV_MIN_THREAD_STACK_SIZE	(256*1024)
-	if(stack_size==0 && default_stack < XPDEV_MIN_THREAD_STACK_SIZE)
+	if(stack_size==0 && pthread_attr_getstacksize(&attr, &default_stack)==0 
+			&& default_stack < XPDEV_MIN_THREAD_STACK_SIZE)
 		stack_size=XPDEV_MIN_THREAD_STACK_SIZE;
 
 	if(stack_size!=0)
@@ -92,8 +88,10 @@ ulong _beginthread(void( *start_address )( void * )
 		/* POSIX defines this arg as "void *(*start_address)" */
 		,(void * (*)(void *)) start_address
 		,arglist)==0)
+		pthread_attr_destroy(&attr);
 		return((int) thread /* thread handle */);
 
+	pthread_attr_destroy(&attr);
 	return(-1);	/* error */
 }
 #else
-- 
GitLab