本帖最后由 原来的你 于 2017-3-11 21:29 编辑
上节给大家介绍了MOE操作系统在LPC54608上面的知识,本节介绍一下PT协程原理。
/* PT init, use this macro at the beginning of PT task */
#define PT_INIT() static unsigned char sg_u8RunPoint = 0;
/* PT begin, use it with PT_END to contain the pt process */
#define PT_BEGIN() unsigned char u8YieldFlag = 1;\
switch(sg_u8RunPoint)\
{\
case 0:
/* PT end, use it with PT_BEGIN to contain the pt process */
#define PT_END() }\
u8YieldFlag = 0;\
sg_u8RunPoint = 0;
/* PT yield, return from process */
#define PT_YIELD() do\
{\
u8YieldFlag = 0;\
sg_u8RunPoint = __LINE__;case __LINE__:\
if(0 == u8YieldFlag)\
{\
return SW_OK;\
}\
}\
while(0)
/* PT yield until a condition */
#define PT_YIELD_UNTIL(c) do\
{\
u8YieldFlag = 0;\
sg_u8RunPoint = __LINE__;case __LINE__:\
if((0 == u8YieldFlag) || !(c))\
{\
return SW_OK;\
}\
}\
while(0)
/* Wait a condition */
#define PT_WAIT_UNTIL(c) do\
{\
u8YieldFlag = 0;\
sg_u8RunPoint = __LINE__;case __LINE__:\
if(!(c))\
{\
return SW_OK;\
}\
}\
while(0)
/* Wait for calling */
#define PT_WAIT_CALL() PT_YIELD()
/* Wait an event */
#define PT_WAIT_EVENT(e) PT_YIELD_UNTIL(e == u8Evt)
/* Wait a condition */
#define PT_WAIT_COND(c) PT_YIELD_UNTIL(c)
/* Check a condition */
#define PT_CHECK_COND(c) PT_WAIT_UNTIL(c)
/* Return and recall after delay */
#define PT_DELAY(t) Moe_Timer_Delay(t);\
PT_WAIT_EVENT(EVENT_DELAY)
/* Return and recall as soon as possible */
#define PT_BREAK() Moe_Event_Set(sg_u8TaskID, EVENT_BREAK, MOE_EVENT_NORMAL, NULL);\
PT_YIELD()
/* Wait for a message event */
#define PT_WAIT_MSG() PT_WAIT_EVENT(EVENT_MSG)
#define PTV static
PT_INIT() 表示: 初始化一个PT协程,就是初始化状态变量。
PT_BEGIN() 表示:PT协程入口点, u8YieldFlag =0表示出让,=1表示不出让,放在 switch 语句前面,下次调用的时候可以跳转到上次出让点继续执行。
PT_END() 表示:PT协程退出点,到此一个PT协程算是结束,清空所有上下文和标志。
PT_YIELD()表示:PT协程出让点,如果这时协程状态变量 sg_u8RunPoint,已经变为 __LINE__ 跳转过来的,那么 u8YieldFlag = 1,表示从出让点继续执行。
PT_YIELD_UNTIL(c) 表示:PT协程出让点附加出让条件。
PT_WAIT_UNTIL(c)表示:PT协程阻塞点,其实实质上和PT_YIELD_UNTIL一样,只是退出条件不同,用来同步信号。
对于PT协程使用时必须成对出现的,就像上面的一样。
|