Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMP: Add task creation with affinity functions #470

Merged
merged 1 commit into from Mar 18, 2022

Conversation

Dazza0
Copy link
Contributor

@Dazza0 Dazza0 commented Mar 9, 2022

Description

We have some instances when a task function must be guaranteed to only ever run on a particular core such as:

  • When the task function accesses some shared array indexed by xCoreID
  • When the task function accesses some per-cpu specific resources (such as coprocessors)

The current task creation API expects users to set a task's affinity (using vTaskCoreAffinitySet()) after the task is created. For example, if Task A creates Task B using xTaskCreate(), Task A should then set the core affinity of Task B using vTaskCoreAffinitySet().

However, as soon as Task B is created, it is possible for Task B to be scheduled on any core (as created tasks have no core affinity by default). Thus, extra logic is required in both Task A and Task B in order to guarantee that bulk of Task B only ever runs on its intended core. For example:

void TaskB(void *arg)
{
    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);  //Wait to be started by Task A after affinity is set
    ...
}

void TaskA(void *arg)
{
    TaskHandle_t hdl_B;
    xTaskCreate(..., &hdl_B); //Create Task B
    vTaskCoreAffinitySet(hdl_B, 1 << 0);  //Pin Task B to core 0
    xTaskNotifyGive(hdl_B); //Start TaskB
}

This PR adds the convenience function listed below, which allow a task's core affinity to be specified on creation. This will guarantee that the task will only ever run on it's intended cores, and saves us from needing the extra logic demonstrated in the example above.

  • xTaskCreateAffinitySet()
  • xTaskCreateStaticAffinitySet()
  • xTaskCreateRestrictedAffinitySet()
  • xTaskCreateRestrictedStaticAffinitySet()

Test Steps

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.

This commit adds the functions listed below. These functions allow
tasks to be created with their core affinity already set.

- xTaskCreateAffinitySet()
- xTaskCreateStaticAffinitySet()
- xTaskCreateRestrictedAffinitySet()
- xTaskCreateRestrictedStaticAffinitySet()
@Dazza0 Dazza0 requested a review from a team as a code owner March 9, 2022 12:41
@Dazza0 Dazza0 changed the title Add task creation with affinity functions SMP: Add task creation with affinity functions Mar 9, 2022
@aggarg aggarg merged commit a97741a into FreeRTOS:smp Mar 18, 2022
@Dazza0 Dazza0 deleted the feature/task_creation_with_affinity branch March 21, 2022 06:47
@snikeguo
Copy link

prvCreateIdleTasks->xTaskCreate->xTaskCreateAffinitySet->prvInitialiseNewTask->pxPortInitialiseStack

in tricore :
pxPortInitialiseStack()
{
var csa=GetCurrentCpuFreeCsaArea();
init csa;
init sp;
int .....
}
For the TRICORE architecture, each core can only access its own CSA and cannot access the CSAs of other cores, so xTaskCreate must be executed on the corresponding core!

@dongsheng-autra
Copy link

prvCreateIdleTasks->xTaskCreate->xTaskCreateAffinitySet->prvInitialiseNewTask->pxPortInitialiseStack

in tricore : pxPortInitialiseStack() { var csa=GetCurrentCpuFreeCsaArea(); init csa; init sp; int ..... } For the TRICORE architecture, each core can only access its own CSA and cannot access the CSAs of other cores, so xTaskCreate must be executed on the corresponding core!

Hi @snikeguo ,

I am working on TC397, and trying multi-core support, do you have some code to share?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants