Skip to content

Call key creation before checking if a thread is FreeRTOS thread #1238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions portable/ThirdParty/GCC/Posix/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
/*-----------------------------------------------------------*/

static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT;
static pthread_once_t hThreadKeyOnce = PTHREAD_ONCE_INIT;
static sigset_t xAllSignals;
static sigset_t xSchedulerOriginalSignalMask;
static pthread_t hMainThread = ( pthread_t ) NULL;
Expand All @@ -105,7 +106,6 @@ static BaseType_t xSchedulerEnd = pdFALSE;
static pthread_t hTimerTickThread;
static bool xTimerTickThreadShouldRun;
static uint64_t prvStartTimeNs;
static pthread_mutex_t xThreadMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_key_t xThreadKey = 0;
/*-----------------------------------------------------------*/

Expand Down Expand Up @@ -134,22 +134,15 @@ static void prvThreadKeyDestructor( void * pvData )

static void prvInitThreadKey( void )
{
pthread_mutex_lock( &xThreadMutex );

if( xThreadKey == 0 )
{
pthread_key_create( &xThreadKey, prvThreadKeyDestructor );
}

pthread_mutex_unlock( &xThreadMutex );
pthread_key_create( &xThreadKey, prvThreadKeyDestructor );
}
/*-----------------------------------------------------------*/

static void prvMarkAsFreeRTOSThread( void )
{
uint8_t * pucThreadData = NULL;

prvInitThreadKey();
( void ) pthread_once( &hThreadKeyOnce, prvInitThreadKey );

pucThreadData = malloc( 1 );
configASSERT( pucThreadData != NULL );
Expand All @@ -165,7 +158,10 @@ static BaseType_t prvIsFreeRTOSThread( void )
uint8_t * pucThreadData = NULL;
BaseType_t xRet = pdFALSE;

( void ) pthread_once( &hThreadKeyOnce, prvInitThreadKey );

pucThreadData = ( uint8_t * ) pthread_getspecific( xThreadKey );

if( ( pucThreadData != NULL ) && ( *pucThreadData == 1 ) )
{
xRet = pdTRUE;
Expand All @@ -192,13 +188,13 @@ void prvFatalError( const char * pcCall,
}
/*-----------------------------------------------------------*/

static void prvPortSetCurrentThreadName(char * pxThreadName)
static void prvPortSetCurrentThreadName( char * pxThreadName )
{
#ifdef __APPLE__
pthread_setname_np(pxThreadName);
#else
pthread_setname_np(pthread_self(), pxThreadName);
#endif
#ifdef __APPLE__
pthread_setname_np( pxThreadName );
#else
pthread_setname_np( pthread_self(), pxThreadName );
#endif
}
/*-----------------------------------------------------------*/

Expand Down Expand Up @@ -269,7 +265,7 @@ BaseType_t xPortStartScheduler( void )
sigset_t xSignals;

hMainThread = pthread_self();
prvPortSetCurrentThreadName("Scheduler");
prvPortSetCurrentThreadName( "Scheduler" );

/* Start the timer that generates the tick ISR(SIGALRM).
* Interrupts are disabled here already. */
Expand Down Expand Up @@ -303,9 +299,12 @@ BaseType_t xPortStartScheduler( void )
* memset the internal struct members for MacOS/Linux Compatibility */
#if __APPLE__
hSigSetupThread.__sig = _PTHREAD_ONCE_SIG_init;
memset( ( void * ) &hSigSetupThread.__opaque, 0, sizeof(hSigSetupThread.__opaque));
hThreadKeyOnce.__sig = _PTHREAD_ONCE_SIG_init;
memset( ( void * ) &hSigSetupThread.__opaque, 0, sizeof( hSigSetupThread.__opaque ) );
memset( ( void * ) &hThreadKeyOnce.__opaque, 0, sizeof( hThreadKeyOnce.__opaque ) );
#else /* Linux PTHREAD library*/
hSigSetupThread = PTHREAD_ONCE_INIT;
hThreadKeyOnce = PTHREAD_ONCE_INIT;
#endif /* __APPLE__*/

/* Restore original signal mask. */
Expand Down Expand Up @@ -392,7 +391,7 @@ void vPortDisableInterrupts( void )
{
if( prvIsFreeRTOSThread() == pdTRUE )
{
pthread_sigmask(SIG_BLOCK, &xAllSignals, NULL);
pthread_sigmask( SIG_BLOCK, &xAllSignals, NULL );
}
}
/*-----------------------------------------------------------*/
Expand Down Expand Up @@ -540,7 +539,7 @@ static void * prvWaitForStart( void * pvParams )
vPortEnableInterrupts();

/* Set thread name */
prvPortSetCurrentThreadName(pcTaskGetName(xTaskGetCurrentTaskHandle()));
prvPortSetCurrentThreadName( pcTaskGetName( xTaskGetCurrentTaskHandle() ) );

/* Call the task's entry point. */
pxThread->pxCode( pxThread->pvParams );
Expand Down