-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.c
115 lines (102 loc) · 2.08 KB
/
time.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
#include <stdlib.h>
#include "runtime.h"
static int less(Event *, Event *);
static void up(Event **, int);
static int down(Event **, int, int);
static void push(Event **, Event *, int);
static Event *pop(Event **, int);
void timersinit(Timers *tp) {
tp->heap = malloc(64);
if (!tp->heap)
throw("timersinit: out of memory");
tp->len = 0;
tp->cap = 64;
}
long timerscheck(Timers *tp) {
Event *top;
long now, delay, size;
if (tp->len == 0)
return -1;
now = timens();
if (tp->len > 0) {
if (tp->cap - tp->len > 64) {
size = tp->len;
tp->heap = realloc(tp->heap, size);
tp->cap = size;
}
for (; tp->len > 0;) {
top = tp->heap[0];
delay = now - top->when;
if (delay < 0)
return -delay;
if (top->period > 0) {
top->when += top->period + delay;
down(tp->heap, 0, tp->len);
} else {
pop(tp->heap, tp->len);
tp->len--;
}
top->f(top->arg, delay);
}
}
return -1;
}
void timersopen(Timers *tp, Event *ep) {
int size;
if (tp->len >= tp->cap) {
size = tp->cap * 3 / 2; /* 1.5x */
tp->heap = realloc(tp->heap, size);
tp->cap = size;
}
push(tp->heap, ep, tp->len);
tp->len++;
}
static int less(Event *a, Event *b) {
return a->when < b->when;
}
static void up(Event **h, int i) {
Event *tmp;
int j;
for (;;) {
j = (i - 1) / 2; /* parent */
if (j == i || less(h[j], h[i]))
break;
tmp = h[i];
h[i] = h[j];
h[j] = tmp;
i = j;
}
}
static int down(Event **h, int i, int n) {
Event *tmp;
int t = i, j, j1, j2;
for (;;) {
j1 = 2 * i + 1; /* left child */
if (j1 >= n)
break;
j = j1;
j2 = j1 + 1; /* right child */
if (j2 < n && less(h[j2], h[j1]))
j = j2;
if (less(h[i], h[j]))
break;
tmp = h[i];
h[i] = h[j];
h[j] = tmp;
i = j;
}
return i > t;
}
static void push(Event **h, Event *ep, int n) {
h[n] = ep;
up(h, n);
}
static Event *pop(Event **h, int n) {
Event *ep;
if (n <= 0)
return NULL;
ep = h[0];
h[0] = h[n - 1];
down(h, 0, n);
return ep;
}