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,
53+ * suppress SIGUSR1 to prevent debugger interference. This can be
54+ * done by adding the following line to ~/.lldbinit:
55+ * `process handle SIGUSR1 -n true -p true -s false`
5156*----------------------------------------------------------*/
5257#ifdef __linux__
5358 #define _GNU_SOURCE
@@ -97,6 +102,7 @@ static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
97102/*-----------------------------------------------------------*/
98103
99104static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT ;
105+ static pthread_once_t hThreadKeyOnce = PTHREAD_ONCE_INIT ;
100106static sigset_t xAllSignals ;
101107static sigset_t xSchedulerOriginalSignalMask ;
102108static pthread_t hMainThread = ( pthread_t ) NULL ;
@@ -105,7 +111,6 @@ static BaseType_t xSchedulerEnd = pdFALSE;
105111static pthread_t hTimerTickThread ;
106112static bool xTimerTickThreadShouldRun ;
107113static uint64_t prvStartTimeNs ;
108- static pthread_mutex_t xThreadMutex = PTHREAD_MUTEX_INITIALIZER ;
109114static pthread_key_t xThreadKey = 0 ;
110115/*-----------------------------------------------------------*/
111116
@@ -134,22 +139,15 @@ static void prvThreadKeyDestructor( void * pvData )
134139
135140static void prvInitThreadKey ( void )
136141{
137- pthread_mutex_lock ( & xThreadMutex );
138-
139- if ( xThreadKey == 0 )
140- {
141- pthread_key_create ( & xThreadKey , prvThreadKeyDestructor );
142- }
143-
144- pthread_mutex_unlock ( & xThreadMutex );
142+ pthread_key_create ( & xThreadKey , prvThreadKeyDestructor );
145143}
146144/*-----------------------------------------------------------*/
147145
148146static void prvMarkAsFreeRTOSThread ( void )
149147{
150148 uint8_t * pucThreadData = NULL ;
151149
152- prvInitThreadKey ( );
150+ ( void ) pthread_once ( & hThreadKeyOnce , prvInitThreadKey );
153151
154152 pucThreadData = malloc ( 1 );
155153 configASSERT ( pucThreadData != NULL );
@@ -165,7 +163,10 @@ static BaseType_t prvIsFreeRTOSThread( void )
165163 uint8_t * pucThreadData = NULL ;
166164 BaseType_t xRet = pdFALSE ;
167165
166+ ( void ) pthread_once ( & hThreadKeyOnce , prvInitThreadKey );
167+
168168 pucThreadData = ( uint8_t * ) pthread_getspecific ( xThreadKey );
169+
169170 if ( ( pucThreadData != NULL ) && ( * pucThreadData == 1 ) )
170171 {
171172 xRet = pdTRUE ;
@@ -192,13 +193,13 @@ void prvFatalError( const char * pcCall,
192193}
193194/*-----------------------------------------------------------*/
194195
195- static void prvPortSetCurrentThreadName (char * pxThreadName )
196+ static void prvPortSetCurrentThreadName ( char * pxThreadName )
196197{
197- #ifdef __APPLE__
198- pthread_setname_np (pxThreadName );
199- #else
200- pthread_setname_np (pthread_self (), pxThreadName );
201- #endif
198+ #ifdef __APPLE__
199+ pthread_setname_np ( pxThreadName );
200+ #else
201+ pthread_setname_np ( pthread_self (), pxThreadName );
202+ #endif
202203}
203204/*-----------------------------------------------------------*/
204205
@@ -269,7 +270,7 @@ BaseType_t xPortStartScheduler( void )
269270 sigset_t xSignals ;
270271
271272 hMainThread = pthread_self ();
272- prvPortSetCurrentThreadName ("Scheduler" );
273+ prvPortSetCurrentThreadName ( "Scheduler" );
273274
274275 /* Start the timer that generates the tick ISR(SIGALRM).
275276 * Interrupts are disabled here already. */
@@ -303,9 +304,12 @@ BaseType_t xPortStartScheduler( void )
303304 * memset the internal struct members for MacOS/Linux Compatibility */
304305 #if __APPLE__
305306 hSigSetupThread .__sig = _PTHREAD_ONCE_SIG_init ;
306- memset ( ( void * ) & hSigSetupThread .__opaque , 0 , sizeof (hSigSetupThread .__opaque ));
307+ hThreadKeyOnce .__sig = _PTHREAD_ONCE_SIG_init ;
308+ memset ( ( void * ) & hSigSetupThread .__opaque , 0 , sizeof ( hSigSetupThread .__opaque ) );
309+ memset ( ( void * ) & hThreadKeyOnce .__opaque , 0 , sizeof ( hThreadKeyOnce .__opaque ) );
307310 #else /* Linux PTHREAD library*/
308- hSigSetupThread = PTHREAD_ONCE_INIT ;
311+ hSigSetupThread = ( pthread_once_t ) PTHREAD_ONCE_INIT ;
312+ hThreadKeyOnce = ( pthread_once_t ) PTHREAD_ONCE_INIT ;
309313 #endif /* __APPLE__*/
310314
311315 /* Restore original signal mask. */
@@ -392,7 +396,7 @@ void vPortDisableInterrupts( void )
392396{
393397 if ( prvIsFreeRTOSThread () == pdTRUE )
394398 {
395- pthread_sigmask (SIG_BLOCK , & xAllSignals , NULL );
399+ pthread_sigmask ( SIG_BLOCK , & xAllSignals , NULL );
396400 }
397401}
398402/*-----------------------------------------------------------*/
@@ -540,7 +544,7 @@ static void * prvWaitForStart( void * pvParams )
540544 vPortEnableInterrupts ();
541545
542546 /* Set thread name */
543- prvPortSetCurrentThreadName (pcTaskGetName (xTaskGetCurrentTaskHandle ()) );
547+ prvPortSetCurrentThreadName ( pcTaskGetName ( xTaskGetCurrentTaskHandle () ) );
544548
545549 /* Call the task's entry point. */
546550 pxThread -> pxCode ( pxThread -> pvParams );
0 commit comments