Skip to content

Commit aeea558

Browse files
committed
POSIX port - Switch from allowing the user to specify the stack, to allowing 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.
1 parent 3c3c88c commit aeea558

File tree

1 file changed

+5
-3
lines changed
  • portable/ThirdParty/GCC/Posix

1 file changed

+5
-3
lines changed

portable/ThirdParty/GCC/Posix/port.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
167167
thread->pvParams = pvParameters;
168168
thread->xDying = pdFALSE;
169169

170+
/* Ensure ulStackSize is at least PTHREAD_STACK_MIN */
171+
ulStackSize = (ulStackSize < PTHREAD_STACK_MIN) ? PTHREAD_STACK_MIN : ulStackSize;
172+
170173
pthread_attr_init( &xThreadAttributes );
171-
iRet = pthread_attr_setstack( &xThreadAttributes, pxEndOfStack, ulStackSize );
174+
iRet = pthread_attr_setstacksize( &xThreadAttributes, ulStackSize );
172175

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

179181
thread->ev = event_create();

0 commit comments

Comments
 (0)