Skip to content

Commit

Permalink
change(freertos/smp): Update timers.c locking
Browse files Browse the repository at this point in the history
Updated timers.c to use granular locking

- Added xTaskSpinlock and xISRSpinlock
- Replaced critical section macros with data group locking macros
such as taskENTER/EXIT_CRITICAL() with tmrENTER/EXIT_CRITICAL().

Co-authored-by: Sudeep Mohanty <[email protected]>
  • Loading branch information
Dazza0 and sudeep-mohanty committed Jan 15, 2025
1 parent f073b35 commit fc47127
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@
#define tmrSTATUS_IS_STATICALLY_ALLOCATED ( 0x02U )
#define tmrSTATUS_IS_AUTORELOAD ( 0x04U )

/*
* Macro to mark the start of a critical code region.
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define tmrENTER_CRITICAL( pxTaskSpinlock, pxISRSpinlock ) portLOCK_DATA_GROUP( ( portSPINLOCK_TYPE * ) pxTaskSpinlock, ( portSPINLOCK_TYPE * ) pxISRSpinlock )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define tmrENTER_CRITICAL( pxTaskSpinlock, pxISRSpinlock ) do { ( void ) pxTaskSpinlock; ( void ) pxISRSpinlock; taskENTER_CRITICAL(); } while( 0 )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */

/*
* Macro to mark the end of a critical code region.
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define tmrEXIT_CRITICAL( pxTaskSpinlock, pxISRSpinlock ) portUNLOCK_DATA_GROUP( ( portSPINLOCK_TYPE * ) pxTaskSpinlock, ( portSPINLOCK_TYPE * ) pxISRSpinlock )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define tmrEXIT_CRITICAL( pxTaskSpinlock, pxISRSpinlock ) do { ( void ) pxTaskSpinlock; ( void ) pxISRSpinlock; taskEXIT_CRITICAL(); } while( 0 )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */

/* The definition of the timers themselves. */
typedef struct tmrTimerControl /* The old naming convention is used to prevent breaking kernel aware debuggers. */
{
Expand Down Expand Up @@ -149,6 +167,11 @@
PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;

#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
PRIVILEGED_DATA static portSPINLOCK_TYPE xTaskSpinlock = portINIT_TIMERS_TASK_SPINLOCK_STATIC;
PRIVILEGED_DATA static portSPINLOCK_TYPE xISRSpinlock = portINIT_TIMERS_ISR_SPINLOCK_STATIC;
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */

/*-----------------------------------------------------------*/

/*
Expand Down Expand Up @@ -576,7 +599,7 @@
traceENTER_vTimerSetReloadMode( xTimer, xAutoReload );

configASSERT( xTimer );
taskENTER_CRITICAL();
tmrENTER_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
{
if( xAutoReload != pdFALSE )
{
Expand All @@ -587,7 +610,7 @@
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_AUTORELOAD );
}
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL( &xTaskSpinlock, &xISRSpinlock );

traceRETURN_vTimerSetReloadMode();
}
Expand All @@ -601,7 +624,7 @@
traceENTER_xTimerGetReloadMode( xTimer );

configASSERT( xTimer );
portBASE_TYPE_ENTER_CRITICAL();
tmrENTER_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
{
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) == 0U )
{
Expand All @@ -614,7 +637,7 @@
xReturn = pdTRUE;
}
}
portBASE_TYPE_EXIT_CRITICAL();
tmrEXIT_CRITICAL( &xTaskSpinlock, &xISRSpinlock );

traceRETURN_xTimerGetReloadMode( xReturn );

Expand Down Expand Up @@ -1113,7 +1136,7 @@
/* Check that the list from which active timers are referenced, and the
* queue used to communicate with the timer service, have been
* initialised. */
taskENTER_CRITICAL();
tmrENTER_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
{
if( xTimerQueue == NULL )
{
Expand Down Expand Up @@ -1155,7 +1178,7 @@
mtCOVERAGE_TEST_MARKER();
}
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
}
/*-----------------------------------------------------------*/

Expand All @@ -1169,7 +1192,7 @@
configASSERT( xTimer );

/* Is the timer in the list of active timers? */
portBASE_TYPE_ENTER_CRITICAL();
tmrENTER_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
{
if( ( pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE ) == 0U )
{
Expand All @@ -1180,7 +1203,7 @@
xReturn = pdTRUE;
}
}
portBASE_TYPE_EXIT_CRITICAL();
tmrEXIT_CRITICAL( &xTaskSpinlock, &xISRSpinlock );

traceRETURN_xTimerIsTimerActive( xReturn );

Expand All @@ -1197,11 +1220,11 @@

configASSERT( xTimer );

taskENTER_CRITICAL();
tmrENTER_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
{
pvReturn = pxTimer->pvTimerID;
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL( &xTaskSpinlock, &xISRSpinlock );

traceRETURN_pvTimerGetTimerID( pvReturn );

Expand All @@ -1218,11 +1241,11 @@

configASSERT( xTimer );

taskENTER_CRITICAL();
tmrENTER_CRITICAL( &xTaskSpinlock, &xISRSpinlock );
{
pxTimer->pvTimerID = pvNewID;
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL( &xTaskSpinlock, &xISRSpinlock );

traceRETURN_vTimerSetTimerID();
}
Expand Down

0 comments on commit fc47127

Please sign in to comment.