summaryrefslogtreecommitdiffstats
path: root/Python/thread_pthread.h
diff options
context:
space:
mode:
authorAndrew MacIntyre <andymac@bullseye.apana.org.au>2006-06-13 15:04:24 (GMT)
committerAndrew MacIntyre <andymac@bullseye.apana.org.au>2006-06-13 15:04:24 (GMT)
commit9291332de137141057591386b4ba449ae3a5ed48 (patch)
tree31e4a0a2411052ceb8e05284fe9409c6995f79ca /Python/thread_pthread.h
parentc6f5b3ad6c9e93235f9aa53d1ed8086030fbcd6c (diff)
downloadcpython-9291332de137141057591386b4ba449ae3a5ed48.zip
cpython-9291332de137141057591386b4ba449ae3a5ed48.tar.gz
cpython-9291332de137141057591386b4ba449ae3a5ed48.tar.bz2
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).
Diffstat (limited to 'Python/thread_pthread.h')
-rw-r--r--Python/thread_pthread.h77
1 files changed, 74 insertions, 3 deletions
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 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
#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 @@ PyThread_release_lock(PyThread_type_lock lock)
}
#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)