Task can not be scheduled

Hi experts,

I’m facing a problem that a task can not be scheduled when the task is created in another task. I have tested following scenarios.

Scenario 1:
task0.create(taskFunc0, priority = 4);
start the scheduler;

taskFunc0
{
task1.create(taskFunc1, priority = 10); // taskFunc1 will run
task2.create(taskFunc2, priority = 3); // taskFunc2 will run
}

Scenario 2:
task0.create(taskFunc0, priority = 4);
start the scheduler;

taskFunc0
{
task1.create(taskFunc1, priority = 10); // taskFunc1 will run
task2.create(taskFunc2, priority = 3); // taskFunc2 will not run
task3.create(taskFunc3, priority = 3); // taskFunc3 will run
}

Scenario 3:
task0.create(taskFunc0, priority = 4);
start the scheduler;

taskFunc0
{
task1.create(taskFunc1, priority = 10); // taskFunc1 will run
task2.create(taskFunc2, priority = 3); // taskFunc2 will not run
task3.create(taskFunc3, priority = 3); // taskFunc3 will not run
task4.create(taskFunc4, priority = 6); // taskFunc4 will run
}

Scenario 4:
task0.create(taskFunc0, priority = 4);
start the scheduler;

taskFunc0
{
task1.create(taskFunc1, priority = 10); // taskFunc1 will run
task2.create(taskFunc2, priority = 3); // taskFunc2 will not run
task3.create(taskFunc3, priority = 6); // taskFunc3 will run
}

Scenario 5:
task0.create(taskFunc0, priority = 4);
start the scheduler;

taskFunc0
{
task1.create(taskFunc1, priority = 10); // taskFunc1 will run
task2.create(taskFunc2, priority = 3); // taskFunc2 will run
delay(1s);
task3.create(taskFunc3, priority = 6); // taskFunc3 will run
}

In the above scenarios, all the tasks are statically and successfully, I can see task2’s state is blocked when not run. It is weird that when a task create directly after task2, task2 will not run. Is there any limitation in this scenario? Any rules of task creation I missed?

Thanks a lot for your time.

we would need to know what the tasks do.

What I notice is that any task below priority 4 will not run, and task0 is at priority 4.

What does your wrapper system do when a task function returns to the wrapper stub, or are the functions directly used as task functions?

FreeRTOS doesn’t allow the task functions that it directly calls to return, they must either loop forever in their operation loop (with blocking) or delete themselves.

Hi Richard,

@richard-damon Thanks for your reply. Our wrapper system will delete the task itself when a task function returns.

It is weird that in scenario1, the task2 with priority = 3 works fine, but once an other task no matter its priority is higher or lower than 4 created after task2, task2 will not run(scenario 2&4), but if a delay added between task2 and task3, task2 will run.

I also tried to add a infinite loop in task function0, it seems no effect.

Hi RAc,

Thanks for your reply. Task0 is only used to create other tasks in its task function, task1 is a IPC task and will block by semaphore once it starts, task2 is a UART console task, task 3 is a simple task to modify a variable periodically, and task 4 is command processor task, it will block by semaphore.

So if you write “task 2 (your uart console task) will not run,” what do you mean by that? Will it not get scheduledvat all, or run but not work as expected? In what respect?

Note that serial output is not a reliable indicator of what happens. If you are under debugger control, set a break point to the beginning of your task 2 fn and try to trace through.

I suspect that there is a race condition that prevents your uart from being initialized correctly, so your task runs as expected in terms of the scheduler but simply does nothing visible.

I agree with @RAc, I would like to know how you determine whether a task is running or not.

As mentioned, there might be a race condition which gets hidden when you add a delay. Adding a delay might have just allowed the task to initialise the UART properly.

Can you also let us know whether your task2 (UART console task) is blocked on something (I am thinking of a semaphore which gets released from UART interrupt).

Task2 will not run means the task is created but the task function seems not run.

I add a print at the beginning of the task function, but nothing print out. I though there may be somewhere blocked the UART routine, then I changed the task2 to a very simple task which just modify a shared variable periodically, and get a same result, the variable doesn’t change. so I think this should not be a UART routine issue, but more like a schedule issue.

As I know, when a task is created, the state of this task should be Ready, but somehow the state becomes Blocked.

If I change the priority of task2 to 5, the task function will run fine.

Please re-read my comment. DO verify whether your task runs or not by a breakpoint. DO NOT rely on serial output, in particular because your task 2 appears to be in charge of exactly that.

Please allow me clarify the test I mentioned above, when change task2 to a very simple task which just modify a shared variable periodically, I didn’t read the variable from a UART print, but read it from a debugger, I didn’t make this point clear.

And I will add breakpoint to test as well.

One thing I note is that you are creating at run time task higher in priority than the task that created it. One thing you need to watch out in a wrapper is that the created task in that case starts as soon as the creation task is run, and any wrapper code after that won’t run until the task that was created blocks or finishes, so you need to be careful what you need to do after the create call.

1 Like