60
60
#include <sys/time.h>
61
61
#include <sys/times.h>
62
62
#include <time.h>
63
- #include <unistd.h>
64
63
65
64
#ifdef __APPLE__
66
65
#include <mach/mach_vm.h>
@@ -104,9 +103,10 @@ static sigset_t xAllSignals;
104
103
static sigset_t xSchedulerOriginalSignalMask ;
105
104
static pthread_t hMainThread = ( pthread_t ) NULL ;
106
105
static volatile BaseType_t uxCriticalNesting ;
107
- static List_t xThreadList ;
108
- static pthread_t hTimerTickThread ;
109
106
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. */
110
110
/*-----------------------------------------------------------*/
111
111
112
112
static void prvSetupSignalsAndSchedulerPolicy ( void );
@@ -130,6 +130,7 @@ void prvFatalError( const char * pcCall,
130
130
fprintf ( stderr , "%s: %s\n" , pcCall , strerror ( iErrno ) );
131
131
abort ();
132
132
}
133
+ /*-----------------------------------------------------------*/
133
134
134
135
/*
135
136
* See header file for description.
@@ -166,13 +167,15 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
166
167
thread -> pvParams = pvParameters ;
167
168
thread -> xDying = pdFALSE ;
168
169
170
+ /* Ensure ulStackSize is at least PTHREAD_STACK_MIN */
171
+ ulStackSize = ( ulStackSize < PTHREAD_STACK_MIN ) ? PTHREAD_STACK_MIN : ulStackSize ;
172
+
169
173
pthread_attr_init ( & xThreadAttributes );
170
- iRet = pthread_attr_setstack ( & xThreadAttributes , pxEndOfStack , ulStackSize );
174
+ iRet = pthread_attr_setstacksize ( & xThreadAttributes , ulStackSize );
171
175
172
176
if ( iRet != 0 )
173
177
{
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 );
176
179
}
177
180
178
181
thread -> ev = event_create ();
@@ -242,13 +245,9 @@ BaseType_t xPortStartScheduler( void )
242
245
sigwait ( & xSignals , & iSignal );
243
246
}
244
247
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. */
251
249
pxEndMarker = listGET_END_MARKER ( & xThreadList );
250
+
252
251
for ( pxIterator = listGET_HEAD_ENTRY ( & xThreadList ); pxIterator != pxEndMarker ; pxIterator = listGET_NEXT ( pxIterator ) )
253
252
{
254
253
Thread_t * pxThread = ( Thread_t * ) listGET_LIST_ITEM_OWNER ( pxIterator );
@@ -258,6 +257,16 @@ BaseType_t xPortStartScheduler( void )
258
257
event_delete ( pxThread -> ev );
259
258
}
260
259
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
+
261
270
/* Restore original signal mask. */
262
271
( void ) pthread_sigmask ( SIG_SETMASK , & xSchedulerOriginalSignalMask , NULL );
263
272
@@ -362,7 +371,7 @@ static uint64_t prvGetTimeNs( void )
362
371
return ( uint64_t ) t .tv_sec * ( uint64_t ) 1000000000UL + ( uint64_t ) t .tv_nsec ;
363
372
}
364
373
365
- static uint64_t prvStartTimeNs ;
374
+ /*-----------------------------------------------------------*/
366
375
367
376
/* commented as part of the code below in vPortSystemTickHandler,
368
377
* to adjust timing according to full demo requirements */
@@ -453,6 +462,7 @@ void vPortThreadDying( void * pxTaskToDelete,
453
462
454
463
pxThread -> xDying = pdTRUE ;
455
464
}
465
+ /*-----------------------------------------------------------*/
456
466
457
467
void vPortCancelThread ( void * pxTaskToDelete )
458
468
{
@@ -542,6 +552,7 @@ static void prvSuspendSelf( Thread_t * thread )
542
552
* - A thread with all signals blocked with pthread_sigmask().
543
553
*/
544
554
event_wait ( thread -> ev );
555
+ pthread_testcancel ();
545
556
}
546
557
547
558
/*-----------------------------------------------------------*/
0 commit comments