[Python-checkins] r46914 - in python/branches/aimacintyre-sf1454481: Doc/lib/libthread.tex Doc/lib/libthreading.tex Lib/test/output/test_thread Lib/test/test_thread.py Modules/threadmodule.c Python/thread.c Python/thread_nt.h Python/thread_os2.h Python/thread_pthread.h
andrew.macintyre
python-checkins at python.org
Tue Jun 13 14:00:33 CEST 2006
Author: andrew.macintyre
Date: Tue Jun 13 14:00:30 2006
New Revision: 46914
Modified:
python/branches/aimacintyre-sf1454481/Doc/lib/libthread.tex
python/branches/aimacintyre-sf1454481/Doc/lib/libthreading.tex
python/branches/aimacintyre-sf1454481/Lib/test/output/test_thread
python/branches/aimacintyre-sf1454481/Lib/test/test_thread.py
python/branches/aimacintyre-sf1454481/Modules/threadmodule.c
python/branches/aimacintyre-sf1454481/Python/thread.c
python/branches/aimacintyre-sf1454481/Python/thread_nt.h
python/branches/aimacintyre-sf1454481/Python/thread_os2.h
python/branches/aimacintyre-sf1454481/Python/thread_pthread.h
Log:
Rework of SF #1454481 to address issues on Solaris,
with error handling and other cleanup
Modified: python/branches/aimacintyre-sf1454481/Doc/lib/libthread.tex
==============================================================================
--- python/branches/aimacintyre-sf1454481/Doc/lib/libthread.tex (original)
+++ python/branches/aimacintyre-sf1454481/Doc/lib/libthread.tex Tue Jun 13 14:00:30 2006
@@ -79,16 +79,17 @@
optional \var{size} argument specifies the stack size to be used for
subsequently created threads, and must be 0 (use platform or
configured default) or a positive integer value of at least 32,768 (32kB).
-If changing the thread stack size is unsupported, or the specified size
-is invalid, a RuntimeWarning is issued and the stack size is unmodified.
-32kB is currently the minimum supported stack size value, to guarantee
-sufficient stack space for the interpreter itself.
-Note that some platforms may have particular restrictions on values for
-the stack size, such as requiring allocation in multiples of the system
-memory page size - platform documentation should be referred to for more
-information (4kB pages are common; using multiples of 4096 for the
-stack size is the suggested approach in the absence of more specific
-information).
+If changing the thread stack size is unsupported, a \exception{ThreadError}
+is raised. If the specified stack size is invalid, a \exception{ValueError}
+is raised and the stack size is unmodified. 32kB is currently the minimum
+supported stack size value to guarantee sufficient stack space for the
+interpreter itself. Note that some platforms may have particular
+restrictions on values for the stack size, such as requiring a minimum
+stack size > 32kB or requiring allocation in multiples of the system
+memory page size - platform documentation should be referred to for
+more information (4kB pages are common; using multiples of 4096 for
+the stack size is the suggested approach in the absence of more
+specific information).
Availability: Windows, systems with \POSIX{} threads.
\versionadded{2.5}
\end{funcdesc}
Modified: python/branches/aimacintyre-sf1454481/Doc/lib/libthreading.tex
==============================================================================
--- python/branches/aimacintyre-sf1454481/Doc/lib/libthreading.tex (original)
+++ python/branches/aimacintyre-sf1454481/Doc/lib/libthreading.tex Tue Jun 13 14:00:30 2006
@@ -130,16 +130,17 @@
optional \var{size} argument specifies the stack size to be used for
subsequently created threads, and must be 0 (use platform or
configured default) or a positive integer value of at least 32,768 (32kB).
-If changing the thread stack size is unsupported, or the specified size
-is invalid, a RuntimeWarning is issued and the stack size is unmodified.
-32kB is currently the minimum supported stack size value, to guarantee
-sufficient stack space for the interpreter itself.
-Note that some platforms may have particular restrictions on values for
-the stack size, such as requiring allocation in multiples of the system
-memory page size - platform documentation should be referred to for more
-information (4kB pages are common; using multiples of 4096 for the
-stack size is the suggested approach in the absence of more specific
-information).
+If changing the thread stack size is unsupported, a \exception{ThreadError}
+is raised. If the specified stack size is invalid, a \exception{ValueError}
+is raised and the stack size is unmodified. 32kB is currently the minimum
+supported stack size value to guarantee sufficient stack space for the
+interpreter itself. Note that some platforms may have particular
+restrictions on values for the stack size, such as requiring a minimum
+stack size > 32kB or requiring allocation in multiples of the system
+memory page size - platform documentation should be referred to for
+more information (4kB pages are common; using multiples of 4096 for
+the stack size is the suggested approach in the absence of more
+specific information).
Availability: Windows, systems with \POSIX{} threads.
\versionadded{2.5}
\end{funcdesc}
Modified: python/branches/aimacintyre-sf1454481/Lib/test/output/test_thread
==============================================================================
--- python/branches/aimacintyre-sf1454481/Lib/test/output/test_thread (original)
+++ python/branches/aimacintyre-sf1454481/Lib/test/output/test_thread Tue Jun 13 14:00:30 2006
@@ -6,6 +6,10 @@
all tasks done
*** Changing thread stack size ***
+caught expected ValueError setting stack_size(4096)
+successfully set stack_size(32768)
+successfully set stack_size(4194304)
+successfully set stack_size(0)
trying stack_size = 32768
waiting for all tasks to complete
all tasks done
Modified: python/branches/aimacintyre-sf1454481/Lib/test/test_thread.py
==============================================================================
--- python/branches/aimacintyre-sf1454481/Lib/test/test_thread.py (original)
+++ python/branches/aimacintyre-sf1454481/Lib/test/test_thread.py Tue Jun 13 14:00:30 2006
@@ -128,25 +128,33 @@
from os import name as os_name
if os_name in ("nt", "os2", "posix"):
- for tss, ok in ((4096, 0), (32768, 1), (0x400000, 1), (0, 1)):
- if ok:
- failed = lambda s, e: s != e
- fail_msg = "stack_size(%d) failed - should succeed"
- else:
- failed = lambda s, e: s == e
- fail_msg = "stack_size(%d) succeeded - should fail"
- thread.stack_size(tss)
- if failed(thread.stack_size(), tss):
- raise ValueError, fail_msg % tss
-
- for tss in (32768, 0x400000):
- print 'trying stack_size = %d' % tss
- next_ident = 0
- for i in range(numtasks):
- newtask()
+ tss_supported = 1
+ try:
+ thread.stack_size(4096)
+ except ValueError:
+ print 'caught expected ValueError setting stack_size(4096)'
+ except thread.ThreadError:
+ tss_supported = 0
+ print 'platform does not support changing thread stack size'
- print 'waiting for all tasks to complete'
- done.acquire()
- print 'all tasks done'
+ if tss_supported:
+ failed = lambda s, e: s != e
+ fail_msg = "stack_size(%d) failed - should succeed"
+ for tss in (32768, 0x400000, 0):
+ thread.stack_size(tss)
+ if failed(thread.stack_size(), tss):
+ raise ValueError, fail_msg % tss
+ print 'successfully set stack_size(%d)' % tss
- thread.stack_size(0)
+ for tss in (32768, 0x400000):
+ print 'trying stack_size = %d' % tss
+ next_ident = 0
+ for i in range(numtasks):
+ newtask()
+
+ print 'waiting for all tasks to complete'
+ done.acquire()
+ print 'all tasks done'
+
+ # reset stack size to default
+ thread.stack_size(0)
Modified: python/branches/aimacintyre-sf1454481/Modules/threadmodule.c
==============================================================================
--- python/branches/aimacintyre-sf1454481/Modules/threadmodule.c (original)
+++ python/branches/aimacintyre-sf1454481/Modules/threadmodule.c Tue Jun 13 14:00:30 2006
@@ -589,27 +589,36 @@
static PyObject *
thread_stack_size(PyObject *self, PyObject *args)
{
- size_t old_size, new_size;
+ size_t old_size;
+ Py_ssize_t new_size = 0;
PyObject *set_size = NULL;
+ int rc;
- if (!PyArg_UnpackTuple(args, "stack_size", 0, 1, &set_size))
+ if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
return NULL;
+ if (new_size < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "size must be 0 or a positive value");
+ return NULL;
+ }
+
old_size = PyThread_get_stacksize();
- if (set_size != NULL) {
- if (PyInt_Check(set_size))
- new_size = (size_t) PyInt_AsLong(set_size);
- else {
- PyErr_SetString(PyExc_TypeError,
- "size must be an integer");
- return NULL;
- }
- if (PyThread_set_stacksize(new_size))
- return NULL;
+ rc = PyThread_set_stacksize((size_t) new_size);
+ if (rc == -1) {
+ PyErr_Format(PyExc_ValueError,
+ "size not valid: %zd bytes",
+ new_size);
+ return NULL;
+ }
+ if (rc == -2) {
+ PyErr_SetString(ThreadError,
+ "setting stack size not supported");
+ return NULL;
}
- return PyInt_FromLong((long) old_size);
+ return PyInt_FromSsize_t((Py_ssize_t) old_size);
}
PyDoc_STRVAR(stack_size_doc,
@@ -618,18 +627,19 @@
Return the thread stack size used when creating new threads. The\n\
optional size argument specifies the stack size (in bytes) to be used\n\
for subsequently created threads, and must be 0 (use platform or\n\
-configured default) or a positive integer value of at least 32,768 (32kB).\n\
-If changing the thread stack size is unsupported, or the specified size\n\
-is invalid, a RuntimeWarning is issued and the stack size is unmodified.\n\
-32kB is currently the minimum supported stack size value, to guarantee\n\
+configured default) or a positive integer value of at least 32,768 (32k).\n\
+If changing the thread stack size is unsupported, a ThreadError\n\
+exception is raised. If the specified size is invalid, a ValueError\n\
+exception is raised, and the stack size is unmodified. 32k bytes\n\
+ currently the minimum supported stack size value to guarantee\n\
sufficient stack space for the interpreter itself.\n\
\n\
Note that some platforms may have particular restrictions on values for\n\
-the stack size, such as requiring allocation in multiples of the system\n\
-memory page size - platform documentation should be referred to for more\n\
-information (4kB pages are common; using multiples of 4096 for the\n\
-stack size is the suggested approach in the absence of more specific\n\
-information).");
+the stack size, such as requiring a minimum stack size larger than 32kB or\n\
+requiring allocation in multiples of the system memory page size\n\
+- platform documentation should be referred to for more information\n\
+(4kB pages are common; using multiples of 4096 for the stack size is\n\
+the suggested approach in the absence of more specific information).");
static PyMethodDef thread_methods[] = {
{"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
Modified: python/branches/aimacintyre-sf1454481/Python/thread.c
==============================================================================
--- python/branches/aimacintyre-sf1454481/Python/thread.c (original)
+++ python/branches/aimacintyre-sf1454481/Python/thread.c Tue Jun 13 14:00:30 2006
@@ -99,26 +99,6 @@
or the size specified by the THREAD_STACK_SIZE macro. */
static size_t _pythread_stacksize = 0;
-size_t
-PyThread_get_stacksize(void)
-{
- return _pythread_stacksize;
-}
-
-static int
-_pythread_unsupported_set_stacksize(size_t size)
-{
- return PyErr_Warn(PyExc_RuntimeWarning,
- "setting thread stack size not supported on "
- "this platform");
-}
-
-/* Only platforms with THREAD_SET_STACKSIZE() defined in
- pthread_<platform>.h, overriding this default definition,
- will support changing the stack size.
- Return 1 if an exception is pending, 0 otherwise. */
-#define THREAD_SET_STACKSIZE(x) _pythread_unsupported_set_stacksize(x)
-
#ifdef SGI_THREADS
#include "thread_sgi.h"
#endif
@@ -174,12 +154,26 @@
#endif
*/
-/* use appropriate thread stack size setting routine.
- Return 1 if an exception is pending, 0 otherwise. */
+/* return the current thread stack size */
+size_t
+PyThread_get_stacksize(void)
+{
+ return _pythread_stacksize;
+}
+
+/* Only platforms defining a THREAD_SET_STACKSIZE() macro
+ in thread_<platform>.h support changing the stack size.
+ Return 0 if stack size is valid,
+ -1 if stack size value is invalid,
+ -2 if setting stack size is not supported. */
int
PyThread_set_stacksize(size_t size)
{
+#if defined(THREAD_SET_STACKSIZE)
return THREAD_SET_STACKSIZE(size);
+#else
+ return -2;
+#endif
}
#ifndef Py_HAVE_NATIVE_TLS
Modified: python/branches/aimacintyre-sf1454481/Python/thread_nt.h
==============================================================================
--- python/branches/aimacintyre-sf1454481/Python/thread_nt.h (original)
+++ python/branches/aimacintyre-sf1454481/Python/thread_nt.h Tue Jun 13 14:00:30 2006
@@ -319,7 +319,7 @@
#define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
/* set the thread stack size.
- * Return 1 if an exception is pending, 0 otherwise.
+ * Return 0 if size is valid, -1 otherwise.
*/
static int
_pythread_nt_set_stacksize(size_t size)
@@ -335,15 +335,8 @@
_pythread_stacksize = size;
return 0;
}
- else {
- char warning[128];
- snprintf(warning,
- 128,
- "thread stack size of %#x bytes not supported on Win32",
- size);
- return PyErr_Warn(PyExc_RuntimeWarning, warning);
- }
+
+ return -1;
}
-#undef THREAD_SET_STACKSIZE
#define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)
Modified: python/branches/aimacintyre-sf1454481/Python/thread_os2.h
==============================================================================
--- python/branches/aimacintyre-sf1454481/Python/thread_os2.h (original)
+++ python/branches/aimacintyre-sf1454481/Python/thread_os2.h Tue Jun 13 14:00:30 2006
@@ -286,7 +286,7 @@
#define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */
/* set the thread stack size.
- * Return 1 if an exception is pending, 0 otherwise.
+ * Return 0 if size is valid, -1 otherwise.
*/
static int
_pythread_os2_set_stacksize(size_t size)
@@ -302,15 +302,8 @@
_pythread_stacksize = size;
return 0;
}
- else {
- char warning[128];
- snprintf(warning,
- 128,
- "thread stack size of %#x bytes not supported on OS/2",
- size);
- return PyErr_Warn(PyExc_RuntimeWarning, warning);
- }
+
+ return -1;
}
-#undef THREAD_SET_STACKSIZE
#define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x)
Modified: python/branches/aimacintyre-sf1454481/Python/thread_pthread.h
==============================================================================
--- python/branches/aimacintyre-sf1454481/Python/thread_pthread.h (original)
+++ python/branches/aimacintyre-sf1454481/Python/thread_pthread.h Tue Jun 13 14:00:30 2006
@@ -20,10 +20,6 @@
#endif
/* for safety, ensure a viable minimum stacksize */
#define THREAD_STACK_MIN 0x8000 /* 32kB */
-#if THREAD_STACK_MIN < PTHREAD_STACK_MIN
-#undef THREAD_STACK_MIN
-#define THREAD_STACK_MIN PTHREAD_STACK_MIN
-#endif
#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
#ifdef THREAD_STACK_SIZE
#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
@@ -165,7 +161,8 @@
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
#if defined(THREAD_STACK_SIZE)
tss = (_pythread_stacksize != 0) ? _pythread_stacksize
@@ -491,31 +488,46 @@
#endif /* USE_SEMAPHORES */
/* set the thread stack size.
- * Return 1 if an exception is pending, 0 otherwise.
+ * 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;
}
- /* valid range? */
- if (size >= THREAD_STACK_MIN) {
- _pythread_stacksize = size;
- return 0;
- }
- else {
- char warning[128];
- snprintf(warning,
- 128,
- "thread stack size of %#x bytes not supported",
- size);
- return PyErr_Warn(PyExc_RuntimeWarning, warning);
+#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
}
-#undef THREAD_SET_STACKSIZE
#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
More information about the Python-checkins
mailing list