About the use of rt_task_delete API
Hey all,
I found that there is a task not created when I access my server(Xenomai) at second time. Because this task has already exist by rt_task_inquire API.
Finally, I found the problem in rt_task_delete(RT_TASK * task) API. When rt_task_delete is called, Information in variable task still exists.
So, to illustrate the problem, I wrote a test program as follow:
/************************************Start*************************************************/ #include <stdio.h>
#include <unistd.h>
#include <alchemy/task.h>
#define TASK_NUM 2
RT_TASK task[TASK_NUM];RT_TASK_INFO task_info = {0};
char task_name[100] = {0};
void func(void *arg)
{
rt_task_suspend(NULL);
}int main()
{
int i = 0;
memset(task, 0, TASK_NUM * sizeof(RT_TASK));
//create task
printf("first create task\n");
for(i = 0; i < TASK_NUM; i++)
{
if(rt_task_inquire(&task[i], &task_info) != 0)
{
memset(task_name, 0, sizeof(task_name));
sprintf(task_name, "task%d", i);
rt_task_spawn(&task[i], task_name, 0, 80, 0, func, (void *)i);
printf("create task%d, handle = %lu, thread = %lu\n", i, task[i].handle, task[i].thread);
}
else
{
printf("exist task%d, handle = %lu, thread = %lu\n", i, task[i].handle, task[i].thread);
}
}
rt_task_sleep(3*1000*1000*1000llu);
//delete task
printf("\ndelete task\n");
for(i = 0; i < TASK_NUM; i++)
{
rt_task_delete(&task[i]);
}
for(i = 0; i < TASK_NUM; i++)
{
printf("delete task%d, handle = %lu, thread = %lu\n", i, task[i].handle, task[i].thread);
}
//create task again, inverted sequence
printf("\nsecond create task\n");
for(i = TASK_NUM - 1; i >= 0; i--)
{
if(rt_task_inquire(&task[i], &task_info) != 0)
{
memset(task_name, 0, sizeof(task_name));
sprintf(task_name, "task%d", i);
rt_task_spawn(&task[i], task_name, 0, 80, 0, func, (void *)i);
printf("create task%d, handle = %lu, thread = %lu\n", i, task[i].handle, task[i].thread);
}
else
{
printf("exist task%d, handle = %lu, thread = %lu\n", i, task[i].handle, task[i].thread);
}
}
rt_task_sleep(3*1000*1000*1000llu);
return 0;
}/************************************End*************************************************/
The test program output:
/************************************Start*************************************************/ first create task
create task0, handle = 51377, thread = 140454644737792
create task1, handle = 52401, thread = 140454644668160
delete task
delete task0, handle = 51377, thread = 140454644737792 delete task1, handle = 52401, thread = 140454644668160
second create task
create task1, handle = 51377, thread = 140454644668160
exist task0, handle = 51377, thread = 140454644737792 /************************************End*************************************************/
I'm now manually memset task variable, but I still want to do it in the xenomai library to make more people less detours.
Best regards
Quoted 3 lines · On 04/20/2017 04:58 AM, [email protected] wrote:
*I'm now manually memset task variable, but I still want to do it in the xenomai library
to make more people less detours.
No, you are wrongly assuming that a task descriptor should still contain consistent data after a cancellation request is applied to it; such guarantee does not exist. This means that using rt_task_inquire() to figure out whether a task was deleted is wrong as well. A task descriptor is deemed consistent after a successful call to rt_task_create/spawn(), until that task exits, for whatever reason, voluntarily or not.
The same goes with the POSIX nptl interface, which may recycle the memory which the pthread_t handle refers to at some point after a cancellation request is issued with this handle. That memory is managed into an internal cache, part of which may even be unmapped from the process's address space in order to shrink an overpopulated cache. Therefore, using a stale handle in requests would at best cause the process to crash when referring to invalid memory, or return inconsistent data pulled from reused memory.
Signature · 1 line
Philippe.