Skip to content

Commit ae0a591

Browse files
Call key creation before checking if a thread is FreeRTOS (#1238)
1 parent 0f7edaf commit ae0a591

File tree

1 file changed

+19
-20
lines changed
  • portable/ThirdParty/GCC/Posix

1 file changed

+19
-20
lines changed

portable/ThirdParty/GCC/Posix/port.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
9797
/*-----------------------------------------------------------*/
9898

9999
static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT;
100+
static pthread_once_t hThreadKeyOnce = PTHREAD_ONCE_INIT;
100101
static sigset_t xAllSignals;
101102
static sigset_t xSchedulerOriginalSignalMask;
102103
static pthread_t hMainThread = ( pthread_t ) NULL;
@@ -105,7 +106,6 @@ static BaseType_t xSchedulerEnd = pdFALSE;
105106
static pthread_t hTimerTickThread;
106107
static bool xTimerTickThreadShouldRun;
107108
static uint64_t prvStartTimeNs;
108-
static pthread_mutex_t xThreadMutex = PTHREAD_MUTEX_INITIALIZER;
109109
static pthread_key_t xThreadKey = 0;
110110
/*-----------------------------------------------------------*/
111111

@@ -134,22 +134,15 @@ static void prvThreadKeyDestructor( void * pvData )
134134

135135
static void prvInitThreadKey( void )
136136
{
137-
pthread_mutex_lock( &xThreadMutex );
138-
139-
if( xThreadKey == 0 )
140-
{
141-
pthread_key_create( &xThreadKey, prvThreadKeyDestructor );
142-
}
143-
144-
pthread_mutex_unlock( &xThreadMutex );
137+
pthread_key_create( &xThreadKey, prvThreadKeyDestructor );
145138
}
146139
/*-----------------------------------------------------------*/
147140

148141
static void prvMarkAsFreeRTOSThread( void )
149142
{
150143
uint8_t * pucThreadData = NULL;
151144

152-
prvInitThreadKey();
145+
( void ) pthread_once( &hThreadKeyOnce, prvInitThreadKey );
153146

154147
pucThreadData = malloc( 1 );
155148
configASSERT( pucThreadData != NULL );
@@ -165,7 +158,10 @@ static BaseType_t prvIsFreeRTOSThread( void )
165158
uint8_t * pucThreadData = NULL;
166159
BaseType_t xRet = pdFALSE;
167160

161+
( void ) pthread_once( &hThreadKeyOnce, prvInitThreadKey );
162+
168163
pucThreadData = ( uint8_t * ) pthread_getspecific( xThreadKey );
164+
169165
if( ( pucThreadData != NULL ) && ( *pucThreadData == 1 ) )
170166
{
171167
xRet = pdTRUE;
@@ -192,13 +188,13 @@ void prvFatalError( const char * pcCall,
192188
}
193189
/*-----------------------------------------------------------*/
194190

195-
static void prvPortSetCurrentThreadName(char * pxThreadName)
191+
static void prvPortSetCurrentThreadName( char * pxThreadName )
196192
{
197-
#ifdef __APPLE__
198-
pthread_setname_np(pxThreadName);
199-
#else
200-
pthread_setname_np(pthread_self(), pxThreadName);
201-
#endif
193+
#ifdef __APPLE__
194+
pthread_setname_np( pxThreadName );
195+
#else
196+
pthread_setname_np( pthread_self(), pxThreadName );
197+
#endif
202198
}
203199
/*-----------------------------------------------------------*/
204200

@@ -269,7 +265,7 @@ BaseType_t xPortStartScheduler( void )
269265
sigset_t xSignals;
270266

271267
hMainThread = pthread_self();
272-
prvPortSetCurrentThreadName("Scheduler");
268+
prvPortSetCurrentThreadName( "Scheduler" );
273269

274270
/* Start the timer that generates the tick ISR(SIGALRM).
275271
* Interrupts are disabled here already. */
@@ -303,9 +299,12 @@ BaseType_t xPortStartScheduler( void )
303299
* memset the internal struct members for MacOS/Linux Compatibility */
304300
#if __APPLE__
305301
hSigSetupThread.__sig = _PTHREAD_ONCE_SIG_init;
306-
memset( ( void * ) &hSigSetupThread.__opaque, 0, sizeof(hSigSetupThread.__opaque));
302+
hThreadKeyOnce.__sig = _PTHREAD_ONCE_SIG_init;
303+
memset( ( void * ) &hSigSetupThread.__opaque, 0, sizeof( hSigSetupThread.__opaque ) );
304+
memset( ( void * ) &hThreadKeyOnce.__opaque, 0, sizeof( hThreadKeyOnce.__opaque ) );
307305
#else /* Linux PTHREAD library*/
308306
hSigSetupThread = PTHREAD_ONCE_INIT;
307+
hThreadKeyOnce = PTHREAD_ONCE_INIT;
309308
#endif /* __APPLE__*/
310309

311310
/* Restore original signal mask. */
@@ -392,7 +391,7 @@ void vPortDisableInterrupts( void )
392391
{
393392
if( prvIsFreeRTOSThread() == pdTRUE )
394393
{
395-
pthread_sigmask(SIG_BLOCK, &xAllSignals, NULL);
394+
pthread_sigmask( SIG_BLOCK, &xAllSignals, NULL );
396395
}
397396
}
398397
/*-----------------------------------------------------------*/
@@ -540,7 +539,7 @@ static void * prvWaitForStart( void * pvParams )
540539
vPortEnableInterrupts();
541540

542541
/* Set thread name */
543-
prvPortSetCurrentThreadName(pcTaskGetName(xTaskGetCurrentTaskHandle()));
542+
prvPortSetCurrentThreadName( pcTaskGetName( xTaskGetCurrentTaskHandle() ) );
544543

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

0 commit comments

Comments
 (0)