FreeRTOS-Kernel
FreeRTOS-Kernel copied to clipboard
Improve multiple accessing pxCurrentTCB in a function
Description
Address Richard's feedback to update the usage of pxCurrentTCB in a function.
Since pxCurrentTCB is a volatile variable, accessing it multiple times in a function can be slow if this pointer is not changed.
In this PR:
- Use a const pointer variable to access pxCurrentTCB in a function to prevent multiple accessing a volatile variable.
- The
prvGetCurrentTCB()andprvGetCurrentTCBUnsafe()functions are provided.prvGetCurrentTCBUnsafe()can improve performance in SMP environments as it does not disable/enable interrupts. However, it should only be called in contexts where race conditions cannot occur.
Test Steps
The performance gain is evaluated by comparing main branch with this PR with a hardware cycle counter.
We use the following example to illustrate the performance gain on ulTaskGenericNotifyTake.
static void PerfTaskNotifyHigh( void * pvParameter )
{
( void ) pvParameter;
ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
perfTestEndCycles = HardwareCounter_Read();
HardwareCounter_Stop();
vTaskDelete( NULL );
}
uint32_t PerfTest_xTaskNotifyGive_UnblockHighTask( void )
{
TaskHandle_t notifyTaskHandle;
xTaskCreate( PerfTaskNotifyHigh,
"PerfTaskNotifyHigh",
PERFTEST_TASK_STACK_SIZE,
&pTaskPerfTest,
PERFTEST_TASK_PRIORITY_HIGH,
¬ifyTaskHandle );
HardwareCounter_Start();
perfTestStartCycles = HardwareCounter_Read();
xTaskNotifyGive( notifyTaskHandle );
/* The high priority task delete itself. Wait idle task to free the resource. */
vTaskDelay( 1 );
return( perfTestEndCycles - perfTestStartCycles );
}
In the example above, bottom half of the ulTaskGenericNotifyTake are evaluated with compile option ( -O3 ).
| Cycle counts difference | main branch | This PR | Performance gain |
|---|---|---|---|
| PerfTest_xTaskNotifyGive_UnblockHighTask | 485 | 465 | 20 |
Checklist:
- [x] I have tested my changes. No regression in existing tests.
- [x] I have modified and/or added unit-tests to cover the code changes in this Pull Request. - https://github.com/FreeRTOS/FreeRTOS/pull/1235
Related Issue
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Quality Gate passed
Issues
4 New issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code
Hi @chinglee-iot, are there still plans to merge these optimizations?
