Skip to content

Commit

Permalink
POSIX port - Switch from allowing the user to specify the stack, to a…
Browse files Browse the repository at this point in the history
…llowing them to specify the stack size

Change from pthread_attr_setstack() to pthread_attr_setstacksize(), and automatically adjust the stack size
to be at least PTHREAD_STACK_MIN if it wasn't already, removing the size warning.

This permits the user to increase the pthread stack size beyond the PTHREAD_STACK_MIN default of 16384 if
desired, without producing a warning in the typical case where stacks are minimized for RAM limited targets.

Continue to store thread paramters on the provided stack, for consistency with the MCU targets.

Previously pthread_attr_setstack() was used to enable user defined stacks.

Allowing user stacks has a few issues:

1. Stack sizes are limited to preserve available memory. Memory is not limited on systems running the POSIX port
   so there is no reason to attempt to limit the size of stack memory.

2. pxPortInitialiseStack() would print out warnings, and pthread_addr_setstack() would fail on stacks
   smaller than PTHREAD_STACK_MIN (16384) bytes. However PTHREAD_STACK_MIN may be larger than many task
   stacks so several warnings may be printed out. But, given #1 there is nothing really to worry about here.

3. Apparently it isn't possible to reuse stack memory once its been used in a pthread via pthread_attr_setstack(),
   see https://stackoverflow.com/a/5422134
   Reuse of stack memory was also resulting in valgrind 'invalid write' errors to what was demonstrably
   valid memory. Root cause not determined.
  • Loading branch information
cmorganBE committed Nov 30, 2023
1 parent de1a281 commit 844cd8a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions portable/ThirdParty/GCC/Posix/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
thread->pvParams = pvParameters;
thread->xDying = pdFALSE;

/* Ensure ulStackSize is at least PTHREAD_STACK_MIN */
ulStackSize = (ulStackSize < PTHREAD_STACK_MIN) ? PTHREAD_STACK_MIN : ulStackSize;

pthread_attr_init( &xThreadAttributes );
iRet = pthread_attr_setstack( &xThreadAttributes, pxEndOfStack, ulStackSize );
iRet = pthread_attr_setstacksize( &xThreadAttributes, ulStackSize );

if( iRet != 0 )
{
fprintf( stderr, "[WARN] pthread_attr_setstack failed with return value: %d. Default stack will be used.\n", iRet );
fprintf( stderr, "[WARN] Increase the stack size to PTHREAD_STACK_MIN.\n" );
fprintf( stderr, "[WARN] pthread_attr_setstacksize failed with return value: %d. Default stack size will be used.\n", iRet );
}

thread->ev = event_create();
Expand Down

0 comments on commit 844cd8a

Please sign in to comment.