[资料] ProtoThreads如何工作的(转)
1078 查看
19 回复
 楼主 | 发布于 2018-08-28 | 只看楼主
分享到:

最近把手头的SD卡更新NKNAND项目完成,总算抽空上网瞎逛游了。正好群里面有人推荐了一个系统,说是对小资源的单片机非常适合。这里我就推荐2篇网文:

http://www.daxia.com/bibis/moredata30_1234764_43130.shtml

http://blog.csdn.net/t_larry/archive/2008/10/18/3097191.aspx

2篇网文一个讲系统简介,一个是里面宏的说明。熟悉一下是很不错的。

然后就是官网了:

http://www.sics.se/~adam/pt/index.html

我翻译了里面的under the hood 章节:How protothreads really work

(0 ) (0 )
回复 举报

楼主 | 回复于 2018-08-28 11#

Moving past the while(1) line, we see that the PT_WAIT_UNTIL macro has been expanded into something that contains the number 12. The pt->lc variable is set to 12, and a case 12: statement follows right after the assignment. After this, the counter variable is checked to see if it has reached 1000 or not. If not, the example() function now executed an explicit return! This all may seem surprising: where did the number 12 come from and why does the function return in the middle of the while(1) loop? To understand this we need to take a look at how the code is executed in the example() function the next time it is called.

越过while(1)那行代码,我们看到PT_WAIT_UNTIL宏已经展开一些包含数字12的内容。pt->lc的值被设置为12,同时一个case 12:部分紧随后面。在此之后,对变量counter的值进行检测看它是否达到了1000。如果没有,example()函数执行一个明确的返回。这一切看起来令人吃惊:这个数字12是哪里来的同时为什么这个函数在while(1)循环中跳出?要理解这一点我们需要看看代码在example()函数里面是怎么运行的。

 

The next time the example() function is called from the main() function, the pt->lc variable will not be zero but 12, as it was set in the expansion of the PT_WAIT_UNTIL macro. This makes the switch(pt->lc) jump to the case 12: statement. This statement is just before the if statement where counter variable is checked to see if it has reached 1000! So the counter variable is checked again. If it has not reached 1000, the example() function returns again. The next time the function is invoked, the switch jumps to the case 12: again and reevaluates the counter == 1000 statement. It will continue to do so until the counter variable reaches 1000. Then, the printf statement is executed and the counter variable is set to zero, before the while(1) loop loops again.

下次example()函数被main()函数调用,变量pt->lc的值就不是0而是12,因为它在PT_WAIT_UNTIL宏里面已经被设置过。这使得switch(pt->lc)跳转到case 12:部分。这部分是在判断变量counter值是否达到1000这个if语句前面。所以变量counter再次被检测。如果它没有达到1000example()函数就继续返回。下次这个函数被调用,这个switch语句就继续跳转到case 12:部分并且继续判断counter==1000。它会一直这样运行下去直到变量counter达到1000。然后在while(1)循环继续循环之前printf()语句被执行并且counter被置0
(0 )
评论 (0) 举报

楼主 | 回复于 2018-08-28 12#

But where did the number 12 come from? It is the line number of the PT_WAIT_UNTIL statement (check it by counting lines in the original program on your screen!). The nice thing with line numbers are that they are monotonically increasing. That is, if we put another PT_WAIT_UNTIL statement later in our program, the line number will be different from the first PT_WAIT_UNTIL statement. Therefore, the switch(pt->lc) knows exactly where to jump - there are no ambiguities.

但是这个数字12是哪里来的?它是PT_WAIT_UNTIL宏的代码段数字(检查你程序里面的源程序行就知道了)。用行号的好处就是他们是单调递增的。也就是说,如果我们在项目里面添加另外一个PT_WAIT_UNTIL申明,行号会与第一个PT_WAIT_UNTIL声明有所不同。因此,这个switch(pt->lc)明确知道跳转到哪里。

 

Did you notice anything slightly strange with the code above? The switch(pt->lc) statement jumped right into the while(1) loop. The case 12: statement was inside the while(1) loop! Does this really work? Yes, it does in fact work! This feature of the C programming language was probably first found by Tom Duff in his wonderful programming trick dubbed Duff's Device. The same trick has also been used by Simon Tatham to implement coroutines in C (a terrific piece of code).

你有没有发现上面的代码很奇怪呢?switch(pt->lc)语句跳转到while(1)循环的右边。Case 12:部分在while(1)循环里面。这能工作吗?是的,他确实能工作!C语言的这种功能是被Tom Duff在他的项目里面首次发现的,这种戏法被称作为Duff`s Device。同样的把戏也被Simon Tatham

C实现协同程序(了不起的代码)。

 

Ok, now that you know how protothreads work inside its wicked macros, download the library and try it out for yourself!

好了,现在知道protothreads是如何利用它怪异的宏来工作的吧,下载库并且自己尝试一下吧。
(0 )
评论 (0) 举报

回复于 2018-08-29 13#

感谢分享;
(0 )
评论 (0) 举报

回复于 2018-08-30 14#

谢谢分享!!!!!!!!!!!
(0 )
评论 (0) 举报

回复于 2018-08-30 15#

谢谢分享!!!
(0 )
评论 (0) 举报

楼主 | 回复于 2018-08-30 16#

不客气,一起学习
(0 )
评论 (0) 举报

楼主 | 回复于 2018-08-30 17#

(0 )
评论 (0) 举报

回复于 2018-08-30 18#

谢谢分享!!!
(0 )
评论 (0) 举报

回复于 2018-09-15 19#

支持下,谢谢分享
(0 )
评论 (0) 举报
发表回复
0/3000





举报

请选择举报类别

  • 广告垃圾
  • 违规内容
  • 恶意灌水
  • 重复发帖

全部板块

返回顶部