-
Notifications
You must be signed in to change notification settings - Fork 0
/
runqueue.c
282 lines (228 loc) · 6.23 KB
/
runqueue.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* runqueue.c - a simple task queueing/completion tracking helper
*
* Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include "runqueue.h"
static void
__runqueue_empty_cb(struct uloop_timeout *timeout)
{
struct runqueue *q = container_of(timeout, struct runqueue, timeout);
q->empty_cb(q);
}
void runqueue_init(struct runqueue *q)
{
INIT_SAFE_LIST(&q->tasks_active);
INIT_SAFE_LIST(&q->tasks_inactive);
}
static void __runqueue_start_next(struct uloop_timeout *timeout)
{
struct runqueue *q = container_of(timeout, struct runqueue, timeout);
struct runqueue_task *t;
do {
if (q->stopped)
break;
if (list_empty(&q->tasks_inactive.list))
break;
if (q->max_running_tasks && q->running_tasks >= q->max_running_tasks)
break;
t = list_first_entry(&q->tasks_inactive.list, struct runqueue_task, list.list);
safe_list_del(&t->list);
safe_list_add(&t->list, &q->tasks_active);
t->running = true;
q->running_tasks++;
if (t->run_timeout)
uloop_timeout_set(&t->timeout, t->run_timeout);
t->type->run(q, t);
} while (1);
if (!q->empty &&
list_empty(&q->tasks_active.list) &&
list_empty(&q->tasks_inactive.list)) {
q->empty = true;
if (q->empty_cb) {
q->timeout.cb = __runqueue_empty_cb;
uloop_timeout_set(&q->timeout, 1);
}
}
}
static void runqueue_start_next(struct runqueue *q)
{
if (q->empty)
return;
q->timeout.cb = __runqueue_start_next;
uloop_timeout_set(&q->timeout, 1);
}
static int __runqueue_cancel(void *ctx, struct safe_list *list)
{
struct runqueue_task *t;
t = container_of(list, struct runqueue_task, list);
runqueue_task_cancel(t, 0);
return 0;
}
void runqueue_cancel_active(struct runqueue *q)
{
safe_list_for_each(&q->tasks_active, __runqueue_cancel, NULL);
}
void runqueue_cancel_pending(struct runqueue *q)
{
safe_list_for_each(&q->tasks_inactive, __runqueue_cancel, NULL);
}
void runqueue_cancel(struct runqueue *q)
{
runqueue_cancel_pending(q);
runqueue_cancel_active(q);
}
void runqueue_kill(struct runqueue *q)
{
struct runqueue_task *t;
while (!list_empty(&q->tasks_active.list)) {
t = list_first_entry(&q->tasks_active.list, struct runqueue_task, list.list);
runqueue_task_kill(t);
}
runqueue_cancel_pending(q);
uloop_timeout_cancel(&q->timeout);
}
void runqueue_task_cancel(struct runqueue_task *t, int type)
{
if (!t->queued)
return;
if (!t->running) {
runqueue_task_complete(t);
return;
}
t->cancelled = true;
if (t->cancel_timeout)
uloop_timeout_set(&t->timeout, t->cancel_timeout);
if (t->type->cancel)
t->type->cancel(t->q, t, type);
}
static void
__runqueue_task_timeout(struct uloop_timeout *timeout)
{
struct runqueue_task *t = container_of(timeout, struct runqueue_task, timeout);
if (t->cancelled)
runqueue_task_kill(t);
else
runqueue_task_cancel(t, t->cancel_type);
}
static void _runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running, bool first)
{
struct safe_list *head;
if (t->queued)
return;
if (!t->type->run && !running) {
fprintf(stderr, "BUG: inactive task added without run() callback\n");
return;
}
if (running) {
q->running_tasks++;
head = &q->tasks_active;
} else {
head = &q->tasks_inactive;
}
t->timeout.cb = __runqueue_task_timeout;
t->q = q;
if (first)
safe_list_add_first(&t->list, head);
else
safe_list_add(&t->list, head);
t->cancelled = false;
t->queued = true;
t->running = running;
q->empty = false;
runqueue_start_next(q);
}
void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running)
{
_runqueue_task_add(q, t, running, 0);
}
void runqueue_task_add_first(struct runqueue *q, struct runqueue_task *t, bool running)
{
_runqueue_task_add(q, t, running, 1);
}
void runqueue_task_kill(struct runqueue_task *t)
{
struct runqueue *q = t->q;
bool running = t->running;
if (!t->queued)
return;
runqueue_task_complete(t);
if (running && t->type->kill)
t->type->kill(q, t);
runqueue_start_next(q);
}
void runqueue_stop(struct runqueue *q)
{
q->stopped = true;
}
void runqueue_resume(struct runqueue *q)
{
q->stopped = false;
runqueue_start_next(q);
}
void runqueue_task_complete(struct runqueue_task *t)
{
struct runqueue *q = t->q;
if (!t->queued)
return;
if (t->running)
t->q->running_tasks--;
uloop_timeout_cancel(&t->timeout);
safe_list_del(&t->list);
t->queued = false;
t->running = false;
t->cancelled = false;
if (t->complete)
t->complete(q, t);
runqueue_start_next(q);
}
static void
__runqueue_proc_cb(struct uloop_process *p, int ret)
{
struct runqueue_process *t = container_of(p, struct runqueue_process, proc);
runqueue_task_complete(&t->task);
}
void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t, int type)
{
struct runqueue_process *p = container_of(t, struct runqueue_process, task);
if (!type)
type = SIGTERM;
kill(p->proc.pid, type);
}
void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t)
{
struct runqueue_process *p = container_of(t, struct runqueue_process, task);
uloop_process_delete(&p->proc);
kill(p->proc.pid, SIGKILL);
}
static const struct runqueue_task_type runqueue_proc_type = {
.name = "process",
.cancel = runqueue_process_cancel_cb,
.kill = runqueue_process_kill_cb,
};
void runqueue_process_add(struct runqueue *q, struct runqueue_process *p, pid_t pid)
{
if (p->proc.pending)
return;
p->proc.pid = pid;
p->proc.cb = __runqueue_proc_cb;
if (!p->task.type)
p->task.type = &runqueue_proc_type;
uloop_process_add(&p->proc);
if (!p->task.running)
runqueue_task_add(q, &p->task, true);
}