-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtimeout.c
160 lines (129 loc) · 3.7 KB
/
timeout.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2020 embedd.ch
* Copyright (C) 2020 Felix Fietkau <[email protected]>
* Copyright (C) 2020 John Crispin <[email protected]>
*/
#include <string.h>
#include <libubox/utils.h>
#include "timeout.h"
static int usteer_timeout_cmp(const void *k1, const void *k2, void *ptr)
{
uint32_t ref = (uint32_t) (intptr_t) ptr;
int32_t t1 = (uint32_t) (intptr_t) k1 - ref;
int32_t t2 = (uint32_t) (intptr_t) k2 - ref;
if (t1 < t2)
return -1;
else if (t1 > t2)
return 1;
else
return 0;
}
static int32_t usteer_timeout_delta(struct usteer_timeout *t, uint32_t time)
{
uint32_t val = (uint32_t) (intptr_t) t->node.key;
return val - time;
}
static void usteer_timeout_recalc(struct usteer_timeout_queue *q, uint32_t time)
{
struct usteer_timeout *t;
int32_t delta;
if (avl_is_empty(&q->tree)) {
uloop_timeout_cancel(&q->timeout);
return;
}
t = avl_first_element(&q->tree, t, node);
delta = usteer_timeout_delta(t, time);
if (delta < 1)
delta = 1;
uloop_timeout_set(&q->timeout, delta);
}
static uint32_t ampgr_timeout_current_time(void)
{
struct timespec ts;
uint32_t val;
clock_gettime(CLOCK_MONOTONIC, &ts);
val = ts.tv_sec * 1000;
val += ts.tv_nsec / 1000000;
return val;
}
static void usteer_timeout_cb(struct uloop_timeout *timeout)
{
struct usteer_timeout_queue *q;
struct usteer_timeout *t, *tmp;
bool found;
uint32_t time;
q = container_of(timeout, struct usteer_timeout_queue, timeout);
do {
found = false;
time = ampgr_timeout_current_time();
avl_for_each_element_safe(&q->tree, t, node, tmp) {
if (usteer_timeout_delta(t, time) > 0)
break;
usteer_timeout_cancel(q, t);
if (q->cb)
q->cb(q, t);
found = true;
}
} while (found);
usteer_timeout_recalc(q, time);
}
void usteer_timeout_init(struct usteer_timeout_queue *q)
{
avl_init(&q->tree, usteer_timeout_cmp, true, NULL);
q->timeout.cb = usteer_timeout_cb;
}
static void __usteer_timeout_cancel(struct usteer_timeout_queue *q,
struct usteer_timeout *t)
{
avl_delete(&q->tree, &t->node);
}
void usteer_timeout_set(struct usteer_timeout_queue *q, struct usteer_timeout *t,
int msecs)
{
uint32_t time = ampgr_timeout_current_time();
uint32_t val = time + msecs;
bool recalc = false;
q->tree.cmp_ptr = (void *) (intptr_t) time;
if (usteer_timeout_isset(t)) {
if (avl_is_first(&q->tree, &t->node))
recalc = true;
__usteer_timeout_cancel(q, t);
}
t->node.key = (void *) (intptr_t) val;
avl_insert(&q->tree, &t->node);
if (avl_is_first(&q->tree, &t->node))
recalc = true;
if (recalc)
usteer_timeout_recalc(q, time);
}
void usteer_timeout_cancel(struct usteer_timeout_queue *q,
struct usteer_timeout *t)
{
if (!usteer_timeout_isset(t))
return;
__usteer_timeout_cancel(q, t);
memset(&t->node.list, 0, sizeof(t->node.list));
}
void usteer_timeout_flush(struct usteer_timeout_queue *q)
{
struct usteer_timeout *t, *tmp;
uloop_timeout_cancel(&q->timeout);
avl_remove_all_elements(&q->tree, t, node, tmp) {
memset(&t->node.list, 0, sizeof(t->node.list));
if (q->cb)
q->cb(q, t);
}
}