-
Notifications
You must be signed in to change notification settings - Fork 0
/
jv_timer.c
executable file
·165 lines (119 loc) · 3.9 KB
/
jv_timer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <jv_timer.h>
volatile jv_msec_t jv_current_msec;
jv_timer_t *jv_timer_create() {
jv_timer_t *timer;
jv_rbtree_t *rbtree;
if ((timer = malloc(sizeof(jv_timer_t))) == NULL) {
printf("jv_timer_create() failed\n");
return NULL;
}
if ((rbtree = jv_rbtree_create()) == NULL) {
printf("jv_timer_create() failed\n");
return NULL;
}
timer->rbtree = rbtree;
timer->alloc_count = 0;
timer->active_count = 0;
timer->idle_count = 0;
return timer;
}
jv_timer_task_t *jv_timer_add(jv_timer_t *timer, jv_msec_t delay, jv_msec_t period, jv_timer_schedule *schedule, void *data) {
jv_timer_task_t *task;
jv_rbtree_data_t rbtree_data;
if (delay == 0 && period == 0) {
printf("delay millisecond and period millisecond are zero, add timer failed\n");
return NULL;
}
if ((task = malloc(sizeof(jv_timer_task_t))) == NULL) {
printf("jv_timer_add() failed\n");
return NULL;
}
task->schedule = schedule;
task->data = data;
task->period = period;
task->trigger = jv_current_msec + delay + period;
rbtree_data.key = task->trigger;
rbtree_data.value = task;
if (jv_rbtree_insert(timer->rbtree, &rbtree_data) == JV_ERROR) {
free(task);
return NULL;
}
timer->alloc_count++;
timer->active_count++;
return task;
}
void jv_timer_del(jv_timer_t *timer, jv_timer_task_t *task) {
jv_rbtree_node_t *node;
node = jv_rbtree_get(timer->rbtree, task->trigger);
if (node != NULL) {
jv_rbtree_remove(timer->rbtree, node);
free(task);
}
}
void jv_timer_run(jv_timer_t *timer) {
jv_rbtree_t *rbtree;
jv_rbtree_node_t *node;
jv_msec_t trigger;
rbtree = timer->rbtree;
/* printf("1 rbtree->root:%lu, rbtree->sentinel:%lu\n", (unsigned long)rbtree->root, (unsigned long)rbtree->sentinel); */
if (rbtree->root == rbtree->sentinel) {
printf("no task in timer\n");
return;
}
/* while (1) { */
node = jv_rbtree_min(rbtree->root, rbtree->sentinel);
/*
if (node->left == rbtree->sentinel) {
jv_log_debug(timer->pool->log, JV_LOG_DEBUG, "no timing schedule task");
return;
} */
trigger = node->data->key;
/* printf("jv_current_msec :%d, trigger: %d\n", jv_current_msec, trigger); */
if (jv_current_msec >= trigger) { /* trigger timer */
jv_timer_task_t *task;
task = (jv_timer_task_t *) node->data->value;
task->schedule(task->data);
jv_rbtree_remove(timer->rbtree, node);
if (task->period > 0) { /* task is a periodic schedule, must be reschedule */
jv_rbtree_data_t rbtree_data;
task->trigger = jv_current_msec + task->period;
/* printf("trigger:%d\n", task->trigger); */
rbtree_data.key = task->trigger;
rbtree_data.value = task;
if (jv_rbtree_insert(timer->rbtree, &rbtree_data) == JV_ERROR) {
free(task);
}
} else { /* task is a one-time schedule */
printf("task is a one-time schedule.\n");
free(task);
}
/* } else {
return;
}*/
}
}
jv_msec_t jv_timer_mstimeout(jv_timer_t *timer) {
jv_rbtree_t *rbtree;
jv_rbtree_node_t *node;
jv_timer_task_t *task;
jv_int_t ms;
rbtree = timer->rbtree;
/* printf("2 rbtree->root:%lu, rbtree->sentinel:%lu\n", (unsigned long)rbtree->root, (unsigned long)rbtree->sentinel); */
if (rbtree->root == rbtree->sentinel) { /* 无定时器 */
printf("have no timer schedule\n");
return JV_INFTIM;
}
node = jv_rbtree_min(rbtree->root, rbtree->sentinel);
task = (jv_timer_task_t *) node->data->value;
ms = task->trigger - jv_current_msec;
return ms <= 0 ? 0 /* 无定时任务 */ : ms;
}
jv_int_t jv_timer_reset(jv_timer_t *timer, jv_timer_task_t *task) {
return JV_OK;
}
void jv_timer_cleanup(jv_timer_t *timer) {
}
void jv_timer_destroy(jv_timer_t *timer) {
free(timer->rbtree);
free(timer);
}