taskVarAdd 对同一变量使用了两次
我使用 taskVarAdd() API 将全局结构添加到我的任务中。
但在某些情况下,使用 taskVarAdd() API 再次将相同的全局结构添加到相同的任务中。 [即,taskVarAdd() 从任务中针对同一变量调用两次]。
该结构将维护该任务的任务ID、消息队列ID
以下是Benoit 用来解释该场景的示例程序,
int v1;
无效 tvl()
{
int i = 0;
v1 = 1;
任务VarAdd(0, &v1);
v1 = 2;
任务VarAdd(0, &v1);
v1 = 3;
任务延迟(1);
printf("初始 v1 = %d\n", v1);
for(i = 0;i<10;i++)
{
v1++;
taskDelay(60);
printf("v1 = %d\n", v1);
}
}
当我使用龙卷风测试代码时,每次执行程序时都会得到不同的结果
//第一次尝试 -> tv1
Inital v1 = V1 = 3
V1 = 3
V1 = 2
V1 = 4
V1 = 4
V1 = 3
V1 = 5
V1 = 5
V1 = 5
V1 = 6
//第二次尝试
-> tv1
Inital v1 = V1 = 1
V1 = 3
V1 = 4
V1 = 2
V1 = 4
V1 = 5
V1 = 3
V1 = 5
V1 = 6
但是当我评论第二个taskVarAdd()并测试它时,我得到了一致的预期结果如下
//第一次尝试
-> tv1
初始 v1 = 3
V1 = 4
V1 = 5
V1 = 6
V1 = 7
V1 = 8
V1 = 9
V1 = 10
V1 = 11
V1 = 12
V1 = 13
//第二次尝试
-> tv1
Inital v1 = 3
V1 = 4
V1 = 5
V1 = 6
V1 = 7
V1 = 8
V1 = 9
V1 = 10
V1 = 11
V1 = 12
V1 = 13
我们正在使用 VxWork 5.5
我的问题:
是否正确在同一任务中对同一变量使用两个taskVarAdd()?
如果两个taskVarAdd()用于同一个任务内的同一个变量,会出现什么行为?
请解释一下你的程序的流程
I have a global struct added to my task using 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].
This struct will maintain the taskID, message queue ids for that task
Following is the sample program witten by Benoit to explain the scenario,
int v1;
void tvl()
{
int i = 0;
v1 = 1;
taskVarAdd(0, &v1);
v1 = 2;
taskVarAdd(0, &v1);
v1 = 3;
taskDelay(1);
printf("Initial v1 = %d\n", v1);
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
Inital v1 = V1 = 3
V1 = 3
V1 = 2
V1 = 4
V1 = 4
V1 = 3
V1 = 5
V1 = 5
V1 = 5
V1 = 6
//2nd attempt
-> tv1
Inital v1 = V1 = 1
V1 = 3
V1 = 4
V1 = 2
V1 = 4
V1 = 5
V1 = 3
V1 = 5
V1 = 6
But When i commented the second taskVarAdd() and tested it, i am getting the consistent expected results as follows
//1st Attempt
-> tv1
Inital v1 = 3
V1 = 4
V1 = 5
V1 = 6
V1 = 7
V1 = 8
V1 = 9
V1 = 10
V1 = 11
V1 = 12
V1 = 13
//2nd Attempt
-> tv1
Inital v1 = 3
V1 = 4
V1 = 5
V1 = 6
V1 = 7
V1 = 8
V1 = 9
V1 = 10
V1 = 11
V1 = 12
V1 = 13
We are using VxWork 5.5
My Questions:
Is it right to use two taskVarAdd() inside same task for same variable?
What will be the behavior if two taskVarAdd() is used for same variable inside same task?
Kindly explain the flow of your program
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说两次添加taskVarAdd是不对的:
首先,vxworks的实现没有处理这种情况。(我检查了vxworks源码,当两个taskvar具有相同的地址时,taskVar将设置两次会得到错误的值。由于 vxworks 是闭源代码,所以我不打算将源代码粘贴到这里,但这是 vxworks 的实现导致错误的)
第二,为什么需要添加两次? taskAddVar 为当前任务设置一个私有地址,当任务切换时,该值被保存到任务的 TCB 中; 当任务切换回来时,私有地址的值将被复制回来。 所以即使在同一个地址,不同的任务也会有不同的值。
vxworks通过添加一个switch hook来实现taskVar机制,使任务的私有变量在任务切换时保存/设置,这使得相同的例程但在不同的任务上下文中运行可以在同一地址具有不同的私有值。
I'd like to say it is not right to add taskVarAdd two times:
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)
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.
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.