Skip to content

Commit 3baa3dd

Browse files
cmorganBEpaulbartell
authored andcommitted
POSIX port - Switch from allowing the user to specify the stack memory itself, 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. Note that: 1. The stack size can still be specified by the user. 2. pxPortInitialiseStack(), and pthread_addr_setstack() was failing on stacks of typical size, as these are smaller than PTHREAD_STACK_MIN (16384) bytes, and printing out a series of warnings. Improve usability by having the posix port automatically increase the stack size to be at least PTHREAD_STACK_MIN as posix platforms have enough memory for this not to be a concern. 3. Reuse of stack memory will also result in valgrind 'invalid write' errors to what is demonstrably valid memory. Root cause is that Valgrind is tracking a stack pointer as the stack is used. Reuse of a stack buffer results in the stack being used at its start, in an area that Valgrind thinks is far away from the start of the stack. There are ways to notify Valgrind of these changes however this would require linking against and calling Valgrind functions from the FreeRTOS application using the posix port, https://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.clientreq. Also, apparently it isn't permitted by posix to reuse stack memory once its been used in a pthread via pthread_attr_setstack(), see https://stackoverflow.com/a/5422134
1 parent 6222066 commit 3baa3dd

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,15 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
169169
thread->pvParams = pvParameters;
170170
thread->xDying = pdFALSE;
171171

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

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

181183
thread->ev = event_create();

0 commit comments

Comments
 (0)