Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15244
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 54
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 08:58
个人简介

Be better,do best.

文章分类

全部博文(4)

文章存档

2014年(4)

我的朋友

分类: C/C++

2014-04-04 15:54:05

pjlib是一个用C实现的非常好的开源基础库,日前研究了一下pjlib的一些组件。

pjlib的timer heap似乎不支持循环定时器(目前我还未看到),修改源码应该可以实现,但现在我先从应用的角度,研究了一下使用timer heap的接口实现一个循环定时器。

代码如下,实现细节可参考注释。

点击(此处)折叠或打开

  1. /*****************************************************************************
  2.  函 数 名 : printTimeStamp
  3.  功能描述 : 打印时间戳
  4.  输入参数 :
  5.  输出参数 :
  6.  返 回 值 : static void
  7. *****************************************************************************/
  8. static void printTimeStamp(void)
  9. {
  10.     pj_time_val tv;
  11.     pj_parsed_time pt;

  12.     pj_gettimeofday(&tv);
  13.     pj_time_decode(&tv, &pt);
  14.     PJ_LOG(3,("xxx",
  15.      "...%04d-%02d-%02d %02d:%02d:%02d.%03d",
  16.      pt.year, pt.mon, pt.day,
  17.      pt.hour, pt.min, pt.sec, pt.msec));
  18.     
  19.     return;
  20. }

  21. /*****************************************************************************
  22.  函 数 名 : test_timer_pthread
  23.  功能描述 : 定时器线程回调函数
  24.  输入参数 :
  25.  输出参数 :
  26.  返 回 值 : static void *
  27. *****************************************************************************/
  28. static void *test_timer_pthread(void *parg)
  29. {
  30.     int i = 0;
  31.     pj_status_t rc;

  32.     delay.sec = 1;
  33.     delay.msec = 0;
  34.     
  35.     printf("enter thread test_timer_pthread.........\n");

  36.     /* 增加一个timer entry */
  37.     pj_timer_heap_schedule(timer, &entry[1], &delay);
  38.     while (1)
  39.     {
  40.         /* 询问定时器是否到期,到期则执行回调
  41.          * 定时器到期rc返回值为1,表示有一个定时器到期 */
  42.         rc = pj_timer_heap_poll(timer, NULL);
  43.         if (1 != rc)
  44.             continue;
  45.         /* 这里再次poll,为了让定时器到期数减为0,即rc值变为0 */
  46.         rc = pj_timer_heap_poll(timer, NULL);
  47.         /* 再次加入相同的timer entry,实现循环定时器 */
  48.         pj_timer_heap_schedule(timer, &entry[1], &delay);
  49.     }

  50.     return NULL;
  51. }

  52. /*****************************************************************************
  53.  函 数 名 : test_create_timer_pthread
  54.  功能描述 : 创建定时器线程
  55.  输入参数 :
  56.  输出参数 :
  57.  返 回 值 : static void
  58. *****************************************************************************/
  59. static void test_create_timer_pthread(void)
  60. {
  61.     int lRet = -1;
  62.     int lUsamPthreadId = -1;

  63.     lRet = pthread_create( &lUsamPthreadId, NULL, test_timer_pthread, NULL );
  64.     if (0 != lRet)
  65.     {
  66.         printf("pthread_create lUsamPthreadId error: %d\n", lRet);
  67.         return;
  68.     }
  69.     /* 设置子线程为分离态 */
  70.     pthread_detach(lUsamPthreadId);

  71.     return;
  72. }

  73. /*****************************************************************************
  74.  函 数 名 : timer_callback
  75.  功能描述 : 定时器回调函数
  76.  输入参数 :
  77.  输出参数 :
  78.  返 回 值 : static void
  79. *****************************************************************************/
  80. static void timer_callback(pj_timer_heap_t *ht, pj_timer_entry *e)
  81. {
  82.     if (NULL == e)
  83.     {
  84.         printf("NULL e ptr!!!!\n");
  85.         
  86.     return;
  87.     }

  88.     printf("user_data = %d\n", *(int *)(e->user_data));
  89.     printTimeStamp();
  90. }
  91. static int testUserDataA[MAX_COUNT] = {0};

  92. /*****************************************************************************
  93.  函 数 名 : test_timer_heap_k
  94.  功能描述 : 测试定时器堆
  95.  输入参数 : void
  96.  输出参数 :
  97.  返 回 值 : static int
  98. *****************************************************************************/
  99. static int test_timer_heap_k(void)
  100. {
  101.     int i, j;
  102.     pj_pool_t *pool;
  103.     pj_time_val timout_kk;
  104.     pj_status_t rc; int err=0;
  105.     unsigned size, count;
  106.     pj_time_val now, expire;

  107.     size = pj_timer_heap_mem_size(MAX_COUNT)+MAX_COUNT*sizeof(pj_timer_entry);
  108.     pool = pj_pool_create( mem, NULL, size, 4000, NULL);
  109.     if (!pool) {
  110.     PJ_LOG(3,("test", "...error: unable to create pool of %u bytes",
  111.          size));
  112.     return -10;
  113.     }

  114.     /* 为timer entry分配空间 */
  115.     entry = (pj_timer_entry*)pj_pool_calloc(pool, MAX_COUNT, sizeof(*entry));
  116.     if (!entry)
  117.     return -20;

  118.     for (i=0; i<MAX_COUNT; ++i) {
  119.     testUserDataA[i] = i;
  120.     /* 注册timer entry的回调函数 */
  121.     pj_timer_entry_init(entry + i, i, &testUserDataA[i], timer_callback);
  122.     }

  123.     /* 创建定时器堆,设置定时器堆pool大小 */
  124.     rc = pj_timer_heap_create(pool, MAX_COUNT, &timer);
  125.     if (rc != PJ_SUCCESS) {
  126.         app_perror("...error: unable to create timer heap", rc);
  127.     return -30;
  128.     }
  129.     
  130.     /* 创建定时器线程,用于poll */
  131.     test_create_timer_pthread();
  132.     
  133.     return err;
  134. }

阅读(2231) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:查看CPU是否支持虚拟化

给主人留下些什么吧!~~