2 * runqueue.c - a simple task queueing/completion tracking helper
4 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 __runqueue_empty_cb(struct uloop_timeout *timeout)
26 struct runqueue *q = container_of(timeout, struct runqueue, timeout);
31 void runqueue_init(struct runqueue *q)
33 INIT_SAFE_LIST(&q->tasks_active);
34 INIT_SAFE_LIST(&q->tasks_inactive);
37 static void __runqueue_start_next(struct uloop_timeout *timeout)
39 struct runqueue *q = container_of(timeout, struct runqueue, timeout);
40 struct runqueue_task *t;
46 if (list_empty(&q->tasks_inactive.list))
49 if (q->max_running_tasks && q->running_tasks >= q->max_running_tasks)
52 t = list_first_entry(&q->tasks_inactive.list, struct runqueue_task, list.list);
53 safe_list_del(&t->list);
54 safe_list_add(&t->list, &q->tasks_active);
58 uloop_timeout_set(&t->timeout, t->run_timeout);
63 list_empty(&q->tasks_active.list) &&
64 list_empty(&q->tasks_inactive.list)) {
67 q->timeout.cb = __runqueue_empty_cb;
68 uloop_timeout_set(&q->timeout, 1);
73 static void runqueue_start_next(struct runqueue *q)
78 q->timeout.cb = __runqueue_start_next;
79 uloop_timeout_set(&q->timeout, 1);
82 static int __runqueue_cancel(void *ctx, struct safe_list *list)
84 struct runqueue_task *t;
86 t = container_of(list, struct runqueue_task, list);
87 runqueue_task_cancel(t, 0);
92 void runqueue_cancel_active(struct runqueue *q)
94 safe_list_for_each(&q->tasks_active, __runqueue_cancel, NULL);
97 void runqueue_cancel_pending(struct runqueue *q)
99 safe_list_for_each(&q->tasks_inactive, __runqueue_cancel, NULL);
102 void runqueue_cancel(struct runqueue *q)
104 runqueue_cancel_pending(q);
105 runqueue_cancel_active(q);
108 void runqueue_kill(struct runqueue *q)
110 struct runqueue_task *t;
112 while (!list_empty(&q->tasks_active.list)) {
113 t = list_first_entry(&q->tasks_active.list, struct runqueue_task, list.list);
114 runqueue_task_kill(t);
116 runqueue_cancel_pending(q);
117 uloop_timeout_cancel(&q->timeout);
120 void runqueue_task_cancel(struct runqueue_task *t, int type)
126 runqueue_task_complete(t);
131 if (t->cancel_timeout)
132 uloop_timeout_set(&t->timeout, t->cancel_timeout);
134 t->type->cancel(t->q, t, type);
138 __runqueue_task_timeout(struct uloop_timeout *timeout)
140 struct runqueue_task *t = container_of(timeout, struct runqueue_task, timeout);
143 runqueue_task_kill(t);
145 runqueue_task_cancel(t, t->cancel_type);
148 void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running)
150 struct safe_list *head;
155 if (!t->type->run && !running) {
156 fprintf(stderr, "BUG: inactive task added without run() callback\n");
162 head = &q->tasks_active;
164 head = &q->tasks_inactive;
167 t->timeout.cb = __runqueue_task_timeout;
169 safe_list_add(&t->list, head);
170 t->cancelled = false;
172 t->running = running;
175 runqueue_start_next(q);
178 void runqueue_task_kill(struct runqueue_task *t)
180 struct runqueue *q = t->q;
181 bool running = t->running;
186 runqueue_task_complete(t);
187 if (running && t->type->kill)
190 runqueue_start_next(q);
193 void runqueue_stop(struct runqueue *q)
198 void runqueue_resume(struct runqueue *q)
201 runqueue_start_next(q);
204 void runqueue_task_complete(struct runqueue_task *t)
206 struct runqueue *q = t->q;
212 t->q->running_tasks--;
214 safe_list_del(&t->list);
217 t->cancelled = false;
220 runqueue_start_next(t->q);
224 __runqueue_proc_cb(struct uloop_process *p, int ret)
226 struct runqueue_process *t = container_of(p, struct runqueue_process, proc);
228 runqueue_task_complete(&t->task);
231 void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t, int type)
233 struct runqueue_process *p = container_of(t, struct runqueue_process, task);
238 kill(p->proc.pid, type);
241 void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t)
243 struct runqueue_process *p = container_of(t, struct runqueue_process, task);
245 uloop_process_delete(&p->proc);
246 kill(p->proc.pid, SIGKILL);
249 static const struct runqueue_task_type runqueue_proc_type = {
251 .cancel = runqueue_process_cancel_cb,
252 .kill = runqueue_process_kill_cb,
255 void runqueue_process_add(struct runqueue *q, struct runqueue_process *p, pid_t pid)
261 p->proc.cb = __runqueue_proc_cb;
263 p->task.type = &runqueue_proc_type;
264 uloop_process_add(&p->proc);
265 if (!p->task.running)
266 runqueue_task_add(q, &p->task, true);