13ab864e8671fdd412a062d60bcb1b5c6a188638
[project/libubox.git] / tests / test-runqueue.c
1 /*
2 * runqueue-example.c
3 *
4 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include "uloop.h"
24 #include "runqueue.h"
25
26 static struct runqueue q;
27
28 struct sleeper {
29 struct runqueue_process proc;
30 int val;
31 };
32
33 static void q_empty(struct runqueue *q)
34 {
35 fprintf(stderr, "All done!\n");
36 uloop_end();
37 }
38
39 static void q_sleep_run(struct runqueue *q, struct runqueue_task *t)
40 {
41 struct sleeper *s = container_of(t, struct sleeper, proc.task);
42 char str[32];
43 pid_t pid;
44
45 fprintf(stderr, "[%d/%d] start 'sleep %d'\n", q->running_tasks, q->max_running_tasks, s->val);
46
47 pid = fork();
48 if (pid < 0)
49 return;
50
51 if (pid) {
52 runqueue_process_add(q, &s->proc, pid);
53 return;
54 }
55
56 sprintf(str, "%d", s->val);
57 execlp("sleep", "sleep", str, NULL);
58 exit(1);
59 }
60
61 static void q_sleep_cancel(struct runqueue *q, struct runqueue_task *t, int type)
62 {
63 struct sleeper *s = container_of(t, struct sleeper, proc.task);
64
65 fprintf(stderr, "[%d/%d] cancel 'sleep %d'\n", q->running_tasks, q->max_running_tasks, s->val);
66 runqueue_process_cancel_cb(q, t, type);
67 }
68
69 static void q_sleep_complete(struct runqueue *q, struct runqueue_task *p)
70 {
71 struct sleeper *s = container_of(p, struct sleeper, proc.task);
72
73 fprintf(stderr, "[%d/%d] finish 'sleep %d'\n", q->running_tasks, q->max_running_tasks, s->val);
74 free(s);
75 }
76
77 static void add_sleeper(int val)
78 {
79 static const struct runqueue_task_type sleeper_type = {
80 .run = q_sleep_run,
81 .cancel = q_sleep_cancel,
82 .kill = runqueue_process_kill_cb,
83 };
84 struct sleeper *s;
85
86 s = calloc(1, sizeof(*s));
87 s->proc.task.type = &sleeper_type;
88 s->proc.task.run_timeout = 500;
89 s->proc.task.complete = q_sleep_complete;
90 s->val = val;
91 runqueue_task_add(&q, &s->proc.task, false);
92 }
93
94 int main(int argc, char **argv)
95 {
96 uloop_init();
97
98 runqueue_init(&q);
99 q.empty_cb = q_empty;
100 q.max_running_tasks = 1;
101
102 if (argc > 1)
103 q.max_running_tasks = atoi(argv[1]);
104
105 add_sleeper(1);
106 add_sleeper(1);
107 add_sleeper(1);
108 uloop_run();
109 uloop_done();
110
111 return 0;
112 }