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

**URL:** <https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089>\
**Category:** Kernel\
**Created:** [October 2, 2019, 10:14am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089 "2019-10-02T10:14:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 2, 2019, 10:14am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/1 "2019-10-02T10:14:51Z")

</div>

**[osmonnb](https://sourceforge.net/u/osmonnb/)** wrote on [Wednesday, October 02, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#d2a1):

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

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

```

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [October 2, 2019, 10:34am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/2 "2019-10-02T10:34:31Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Wednesday, October 02, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#d93f):

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.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 2, 2019, 1:36pm UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/3 "2019-10-02T13:36:11Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, October 02, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#d93f/909a):

…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.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 3, 2019, 1:45am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/4 "2019-10-03T01:45:16Z")

</div>

**[osmonnb](https://sourceforge.net/u/osmonnb/)** wrote on [Thursday, October 03, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#038a):

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

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 3, 2019, 2:25am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/5 "2019-10-03T02:25:22Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 03, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#038a/f853):

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.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [October 3, 2019, 2:29am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/6 "2019-10-03T02:29:53Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, October 03, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#d959):

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).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 3, 2019, 3:16am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/7 "2019-10-03T03:16:47Z")

</div>

**[osmonnb](https://sourceforge.net/u/osmonnb/)** wrote on [Thursday, October 03, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#38b4):

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

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 3, 2019, 3:28am UTC](https://forums.freertos.org/t/sequence-of-calling-prvinitialisenewtask-prvaddnewtasktoreadylist-in-xtaskcreate/8089/8 "2019-10-03T03:28:14Z")

</div>

**[osmonnb](https://sourceforge.net/u/osmonnb/)** wrote on [Thursday, October 03, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/c33d66f3b5/#15b8):

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
