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.
19 #include <libubox/uloop.h>
26 static struct runqueue q
;
29 struct runqueue_process proc
;
33 static void q_empty(struct runqueue
*q
)
35 fprintf(stderr
, "All done!\n");
39 static void q_sleep_run(struct runqueue
*q
, struct runqueue_task
*t
)
41 struct sleeper
*s
= container_of(t
, struct sleeper
, proc
.task
);
45 fprintf(stderr
, "[%d/%d] start 'sleep %d'\n", q
->running_tasks
, q
->max_running_tasks
, s
->val
);
52 runqueue_process_add(q
, &s
->proc
, pid
);
56 sprintf(str
, "%d", s
->val
);
57 execlp("sleep", "sleep", str
, NULL
);
61 static void q_sleep_cancel(struct runqueue
*q
, struct runqueue_task
*t
, int type
)
63 struct sleeper
*s
= container_of(t
, struct sleeper
, proc
.task
);
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
);
69 static void q_sleep_complete(struct runqueue
*q
, struct runqueue_task
*p
)
71 struct sleeper
*s
= container_of(p
, struct sleeper
, proc
.task
);
73 fprintf(stderr
, "[%d/%d] finish 'sleep %d'\n", q
->running_tasks
, q
->max_running_tasks
, s
->val
);
77 static void add_sleeper(int val
)
79 static const struct runqueue_task_type sleeper_type
= {
81 .cancel
= q_sleep_cancel
,
82 .kill
= runqueue_process_kill_cb
,
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
;
91 runqueue_task_add(&q
, &s
->proc
.task
, false);
94 int main(int argc
, char **argv
)
100 q
.max_running_tasks
= 1;
103 q
.max_running_tasks
= atoi(argv
[1]);