Skip to content

Commit 03db672

Browse files
Fix race in POSIX port vPortEndScheduler (#1262)
* Fix race in POSIX port `vPortEndScheduler` The `vPortEndScheduler` checks whether it's a FreeRTOS thread after signalling the scheduler thread to stop. This creates a race between the check and the destruction of the thread key. By moving the signal to the scheduler thread after the check, the race is prevented. * Code review suggestions Signed-off-by: Gaurav Aggarwal <[email protected]> --------- Signed-off-by: Gaurav Aggarwal <[email protected]> Co-authored-by: Gaurav Aggarwal <[email protected]>
1 parent 0030d60 commit 03db672

File tree

1 file changed

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

1 file changed

+9
-3
lines changed

portable/ThirdParty/GCC/Posix/port.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
* stdio (printf() and friends) should be called from a single task
4949
* only or serialized with a FreeRTOS primitive such as a binary
5050
* semaphore or mutex.
51-
*
52-
* Note: When using LLDB (the default debugger on macOS) with this port,
51+
*
52+
* Note: When using LLDB (the default debugger on macOS) with this port,
5353
* suppress SIGUSR1 to prevent debugger interference. This can be
5454
* done by adding the following line to ~/.lldbinit:
5555
* `process handle SIGUSR1 -n true -p false -s false`
@@ -324,17 +324,23 @@ BaseType_t xPortStartScheduler( void )
324324
void vPortEndScheduler( void )
325325
{
326326
Thread_t * pxCurrentThread;
327+
BaseType_t xIsFreeRTOSThread;
327328

328329
/* Stop the timer tick thread. */
329330
xTimerTickThreadShouldRun = false;
330331
pthread_join( hTimerTickThread, NULL );
331332

333+
/* Check whether the current thread is a FreeRTOS thread.
334+
* This has to happen before the scheduler is signaled to exit
335+
* its loop to prevent data races on the thread key. */
336+
xIsFreeRTOSThread = prvIsFreeRTOSThread();
337+
332338
/* Signal the scheduler to exit its loop. */
333339
xSchedulerEnd = pdTRUE;
334340
( void ) pthread_kill( hMainThread, SIG_RESUME );
335341

336342
/* Waiting to be deleted here. */
337-
if( prvIsFreeRTOSThread() == pdTRUE )
343+
if( xIsFreeRTOSThread == pdTRUE )
338344
{
339345
pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
340346
event_wait( pxCurrentThread->ev );

0 commit comments

Comments
 (0)