@@ -489,8 +489,9 @@ PyUnstable_ThreadState_SetStack(PyThreadState *tstate,
489489}
490490
491491
492- void
493- PyUnstable_ThreadState_ResetStack (PyThreadState * tstate )
492+ // Get the stack start address and stack size (in bytes)
493+ static void
494+ get_stack (uintptr_t * start , size_t * size )
494495{
495496#ifdef WIN32
496497 ULONG_PTR low , high ;
@@ -499,15 +500,14 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
499500 ULONG guarantee = 0 ;
500501 SetThreadStackGuarantee (& guarantee );
501502
502- uintptr_t start = (uintptr_t )low + guarantee ;
503- size_t size = (uintptr_t )high - start ;
504- tstate_set_stack (tstate , (void * )start , size );
503+ * start = (uintptr_t )low + guarantee ;
504+ * size = (uintptr_t )high - start ;
505505
506506#elif defined(__APPLE__ )
507507 pthread_t this_thread = pthread_self ();
508508 void * top = pthread_get_stackaddr_np (this_thread ); // top of the stack
509- size_t size = pthread_get_stacksize_np (this_thread );
510- tstate_set_stack ( tstate , ( char * )top - size , size ) ;
509+ * size = pthread_get_stacksize_np (this_thread );
510+ * start = ( uintptr_t )top - * size ;
511511
512512#else
513513 // XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size
@@ -525,23 +525,40 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
525525 err |= pthread_attr_destroy (& attr );
526526 }
527527 if (err == 0 ) {
528- uintptr_t base = ((uintptr_t )stack_addr ) + guard_size ;
529- uintptr_t start = base ;
530- size_t pystack_size = (base + stack_size ) - start ;
531- tstate_set_stack (tstate , (void * )start , pystack_size );
528+ uintptr_t base = (uintptr_t )stack_addr + guard_size ;
529+ * start = base ;
530+ * size = (base + stack_size ) - * start ;
532531 }
533532 else
534533# endif
535534 {
536535 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
537536 uintptr_t top = _Py_SIZE_ROUND_UP (here_addr , 4096 );
538- uintptr_t start = top - Py_C_STACK_SIZE ;
539- size_t pystack_size = top - start ;
540- tstate_set_stack (tstate , (void * )start , pystack_size );
537+ * size = Py_C_STACK_SIZE ;
538+ * start = top - * size ;
541539 }
542540#endif
543541}
544542
543+ void
544+ PyUnstable_ThreadState_ResetStack (PyThreadState * tstate )
545+ {
546+ _PyThreadStateImpl * ts = (_PyThreadStateImpl * )tstate ;
547+ if (ts -> c_stack_init_start != 0 ) {
548+ tstate_set_stack (tstate ,
549+ (void * )ts -> c_stack_init_start ,
550+ ts -> c_stack_init_size );
551+ return ;
552+ }
553+
554+ uintptr_t start ;
555+ size_t size ;
556+ get_stack (& start , & size );
557+ tstate_set_stack (tstate , (void * )start , size );
558+ ts -> c_stack_init_start = start ;
559+ ts -> c_stack_init_size = size ;
560+ }
561+
545562
546563/* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall()
547564 if the recursion_depth reaches recursion_limit. */
0 commit comments