forked from unbit/uwsgi
/
rb_timers.c
64 lines (40 loc) · 1.2 KB
/
rb_timers.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
/*
uWSGI timers implementation
based on Linux hrtimers and nginx timeout management.
rbtree with allowed duplicate keys is used
*/
#include "uwsgi.h"
struct rb_root *uwsgi_init_rb_timer() {
struct rb_root *u_rb_root = uwsgi_malloc(sizeof(struct rb_root));
u_rb_root->rb_node = NULL;
return u_rb_root;
}
struct uwsgi_rb_timer *uwsgi_min_rb_timer(struct rb_root *root) {
struct rb_node *node = root->rb_node;
if (node == NULL) return NULL;
while(node->rb_left != NULL) {
node = node->rb_left;
}
return ((struct uwsgi_rb_timer *) node);
}
struct uwsgi_rb_timer *uwsgi_add_rb_timer(struct rb_root *root, time_t key, void *data) {
struct uwsgi_rb_timer *urbt = uwsgi_malloc(sizeof(struct uwsgi_rb_timer));
struct rb_node **p = &root->rb_node, *parent = NULL;
struct uwsgi_rb_timer *current_rb_timer;
memset(urbt, 0, sizeof(struct uwsgi_rb_timer));
urbt->key = key;
urbt->data = data;
while(*p) {
parent = *p;
current_rb_timer = (struct uwsgi_rb_timer *) parent;
if (key <= current_rb_timer->key) {
p = &(*p)->rb_left;
}
else {
p = &(*p)->rb_right;
}
}
rb_link_node((struct rb_node *) urbt, parent, p);
rb_insert_color((struct rb_node *) urbt, root);
return urbt;
}