1、前言
由于项目需要用到毫秒级定时器,之前看过thttpd源码,知道有个定时器,所以把其定时器移植到海思平台,记录一下。
2、源码下载
thttpd源码下载 http://www.acme.com/software/thttpd/
3、编译
单独把timers.h和timers.c单独提取出来,做成动态库形式。
4、应用举例
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "timers.h"
static void timefun( ClientData client_data, struct timeval* nowP )
{
static int count = 0;
struct timeval tv;
tmr_prepare_timeval( &tv );
printf("timefun count=%03d tv_sec=%lu ms=%lu\n", count++, tv.tv_sec, tv.tv_usec/1000);
}
ClientData JunkClientData;
int main()
{
tmr_init();
if(tmr_create( (struct timeval*) 0, timefun, JunkClientData, 1 * 1000L, 1 ) == (Timer*) 0 )
{
printf("tmr_create(occasional) failed" );
return 0;
}
struct timeval tv;
static int flag = 1 ;
tmr_prepare_timeval( &tv );
printf("tv_sec=%lu tv_usec=%lu\n", tv.tv_sec, tv.tv_usec);
while(1)
{
tmr_prepare_timeval( &tv );
tmr_run( &tv );
usleep(10);
}
tmr_destroy();
return 0;
}
运行结果如下: