Skip to content

Commit 9f48e12

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 2171091 commit 9f48e12

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
@@ -170,13 +170,15 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
170170
thread->pvParams = pvParameters;
171171
thread->xDying = pdFALSE;
172172

173+
/* Ensure ulStackSize is at least PTHREAD_STACK_MIN */
174+
ulStackSize = (ulStackSize < PTHREAD_STACK_MIN) ? PTHREAD_STACK_MIN : ulStackSize;
175+
173176
pthread_attr_init( &xThreadAttributes );
174-
iRet = pthread_attr_setstack( &xThreadAttributes, pxEndOfStack, ulStackSize );
177+
iRet = pthread_attr_setstacksize( &xThreadAttributes, ulStackSize );
175178

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

182184
thread->ev = event_create();

0 commit comments

Comments
 (0)