48
48
* stdio (printf() and friends) should be called from a single task
49
49
* only or serialized with a FreeRTOS primitive such as a binary
50
50
* 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`
51
56
*----------------------------------------------------------*/
52
57
#ifdef __linux__
53
58
#define _GNU_SOURCE
@@ -97,6 +102,7 @@ static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
97
102
/*-----------------------------------------------------------*/
98
103
99
104
static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT ;
105
+ static pthread_once_t hThreadKeyOnce = PTHREAD_ONCE_INIT ;
100
106
static sigset_t xAllSignals ;
101
107
static sigset_t xSchedulerOriginalSignalMask ;
102
108
static pthread_t hMainThread = ( pthread_t ) NULL ;
@@ -105,7 +111,6 @@ static BaseType_t xSchedulerEnd = pdFALSE;
105
111
static pthread_t hTimerTickThread ;
106
112
static bool xTimerTickThreadShouldRun ;
107
113
static uint64_t prvStartTimeNs ;
108
- static pthread_mutex_t xThreadMutex = PTHREAD_MUTEX_INITIALIZER ;
109
114
static pthread_key_t xThreadKey = 0 ;
110
115
/*-----------------------------------------------------------*/
111
116
@@ -134,22 +139,15 @@ static void prvThreadKeyDestructor( void * pvData )
134
139
135
140
static void prvInitThreadKey ( void )
136
141
{
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 );
145
143
}
146
144
/*-----------------------------------------------------------*/
147
145
148
146
static void prvMarkAsFreeRTOSThread ( void )
149
147
{
150
148
uint8_t * pucThreadData = NULL ;
151
149
152
- prvInitThreadKey ( );
150
+ ( void ) pthread_once ( & hThreadKeyOnce , prvInitThreadKey );
153
151
154
152
pucThreadData = malloc ( 1 );
155
153
configASSERT ( pucThreadData != NULL );
@@ -165,7 +163,10 @@ static BaseType_t prvIsFreeRTOSThread( void )
165
163
uint8_t * pucThreadData = NULL ;
166
164
BaseType_t xRet = pdFALSE ;
167
165
166
+ ( void ) pthread_once ( & hThreadKeyOnce , prvInitThreadKey );
167
+
168
168
pucThreadData = ( uint8_t * ) pthread_getspecific ( xThreadKey );
169
+
169
170
if ( ( pucThreadData != NULL ) && ( * pucThreadData == 1 ) )
170
171
{
171
172
xRet = pdTRUE ;
@@ -192,13 +193,13 @@ void prvFatalError( const char * pcCall,
192
193
}
193
194
/*-----------------------------------------------------------*/
194
195
195
- static void prvPortSetCurrentThreadName (char * pxThreadName )
196
+ static void prvPortSetCurrentThreadName ( char * pxThreadName )
196
197
{
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
202
203
}
203
204
/*-----------------------------------------------------------*/
204
205
@@ -269,7 +270,7 @@ BaseType_t xPortStartScheduler( void )
269
270
sigset_t xSignals ;
270
271
271
272
hMainThread = pthread_self ();
272
- prvPortSetCurrentThreadName ("Scheduler" );
273
+ prvPortSetCurrentThreadName ( "Scheduler" );
273
274
274
275
/* Start the timer that generates the tick ISR(SIGALRM).
275
276
* Interrupts are disabled here already. */
@@ -303,9 +304,12 @@ BaseType_t xPortStartScheduler( void )
303
304
* memset the internal struct members for MacOS/Linux Compatibility */
304
305
#if __APPLE__
305
306
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 ) );
307
310
#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 ;
309
313
#endif /* __APPLE__*/
310
314
311
315
/* Restore original signal mask. */
@@ -392,7 +396,7 @@ void vPortDisableInterrupts( void )
392
396
{
393
397
if ( prvIsFreeRTOSThread () == pdTRUE )
394
398
{
395
- pthread_sigmask (SIG_BLOCK , & xAllSignals , NULL );
399
+ pthread_sigmask ( SIG_BLOCK , & xAllSignals , NULL );
396
400
}
397
401
}
398
402
/*-----------------------------------------------------------*/
@@ -540,7 +544,7 @@ static void * prvWaitForStart( void * pvParams )
540
544
vPortEnableInterrupts ();
541
545
542
546
/* Set thread name */
543
- prvPortSetCurrentThreadName (pcTaskGetName (xTaskGetCurrentTaskHandle ()) );
547
+ prvPortSetCurrentThreadName ( pcTaskGetName ( xTaskGetCurrentTaskHandle () ) );
544
548
545
549
/* Call the task's entry point. */
546
550
pxThread -> pxCode ( pxThread -> pvParams );
0 commit comments