抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

参考文章

算法图示说明

代码,仅为思路参考

c文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @brief 静态定时器队列初始化
* @param timer_node_cycle_queue 初始化对象
* @retval none
*/
void static_single_cycle_queue_timer_init(struct timer_node_cycle_queue_struct *timer_node_cycle_queue)
{
/*!< 初始化等待链表信息 */
uint32_t i = 0;
static_link_list_init(&timer_node_cycle_queue->timer_node_wait_list);
for (i = 0; i < TIMER_NODE_COUNT_MAX_LEN; i++)
{
static_link_list_init(&timer_node_cycle_queue->timer_node_count[i]);
}
}
/**
* @brief
* @param timer_node_cycle_queue 操作的定时器队列对象
* @retval
*/
void static_single_cycle_queue_timer_run(struct timer_node_cycle_queue_struct *timer_node_cycle_queue)
{
struct timer_node_struct *timer;
//判断计时队列
do
{
timer = (struct timer_node_struct *)static_link_get_node(&timer_node_cycle_queue->timer_node_count[timer_node_cycle_queue->timer_node_counter],1);
if (timer != 0)
{
if(timer->is_right_now && timer->call_back != 0)
{
timer->call_back(timer->param);
}
timer->is_timeout = 1;
static_link_list_delete_node(&timer_node_cycle_queue->timer_node_count[timer_node_cycle_queue->timer_node_counter],1);
}
}while (timer != 0);

timer_node_cycle_queue->timer_node_counter++;
timer_node_cycle_queue->timer_node_counter %= TIMER_NODE_COUNT_MAX_LEN;
//判断等待队列
static uint32_t wait_list_counter = 0;
do
{
timer = (struct timer_node_struct *)static_link_get_node(&timer_node_cycle_queue->timer_node_wait_list,wait_list_counter + 1); //获取链表中的定时器
if (timer != 0)
{
timer->wait_tick--;
if (timer->wait_tick < TIMER_NODE_COUNT_MAX_LEN)
{
//注册到计时队列上
static_link_list_node_register(&timer_node_cycle_queue->timer_node_count[(timer_node_cycle_queue->timer_node_counter + timer->wait_tick) % TIMER_NODE_COUNT_MAX_LEN],(struct static_link_list_node_struct *)timer);
//定时器出链表
static_link_list_delete_node(&timer_node_cycle_queue->timer_node_wait_list,wait_list_counter + 1);
}
wait_list_counter++;
}else
{
wait_list_counter = 0;
}
}while (timer != 0);
}
void static_single_timer_register(struct timer_node_cycle_queue_struct *timer_node_cycle_queue,
{
timer->wait_tick = time;
timer->is_timeout = 0;
timer->is_right_now = is_right_now;
timer->param = param;
timer->call_back = call_back;
if(time / TIMER_NODE_COUNT_MAX_LEN == 0)
{
static_link_list_node_register(&timer_node_cycle_queue->timer_node_count[time % TIMER_NODE_COUNT_MAX_LEN],(struct static_link_list_node_struct *)timer);
}else
{
static_link_list_node_register(&timer_node_cycle_queue->timer_node_wait_list,(struct static_link_list_node_struct *)timer);
}
}

h文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef __STATIC_SINGLE_CYCLE_QUEUE_TIMER_COMMON_H__
#define __STATIC_SINGLE_CYCLE_QUEUE_TIMER_COMMON_H__
#include "stdint.h"
#include "static_link_list/static_link_list.h"

#define TIMER_NODE_COUNT_MAX_LEN 8 /*!< 计时队列的长度 */
#define TIMER_NODE_COUNT_MAX_LEN_POWER 3 /*!< 这个是计时队列的幂指数,2为基数 */

/*!< 时钟节点 */
struct timer_node_struct
{
struct static_link_list_node_struct static_link_list_node; /*!< 继承链表节点 */
uint32_t wait_tick; /*!< 需要等待的时间,单位为定时器的滴答时间 */
int32_t (*call_back)(void *); /*!< 回调函数 */
void *param; /*!< 定时器回调函数自身使用参数 */
uint8_t is_right_now; /*!< 是否马上执行callback函数 */
uint8_t is_timeout; /*!< 定时器已到时间 */
};
/*!< 循环队列对象 */
struct timer_node_cycle_queue_struct
{
struct static_link_list_struct timer_node_wait_list;
struct static_link_list_struct timer_node_count[ TIMER_NODE_COUNT_MAX_LEN ];
uint32_t timer_node_counter;
};

评论