taskVarAdd对同一个变量使用了两次

时间:2021-11-18 23:29:22

I have a global struct added to my task using taskVarAdd() API.

我使用taskVarAdd()API将一个全局结构添加到我的任务中。

But during some scenarios, the same global struct is is added to the same task again using taskVarAdd() API. [i.e., taskVarAdd() is called twice from a task for a same variable].

但在某些情况下,使用taskVarAdd()API将相同的全局结构再次添加到同一任务中。 [即,从同一变量的任务中调用taskVarAdd()两次]。

This struct will maintain the taskID, message queue ids for that task

此结构将维护该任务的taskID,消息队列ID

Following is the sample program witten by Benoit to explain the scenario,

以下是Benoit解释方案的示例程序,

int v1;

void tvl()

{

int i = 0;

int i = 0;

v1 = 1;

v1 = 1;

taskVarAdd(0, &v1);

v1 = 2;

v1 = 2;

taskVarAdd(0, &v1);

v1 = 3;

v1 = 3;

taskDelay(1);

printf("Initial v1 = %d\n", v1);

printf(“初始v1 =%d \ n”,v1);

for(i = 0;i<10;i++)

for(i = 0; i <10; i ++)

{

 v1++;

 taskDelay(60);

 printf("v1 = %d\n", v1);

}

}

When I tested the code using tornado and i am getting different results every time i execute your program

当我使用龙卷风测试代码时,每次执行程序时我都会得到不同的结果

//1st Attempt -> tv1

//第一次尝试 - > tv1

Inital v1 = V1 = 3

初始v1 = V1 = 3

V1 = 3

V1 = 3

V1 = 2

V1 = 2

V1 = 4

V1 = 4

V1 = 4

V1 = 4

V1 = 3

V1 = 3

V1 = 5

V1 = 5

V1 = 5

V1 = 5

V1 = 5

V1 = 5

V1 = 6

V1 = 6

//2nd attempt

-> tv1

Inital v1 = V1 = 1

初始v1 = V1 = 1

V1 = 3

V1 = 3

V1 = 4

V1 = 4

V1 = 2

V1 = 2

V1 = 4

V1 = 4

V1 = 5

V1 = 5

V1 = 3

V1 = 3

V1 = 5

V1 = 5

V1 = 6

V1 = 6

But When i commented the second taskVarAdd() and tested it, i am getting the consistent expected results as follows

但是当我评论第二个taskVarAdd()并对其进行测试时,我得到了如下一致的预期结果

//1st Attempt

-> tv1

Inital v1 = 3

初始v1 = 3

V1 = 4

V1 = 4

V1 = 5

V1 = 5

V1 = 6

V1 = 6

V1 = 7

V1 = 7

V1 = 8

V1 = 8

V1 = 9

V1 = 9

V1 = 10

V1 = 10

V1 = 11

V1 = 11

V1 = 12

V1 = 12

V1 = 13

V1 = 13

//2nd Attempt

-> tv1

Inital v1 = 3

初始v1 = 3

V1 = 4

V1 = 4

V1 = 5

V1 = 5

V1 = 6

V1 = 6

V1 = 7

V1 = 7

V1 = 8

V1 = 8

V1 = 9

V1 = 9

V1 = 10

V1 = 10

V1 = 11

V1 = 11

V1 = 12

V1 = 12

V1 = 13

V1 = 13

We are using VxWork 5.5

我们正在使用VxWork 5.5

My Questions:

  1. Is it right to use two taskVarAdd() inside same task for same variable?

    在同一个任务中为同一个变量使用两个taskVarAdd()是否正确?

  2. What will be the behavior if two taskVarAdd() is used for same variable inside same task?

    如果在同一任务中将两个taskVarAdd()用于同一个变量,会有什么行为?

  3. Kindly explain the flow of your program

    请解释您的计划流程

1 个解决方案

#1


I'd like to say it is not right to add taskVarAdd two times:

我想说两次添加taskVarAdd是不对的:

  1. first, the vxworks's implementation did not deal with this situation.(I checked the vxworks source, when two taskvar have the same address, the taskVar will set two times which will get the value wrong. As vxworks is close source, so I am not going to paste the source here, but it's the vxworks' implementation makes it wrong)

    首先,vxworks的实现没有处理这种情况。(我检查了vxworks源代码,当两个taskvar具有相同的地址时,taskVar将设置两次,这将得到错误的值。因为vxworks是接近来源,所以我不是要在这里粘贴源代码,但这是vxworks的实现使其错误)

  2. second, why you need to add two times? taskAddVar make a address to be private to current task, when the task is switch out, the value be saved to task's TCB; and when the task is switch back, the private address's value will be copied back. So different task will have different value even in the same address.

    第二,为什么你需要添加两次? taskAddVar使一个地址对当前任务是私有的,当任务切换出来时,该值保存到任务的TCB;当任务切换回来时,私有地址的值将被复制回来。因此,即使在同一地址,不同的任务也会有不同的价值。

  3. vxworks implements the taskVar mechanism by adding a switch hook to make a task's private var to be save/set at task switch, which make same routine but run in different task context can have different private value at same address.

    vxworks通过添加一个switch钩子来实现taskVar机制,使任务的私有var在任务切换时保存/设置,这使得相同的例程但在不同的任务上下文中运行可以在同一地址具有不同的私有值。

#1


I'd like to say it is not right to add taskVarAdd two times:

我想说两次添加taskVarAdd是不对的:

  1. first, the vxworks's implementation did not deal with this situation.(I checked the vxworks source, when two taskvar have the same address, the taskVar will set two times which will get the value wrong. As vxworks is close source, so I am not going to paste the source here, but it's the vxworks' implementation makes it wrong)

    首先,vxworks的实现没有处理这种情况。(我检查了vxworks源代码,当两个taskvar具有相同的地址时,taskVar将设置两次,这将得到错误的值。因为vxworks是接近来源,所以我不是要在这里粘贴源代码,但这是vxworks的实现使其错误)

  2. second, why you need to add two times? taskAddVar make a address to be private to current task, when the task is switch out, the value be saved to task's TCB; and when the task is switch back, the private address's value will be copied back. So different task will have different value even in the same address.

    第二,为什么你需要添加两次? taskAddVar使一个地址对当前任务是私有的,当任务切换出来时,该值保存到任务的TCB;当任务切换回来时,私有地址的值将被复制回来。因此,即使在同一地址,不同的任务也会有不同的价值。

  3. vxworks implements the taskVar mechanism by adding a switch hook to make a task's private var to be save/set at task switch, which make same routine but run in different task context can have different private value at same address.

    vxworks通过添加一个switch钩子来实现taskVar机制,使任务的私有var在任务切换时保存/设置,这使得相同的例程但在不同的任务上下文中运行可以在同一地址具有不同的私有值。