comparison Python/thread_pthread.h @ 38794:f7d23bca599f legacy-trunk

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).
author Andrew MacIntyre <andymac@bullseye.apana.org.au>
date Tue, 13 Jun 2006 15:04:24 +0000
parents c4587a5f5e94
children e38335d4df91 df5719c298db
comparison
equal deleted inserted replaced
38793:992126a29e0f 38794:f7d23bca599f
9 #include <pthread.h> 9 #include <pthread.h>
10 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR) 10 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
11 #undef destructor 11 #undef destructor
12 #endif 12 #endif
13 #include <signal.h> 13 #include <signal.h>
14
15 /* The POSIX spec requires that use of pthread_attr_setstacksize
16 be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
17 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
18 #ifndef THREAD_STACK_SIZE
19 #define THREAD_STACK_SIZE 0 /* use default stack size */
20 #endif
21 /* for safety, ensure a viable minimum stacksize */
22 #define THREAD_STACK_MIN 0x8000 /* 32kB */
23 #else /* !_POSIX_THREAD_ATTR_STACKSIZE */
24 #ifdef THREAD_STACK_SIZE
25 #error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
26 #endif
27 #endif
14 28
15 /* The POSIX spec says that implementations supporting the sem_* 29 /* The POSIX spec says that implementations supporting the sem_*
16 family of functions must indicate this by defining 30 family of functions must indicate this by defining
17 _POSIX_SEMAPHORES. */ 31 _POSIX_SEMAPHORES. */
18 #ifdef _POSIX_SEMAPHORES 32 #ifdef _POSIX_SEMAPHORES
136 pthread_t th; 150 pthread_t th;
137 int status; 151 int status;
138 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) 152 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
139 pthread_attr_t attrs; 153 pthread_attr_t attrs;
140 #endif 154 #endif
155 #if defined(THREAD_STACK_SIZE)
156 size_t tss;
157 #endif
158
141 dprintf(("PyThread_start_new_thread called\n")); 159 dprintf(("PyThread_start_new_thread called\n"));
142 if (!initialized) 160 if (!initialized)
143 PyThread_init_thread(); 161 PyThread_init_thread();
144 162
145 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) 163 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
146 pthread_attr_init(&attrs); 164 if (pthread_attr_init(&attrs) != 0)
147 #endif 165 return -1;
148 #ifdef THREAD_STACK_SIZE 166 #endif
149 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE); 167 #if defined(THREAD_STACK_SIZE)
168 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
169 : THREAD_STACK_SIZE;
170 if (tss != 0) {
171 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
172 pthread_attr_destroy(&attrs);
173 return -1;
174 }
175 }
150 #endif 176 #endif
151 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) 177 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
152 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); 178 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
153 #endif 179 #endif
154 180
458 status = pthread_cond_signal( &thelock->lock_released ); 484 status = pthread_cond_signal( &thelock->lock_released );
459 CHECK_STATUS("pthread_cond_signal"); 485 CHECK_STATUS("pthread_cond_signal");
460 } 486 }
461 487
462 #endif /* USE_SEMAPHORES */ 488 #endif /* USE_SEMAPHORES */
489
490 /* set the thread stack size.
491 * Return 0 if size is valid, -1 if size is invalid,
492 * -2 if setting stack size is not supported.
493 */
494 static int
495 _pythread_pthread_set_stacksize(size_t size)
496 {
497 #if defined(THREAD_STACK_SIZE)
498 pthread_attr_t attrs;
499 size_t tss_min;
500 int rc = 0;
501 #endif
502
503 /* set to default */
504 if (size == 0) {
505 _pythread_stacksize = 0;
506 return 0;
507 }
508
509 #if defined(THREAD_STACK_SIZE)
510 #if defined(PTHREAD_STACK_MIN)
511 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
512 : THREAD_STACK_MIN;
513 #else
514 tss_min = THREAD_STACK_MIN;
515 #endif
516 if (size >= tss_min) {
517 /* validate stack size by setting thread attribute */
518 if (pthread_attr_init(&attrs) == 0) {
519 rc = pthread_attr_setstacksize(&attrs, size);
520 pthread_attr_destroy(&attrs);
521 if (rc == 0) {
522 _pythread_stacksize = size;
523 return 0;
524 }
525 }
526 }
527 return -1;
528 #else
529 return -2;
530 #endif
531 }
532
533 #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)