Patch #1454481:  Make thread stack size runtime tunable.

Heavily revised, comprising revisions:
46640 - original trunk revision (backed out in r46655)
46647 - markup fix (backed out in r46655)
46692:46918 merged from branch aimacintyre-sf1454481

branch tested on buildbots (Windows buildbots had problems
not related to these changes).
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index c29a61c..60d2fb2 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -12,6 +12,20 @@
 #endif
 #include <signal.h>
 
+/* The POSIX spec requires that use of pthread_attr_setstacksize
+   be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
+#ifdef _POSIX_THREAD_ATTR_STACKSIZE
+#ifndef THREAD_STACK_SIZE
+#define	THREAD_STACK_SIZE	0	/* use default stack size */
+#endif
+/* for safety, ensure a viable minimum stacksize */
+#define	THREAD_STACK_MIN	0x8000	/* 32kB */
+#else  /* !_POSIX_THREAD_ATTR_STACKSIZE */
+#ifdef THREAD_STACK_SIZE
+#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
+#endif
+#endif
+
 /* The POSIX spec says that implementations supporting the sem_*
    family of functions must indicate this by defining
    _POSIX_SEMAPHORES. */   
@@ -138,15 +152,27 @@
 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
 	pthread_attr_t attrs;
 #endif
+#if defined(THREAD_STACK_SIZE)
+	size_t	tss;
+#endif
+
 	dprintf(("PyThread_start_new_thread called\n"));
 	if (!initialized)
 		PyThread_init_thread();
 
 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
-	pthread_attr_init(&attrs);
+	if (pthread_attr_init(&attrs) != 0)
+		return -1;
 #endif
-#ifdef THREAD_STACK_SIZE
-	pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
+#if defined(THREAD_STACK_SIZE)
+	tss = (_pythread_stacksize != 0) ? _pythread_stacksize
+					 : THREAD_STACK_SIZE;
+	if (tss != 0) {
+		if (pthread_attr_setstacksize(&attrs, tss) != 0) {
+			pthread_attr_destroy(&attrs);
+			return -1;
+		}
+	}
 #endif
 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
         pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
@@ -460,3 +486,48 @@
 }
 
 #endif /* USE_SEMAPHORES */
+
+/* set the thread stack size.
+ * Return 0 if size is valid, -1 if size is invalid,
+ * -2 if setting stack size is not supported.
+ */
+static int
+_pythread_pthread_set_stacksize(size_t size)
+{
+#if defined(THREAD_STACK_SIZE)
+	pthread_attr_t attrs;
+	size_t tss_min;
+	int rc = 0;
+#endif
+
+	/* set to default */
+	if (size == 0) {
+		_pythread_stacksize = 0;
+		return 0;
+	}
+
+#if defined(THREAD_STACK_SIZE)
+#if defined(PTHREAD_STACK_MIN)
+	tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
+						       : THREAD_STACK_MIN;
+#else
+	tss_min = THREAD_STACK_MIN;
+#endif
+	if (size >= tss_min) {
+		/* validate stack size by setting thread attribute */
+		if (pthread_attr_init(&attrs) == 0) {
+			rc = pthread_attr_setstacksize(&attrs, size);
+			pthread_attr_destroy(&attrs);
+			if (rc == 0) {
+				_pythread_stacksize = size;
+				return 0;
+			}
+		}
+	}
+	return -1;
+#else
+	return -2;
+#endif
+}
+
+#define THREAD_SET_STACKSIZE(x)	_pythread_pthread_set_stacksize(x)