解析Contiki系统protothread的预编译C代码
来源:互联网 发布:修车技术资料大全软件 编辑:程序博客网 时间:2024/05/22 14:16
Contiki操作系统最大的特点(对于嵌入式系统也是最大的优点)是它支持protothread进程,这样一来多个进程可以共享一个栈,以达到最大限度节省内存的目的。
Protothread是用一系列的宏定义API来实现的,不过,做为一个成熟的程序员,我们还是需要了解这些API后面的“真面目”,只有这样才能深入理解protothread的工作原理,同时才能开发正确高效的程序。
现在贴出一个protothread代码和预编译后的C代码,让我们一起看看它是怎样实现的。为了保证正确性,这两份代码都在Contiki-V2.7系统中编译和调试通过。
一个细节:预编译后的C代码中的case (__LINE__ - 1)为什么要添加“减一”呢?答案在本博客的最后面哦。
/*------------------------------------- Contiki Codes --------------------------------------*/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
PROCESS_THREAD(hello_world_process, ev, data)
{
clock_time_t tClock;
static struct etimer timer;
PROCESS_BEGIN();
etimer_set(&timer, CLOCK_CONF_SECOND / 2); /* 500ms */
while (1)
{
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
tClock = clock_time();
PRINT_STR("Hello, world! ");
PRINTF(NULL, tClock, PRINTF_FORMAT_DEC);
PRINT_STR("\r\n");
etimer_reset(&timer);
}
PROCESS_END();
}
/*------------------------------------- Convert to C Codes --------------------------------------*/
static char
process_thread_hello_world_process( struct pt *process_pt,
process_event_t ev,
process_data_t data );
struct process hello_world_process =
{
NULL,
"Hello world process",
process_thread_hello_world_process
};
struct process * const autostart_processes[] =
{
&hello_world_process,
NULL
};
static char
process_thread_hello_world_process( struct pt *process_pt,
process_event_t ev,
process_data_t data )
{
clock_time_t tClock;
static struct etimer timer;
{
char PT_YIELD_FLAG = 1;
if (PT_YIELD_FLAG)
{
;
}
switch ((process_pt)->lc)
{
case 0:
etimer_set(&timer, CLOCK_CONF_SECOND / 2); /* 500ms */
while (1)
{
do
{
PT_YIELD_FLAG = 0;
(process_pt)->lc = __LINE__;
case (__LINE__ - 1):
if ((PT_YIELD_FLAG == 0) || !(ev == PROCESS_EVENT_TIMER))
{
return PT_YIELDED;
}
} while(0);
tClock = clock_time();
PRINT_STR("Hello, world! ");
PRINTF(NULL, tClock, PRINTF_FORMAT_DEC);
PRINT_STR("\r\n");
etimer_reset(&timer);
}/*while*/
}/*switch*/
PT_YIELD_FLAG = 0;
(process_pt)->lc = 0;
return PT_ENDED;
}
}
答案:对于C编译器,__LINE__总是代表当前行数。case语句中的行数必须与前一句代码 (process_pt)->lc = __LINE__中的__LINE__是同一个数值,否则将无法正确运行,2个__LINE__相差为1,所以要减一。
- 解析Contiki系统protothread的预编译C代码
- 从ProtoThread开始到contiki的学习
- [Contiki系列论文之3]Protothread:简化内存受限系统上的事件驱动编程
- C语言预编译的作用解析
- Contiki:Protothread切换机制理解
- 四.Contiki之Protothread机制
- contiki学习笔记-udp-server.c文件详细的解析
- C/C++的预编译宏做成代码模版使用
- C语言的预编译
- C语言的预编译
- C语言的预编译
- C语言的预编译
- C语言的预编译
- Protothread机制文档(contiki-2.6\doc\pt-doc.txt)
- 学习boost预编译的代码
- 学习boost预编译的代码:查表法
- vs查看预编译后的代码
- C++ 预编译解析
- 抓交通事故肇事犯
- 黑马程序员——Java编程语言学习总结 – Collection集合框架
- RFID系统构成及其功能
- Objective-C学习之旅(十)----Blocks语法
- ulimit 的说明及学习
- 解析Contiki系统protothread的预编译C代码
- spring4.2更好的应用事件
- WebRTC 入门 01 简介 III
- 数据延迟加载
- swift UITabBarItem修改字体颜色和图标颜色
- height、clientHeight、scrollHeight、offsetHeight区别
- ng-repeat
- 产品经理模型建设工具axure
- 黑马程序员——Java编程语言学习总结 –Map集合