Sequence of calling prvInitialiseNewTask(), prvAddNewTaskToReadyList() in xTaskCreate()

osmonnb wrote on Wednesday, October 02, 2019:

In xTaskCreate(), is it alright to reverse the order of calling the above functions? i.e

...
prvAddNewTaskToReadyList( pxNewTCB );
prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
...

richard_damon wrote on Wednesday, October 02, 2019:

Since FreeRTOS is open source, you are of course free to make any sort of changes you want (as long as you don’t claim that the results are the official version).

Such a change will introduce a big bug into the code, a prvInitializeNewTask, as the name implies, initialized the data structure for the task, so calling prvAddNewTaskToReadyList would see ‘garbage’ in the TCB when it processes it (and thus likely corrupt some datastructures) and then the changes it did to the TCB will be erased by the call to prvInitializeNewTask.

I will say that I don’t understand WHY you would even think of making that sort of change if you don’t have the background to figure out that sort of impact.

rtel wrote on Wednesday, October 02, 2019:

…in these situations it is best to state what you are trying to
achieve, rather than just how you are trying to achieve it - that way we
may be able to suggest alternatives.

osmonnb wrote on Thursday, October 03, 2019:

I am using an MCU which have 16 special banks for pushing/popping of CPU registers; one instruction is capable of pushing/popping several registers in very few cycles

I am trying to use each of these banks for task’s R1-R15 context save/restore

To do this, I need a number which is unique to a task, so that I can use it to point to a specific bank#. For example, task “1” use bank#1, task “2” use bank#2

So I think uxTaskNumber (or uxTCBNumber) is a good choice

However, uxTaskNumber of a task is unknown before prvInitialiseNewTask() is called. Therefore they cannot be used in pxPortInitialiseStack()

If the sequence of prvInitialiseNewTask() and prvAddNewTaskToReadyList() can be swapped, then uxTaskNumber is known and can be used for initialising the stack

rtel wrote on Thursday, October 03, 2019:

uxTaskNumber is also a file scope variable inside tasks.c - so you can
also just access the variable directly at any time before or after it
has been incremented and added into the TCB.

richard_damon wrote on Thursday, October 03, 2019:

My first thought is that the TCB number isn’t a good use for that, as when you delete a task that number will never get reused, so that bank won’t get used. Better would be for the port layer to keep track of what banks have been allocated and take one of the unused banks and assign it to each task as it get created, and then put the bank back when the task is deleted (this may need adding a hook into the task delete code).

osmonnb wrote on Thursday, October 03, 2019:

Dear Richard Damon,

Better would be for the port layer to keep track of what banks have been allocated and take one of the unused banks and assign it to each task as it get created, and then put the bank back when the task is deleted (this may need adding a hook into the task delete code).

We have considered keeping track of which bank is available, but then this would involves use of an array or something similar, and the program needs to loop through the array to find which bank is available. This is a very expensive process in terms of saving processor cycle, and defeat the purpose of using bank

osmonnb wrote on Thursday, October 03, 2019:

Dear Richard Damon,
Ok I understand what your proposed, checking which bank is available needs only to be done once when a task is created, so this is ok. But the consideration is still tagging a task to a bank. I think it can be done… but now I am considering Richard Barry’s suggestion of using uxTaskNumber which has a file scope