Skip to content

Commit 7ba4124

Browse files
chinglee-iotpaulbartell
authored andcommitted
Add back the pthread stack fit
1 parent 6b698ff commit 7ba4124

File tree

1 file changed

+24
-13
lines changed
  • portable/ThirdParty/GCC/Posix

1 file changed

+24
-13
lines changed

portable/ThirdParty/GCC/Posix/port.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
#include <sys/time.h>
6161
#include <sys/times.h>
6262
#include <time.h>
63-
#include <unistd.h>
6463

6564
#ifdef __APPLE__
6665
#include <mach/mach_vm.h>
@@ -104,9 +103,10 @@ static sigset_t xAllSignals;
104103
static sigset_t xSchedulerOriginalSignalMask;
105104
static pthread_t hMainThread = ( pthread_t ) NULL;
106105
static volatile BaseType_t uxCriticalNesting;
107-
static List_t xThreadList;
108-
static pthread_t hTimerTickThread;
109106
static BaseType_t xSchedulerEnd = pdFALSE;
107+
static pthread_t hTimerTickThread;
108+
static uint64_t prvStartTimeNs;
109+
static List_t xThreadList; /* The list to track all the pthreads which are not deleted. */
110110
/*-----------------------------------------------------------*/
111111

112112
static void prvSetupSignalsAndSchedulerPolicy( void );
@@ -130,6 +130,7 @@ void prvFatalError( const char * pcCall,
130130
fprintf( stderr, "%s: %s\n", pcCall, strerror( iErrno ) );
131131
abort();
132132
}
133+
/*-----------------------------------------------------------*/
133134

134135
/*
135136
* See header file for description.
@@ -166,13 +167,15 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
166167
thread->pvParams = pvParameters;
167168
thread->xDying = pdFALSE;
168169

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

172176
if( iRet != 0 )
173177
{
174-
fprintf( stderr, "[WARN] pthread_attr_setstack failed with return value: %d. Default stack will be used.\n", iRet );
175-
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 );
176179
}
177180

178181
thread->ev = event_create();
@@ -242,13 +245,9 @@ BaseType_t xPortStartScheduler( void )
242245
sigwait( &xSignals, &iSignal );
243246
}
244247

245-
/*
246-
* cancel and join any remaining pthreads
247-
* to ensure their resources are freed
248-
*
249-
* https://stackoverflow.com/a/5612424
250-
*/
248+
/* Cancel all the running thread. */
251249
pxEndMarker = listGET_END_MARKER( &xThreadList );
250+
252251
for( pxIterator = listGET_HEAD_ENTRY( &xThreadList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
253252
{
254253
Thread_t *pxThread = ( Thread_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
@@ -258,6 +257,16 @@ BaseType_t xPortStartScheduler( void )
258257
event_delete( pxThread->ev );
259258
}
260259

260+
/*
261+
* clear out the variable that is used to end the scheduler, otherwise
262+
* subsequent scheduler restarts will end immediately.
263+
*/
264+
xSchedulerEnd = pdFALSE;
265+
266+
/* Reset the pthread_once_t structure. This is required if the port
267+
* starts the scheduler again. */
268+
hSigSetupThread = PTHREAD_ONCE_INIT;
269+
261270
/* Restore original signal mask. */
262271
( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL );
263272

@@ -362,7 +371,7 @@ static uint64_t prvGetTimeNs( void )
362371
return ( uint64_t ) t.tv_sec * ( uint64_t ) 1000000000UL + ( uint64_t ) t.tv_nsec;
363372
}
364373

365-
static uint64_t prvStartTimeNs;
374+
/*-----------------------------------------------------------*/
366375

367376
/* commented as part of the code below in vPortSystemTickHandler,
368377
* to adjust timing according to full demo requirements */
@@ -453,6 +462,7 @@ void vPortThreadDying( void * pxTaskToDelete,
453462

454463
pxThread->xDying = pdTRUE;
455464
}
465+
/*-----------------------------------------------------------*/
456466

457467
void vPortCancelThread( void * pxTaskToDelete )
458468
{
@@ -542,6 +552,7 @@ static void prvSuspendSelf( Thread_t * thread )
542552
* - A thread with all signals blocked with pthread_sigmask().
543553
*/
544554
event_wait( thread->ev );
555+
pthread_testcancel();
545556
}
546557

547558
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)