diff options
author | Andrew MacIntyre <andymac@bullseye.apana.org.au> | 2006-06-13 15:04:24 (GMT) |
---|---|---|
committer | Andrew MacIntyre <andymac@bullseye.apana.org.au> | 2006-06-13 15:04:24 (GMT) |
commit | 9291332de137141057591386b4ba449ae3a5ed48 (patch) | |
tree | 31e4a0a2411052ceb8e05284fe9409c6995f79ca /Lib | |
parent | c6f5b3ad6c9e93235f9aa53d1ed8086030fbcd6c (diff) | |
download | cpython-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 'Lib')
-rw-r--r-- | Lib/dummy_thread.py | 8 | ||||
-rw-r--r-- | Lib/test/output/test_thread | 12 | ||||
-rw-r--r-- | Lib/test/test_thread.py | 43 | ||||
-rw-r--r-- | Lib/test/test_threading.py | 16 | ||||
-rw-r--r-- | Lib/threading.py | 4 |
5 files changed, 82 insertions, 1 deletions
diff --git a/Lib/dummy_thread.py b/Lib/dummy_thread.py index 21fd03f..7c26f9e 100644 --- a/Lib/dummy_thread.py +++ b/Lib/dummy_thread.py @@ -20,6 +20,7 @@ __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock', 'interrupt_main', 'LockType'] import traceback as _traceback +import warnings class error(Exception): """Dummy implementation of thread.error.""" @@ -75,6 +76,13 @@ def allocate_lock(): """Dummy implementation of thread.allocate_lock().""" return LockType() +def stack_size(size=None): + """Dummy implementation of thread.stack_size().""" + if size is not None: + msg = "setting thread stack size not supported on this platform" + warnings.warn(msg, RuntimeWarning) + return 0 + class LockType(object): """Class implementing dummy implementation of thread.LockType. diff --git a/Lib/test/output/test_thread b/Lib/test/output/test_thread index d49651d..d8174ab 100644 --- a/Lib/test/output/test_thread +++ b/Lib/test/output/test_thread @@ -4,3 +4,15 @@ all tasks done *** Barrier Test *** all tasks done + +*** Changing thread stack size *** +caught expected ValueError setting stack_size(4096) +successfully set stack_size(32768) +successfully set stack_size(1048576) +successfully set stack_size(0) +trying stack_size = 32768 +waiting for all tasks to complete +all tasks done +trying stack_size = 1048576 +waiting for all tasks to complete +all tasks done diff --git a/Lib/test/test_thread.py b/Lib/test/test_thread.py index ea345b6..7b523e2 100644 --- a/Lib/test/test_thread.py +++ b/Lib/test/test_thread.py @@ -115,3 +115,46 @@ for i in range(numtasks): thread.start_new_thread(task2, (i,)) done.acquire() print 'all tasks done' + +# not all platforms support changing thread stack size +print '\n*** Changing thread stack size ***' +if thread.stack_size() != 0: + raise ValueError, "initial stack_size not 0" + +thread.stack_size(0) +if thread.stack_size() != 0: + raise ValueError, "stack_size not reset to default" + +from os import name as os_name +if os_name in ("nt", "os2", "posix"): + + 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' + + if tss_supported: + failed = lambda s, e: s != e + fail_msg = "stack_size(%d) failed - should succeed" + for tss in (32768, 0x100000, 0): + thread.stack_size(tss) + if failed(thread.stack_size(), tss): + raise ValueError, fail_msg % tss + print 'successfully set stack_size(%d)' % tss + + for tss in (32768, 0x100000): + 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) diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 7eb9758..02f338a 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -85,6 +85,22 @@ class ThreadTests(unittest.TestCase): print 'all tasks done' self.assertEqual(numrunning.get(), 0) + # run with a minimum thread stack size (32kB) + def test_various_ops_small_stack(self): + if verbose: + print 'with 32kB thread stack size...' + threading.stack_size(0x8000) + self.test_various_ops() + threading.stack_size(0) + + # run with a large thread stack size (1MB) + def test_various_ops_large_stack(self): + if verbose: + print 'with 1MB thread stack size...' + threading.stack_size(0x100000) + self.test_various_ops() + threading.stack_size(0) + def test_foreign_thread(self): # Check that a "foreign" thread can use the threading module. def f(mutex): diff --git a/Lib/threading.py b/Lib/threading.py index c27140d..5655dde 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -15,7 +15,7 @@ from collections import deque # Rename some stuff so "from threading import *" is safe __all__ = ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', - 'Timer', 'setprofile', 'settrace', 'local'] + 'Timer', 'setprofile', 'settrace', 'local', 'stack_size'] _start_new_thread = thread.start_new_thread _allocate_lock = thread.allocate_lock @@ -713,6 +713,8 @@ def enumerate(): _active_limbo_lock.release() return active +from thread import stack_size + # Create the main thread object _MainThread() |