gilles buloz

Missing info about _task_create() and priority issues

Discussion created by gilles buloz on Nov 11, 2011

When using _task_create() to create a task that is not defined in the task template list (second parameter set to 0 and third set to a pointer to a TASK_TEMPLATE_STRUCT), the priority value assigned to the created task must be lower (higher priority) than all other tasks listed in the template list.
This applies to explicit _task_create() calls, but also to the tasks created by RTCS when you start RTCS, Telnet server, HTTP server, ...

With MQX 3.7, this is checked and _task_create() returns an error if this condition is not met. Nice !

But with older releases (at least 3.5 and 3.4) this is not checked unless you set both MQX_CHECK_ERRORS (should be set) and MQX_USE_IDLE_TASK (not set by default). As a result _task_create() returns "OK" but creates an hidden overflow of the ready queue list.
The ready queue list (pointed to by kernel_data->READY_Q_LIST) is created at MQX startup (by _psp_init_readyqs() in /mqx/source/psp/coldfire/sc_irdyq.c) but its size is based on the max priority value found in the task template list, and then is kept unchanged. When calling _task_create() with a bigger priority value, the overflow occurs in _task_init_internal() in mqx/source/kernel/ta_init.c, near line 177 (dependign on MQX version) :
ready_q_ptr = kernel_data->READY_Q_LIST - template_ptr->TASK_PRIORITY;
because no entry in the queue exists for this new priority.
This leads to unexpected immediate or DIFFERED results, leading to a potential CRASH.

So to make it work with all MQX releases :
- make sure not to call _task_create() to create a task not listed in the template list, at a priority value greater (lower priority) than all other tasks listed in the template list.
- for RTCS : make sure you have a task in the tasklist at a priority value of 8 or greater (unless you explicitly change the priority of RTCS tasks)
- If neded, create a dummy task in the template list at the required priority without running it.

I write this post because I experienced some crashes of my application using MQX 3.4 and dicovered that once ported to MQX 3.7 the HTTP server task was unable to be created. After some debug, I found that the HTTP server was set to priority 8 but that the highest priority value used in the task template list was 7.
Even in MQX 3.7 documentation I did not find any warning about that (just about the priority value of the RTCS server tasks that must be bigger than the one of the RTCS task itself).

I hope this post will be useful.